Question: Write a program to add an option 'g' to your menu program. If this option is chosen, the program gets a string from the user

Write a program to add an option 'g' to your 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

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. Check for Average, Highest, Lowest"); System.out.println("g. Swapping Characters"); System.out.println("q. Quit"); option = kb.next().trim().toLowerCase().charAt(0); switch (option) { case 'a' : displayNameAndtutor(); break; case 'b' : displayLargestAndSmallest(); break; case 'c' : displayNumbersAndSum(); break; case 'd' : checkTriangle(); break; case 'e' : checkPrime(); break; case 'f': AvgHighLow(); break; case 'g': Swapping(); break; 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."); } } public static void AvgHighLow() { int[] numbers = new int[10]; System.out.println("Enter 10 numbers: "); int highest = Integer.MIN_VALUE; int lowest = Integer.MAX_VALUE; int sum = 0; for (int i = 0; i < 10; i++) { numbers[i] = kb.nextInt(); sum += numbers[i]; if (numbers[i] > highest) { highest = numbers[i]; } if (numbers[i] < lowest) { lowest = numbers[i]; } } double average = (double) sum / 10; System.out.println("The average of the numbers is: " + average); System.out.println("The highest number is: " + highest); System.out.println("The lowest number is: " + lowest); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!