JAVA Define a class called Converter that has four static methods: - One which t
ID: 3876260 • Letter: J
Question
JAVA
Define a class called Converter that has four static methods:
- One which takes in a number of kilograms and converts to pounds (1 kg = 2.2 pounds)
- One which takes in a number of pounds and converts to kilograms (1 pound = 0.45 kg)
- One which takes in a number of degrees and converts to radians (1 degree = Pi/180 radians)
- One which takes in a number of radians and converts to degrees (1 radian = 180/Pi degrees)
This class should store Pi as 3.14 and ensure that its value cannot be changed. Each method should be defined to take one number as an argument, perform a calculation on the number, and return a result.
Write a program that accepts user inputs. The program should ask a user to enter a number and choose which of the four methods to call. Whenever the user chooses a valid option, the program should invoke an appropriate method of the Converter class and display its result.
Explanation / Answer
Menu.java
import java.util.Scanner;
public class Menu {
//Declaring constant
static double PI = 3.14;
public static void main(String[] args) {
//Declaring variables
int choice = 0;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
/* This while loop continues to execute
* until the user enters a valid choice
*/
while (true) {
//displaying the menu
System.out.println(" ::MENU ::");
System.out.println("1.Kilograms to Pounds");
System.out.println("2.Pounds to kilograms");
System.out.println("3.Degrees to radians");
System.out.println("4.Radians to Degrees");
System.out.println("5.Quit");
//getting the choice entered by the user
System.out.print(" Enter choice :");
choice = sc.nextInt();
//Based on the user choice the corresponding case will be executed
switch (choice) {
case 1:
{
double kgs,
pounds;
//Getting the input entered by the user
System.out.print("Enter no of Kgs :");
kgs = sc.nextDouble();
//calling the method
pounds = kilogramsToPounds(kgs);
System.out.println(kgs + " kgs is " + pounds + " Pounds.");
continue;
}
case 2:
{
double kgs,
pounds;
//Getting the input entered by the user
System.out.print("Enter no of Pounds :");
pounds = sc.nextDouble();
//calling the method
kgs = poundsToKilograms(pounds);
System.out.println(pounds + " Pounds is " + kgs + " Kgs.");
continue;
}
case 3:
{
double degrees,
radians;
//Getting the input entered by the user
System.out.print("Enter Degrees :");
degrees = sc.nextDouble();
//calling the method
radians = degreesToRadians(degrees);
System.out.println(degrees + " Degrees is " + radians + " Radians.");
continue;
}
case 4:
{
double degrees,
radians;
//Getting the input entered by the user
System.out.print("Enter Radians :");
radians = sc.nextDouble();
//calling the method
degrees = radiansToDegrees(radians);
System.out.println(radians + " Radians is " + degrees + " Degrees.");
continue;
}
case 5:
{
break;
}
default:
{
System.out.println("** Invalid Choice **");
continue;
}
}
break;
}
}
//This method will convert radians to degrees
private static double radiansToDegrees(double radians) {
return 180 / PI;
}
//This method will convert degrees to radians
private static double degreesToRadians(double degrees) {
return PI / 180;
}
//This method will convert pounds to kgs
private static double poundsToKilograms(double pounds) {
return pounds * 0.45;
}
//This method will convert kgs to pounds
private static double kilogramsToPounds(double kgs) {
return 2.2 * kgs;
}
}
__________________
Output:
::MENU ::
1.Kilograms to Pounds
2.Pounds to kilograms
3.Degrees to radians
4.Radians to Degrees
5.Quit
Enter choice :1
Enter no of Kgs :500
500.0 kgs is 1100.0 Pounds.
::MENU ::
1.Kilograms to Pounds
2.Pounds to kilograms
3.Degrees to radians
4.Radians to Degrees
5.Quit
Enter choice :2
Enter no of Pounds :450
450.0 Pounds is 202.5 Kgs.
::MENU ::
1.Kilograms to Pounds
2.Pounds to kilograms
3.Degrees to radians
4.Radians to Degrees
5.Quit
Enter choice :3
Enter Degrees :120
120.0 Degrees is 0.017444444444444446 Radians.
::MENU ::
1.Kilograms to Pounds
2.Pounds to kilograms
3.Degrees to radians
4.Radians to Degrees
5.Quit
Enter choice :4
Enter Radians :430
430.0 Radians is 57.324840764331206 Degrees.
::MENU ::
1.Kilograms to Pounds
2.Pounds to kilograms
3.Degrees to radians
4.Radians to Degrees
5.Quit
Enter choice :5
_______________Could you plz rate me well.Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.