Write a program to extend the below program by adding an option 'g' to the menu program. If this option is chosen, the program gets a string from the user (up to a maximum of 50 characters) and swaps the first and last characters. Main.java import java.util.Scanner; public class Main { static Scanner kb = new Scanner(System.in); public static void displayMenu() { System.out.println("a-Print Name and Tutor Name"); System.out.println("b-Largest of 3 Number"); System.out.println("c-Display Numbers in range of two numbers"); System.out.println("d-Check for Triangles"); System.out.println("e-Check for prime"); System.out.println("f-Arrays Example"); //adding statement System.out.println("q-Quit"); System.out.print("Enter Your Choice (a,b,c,d,e,f,q) : "); } public static void findLargestOf3() { double x, y, z, smallest, largest; System.out.println("Enter 3 Numbers : "); System.out.print("x : "); x = Double.parseDouble(kb.nextLine()); System.out.print("y : "); y = Double.parseDouble(kb.nextLine()); System.out.print("z : "); z = Double.parseDouble(kb.nextLine()); smallest = (x < y && x < z) ? x : (y < x && y < z) ? y : z; largest = (x > y && x > z) ? x : (y > x && y > z) ? y : z; System.out.println("The Smallest Number is : " + smallest); System.out.println("The Largest Number is : " + largest); } public static void displayNumbersinRange() { int m, n, s = 0, t; System.out.println("Enter 2 Numbers : "); System.out.print("m : "); m = Integer.parseInt(kb.nextLine()); System.out.print("n : "); n = Integer.parseInt(kb.nextLine()); if (m > n) { t = n; n = m; m = t; } for (int i = m, j = 1; i < n; i++, j++) { System.out.print(i + " "); if (j % 5 == 0) System.out.println(); if (i % 2 != 0) s += i; } System.out.println("\nThe Sum of all odd numbers between " + m + " and " + n + " is : " + s); } public static void checkTriangle() { double s1, s2, s3; System.out.println("Enter 3 Sides of Triangle : "); System.out.print("s1 : "); s1 = Double.parseDouble(kb.nextLine()); System.out.print("s2 : "); s2 = Double.parseDouble(kb.nextLine()); System.out.print("s3 : "); s3 = Double.parseDouble(kb.nextLine()); if (s1 > s2 + s3 || s2 > s1 + s3 || s3 > s1 + s2) { System.out.println("The Given sides can from a Triangle"); } else System.out.println("The Given sides can not from a Triangle"); } public static void checkPrime() { int m, c = 0; System.out.print("Enter a Numbers : "); m = Integer.parseInt(kb.nextLine()); for (int i = 1; i <= m; i++) { if (m % i == 0) c++; } if (c == 2) System.out.println("The given number is Prime"); else System.out.println("The given number is not a Prime"); } public static void main(String[] args) { char ch = 'x'; while (ch != 'q') { displayMenu(); ch = kb.nextLine().toLowerCase().charAt(0); switch (ch) { case 'a': System.out.println("My Name \n My Tutor Name"); break; case 'b': findLargestOf3(); break; case 'c': displayNumbersinRange(); break; case 'd': checkTriangle(); break; case 'e': checkPrime(); break; case 'f': //adding another case newFunction(); break; case 'q': break; default: System.out.println("Error : Invalid Input "); break; } } } public static void newFunction() { int[] array=new int[10]; //declare array System.out.println("Enter 10 elements into array"); for(int i=0;i<10;i++) //i from 0 to 9 array[i] = Integer.parseInt(kb.nextLine()); //input element at index i int min=array[0], max=array[0], sum=array[0]; //initialize all variables for(int i=1;i<10;i++) { //i from 1 to 9 if(array[i]max) //if element at index i more than max max=array[i]; sum=sum+array[i]; //add element at index i to sum } //print all values System.out.println("Average of 10 numbers is: "+(sum/10.0)); System.out.println("Highest number is: "+max); System.out.println("Lowest number is: "+min); } }
import java.util.Scanner;
public class Main {
static Scanner kb = new Scanner(System.in);
public static void displayMenu() {
System.out.println("a-Print Name and Tutor Name");
System.out.println("b-Largest of 3 Number");
System.out.println("c-Display Numbers in range of two numbers");
System.out.println("d-Check for Triangles");
System.out.println("e-Check for prime");
System.out.println("f-Arrays Example"); //adding statement
System.out.println("q-Quit");
System.out.print("Enter Your Choice (a,b,c,d,e,f,q) : ");
}
public static void findLargestOf3() {
double x, y, z, smallest, largest;
System.out.println("Enter 3 Numbers : ");
System.out.print("x : ");
x = Double.parseDouble(kb.nextLine());
System.out.print("y : ");
y = Double.parseDouble(kb.nextLine());
System.out.print("z : ");
z = Double.parseDouble(kb.nextLine());
smallest = (x < y && x < z) ? x : (y < x && y < z) ? y : z;
largest = (x > y && x > z) ? x : (y > x && y > z) ? y : z;
System.out.println("The Smallest Number is : " + smallest);
System.out.println("The Largest Number is : " + largest);
}
public static void displayNumbersinRange() {
int m, n, s = 0, t;
System.out.println("Enter 2 Numbers : ");
System.out.print("m : ");
m = Integer.parseInt(kb.nextLine());
System.out.print("n : ");
n = Integer.parseInt(kb.nextLine());
if (m > n) {
t = n;
n = m;
m = t;
}
for (int i = m, j = 1; i < n; i++, j++) {
System.out.print(i + " ");
if (j % 5 == 0)
System.out.println();
if (i % 2 != 0)
s += i;
}
System.out.println("\nThe Sum of all odd numbers between " + m + " and " + n + " is : " + s);
}
public static void checkTriangle() {
double s1, s2, s3;
System.out.println("Enter 3 Sides of Triangle : ");
System.out.print("s1 : ");
s1 = Double.parseDouble(kb.nextLine());
System.out.print("s2 : ");
s2 = Double.parseDouble(kb.nextLine());
System.out.print("s3 : ");
s3 = Double.parseDouble(kb.nextLine());
if (s1 > s2 + s3 || s2 > s1 + s3 || s3 > s1 + s2) {
System.out.println("The Given sides can from a Triangle");
} else
System.out.println("The Given sides can not from a Triangle");
}
public static void checkPrime() {
int m, c = 0;
System.out.print("Enter a Numbers : ");
m = Integer.parseInt(kb.nextLine());
for (int i = 1; i <= m; i++) {
if (m % i == 0)
c++;
}
if (c == 2)
System.out.println("The given number is Prime");
else
System.out.println("The given number is not a Prime");
}
public static void main(String[] args) {
char ch = 'x';
while (ch != 'q') {
displayMenu();
ch = kb.nextLine().toLowerCase().charAt(0);
switch (ch) {
case 'a':
System.out.println("My Name \n My Tutor Name");
break;
case 'b':
findLargestOf3();
break;
case 'c':
displayNumbersinRange();
break;
case 'd':
checkTriangle();
break;
case 'e':
checkPrime();
break;
case 'f': //adding another case
newFunction();
break;
case 'q':
break;
default:
System.out.println("Error : Invalid Input ");
break;
}
}
}
public static void newFunction() {
int[] array=new int[10]; //declare array
System.out.println("Enter 10 elements into array");
for(int i=0;i<10;i++) //i from 0 to 9
array[i] = Integer.parseInt(kb.nextLine()); //input element at index i
int min=array[0], max=array[0], sum=array[0]; //initialize all variables
for(int i=1;i<10;i++) { //i from 1 to 9
if(array[i]<min) //if element at index i less than min
min=array[i];
if(array[i]>max) //if element at index i more than max
max=array[i];
sum=sum+array[i]; //add element at index i to sum
}
//print all values
System.out.println("Average of 10 numbers is: "+(sum/10.0));
System.out.println("Highest number is: "+max);
System.out.println("Lowest number is: "+min);
}
}
Step by step
Solved in 3 steps with 1 images