# Java homework help

• (2 Pages)
• 1
• 2

## 15 Replies - 13493 Views - Last Post: 08 December 2011 - 12:05 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=258957&amp;s=702a352ba55c81dbb490c3d00fc8c1bb&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ambrosial921

Reputation: 0
• Posts: 8
• Joined: 07-December 11

# Java homework help

Posted 07 December 2011 - 08:07 PM

I need help with a Java Conversion Program.
Here is the problem:

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 ask convert the distance to kilometers, inches, or feet, depending on the user's selection. Here are the specific requirements:

-Write a void method named showKilometers, which accepts the number of meters as an argument. The method should display the argument converted to kilometers. Convert the meters to kilometers using the following formula:
kilometers = meters * 0.001

-Write a void method named showInches, which accepts the number of meters as an argument. The method should display the argument converted to inches. Convert the meters to inches using following formula:
inches = meters * 39.37

-Write a void method named showFeet, which accepts the number of meters as an argument. The method should display the argument converted to feet. Convert the meters to feeet using 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 and invalid choice from the menu, the program should display an error message.

-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 user's input is shown in bold.
Enter a distance in meters: 500(Enter)
1. Convert to kilometers
2. Convert o inches
3. Convert to feet
4. Quit the program

500 meters is 0.5 kilometers
1. Convert to kilometers
2. Convert o inches
3. Convert to feet
4. Quit the program

500 meters is 1640.5 feet
1. Convert to kilometers
2. Convert o inches
3. Convert to feet
4. Quit the program

Bye!

I am not good at Java, I really need help. Any help would be greatly appreciated. Here is what I have so far and I am getting a lot of errors:

```import java.util.Scanner;

public class conversionProgram
{
public static void main (String [] args)
{
double meters, choice;
double kilometers, inches, feet;
Scanner keyboard = new Scanner (System.in);

System.out.println("Enter a distance in meters: ");
meters = keyboard.nextDouble();

choice = keyboard.nextDouble;

if (choice == 1)
{
showKilometers(meters,choice);
}

else if (choice == 2)
{
showInches(meters,choice);
}

else if (choice == 3)
{
showFeet(meters,choice);
}

else if (choice == 4)
{
quit(meters,choice);
}
}
{
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 ");
System.out.println(" ");
}

public static void showKilometers()
{
kilometers = meters * 0.001;
System.out.println(meters +" meters is " + kilometers + " kilometers.");
}

public static void showInches()
{
inches = meters * 39.37;
System.out.println(meters +" meters is " + inches + " inches.");
}

public static void showfeet()
{
feet = meters * 3.281;
System.out.println(meters +" meters is " + feet + " feet.");
}

public static void quitProgram()
{
System.out.println("Goodbye!");
System.out.println(0);
}

}

```

This post has been edited by smohd: 07 December 2011 - 08:11 PM
Reason for edit:: Code tags added. Please use [code] tags when posting codes

Is This A Good Question/Topic? 0

## Replies To: Java homework help

### #2 smohd

• Critical Section

Reputation: 1822
• Posts: 4,627
• Joined: 14-March 10

## Re: Java homework help

Posted 07 December 2011 - 08:15 PM

What are those lots of errors?
```while (menu)
```
you have not declared menu in your main(). Also while() needs an opening and closing {} to show what block of code to loop.
Correct those errors and tell us if more errors are raising and what are they?

### #3 lilVaratep

• D.I.C Regular

Reputation: 37
• Posts: 287
• Joined: 09-October 11

## Re: Java homework help

Posted 07 December 2011 - 08:58 PM

Please provide us with the error(s). And don't forget: indentation/format/spacing is very important. It is a good habit to code for OTHER people to read your code. It will be helpful in finding errors quickly, making things neat and such.

### #4 ambrosial921

Reputation: 0
• Posts: 8
• Joined: 07-December 11

## Re: Java homework help

Posted 07 December 2011 - 09:03 PM

