You are to design a Java application using 2-dimensional arrays for a simple spr
ID: 3533299 • Letter: Y
Question
You are to design a Java application using 2-dimensional arrays for a simple spreadsheet with no more than 20 cells. The spreadsheet offers the following operations. The user may order as many operations as he wants until he decides to quit.
A cell is identified by a row number and a column number. Graphics is not required. After each operation, print the latest spreadsheet on the screen and save it to a text file for next time. A calculation which involves any blank cell or mismatched data types is considered as an error/exception. Yo are required to design the exception class and handler to prevent the program crash.
The program must include 2 classes: Spreadsheet and Cell. The Cell class includes the data in a cell and all of its operations.The Spreadsheet class includes a 2-dimensional array of Cell and all the operations on a spreadsheet.
Explanation / Answer
import java.io.IOException;
import java.util.Scanner;
public class Sheet {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
double [][] Sheet = new double[5][7];
for(int row = 0; row < 5 ; row++)
for(int col = 0; col < 7; col++);
//twoDimArray[row][col]= (double)row * 100.0 + (double)col;
Scanner in = new Scanner(System.in);
System.out.println("Enter the Cell Column, Row, and Value: ");
String S = in.next();
char colLet = S.charAt(0);
int col = (int) colLet;
if ((col >= 65) && (col <= 90)) col = col - 65;
else if ((col >= 97) && (col <= 122)) col = col - 97;
else System.out.println("Invalid row letter entered.");
// col = col- 65;
int row = in.nextInt();
double value = in.nextDouble();
Sheet[row][col] = value;
String S2 = in.next();
char colLet1 = S2.charAt(0);
int col2 = (int) colLet1;
if ((col2 >= 65) && (col2 <= 90)) col2 = col2 - 65;
else if ((col2 >= 97) && (col2 <= 122)) col2 = col2 - 97;
else System.out.println("Invalid row letter entered.");
//col2 = col2 - 65;
int row2 = in.nextInt();
double value1 = in.nextDouble();
Sheet[row2][col2] = value1;
//store = colLet1 + String.valueOf(value1) + row2;
String s1 = in.next();
// operation string add, sub, multiply, division,
double total = 0;
// ignore input case from string "+", and add to string together
if (s1.equalsIgnoreCase("+"))
{
System.out.println( + value + " + " + value1 + " = " + (value+value1));
total = value + value1;
}
else if (s1.equalsIgnoreCase("-"))
{
System.out.println( + value + " - " + value1 + " = " + (value-value1));
total = value - value1;
}
else if (s1.equalsIgnoreCase("*"))
{
System.out.println(+ value+ " * " + value1 + " = " + (value*value1));
total = value * value1;
}
else if (s1.equalsIgnoreCase("/"))
{
System.out.println( + value + "/" + value1 + " = " + (value/value1));
total = value / value1;
}
String S3 = in.next();
char colLet2 = S3.charAt(0);
int col3 = (int) colLet2;
if ((col3 >= 65) && (col3 <= 90)) col3 = col3 - 65;
else if ((col3 >= 97) && (col3 <= 122)) col3 = col3 - 97;
else System.out.println("Invalid row letter entered.");
// col3 = col3 - 65;
int row3 = in.nextInt();
Sheet[row3 ][col3] = total;
arrayPrt(Sheet);
System.out.println("Enter to change the row and col: ");
String S4 = in.next();
char colLet4 = S4.charAt(0);
int col4 = (int) colLet4;
col4 = col4 - 65;
int row4 = in.nextInt();
value = in.nextDouble();
//Sheet[row3][col3] = total;
Sheet[row3 ][col3] = total;
Sheet[row4][col4] = value;
//String store = String.valueOf(value) + s1 + String.valueOf(value1) + " = " + String.valueOf(total);
//System.out.println(store);
if (s1.equalsIgnoreCase("+"))
{
System.out.println( + value + " + " + value1 + " = " + (value+value1));
total = value + value1;
}
arrayPrt(Sheet);
}
private static void arrayPrt(double[][] A) {
// TODO Auto-generated method stub
double Cols = A[0].length;
double Rows = A.length;
System.out.print(" ");
//char[] letter = {'A'= 1, 'B' = 2, 'C'= 3, 'D' = 4, 'E'= 5};
for (char i = 'A'; i <= 'G'; i++)
System.out.printf("%8.3s ", String.valueOf(i));
System.out.printf(" |");
for(int col = 0; col < Cols; col++) System.out.printf(" ---------|");
for(int row= 0; row < Rows; row++)
{
System.out.printf(" - |" ,row);
for( int col = 0; col < Cols; col++)
System.out.printf("%6.3f |" , A[row][col]);
System.out.printf(" |");
for( int col = 0; col < Cols; col++) System.out.printf(" ---------|");
}
System.out.print(" ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.