This is the contents of Lab11.java
import java.util.Scanner;
import java.io.*;
public class Lab11
{
public static void main(String args[]) throws IOException {
Scanner inFile = new Scanner(new File(args[0]));
Scanner keyboard = new Scanner(System.in);
TwoDArray array = new TwoDArray(inFile);
inFile.close();
int numRows = array.getNumRows();
int numCols = array.getNumCols();
int choice;
do {
System.out.println();
System.out.println("\t1. Find the number of rows in the 2D
array");
System.out.println("\t2. Find the number of columns in the 2D
array");
System.out.println("\t3. Find the sum of elements in a particular
row");
System.out.println("\t4. Find the sum of elements in a particular
column");
System.out.println("\t5. Find the sum of all elements along the
border");
System.out.println("\t6. Find the sum of elements in the top left
to bottom right diagonal");
System.out.println("\t7. Find the sum of elements in the top right
to bottom left diagonal");
System.out.println("\t8. Print the array on the screen");
System.out.println("\t9. Quit the program");
System.out.print("\nYour selection: ");
choice = Integer.parseInt(keyboard.nextLine() );
switch(choice) {
case 1:
System.out.println("There are " + array.getNumRows() + " rows in
the array");
break;
case 2:
System.out.println("There are " + array.getNumCols() + " columns in
the array");
break;
case 3:
System.out.print("Enter the row number. It should be between 1 and
" + numRows + ": ");
int rowNum = Integer.parseInt(keyboard.nextLine() );
if (rowNum >= 1 && rowNum <= numRows) {
System.out.println("The sum of elements in row " + rowNum + " is "
+
array.rowSum(rowNum - 1));
}
else {
System.out.println("The row number you entered " + rowNum + " is
invalid");
}
break;
case 4:
System.out.print("Enter the column number. It should be between 1
and " + numCols + ": ");
int colNum = Integer.parseInt(keyboard.nextLine() );
if (colNum >= 1 && colNum <= numCols) {
System.out.println("The sum of elements in column " + colNum + " is
" +
array.columnSum(colNum - 1));
}
else {
System.out.println("The column number you entered " + colNum + " is
invalid");
}
break;
case 5:
System.out.println("The sum of all the elements along the border is
" +
array.borderSum() );
break;
case 6:
if (numRows != numCols) {
System.out.println("The 2D array is not a square. Diagonal is not
well defined");
}
else {
System.out.println("The sum of elements in the top left to bottom
right diagonal is "+
array.topLeftToBottomRightDiagonalSum() );
}
break;
case 7:
if (numRows != numCols) {
System.out.println("The 2D array is not a square. Diagonal is not
well defined");
}
else {
System.out.println("The sum of elements in the top right to bottom
left diagonal is "+
array.topRightToBottomLeftDiagonalSum() );
}
break;
case 8:
System.out.println(array);
break;
case 9:
System.out.println("Thanks for using my program. Good bye!");
break;
default:
System.out.println("Illegal Choice. Try again");
} // end of switch
} while (choice != 9);
} // main method
}
Please help me, Thank you!
// Java code for above problem
import java.util.Scanner;
import java.io.*;
class TwoDArray
{
int rows=0,cols=0;
int twoDArray[][];
TwoDArray(Scanner inFile)
{
rows=inFile.nextInt();
cols=inFile.nextInt();
twoDArray=new
int[rows][cols];
for(int i=0;i<rows;i++)
{
for(int
j=0;j<cols;j++)
{
twoDArray[i][j]=inFile.nextInt();
}
}
}
public int getNumRows()
{
return this.rows;
}
public int getNumCols()
{
return this.cols;
}
public int rowSum(int rowNum)
{
int sum=0;
for(int j=0;j<cols;j++)
sum+=twoDArray[rowNum][j];
return sum;
}
public int columnSum(int colNum)
{
int sum=0;
for(int i=0;i<rows;i++)
sum+=twoDArray[i][colNum];
return sum;
}
public int borderSum()
{
int sum=0;
for(int j=0;j<cols;j++)
sum+=twoDArray[0][j]+twoDArray[rows-1][j];
for(int i=0;i<rows;i++)
sum+=twoDArray[i][0]+twoDArray[i][cols-1];
sum=sum-twoDArray[0][0]-twoDArray[rows-1][0]-twoDArray[0][cols-1]-twoDArray[rows-1][cols-1];
return sum;
}
public int topLeftToBottomRightDiagonalSum()
{
int sum=0;
for(int i=0;i<rows;i++)
sum+=twoDArray[i][i];
return sum;
}
public int topRightToBottomLeftDiagonalSum()
{
int sum=0;
for(int i=rows-1;i>=0;i--)
sum+=twoDArray[i][i];
return sum;
}
public String toString()
{
String array="";
for(int i=0;i<rows;i++)
{
for(int
j=0;j<cols;j++)
{
array+=String.valueOf(twoDArray[i][j])+"
";
}
array+="\n";
}
return array;
}
}
public class Lab11
{
public static void main(String args[]) throws
IOException
{
Scanner inFile = new Scanner(new
File(args[0]));
Scanner keyboard = new
Scanner(System.in);
TwoDArray array = new
TwoDArray(inFile);
inFile.close();
int numRows =
array.getNumRows();
int numCols =
array.getNumCols();
int choice;
do
{
System.out.println();
System.out.println("\t1. Find the number of rows in the 2D
array");
System.out.println("\t2. Find the number of columns in the 2D
array");
System.out.println("\t3. Find the sum of elements in a particular
row");
System.out.println("\t4. Find the sum of elements in a particular
column");
System.out.println("\t5. Find the sum of all elements along the
border");
System.out.println("\t6. Find the sum of elements in the top left
to bottom right diagonal");
System.out.println("\t7. Find the sum of elements in the top right
to bottom left diagonal");
System.out.println("\t8. Print the array on the screen");
System.out.println("\t9. Quit the program");
System.out.print("\nYour selection: ");
choice =
Integer.parseInt(keyboard.nextLine() );
switch(choice)
{
case 1:
System.out.println("There are
" + array.getNumRows() + " rows in the array");
break;
case 2:
System.out.println("There are
" + array.getNumCols() + " columns in the array");
break;
case 3:
System.out.print("Enter the
row number. It should be between 1 and " + numRows + ": ");
int rowNum =
Integer.parseInt(keyboard.nextLine() );
if (rowNum >= 1 &&
rowNum <= numRows)
{
System.out.println("The sum of elements in row " + rowNum + " is "
+
array.rowSum(rowNum - 1));
}
else
{
System.out.println("The row number you entered " + rowNum + " is
invalid");
}
break;
case 4:
System.out.print("Enter the
column number. It should be between 1 and " + numCols + ":
");
int colNum =
Integer.parseInt(keyboard.nextLine() );
if (colNum >= 1 &&
colNum <= numCols)
{
System.out.println("The sum of elements in column " + colNum + " is
" +
array.columnSum(colNum - 1));
}
else
{
System.out.println("The column number you entered " + colNum + " is
invalid");
}
break;
case 5:
System.out.println("The sum
of all the elements along the border is " + array.borderSum()
);
break;
case 6:
if (numRows != numCols)
{
System.out.println("The 2D array is not a square. Diagonal is not
well defined");
}
else
{
System.out.println("The sum of elements in the top left to bottom
right diagonal is "+
array.topLeftToBottomRightDiagonalSum() );
}
break;
case 7:
if (numRows != numCols)
{
System.out.println("The 2D array is not a square. Diagonal is not
well defined");
}
else
{
System.out.println("The sum of elements in the top right to bottom
left diagonal is "+
array.topRightToBottomLeftDiagonalSum() );
}
break;
case 8:
System.out.println(array);
break;
case 9:
System.out.println("Thanks
for using my program. Good bye!");
break;
default:
System.out.println("Illegal
Choice. Try again");
} // end of
switch
} while (choice != 9);
} // main method
}
Sample output:
Let the filename be "inputs.txt" and it has following data:
4
4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
then the output is as follows:
// Mention in comments if any mistakes or errors are found. Thank you.
1. Find the number of rows in the 2D array 2. Find the number of columns in the 2D array 3. Find the sum of elements in a particular row 4. Find the sum of elements in a particular column 5. Find the sum of all elements along the border 6. Find the sum of elements in the top left to bottom right diagonal 7. Find the sum of elements in the top right to bottom left diagonal 8. Print the array on the screen 9. Quit the program Your selection: 1 There are 4 rows in the array 1. Find the number of rows in the 2D array 2. Find the number of columns in the 2D array 3. Find the sum of elements in a particular row 4. Find the sum of elements in a particular column 5. Find the sum of all elements along the border 6. Find the sum of elements in the top left to bottom right diagonal 7. Find the sum of elements in the top right to bottom left diagonal 8. Print the array on the screen 9. Quit the program Your selection: 2 There are 4 columns in the array
1. Find the number of rows in the 2D array 2. Find the number of columns in the 2D array 3. Find the sum of elements in a particular row 4. Find the sum of elements in a particular column 5. Find the sum of all elements along the border 6. Find the sum of elements in the top left to bottom right diagonal 7. Find the sum of elements in the top right to bottom left diagonal 8. Print the array on the screen 9. Quit the program Your selection: 3 Enter the row number. It should be between 1 and 4: 4 The sum of elements in row 4 is 58 1. Find the number of rows in the 2D array 2. Find the number of columns in the 2D array 3. Find the sum of elements in a particular row 4. Find the sum of elements in a particular column 5. Find the sum of all elements along the border 6. Find the sum of elements in the top left to bottom right diagonal, 7. Find the sum of elements in the top right to bottom left diagonal 8. Print the array on the screen 9. Quit the program Your selection: 5 The sum of all the elements along the border is 102 1. Find the number of rows in the 2D array 2. Find the number of columns in the 2D array 3. Find the sum of elements in a particular row 4. Find the sum of elements in a particular column 5. Find the sum of all elements along the border 6. Find the sum of elements in the top left to bottom right diagonal 7. Find the sum of elements in the top right to bottom left diagonal 8. Print the array on the screen 9. Quit the program
Your selection: 4 Enter the column number. It should be between 1 and 4: 2, The sum of elements in column 2 is 32 1. Find the number of rows in the 2D array 2. Find the number of columns in the 2D array 3. Find the sum of elements in a particular row 4. Find the sum of elements in a particular column 5. Find the sum of all elements along the border 6. Find the sum of elements in the top left to bottom right diagonal 7. Find the sum of elements in the top right to bottom left diagonal 8. Print the array on the screen 9. Quit the program Your selection: 6 The sum of elements in the top left to bottom right diagonal is 34, 1. Find the number of rows in the 2D array 2. Find the number of columns in the 2D array 3. Find the sum of elements in a particular row 4. Find the sum of elements in a particular column 5. Find the sum of all elements along the border 6. Find the sum of elements in the top left to bottom right diagonal 7. Find the sum of elements in the top right to bottom left diagonal 8. Print the array on the screen 9. Quit the program Your selection: 7 The sum of elements in the top right to bottom left diagonal is 34
1. Find the number of rows in the 2D array 2. Find the number of columns in the 2D array 3. Find the sum of elements in a particular row 4. Find the sum of elements in a particular column 5. Find the sum of all elements along the border 6. Find the sum of elements in the top left to bottom right diagonal, 7. Find the sum of elements in the top right to bottom left diagonal 8. Print the array on the screen 9. Quit the program Your selection: 8 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1. Find the number of rows in the 2D array 2. Find the number of columns in the 2D array 3. Find the sum of elements in a particular row 4. Find the sum of elements in a particular column 5. Find the sum of all elements along the border 6. Find the sum of elements in the top left to bottom right diagonal 7. Find the sum of elements in the top right to bottom left diagonal 8. Print the array on the screen 9. Quit the program Your selection: 9 Thanks for using my program. Good bye!
This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static...
package Lab11; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class Lab10 { public static void main (String [] args) { // ============================================================ // Step 2. Declaring Variables You Need // These constants are used to define 2D array and loop conditions final int NUM_ROWS = 4; final int NUM_COLS = 3; String filename = "Input.txt"; // A String variable used to save the lines read from input...
Can you help me with this code in Java??? import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int rows = 3; int columns = 4; int[][] arr = new int[rows][columns]; for(int i = 0; i < rows; ++i) { for(int j = 0; j < columns; ++j) { System.out.println("Enter a value: "); arr[i][j] = scan.nextInt(); } } System.out.println("The entered matrix:"); for(int i = 0; i < rows; ++i) { for(int j...
Fix this program package chapter8_Test; import java.util.Scanner; public class Chapter8 { public static void main(String[] args) { int[] matrix = {{1,2},{3,4},{5,6},{7,8}}; int columnChoice; int columnTotal = 0; double columnAverage = 0; Scanner input = new Scanner(System.in); System.out.print("Which column would you like to average (1 or 2)? "); columnChoice = input.nextInt(); for(int row = 0;row < matrix.length;++row) { columnTotal += matrix[row][columnChoice]; } columnAverage = columnTotal / (float) matrix.length; System.out.printf("\nThe average of column %d is %.2f\n", columnAverage, columnAverage); } } This program...
Fix the following null pointer error. import java.util.*; import java.io.*; public class PhoneBook { public static void main(String[]args)throws IOException { PhoneBook obj = new PhoneBook(); PhoneContact[]phBook = new PhoneContact[20]; Scanner in = new Scanner(System.in); obj.acceptPhoneContact(phBook,in); PrintWriter pw = new PrintWriter("out.txt"); obj.displayPhoneContacts(phBook,pw); pw.close(); } public void acceptPhoneContact(PhoneContact[]phBook, Scanner k) { //void function that takes in the parameters //phBook array and the scanner so the user can input the information //declaring these variables String fname = ""; String lname = ""; String...
Java Here is the template public class ShiftNumbers { public static void main(String[] args) { // TODO: Declare matrix shell size // TODO: Create first row // TODO: Generate remaining rows // TODO: Display matrix } /** * firstRow * * This will generate the first row of the matrix, given the size n. The * elements of this row will be the values from 1 to n * * @param size int Desired size of the array * @return...
please answer in Java..all excercises. /** * YOUR NAME GOES HERE * 4/7/2017 */ public class Chapter9a_FillInTheCode { public static void main(String[] args) { String [][] geo = {{"MD", "NY", "NJ", "MA", "CA", "MI", "OR"}, {"Detroit", "Newark", "Boston", "Seattle"}}; // ------> exercise 9a1 // this code prints the element at row at index 1 and column at index 2 // your code goes here System.out.println("Done exercise 9a1.\n"); // ------> exercise 9a2 // this code prints all the states in the...
I need the following java code commented import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner (System.in); int productNo=0; double product1; double product2; double product3; double product4; double product5; int quantity; double totalsales=0; while(productNo !=0) System.out.println("Enter Product Number 1-5"); productNo=input.nextInt(); System.out.println("Enter Quantity Sold"); quantity=input.nextInt(); switch (productNo) { case 1: product1=1.00; totalsales+=(1.00*quantity); break; case 2: product2=2.00; totalsales+=(2.00*quantity); break; case 3: product3=6.00; totalsales+=(6.00*quantity); break; case 4: product4=23.00; totalsales+=(23.00*quantity); break; case 5: product5=17.00; totalsales+=(17.00*quantity); break;...
import java.io.*; import java.util.Scanner; public class CrimeStats { private static final String INPUT_FILE = "seattle-crime-stats-by-1990-census-tract-1996-2007.csv"; private static final String OUTPUT_FILE = "crimes.txt"; public static void main(String[] args) { int totalCrimes = 0; // read all the rows from the csv file and add the total count in the totalCrimes variable try { Scanner fileScanner = new Scanner(new File(INPUT_FILE)); String line = fileScanner.nextLine(); // skip first line while (fileScanner.hasNext()) { String[] tokens = fileScanner.nextLine().split(","); if (tokens.length == 4) { totalCrimes +=...
Step 1: Getting Started Create a new .java file named Lab12.java. At the beginning of this file, include your assignment documentation code block. After the documentation block, you will need several import statements. import java.util.Scanner; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; Next, declare the Lab12 class and define the main function. public class Lab12 { public static void main (String [] args) { Step 2: Declaring Variables For this section of the lab, you will need to declare...
import java.util.Scanner; import java.io.File; public class Exception2 { public static void main(String[] args) { int total = 0; int num = 0; File myFile = null; Scanner inputFile = null; myFile = new File("inFile.txt"); inputFile = new Scanner(myFile); while (inputFile.hasNext()) { num = inputFile.nextInt(); total += num; } System.out.println("The total value is " + total); } } /* In the first program, the Scanner may throw an...