Hey guys I just ran into some pretty interesting java problems on a different website and thought some of you might get a kick out of this. Determining on how well this goes I may post some more. So here comes problem 1.
Determine the following output with the use of data.txt
import java.io.File;
import java.util.Scanner;
import java.io.IOException;
class RecArray{
private final static int NUM = 6;
private int [][] myGrid = new int[NUM][NUM];
public RecArray(){
load();
}
public void load(){
int row, col;
String fileName = "data2.txt";
try{
Scanner inFile = new Scanner(new File(fileName));
for (row = 0; row < NUM; row++)
for (col = 0; col < NUM; col++)
myGrid[row][col] = inFile.nextInt();
}catch(IOException i){
System.out.println("Error: " + i.getMessage());
}
}
public int calculate(){
return calculate(0);
}
public int calculate(int count){
if (count >= myGrid.length){
return 0;
}else{
int total = 0;
for (int i = 0; i <= count; i++){
total += myGrid[count][i];
total += myGrid[i][count];
}
total -= myGrid[count][count];
System.out.println("count is: " + count + " and this total is: "
+ total);
return total + calculate(count + 1);
}
}
public void display(){
int row, col;
for (row = 0; row < NUM; row++){
for (col =0; col < NUM; col++){
System.out.print(myGrid[row][col] + " ");
}
System.out.println();
}
System.out.println();
}
}
public static void main(String[] args) {
RecArray app = new RecArray();
int answer = app.calculate();
System.out.println("The final answer is: " + answer);
}
data.txt:
5 6 -4 10 -6 7
2 3 6 3 -8 2
4 -9 8 4 9 -3
7 9 2 -3 5 -7
5 -9 -4 7 6 -5
-1 3 8 -4 0 8
Okay guys get at it! Hopefully this will spark a little competitive spirit within the forum...
~IMtheBESTatJAVA