Write a program to extend the below program, by adding an option 'f' to the menu program. If this option is chosen, the program should ask the user to enter 10 integers. The program should store each integer into an array (as each one is entered). Once the array contains the 10 integers, the program should output:
a) The average of the 10 numbers entered
b) The highest number entered
c) The lowest number entered
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("q-Quit"); System.out.print("Enter Your Choice (a,b,c,d,e,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 'q':break; default : System.out.println("Error : Invalid Input ");break; } } } }
|