Okay, I got the program to work. I need help implementing these parts into it:

-The program should not accept negative numbers for the distance in meters.

-If the user selects and invalid choice from the menu, the program should display an error message.

-If the user selects an invalid choice from the menu, the program should display an error message.

```import java.util.Scanner;

public class conversionProgram

{
public static void showKilometers(double meters)
{
double kilometers = meters * 0.001;
System.out.println(meters +" meters is " + kilometers + " kilometers.");
}

public static void showInches(double meters)
{
double inches = meters * 39.37;
System.out.println(meters +" meters is " + inches + " inches.");
}

public static void showFeet(double meters)
{
double feet = meters * 3.281;
System.out.println(meters +" meters is " + feet + " feet.");
}

public static void quitProgram()
{
System.out.println("Goodbye!");
System.out.println(0);
}
{
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 ");
System.out.println(" ");
}

public static void main (String [] args)
{

double meters;
int choice;

Scanner keyboard = new Scanner (System.in);

System.out.println("Enter a distance in meters: ");
meters = keyboard.nextDouble();

choice = keyboard.nextInt();

switch(choice)
{
case 1: showKilometers(meters);
break;
case 2:showInches(meters);
break;
case 3:showFeet(meters);
break;
case 4:
quitProgram();
}
}
}

```
]

This post has been edited by smohd: 07 December 2011 - 09:18 PM
Reason for edit:: Code tags added. Please use [code] tags when posting codes

### #5 ambrosial921

Reputation: 0
• Posts: 8
• Joined: 07-December 11

## Re: Java homework help

Posted 07 December 2011 - 09:08 PM

Here is the output I get when I run the program but it isn't in the format the assignment is asking for.

Enter a distance in meters:
300
1. Convert to kilometers
2. Convert to inches
3. Convert to feet
4. Quit the program

1
300.0 meters is 0.3 kilometers.

What changes do I have to make it show like this:

500 meters is 0.5 kilometers
1. Convert to kilometers
2. Convert o inches
3. Convert to feet
4. Quit the program

### #6 smohd

• Critical Section

Reputation: 1822
• Posts: 4,627
• Joined: 14-March 10

## Re: Java homework help

Posted 07 December 2011 - 09:22 PM

- You will need if condition to check for user input, if (meters < 0) //invalid

- For choice, you need a default case at the end, which print the error message

- For the print format, just look what should come first, and what next in your code. But that is not logic, someone has to select choice before seeing them!!!!

### #7 ambrosial921

Reputation: 0
• Posts: 8
• Joined: 07-December 11

## Re: Java homework help

Posted 07 December 2011 - 09:41 PM

Where would I implement the while loop so it keeps running as long as the user is entering 1-4?

### #8 smohd

• Critical Section

Reputation: 1822
• Posts: 4,627
• Joined: 14-March 10

## Re: Java homework help

Posted 07 December 2011 - 09:53 PM

You will put in the loop all code that you need to be in the repetition process, that is all about then when user select to quite, just break the loop..

### #9 ambrosial921

Reputation: 0
• Posts: 8
• Joined: 07-December 11

## Re: Java homework help

Posted 07 December 2011 - 10:08 PM

I only can get the code to work once. I tried to implement the below into code and it isn't working.

```while (meters>=0)
{

choice=keyboard.nextInt();
}

while (choice<4)
{
if (choice==1)
{
showKilometers(meters);
}

else if (choice==2)
{
showInches(meters);
}

else if (choice==3)
{
showFeet(meters);
}

break;
}

if (choice==4)
{
System.out.println("You have ended the program.");
System.exit(0);
}

if (choice != 1 && choice != 2 && choice != 3 && choice != 4)
{
System.out.println("Invalid option. Please enter 1,2,3,or 4.");
}
}
}
```

This post has been edited by smohd: 07 December 2011 - 10:26 PM
Reason for edit:: Code tags added. Please use [code] tags when posting codes

