Posted by : Unknown
domingo, 28 de septiembre de 2014
Tuesday, September 16th, 2014
Exercise 01
By: Eduardo Zavala Ramírez
Today, we solved the first exercise of the SDP03 course.
The code lines are written below.
/**
*
*/
package exercise01;
import java.io.File;
import java.util.Scanner;
/**
* @author park
*
*/
public class Exercise01 {
private static String question = "Enter a number or return to end ";
private static final String dataPath = "data.txt";
private static int[][] theData;
private static int maxRow = 0; // These are the height and width of the maze
private static int maxCol = 0;
/**
* @param args
*/
public static void main(String[] args) {
//Create a scanner to read the data
Scanner scanner = null;
try {
scanner = new Scanner(new File(dataPath));
} catch (Exception e) {
e.printStackTrace();
}
//first line is the array size
String line = scanner.nextLine();
//get the array size
Scanner s = new Scanner(line);
maxCol = s.nextInt();
maxRow = s.nextInt();
//initialize the array
theData = new int[maxCol][maxRow];
Scanner l;
//now scan each line for data
for (int r=0;r<maxRow;r++) {
line = scanner.nextLine();
l = new Scanner(line);
int c = 0;
//grab the data from each line
while (l.hasNextInt())
theData[r][c++] = l.nextInt();
}
System.out.println(mazeToString());
scanner.close();
//get ready to ask for a prompt
scanner = new Scanner(System.in);
ask();
int theNumber = 0;
int [][] found;
boolean isFound = false;
while (scanner.hasNextInt()) {
theNumber = scanner.nextInt();
//fetch the locations of any hits of the theNumber
found = findNumberInArray(theNumber);
//System.out.println("Your code goes here");
for(int r=0; r<maxRow; r++){
for(int c=0; c<maxCol; c++){
if(found[r][c] == theNumber){
isFound=true;
System.out.println("Posición [" + r + "][" + c + "]");
}
}
}
if(isFound==false)
System.out.println("Elemento no encontrado");
//System.out.println("Elemento no encontrado");
ask();
}
System.out.println("Thanks!");
}
/**
* Find all the hits of <code>number</code> in the array
* @param number
* @return can return an empty array, initialized only to -1
*/
private static int [][] findNumberInArray(int number) {
System.out.println("Debug finding: "+number);
//create a result array
int [] []result = new int[maxRow][maxCol];
//initialize it to -1
//we use -1 as a signal that nothing was found
for (int r=0;r<maxRow;r++) {
for (int c=0;c<maxCol;c++)
result[r][c]=-1;
}
for (int r=0;r<maxRow;r++) {
for (int c=0;c<maxCol;c++) {
if (theData[r][c] == number)
result[r][c] = number;
}
}
return result;
}
/**
* Ask a question to be answered
*/
private static void ask() {
System.out.println(question);
}
/**
* Create a String representation of the maze
*/
private static String mazeToString(){
String str = "";
for (int i = 0; i < maxRow; i++) {
for (int j = 0; j < maxCol; j++)
str += theData[i][j]+" ";
str += '\n';
}
return str;
}
}
The exercise objective was to execute the app and then, when a user writes a number, to show the position of the number in the array. If the number isn't inside the array, the program will have to show an error message.
The necessary modifications were to call the method findNumberInArray and to create the cycle to number the position.