Java Programming Assignment: User Menu with Conversion and Honors Methods

Assignment Prompt

Write a Java program that does the following:



  1. Prompt the user to select an action:

    • a. Convert cubic feet to U.S. bushels

    • b. Convert miles to kilometers

    • c. Determine graduation with honors title

    • d. Exit program

  2. Your program must have at least these methods:

    • a. A method to convert cubic feet to U.S. bushels (accepts cubic feet, returns bushels)

    • b. A method to convert miles to kilometers (accepts miles, returns kilometers)

    • c. A method to determine graduation honors title (accepts GPA, returns honors title string)


Java Solution

java

import java.util.Scanner; public class MenuConversions { // Convert cubic feet to US bushels (1 cubic foot = 0.803564 US bushels) public static double cubicFeetToBushels(double cubicFeet) { return cubicFeet * 0.803564; } // Convert miles to kilometers (1 mile = 1.60934 kilometers) public static double milesToKilometers(double miles) { return miles * 1.60934; } // Determine honors title based on GPA public static String honorsTitle(double gpa) { if (gpa >= 3.9) return "Summa Cum Laude"; else if (gpa >= 3.7) return "Magna Cum Laude"; else if (gpa >= 3.5) return "Cum Laude"; else if (gpa >= 2.0) return "No Honors"; else return "Not Eligible for Graduation"; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); boolean keepRunning = true; while (keepRunning) { // Show menu System.out.println("\nChoose an action:"); System.out.println("a. Convert cubic feet to U.S. bushels"); System.out.println("b. Convert miles to kilometers"); System.out.println("c. Determine graduation with honors title"); System.out.println("d. Exit program"); System.out.print("Enter your choice (a/b/c/d): "); String choice = scanner.nextLine().trim().toLowerCase(); switch (choice) { case "a": System.out.print("Enter cubic feet: "); double cubicFeet = scanner.nextDouble(); scanner.nextLine(); // consume leftover newline double bushels = cubicFeetToBushels(cubicFeet); System.out.printf("%.2f cubic feet = %.2f US bushels%n", cubicFeet, bushels); break; case "b": System.out.print("Enter miles: "); double miles = scanner.nextDouble(); scanner.nextLine(); // consume leftover newline double kilometers = milesToKilometers(miles); System.out.printf("%.2f miles = %.2f kilometers%n", miles, kilometers); break; case "c": System.out.print("Enter GPA: "); double gpa = scanner.nextDouble(); scanner.nextLine(); // consume leftover newline String honors = honorsTitle(gpa); System.out.println("Honors Title: " + honors); break; case "d": keepRunning = false; System.out.println("Exiting program. Goodbye!"); break; default: System.out.println("Invalid input. Please enter a, b, c, or d."); } } scanner.close(); } }

How to Run

  1. Save the code above as MenuConversions.java.

  2. Compile:



    javac MenuConversions.java
  3. Run:


    java MenuConversions

📩 Need a similar solution? Email me: adel455@hotmail.com








Previous Post Next Post

Comments

Nepali Graphics - Learn design, Animation, and Progrmming