Write a program that asks the user to enter a distance in meters. The program will then present the following menu of selections:
1. Convert to kilometers
2. Convert to inches
3. Convert to feet
4. Quit the program
The program will convert the distance to kilometers, inches, or feet, depending on the users selection. Here are the specific requirements:
Write a method named calcKilometers, which accepts the number of meters as an argument and returns the number of kilometers using the following formula: kilometers = meters * .001
Write a method named calcInches, which accepts the number of meters as an argument and returns the number of inches using the following formula: inches = meters * 39.37
Write a method named calcFeet, which accepts the number of meters as an argument and returns the number of feet using the following formula: feet = meters * 3.281
Write a void method named menu that displays the menu of selections. This method should not accept any arguments.
The program should continue to display the menu until the user enters 4 to quit the program.
The program should not accept negative numbers for the distance in meters.
If the user selects an invalid choice from the menu, the program should display an error message.
Here is an example session with the program, using console input. The users input is shown in bold:
Enter a distance in meters: 500 [Enter]
1. Convert to kilometers.
2. Convert to inches.
3. Convert to feet.
4. Quit the program.
Enter your choice: 1 [Enter]
500 Meters is 0.5 kilometers.
1. Convert to kilometers.
2. Convert to inches.
3. Convert to feet.
4. Quit the program.
Enter your choice: 4 [Enter]
Bye!
The purpose of this assignment is to get familiar with writing and calling methods. I have most of my script already written out (except I need to add error statements when user enters negative number, or invalid menu choice), but I'm having a lot of trouble organizing & correctly laying out my methods/variables/arguments/conditions. I'm getting about 20+ errors, & I know this is due to incorrect format. Otherwise, I'm usually pretty decent at identifying & correcting errors. I was wondering if someone could please offer some advice, an example script, or any other tidbits to get me going on the right track?
Here is my script thus far:
/***********************************************************************
@Title: BSarahConversion.java
@Purpose: To get familiar with writing and calling methods
@Author: (BSarah)
@Date: (October 09, 2010)
@Version: 1.0
************************************************************************/
import java.util.Scanner;
public class BSarahConversion
{
public static void main(String[] args)
{
int quit = 4;
int usersChoice;
Scanner sc = new Scanner(System.in);
System.out.print("Please enter distance in meters: ");
inputMeters = sc.nextDouble();
menu();
System.out.println("Please make a selection between 1-4: ");
usersChoice = sc.nextInt();
do
menu();
while (usersChoice = sc.nextInt());
if (usersChoice==1)
{
calcKilometers();
}
if (usersChoice==2)
{
calcInches();
}
if (usersChoice==3)
{
calcFeet();
}
while (usersChoice==quit);
{
System.out.println("Good bye!");
}
public static void menu()
{
System.out.println("1. Convert to kilometers");
System.out.println("2. Convert to inches");
System.out.println("3. Convert to feet");
System.out.println("4. Quit the program");
}
public static calcKilometers()
{
double convertedInput = (inputMeters * .001);
System.out.println(convertedInput);
}
public static void calcInches()
{
double convertedInput = (inputMeters * 39.37);
System.out.println(convertedInput);
}
public static void calcFeet()
{
double convertedInput = (inputMeters * 3.281);
System.out.println(convertedInput);
}
}
}
**I also suspect my loop is wrong. Especially since I only need to loop the menu & user inputMeters. I think once I place my condition statments in the methods as arugments, then that will correct most of that problem. Still not sure how to do this correctly tho.
This post has been edited by DirtyMartyr: 11 October 2010 - 08:00 PM

New Topic/Question
Reply




MultiQuote







|