### #10 smohd

• Critical Section

Reputation: 1822
• Posts: 4,627
• Joined: 14-March 10

## Re: Java homework help

Posted 07 December 2011 - 10:31 PM

Is that the only part you need it to be repeated? Dont you need user to enter new meter and new choice in every loop?
So the loop block need to contain them all, means every time you loop user is asked for input again..

So you decided to change the cases? your implementation is a little wrong:
```if (choice==1)
//code
else if (choice==2)
//code
//....
else // if none of the above is selected
//wrong choice
```

Edit: fixed what [member = 'pbl'] said

This post has been edited by smohd: 07 December 2011 - 11:18 PM

### #11 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Java homework help

Posted 07 December 2011 - 10:34 PM

smohd, on 08 December 2011 - 01:31 AM, said:

Is that the only part you need it to be repeated? Dont you need user to enter new meter and new choice in every loop?
So the loop block need to contain them all, means every time you loop user is asked for input again..

So you decided to change the cases? your implementation is a little wrong:
```If (choice==1)
//code
else if (choice==1)
//code
//....
else // if none of the above is selected
//wrong choice
```

if choice == 1 else if choice == 1

### #12 ambrosial921

Reputation: 0
• Posts: 8
• Joined: 07-December 11

## Re: Java homework help

Posted 07 December 2011 - 10:43 PM

I'm lost. My friend had sent me hers so I tried to implement something like it into mine. I don't really know what I'm doing.

### #13 smohd

• Critical Section

Reputation: 1822
• Posts: 4,627
• Joined: 14-March 10

## Re: Java homework help

Posted 07 December 2011 - 11:12 PM

What you are lost at?
What you need to do is changing the ifs condition in the way I showed above
Then make your loop to start from where you ask for meter!!!
Which part you didnt understand?

### #14 ambrosial921

Reputation: 0
• Posts: 8
• Joined: 07-December 11

## Re: Java homework help

Posted 07 December 2011 - 11:34 PM

I made the changes you mentioned and these are the errors I'm getting.

[code]
while (meters>=0)
{
choice=keyboard.nextInt();
}

while (choice<4)
{
if (choice == 1)
{
showKilometers(meters);
}

else if (choice == 2)
{
showInches(meters);
}

else if (choice == 3)
{
showFeet(meters);
}
break;
}

else if (choice==4)
{
System.out.println("You have ended the program.");
System.exit(0);
}

else (choice != 1 && choice != 2 && choice != 3 && choice != 4)
{
System.out.println("Invalid option. Please enter 1,2,3,or 4.");
}
}
}
[code]

This is the errors I'm getting when I run the program:
conversionProgrammmm.java:68: error: class, interface, or enum expected
while (meters>=0)
^
conversionProgrammmm.java:71: error: class, interface, or enum expected
choice=keyboard.nextInt();
^
conversionProgrammmm.java:72: error: class, interface, or enum expected
}
^
conversionProgrammmm.java:79: error: class, interface, or enum expected
}
^
conversionProgrammmm.java:84: error: class, interface, or enum expected
}
^
conversionProgrammmm.java:89: error: class, interface, or enum expected
}
^
conversionProgrammmm.java:91: error: class, interface, or enum expected
}
^
conversionProgrammmm.java:96: error: class, interface, or enum expected
System.exit(0);
^
conversionProgrammmm.java:97: error: class, interface, or enum expected
}
^
conversionProgrammmm.java:102: error: class, interface, or enum expected
}
^
10 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

### #15 Phorty Ounce

Reputation: 0
• Posts: 3
• Joined: 07-December 11

## Re: Java homework help

Posted 07 December 2011 - 11:54 PM

I don't understand this line:

```while (meters>=0)
{
choice=keyboard.nextInt();
}

```

Aren't you asking the user for a zero or positive integer value to store in meter, to then be passed as a parameter to your methods in your "menu"?