Question
Write a JAVA program to the code, by adding an option 'f' to your menu program. If this option is chosen, the program should ask
Write a JAVA program to the code, by adding an option 'f' to your 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: The average of the 10 numbers entered; The highest number entered; The lowest number entered
import java.util.Scanner;
public class Lab2 { static Scanner kb = new Scanner(System.in);
public static void main(String[] args) { char option; do { System.out.println("Enter one of the options:"); System.out.println("a. Display name and tutor's name"); System.out.println("b. Display largest and smallest of 3 numbers"); System.out.println("c. Display all numbers between m and n with sum of odd numbers"); System.out.println("d. Check if 3 numbers form a triangle"); System.out.println("e. Check if a number is prime"); System.out.println("f. Display the average, highest and lowest of the 10 numbers"); System.out.println("q. Quit"); option = kb.next().trim().toLowerCase().charAt(0); switch (option) { case 'a' : displayNameAndtutor(); case 'b' : displayLargestAndSmallest(); case 'c' : displayNumbersAndSum(); case 'd' : checkTriangle(); case 'e' : checkPrime(); case 'f' : AvgHighLow(); case 'q' : System.out.println("Quitting"); default : System.out.println("Error: Invalid option"); } } while (option != 'q'); }
public static void displayNameAndtutor () { System.out.println("My name: Ming Cat Chia"); System.out.println("Tutor's name: Steven Loke"); }
public static void displayLargestAndSmallest() { System.out.print("Enter first number: "); double x = kb.nextDouble(); System.out.print("Enter second number: "); double y = kb.nextDouble(); System.out.print("Enter third number: "); double z = kb.nextDouble(); double largest; largest = Math.max(x, Math.max(y, z)); double smallest; smallest = Math.min(x, Math.min(y, z)); System.out.println("Largest number: " + largest); System.out.println("Smallest number: " + smallest); }
public static void displayNumbersAndSum() { System.out.print("Enter first number: "); int m = kb.nextInt(); System.out.print("Enter second number: "); int n = kb.nextInt(); int sum = 0; int count = 0; for (int i = m; i <= n; i++) { System.out.print(i + " "); count++; if (count % 5 == 0) { System.out.println(); } if (i % 2 != 0) { sum += i; } } System.out.println(" Sum of odd numbers: " + sum); }
public static void checkTriangle() { System.out.print("Enter first number: "); int a = kb.nextInt(); System.out.print("Enter second number: "); int b = kb.nextInt(); System.out.print("Enter third number: "); int c = kb.nextInt(); if (a + b > c && a + c > b && b + c > a) { System.out.println("These numbers form a triangle."); } else { System.out.println("These numbers do not form a triangle."); } } public static void checkPrime() { System.out.print("Enter a number: "); int number = kb.nextInt(); boolean prime = true; for (int i = 2; i < number; i++) { if (number % i == 0) { prime = false; break; } } if (prime) { System.out.println("This is a prime number."); } else { System.out.println("This is not a prime number."); } }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started