Write a method that returns the sum of all the elements in a specified column in a 3 x 4 matrix using the following header:
public static double sumColumn(double[][] m, int columnIndex)
The program should be broken down into methods, menu-driven, and check for proper input, etc.
The problem I'm having is I'm trying to get my menu to execute the runProgram method. I'm not sure what should be in the parentheses to direct choice "1" to the method. I'm sure I'll also have that problem with executing choice "2," which should go to the sumColumn method, but I'm not there yet. I'll also need a test method. I'd appreciate any help. Here's my code:
package sumelements;
import java.util.Scanner;
public class SumElements
{
private final static Scanner myScan = new Scanner(System.in);
//=================void
main=====================================================
public static void main(String[] args)
{
char choice;
do
{
choice = GetMenu();
switch(choice)
{
case '1':
{
runProgram();
break;
}
case '2':
{
break;
}
case '3':
{
System.out.println("Thanks for using ACME products.\n");
break;
}
default:
{
System.out.println("Invalid input. Please re-enter.\n");
choice = GetMenu();
}
}
}while(choice != '3');
}
//===============GetMenu=========================================================
private static char GetMenu()
{
System.out.print("=================================\n"
+ " Acme Element Adder \n"
+ "\t(1) Input a 3 x 4 matrix \n "
+ "\t(2) Test system with random number \n"
+ "\t(3) Quit the program\n"
+ "\t Make your choice: ");
char choice = myScan.next().toUpperCase().charAt(0);
return choice;
}
public static void runProgram();
{
double arr[][]=new double[3][4];
System.out.println("Enter a 3-by-4 matrix row by row: ");
for(int i=0;i<3;i++)
{
for(int j=0;j<4;j++)
{
arr[i][j]=myScan.nextDouble();
}
}
for(int i=0;i<4;i++)
{
System.out.println("Sum of the element at column "+i+" is
"+sumColumn(arr,i));
}
}
public static double sumColumn(double[][]m,int
ColumnIndex)
{
double sum=0;
for(int i=0;i<3;i++)
{
sum+=m[i][ColumnIndex];
}
return sum;
}
}
JAVA CODE:
import java.util.*;
public class SumElements {
// Scanner
private final static Scanner myScan = new Scanner(System.in);
public static void main(String[] args){
// Declare int variable for choice
int choice;
while(true){
// calling GetMenu() function get the choice
choice = GetMenu();
// enter choice in the switch case
switch(choice)
{
case 1:
{
// calling runProgram() function
runProgram();
break;
}
case 2:
{
// calling runProgramRandomNumber() function
runProgramRandomNumber();
break;
}
case 3:
{ // Display the message
System.out.println("Thanks for using ACME products.\n");
return;// exit program
}
default:
{ // display message for invalid choice
System.out.println("Invalid input. Please re-enter.\n");
}
}
}
}
// Define GetMenu() method
// This method take the choice from the user
private static int GetMenu()
{
System.out.print("\n=================================\n"
+ " Acme Element Adder \n"
+ "\t(1) Input a 3 x 4 matrix \n "
+ "\t(2) Test system with random number \n"
+ "\t(3) Quit the program\n"
+ "\t Make your choice: ");
// ask for input choice
int choice = myScan.nextInt();
return choice;// return choice
}
// Define runProgram() method
public static void runProgram()
{
// Declare double 3 x 4 array
double arr[][]=new double[3][4];
System.out.println("Enter a 3-by-4 matrix row by row: ");
// Enter the element
for(int i=0;i<3;i++)
{
for(int j=0;j<4;j++)
{
arr[i][j]=myScan.nextDouble();
}
}
// iterate for loop for Sum of the element at column
for(int i=0;i<4;i++)
{
// calling sumColumn() method and display
System.out.println("Sum of the element at column "+i+" is "+sumColumn(arr,i));
}
}
// Define runProgramRandomNumber() method
public static void runProgramRandomNumber()
{
// Declare double 3 x 4 array
double arr[][]=new double[3][4];
System.out.println("Generating random number 3-by-4 matrix : ");
// Random number object
Random rand = new Random();
for(int i=0;i<3;i++)
{
for(int j=0;j<4;j++)
{ // generate random number and them insert into matrix
arr[i][j]=rand.nextInt(10);
}
}
// Display the 3 x 4 matrix
for(int i=0;i<3;i++)
{
for(int j=0;j<4;j++)
{
System.out.print((int)arr[i][j]+" ");
}
System.out.println();
}
// Iterate for loop
for(int i=0;i<4;i++)
{
// calling sumColumn() method and display
System.out.println("Sum of the element at column "+i+" is "+sumColumn(arr,i));
}
}
// Define sumColumn() method
// pass the parameter 3 x 4 metrix and ColumnIndex
public static double sumColumn(double[][]m,int ColumnIndex)
{
// Declare sum variable
double sum=0;
for(int i=0;i<3;i++)
{
// calculate the sum of specific column
sum+=m[i][ColumnIndex];
}
// return sum
return sum;
}
}
OUTPUT:
NOTE: If you don't understand any step please tell me.
Get Answers For Free
Most questions answered within 1 hours.