- 2
Write a java program that creates a driven menu program to perform methods?
The methods are 1)summing the elements of an array 2)finding the largest value in an array 3)finding the largest difference between two element of the array. 4)Locate a key value in an array 5)double the values in an array 6)compare two array that contain the same values but not in the same order in which the value
8 Respostas
+ 6
Try to do your own homework first then ask if you have any problems.
+ 4
Try posting the code here and I'll help you.
+ 1
I did but my teacher said that I was supposed to create an object and a method. I used a switch case statement and it had the same output. I don't know how u would do it with an object. I already have done the code for the menu but my problem is how to put that in an object and have it print out the results in a switch case statement.
0
case 6:
// testing if arrays are equal ------------- below
int[] p = {0,1,2,3,4,5,6,7,8,9};
int[] q = {0,1,2,3,4,5,6,7,8,9};
boolean equal = true;
for(int j = 0; j < 10 && equal == true; j++)
{
if(p[j] == q[j])
{
equal = true;
}
else
{
equal = false;
System.out.println("\n\nArrays p and q are not the same.");
}
}
System.out.println("\n\nArray p:");
for(int z = 0; z < 10; z++) {
System.out.print(p[z] + " ");
}
System.out.println("\n\nArray q:");
for(int b = 0; b < 10; b++) {
System.out.print(q[b] + " ");
}
if(equal == true) {
System.out.println("\n\nArrays p and q are the same!");
}
break;
default :
System.out.println("This is not a valid Menu Option! Please Select Another");
break;
}//end of switch case
}//end of main
}//end of class
0
case 4:
int [] score111 = new int [5];
System.out.println("Enter 5 numbers");
{
//inserting values into array
for(index = 0; index < 5; index++)
{
score111[index] = input.nextInt();
}
System.out.println("\nEnter a number you are searching for in the array."); //searching for number in array
choice = input.nextInt();
boolean found = false;
for(index = 0; index < 5 && found == false; index++) {
if(choice == score111[index]) {
System.out.println("\nYour number was found in index position " + index + " of the array.");
found = true;
}
}
if(found == false) {
System.out.println("Number not found.");
}
}
break;
case 5:
int [] score1111 = new int [5];
System.out.println("Enter 5 numbers");
{
//inserting values into array
for(index = 0; index < 5; index++)
{
score1111[index] = input.nextInt();
}
//doubling each number in each index
for(index = 0; index < 5; index++)
{
score1111[index] = (score1111[index] * 2);
}
System.out.println("\nThe array values after being doubled: \n");
for(index = 0; index <5; index++)
{
System.out.print(score1111[index] + " ");
}
}
break;
0
case 2:
int [] score1 = new int[5];
int Max = 0;
System.out.println("Enter 5 numbers");
{
//inserting values into array
for(index = 0; index < 5; index++)
{
score1[index] = input.nextInt();
}
//determines greatest number in array
for(index = 0; index < 5; index++)
{
if(score1[index] > Max)
{
Max = score1[index];
}
}
//displays greatest number in array
System.out.println("Largest number: " + Max + "\n");
}
break;
case 3:
int [] score11 = new int[5];
int Max1 = 0;
System.out.println("Enter 5 numbers");
{
//inserting values into array
for(index = 0; index < 5; index++)
{
score11[index] = input.nextInt();
}
//determines greatest number in array
for(index = 0; index < 5; index++)
{
if(score11[index] > Max1)
{
Max1 = score11[index];
}
}
//displays greatest number in array
System.out.println("Largest number: " + Max1 + "\n");
}
for(index = 0; index <5; index++)
{
//displays distance from max #
System.out.println("Difference between the largest number to the element in index position " + index + " is: " + (Max1 - score11[index]));
}
break;
0
import java.util.Scanner;
public class CalculatorMenu {
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
boolean mainLoop = true;
int choice;
do{
System.out.print("1.) Summing all the number.\n");
System.out.print("2.) Finding the greatest number in the array list.\n");
System.out.print("3.) Finding the largest difference between two element of the array.\n");
System.out.print("4.) Locate a key value in the array list.\n");
System.out.print("5.) Double the value in an array.\n");
System.out.print("6.) Two arrays that contain the same values but not in the same order.\n");
System.out.print("\nEnter Your Menu Choice: ");
choice = input.nextInt();
}
while(choice > 6);
switch(choice){
case 1:
int [] score = new int[5];
int index, total = 0;
System.out.println("Enter 5 numbers");
{
//inserting values into array
for(index = 0; index < 5; index++)
{
score[index] = input.nextInt();
}
// summing the array
for(index = 0; index < 5; index++)
{
total += score[index];
}
//printing sum of the array
System.out.println("\nSum " + total + "\n");
}
break;
0
I had to put the code in separate replies because all of the code couldn't fit in one.