Download Page 1/8 /Users/erland/CTH/Kursermm/TIN212/examsTIN212

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
/Users/erland/CTH/Kursermm/TIN212/examsTIN212/140215.L/140215.alla.txt
Page 1/8
Saved: 2017-02-06 20:02:03
Printed for: Erland Holmström
Tenta TIN212 2014-02-15 Lösningsskisser¬
¬
Uppg 1¬
==============================================================¬
alla sanna, uppg c är oklar, det borde stått "överskugga"¬
¬
Uppg 2¬
==============================================================¬
static public boolean superPrimeR(int p) {¬
// undvik att anropa isPrime i onödan tex med negativa tal¬
if (p<=1) {¬
return false;¬
} else if (p<9) {¬
return isPrime(p);¬
} else {¬
return isPrime(p) && superPrimeR(p/10);¬
}¬
}¬
¬
static public void allSuperPrimes(int l) {¬
if (l>=1) {¬
// sätt upp start och slutvärden. I princip kan man börja ¬
// på 2 istället och undvika tal som börjar på ¬
// 4, 6 och 9 men det är overkill här¬
// kan göra enkelt med Math.pow() också¬
int start = 1; // skall bli 10000...., l st¬
int slut = 9; // skall bli 99999...., l st¬
for (int i = 1; i<l; i++) {¬
start = start*10;¬
slut = slut*10+9;¬
}¬
int tab = 0;¬
for (int i = start; i <= slut; i++) {¬
if (superPrimeR(i)) {¬
System.out.print(i + "\t");¬
// tabellering behövdes inte¬
tab++;¬
if (tab%10==0) {System.out.println();}¬
}¬
}¬
}¬
System.out.println();¬
}¬
¬
¶
/Users/erland/CTH/Kursermm/TIN212/examsTIN212/140215.L/140215.alla.txt
Page 2/8
Saved: 2017-02-06 20:02:03
Printed for: Erland Holmström
Uppg 3¬
==============================================================¬
import java.awt.Color;¬
import javax.swing.JButton;¬
import javax.swing.border.*;¬
¬
public class Cell extends JButton {¬
private final int row;
¬
private final int col;¬
private String buttonText;¬
private Color cellColor = null;¬
// cellColor behöver ju strikt inte sparas, man kan fråga knappen ¬
//vad den har för färg. Detsamma gäller texten.¬
¬
/**¬
* Construct a cell.¬
* @param buttonText - text for the button¬
* @param row, col - basically two numbers that the cell only save¬
* @param cellColor - background color for the cell¬
*/¬
public Cell(String buttonText, int row, int col, Color cellColor) { //2¬
// row/col behöver inte felhanteras här eftersom en Cell inte ¬
// vet vad dom används till (och inte använder dom)¬
//cellColor felhanteras i setcellColor¬
super(buttonText);¬
this.buttonText = buttonText;¬
this.row = row;¬
this.col = col;¬
setCellColor(cellColor);¬
¬
// 2 trick för att få bakgrundsfärgen att synas¬
// 0ch för att slippa mellanrum mellan knappar¬
// behövdes ej på tentan¬
setBorder(new LineBorder(Color.white, 0) );¬
setOpaque(true);¬
}¬
/**¬
* Obvoius functions for the getters.¬
*/¬
public int getRow() {¬
return row;¬
}¬
public int getCol() {¬
return col;¬
}¬
public Color getCellColor() {¬
return cellColor;¬
}¬
/**¬
* Sets the background color for the cell to one of the colors from State.¬
* @param cellColor - the color to set¬
* @throws IllegalArgumentException - if color not in State¬
*/¬
public void setCellColor(Color cellColor) { //4¬
// null test automatic with this order¬
if ( State.EMPTY.equals(cellColor)¬
|| State.MARKED.equals(cellColor)¬
|| State.NORMAL.equals(cellColor)¬
|| State.ABNORMAL.equals(cellColor)¬
|| State.TEMPORARY.equals(cellColor)¬
|| State.MARKED.equals(cellColor) ) {¬
this.cellColor = cellColor;¬
setBackground(cellColor);¬
} else {¬
throw new IllegalArgumentException("color not allowed or null");¬
}¬
}¬
}¬
¶
/Users/erland/CTH/Kursermm/TIN212/examsTIN212/140215.L/140215.alla.txt
Page 3/8
Saved: 2017-02-06 20:02:03
Printed for: Erland Holmström
Uppg 4 ¬
==============================================================¬
import java.awt.Color;¬
public class BlobModel { ¬
¬
/** A two dimensional grid of colored squares */¬
private Color[][] theGrid; //1¬
/** Number of rows */¬
private int nbrRows;¬
/** Number of columns */¬
private int nbrCols;¬
¬
/** Constructor¬
* Construct a Two Dim Grid of the specified size ¬
* filled with values NORMAL and ABNORMAL.¬
* Exactly how the filling is done is not so important.¬
* @param nbrRows - Number of rows¬
* @param nbrCols - Number of columns¬
* @param probability - nbr between 0.0..1.0 how big part that is ¬
* filled with ABNORMAL or used somehow to create an interesting matrix¬
*/¬
public BlobModel(int nbrRows, int nbrCols, double probability) { //5¬
if (nbrRows<1 || nbrCols<1) {¬
throw new IllegalArgumentException("Rows and Cols must be >0");¬
}¬
// similar test for probability¬
this.nbrRows = nbrRows;¬
this.nbrCols = nbrCols;¬
theGrid = new Color[nbrRows][nbrCols];¬
// initialise with NORMAL¬
for (int i = 0; i < nbrRows; ++i) {¬
for (int j = 0; j < nbrCols; ++j) {¬
theGrid[i][j] = State.NORMAL;¬
}¬
}¬
// exactly how the filling is done is not so important¬
// details are not important¬
// Fill the grid of squares randomly.¬
int filledCells = 0;¬
while (filledCells < probability*nbrRows*nbrCols) {¬
// vill ha slumptal [1..nbrRows-1[¬
// pga tilldelningarna nedan¬
int i = (int) (1+Math.random()*(nbrRows-2));¬
int j = (int) (1+Math.random()*(nbrCols-2));¬
// System.out.println("i=" + i + " j=" + j); // debug¬
// jag gjorde ett kryss här men på tentan räckte det med en fylld¬
// lite större blob behövdes inte¬
theGrid[i][j]
= State.ABNORMAL;¬
theGrid[i+1][j] = State.ABNORMAL;¬
theGrid[i][j+1] = State.ABNORMAL;¬
theGrid[i-1][j] = State.ABNORMAL;¬
theGrid[i][j-1] = State.ABNORMAL;¬
filledCells = filledCells+5;¬
}¬
} // end Constructor¬
¬
/**¬
* Obvoius functions for the getters.¬
* @return nbrCols */¬
public int getNbrCols() {¬
return nbrCols;¬
}¬
/**¬
* @return nbrRows */¬
public int getNbrRows() {¬
return nbrRows;¬
}¬
¬
/**¬
/Users/erland/CTH/Kursermm/TIN212/examsTIN212/140215.L/140215.alla.txt
Page 4/8
Saved: 2017-02-06 20:02:03
Printed for: Erland Holmström
* Get the state at a given coordinate¬
* @param x - The column number¬
* @param y - The row number¬
* @return The state at the given coordinate ¬
*/¬
public Color getState(int x, int y) {¬
//take the automatic ArrayIndexOutOfBounds here for x,y¬
return theGrid[x][y];¬
}¬
¬
/**¬
* Change the state at a given coordinate¬
* @param x - The column number¬
* @param y - The row number¬
* @param newColor - The state to set the cell to ¬
*/¬
public void setState(int x, int y, Color newColor) { // 1¬
//take the automatic ArrayIndexOutOfBounds here for x,y¬
// newColor: see test in Cell.setCellColor¬
theGrid[x][y] = newColor;¬
}¬
¬
/**¬
* Change the state of all cells that are marked with MARKED¬
* to ABNORMAL.¬
*/¬
public void reState() { // 2¬
for ( int i = 0; i < getNbrRows(); ++i ) {¬
for ( int j = 0; j < getNbrCols(); ++j ) {¬
//if ( getState(i, j)==State.MARKED ) { // går bra ty static¬
if ( getState(i, j).equals(State.MARKED) ) {¬
theGrid[i][j] = State.ABNORMAL;¬
}¬
}¬
}¬
} // end reState¬
¬
¬
Uppg 5 ¬
============================================================== ¬
/**¬
* Finds the number of cells in the blob at (x,y).¬
* @pre The cell at (x,y) are in the state ABNORMAL¬
* @post All cells in the blob counted are in the state MARKED.¬
* @param x The x-coordinate of a blob cell¬
* @param y The y-coordinate of a blob cell¬
* @return The number of cells in the blob that contains (x, y)¬
* if the cell at (x,y) are not in the state ABNORMAL zero is returned¬
*/¬
public int findBlob(int x, int y) { // 8¬
if (x < 0 || x >= getNbrRows() // outside the grid¬
|| y < 0 || y >= getNbrCols()) {¬
return 0;¬
} else if (getState(x, y).equals(State.ABNORMAL)) {¬
setState(x, y, State.MARKED);¬
// alla riktningar behövs ej på tentan¬
return 1
// medsols¬
+ findBlob(x, y + 1)
// upp¬
+ findBlob(x + 1, y)
// höger¬
+ findBlob(x, y - 1)
// ner
¬
+ findBlob(x - 1, y
// vänster¬
//+ findBlob(x - 1, y + 1) // övre vänstra diag¬
//+ findBlob(x + 1, y + 1) // övre högra diag¬
//+ findBlob(x + 1, y - 1) // nedre högra diag¬
//+ findBlob(x - 1, y - 1) // nedre vänstra diag¬
);
¬
} else {¬
return 0;¬
}¬
/Users/erland/CTH/Kursermm/TIN212/examsTIN212/140215.L/140215.alla.txt
Page 5/8
Saved: 2017-02-06 20:02:03
Printed for: Erland Holmström
} // end ¬
}¬
¶
/Users/erland/CTH/Kursermm/TIN212/examsTIN212/140215.L/140215.alla.txt
Page 6/8
Saved: 2017-02-06 20:02:03
Printed for: Erland Holmström
Uppg 6 ¬
==============================================================¬
¬
import java.awt.*;¬
import java.awt.event.*;¬
import javax.swing.*;¬
¬
/**¬
* BlobView is a View of a two dimensional array of cells.¬
*/¬
public class BlobView extends JPanel {¬
¬
/** Prefered button size */¬
private static final int PREFERED_BUTTON_SIZE = 15; // 1¬
private BlobModel bm;¬
private BlobControl bc;¬
private Cell[][] theButtonGrid;¬
private int nRows;¬
private int nCols;¬
¬
/**¬
* Construct and initialise a two dimensional grid of cells¬
*/¬
public BlobView(BlobModel bm, BlobControl bc) {
// 5-6¬
if (bm==null || bc==null) {¬
throw new IllegalArgumentException("bm==null || bc==null");¬
}¬
this.bm = bm;¬
this.bc = bc;¬
nRows = bm.getNbrRows();¬
nCols = bm.getNbrCols();¬
// a panel for the cells¬
//JPanel cellPanel = new JPanel();¬
Dimension d = new Dimension(nCols * PREFERED_BUTTON_SIZE,¬
nRows * PREFERED_BUTTON_SIZE);¬
setPreferredSize(d);¬
setLayout(new GridLayout(nRows, nCols));¬
theButtonGrid = new Cell[nRows][nCols];¬
for (int i = 0; i < nRows; ++i) {¬
for (int j = 0; j < nCols; ++j) {¬
theButtonGrid[i][j] = new Cell( "", i, j, bm.getState(i, j) );¬
theButtonGrid[i][j].addActionListener(bc);¬
add(theButtonGrid[i][j]);¬
}¬
}¬
} // end constructor¬
¬
/**¬
* Transforms the state (color) from the model to the cells¬
*/¬
public void reDraw() {
// 1-2¬
for (int i = 0; i < nRows; ++i) {¬
for (int j = 0; j < nCols; ++j) {¬
// överför färgen från modellen till knapparna¬
theButtonGrid[i][j].setCellColor( bm.getState(i, j) );¬
}¬
}¬
} // end reDraw¬
}¬
// =============================================================¬
¬
¶
/Users/erland/CTH/Kursermm/TIN212/examsTIN212/140215.L/140215.alla.txt
Page 7/8
Saved: 2017-02-06 20:02:03
Printed for: Erland Holmström
Uppg 7 ¬
==============================================================¬
import java.awt.*;¬
import java.awt.event.*;¬
import javax.swing.*;¬
¬
/**¬
* BlobControl is the controller for the blob application ¬
**/¬
public class BlobControl extends JFrame implements ActionListener {¬
¬
private BlobModel bm;
//0-1¬
private BlobView cellPanel;¬
private JLabel blobResult;¬
¬
/**¬
* Construct the controler¬
*/¬
public BlobControl(BlobModel bm) {
//4-5¬
// null test see BlobView¬
this.bm = bm;¬
setTitle("The Blob Analyser");¬
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);¬
setLayout(new BorderLayout());¬
¬
// a panel for the cells¬
cellPanel = new BlobView(bm, this);¬
add(cellPanel, BorderLayout.NORTH);¬
¬
blobResult = new JLabel("--");¬
blobResult.setHorizontalAlignment(SwingConstants.CENTER); // behövs ej¬
add(blobResult, BorderLayout.CENTER);¬
// New random blob and choise list here¬
¬
setVisible(true);¬
pack();¬
} // end constructor¬
¬
public void actionPerformed(ActionEvent e) {
// 3¬
if (e.getSource() instanceof Cell) {¬
Cell b = (Cell) e.getSource(); ¬
bm.reState();¬
blobResult.setText( "" + bm.findBlob(b.getRow(), b.getCol()) ); ¬
cellPanel.reDraw(); ¬
}¬
/*¬
else if (e.getSource() instanceof JButton) { // New random blob¬
¬
}*/¬
}¬
}¬
// =============================================================¬
¬
¬
¶
/Users/erland/CTH/Kursermm/TIN212/examsTIN212/140215.L/140215.alla.txt
Page 8/8
Saved: 2017-02-06 20:02:03
Printed for: Erland Holmström
Uppg 8 ==============================================================¬
import javax.swing.*;¬
import java.util.Scanner;¬
import java.io.*;¬
¬
public class BlobMain2 {¬
¬
/** ¬
* @param args - Optional x,y or input file containing Blob¬
*/¬
public static void main(String[] args) {¬
try {¬
if (args.length < 1) {
//2¬
// no args given¬
String reply = JOptionPane.showInputDialog("Enter number of rows");¬
int nRows = Integer.parseInt(reply);¬
reply = JOptionPane.showInputDialog("Enter number of columns");¬
int nCols = Integer.parseInt(reply);¬
BlobModel bm = new BlobModel(nRows, nCols, 0.35);¬
new BlobControl(bm);¬
¬
} else if ( Character.isDigit(args[0].charAt(0)) ) {
// 1¬
// no filename given¬
BlobModel aGrid = new BlobModel(Integer.parseInt(args[0]), ¬
Integer.parseInt(args[1]), 0.35 );¬
new BlobControl(aGrid);¬
¬
} else { // filename given
// 3¬
// Create grid from a data file, 1=abnormal, 0=normal¬
Scanner sc = new Scanner(new File(args[0]));¬
// Read each data line (a string), assuming at least 2 lines¬
int nRows = Integer.parseInt(sc.nextLine().trim());¬
int nCols = Integer.parseInt(sc.nextLine().trim());¬
String line;¬
String[] lines = new String[nRows];¬
int i = -1;¬
while ( sc.hasNext() ) {¬
line = sc.nextLine();¬
i++;¬
lines[i] = line;¬
}¬
// utskrifter¬
System.out.println("nbr of rows= " + nRows);¬
System.out.println("nbr of cols= " + nCols);¬
for (int j=0; j<=i; j++) {¬
System.out.println(lines[i]);¬
}¬
}¬
// skall vara i rätt ordning dvs Number format först¬
} catch (NumberFormatException ex) {
//4¬
System.err.println("*****BlobControl: fel format på indata (parseInt)");¬
ex.printStackTrace();¬
System.exit(1);¬
} catch (IllegalArgumentException ex) {¬
System.err.println("*****BlobControl: felaktigt argument");¬
ex.printStackTrace();¬
System.exit(1);¬
} catch (FileNotFoundException ex) {¬
System.err.println("*****BlobControl: filen existerar inte");¬
ex.printStackTrace();¬
System.exit(1);¬
} catch (ArrayIndexOutOfBoundsException ex) {¬
System.err.println("*****BlobControl: AIOB, perhaps in ¬
parseInt(args[1] ");¬
ex.printStackTrace();¬
System.exit(1);¬
}¬
} // end main¬