# Homework for class, teacher's not helping

• February 7th, 2013, 12:01 PM
DarkHatter
Homework for class, teacher's not helping
my code is looks like this:
Code java:

```import java.util.*; public class PojectConvert{   public void showFeet(double meters){ double feet = meters * 3.281; System.out.println(meters+" meters is "+feet+" feet"); } public void showMeter(double feet){ double meters = feet * 0.3048; System.out.println(feet +" feet is "+ meters +" meters."); } public void showKilograms(double pounds){ double kilograms = pounds * 0.454; System.out.println(pounds +" pounds is "+kilograms+" kilograms"); } public void showPounds(double kilograms){ double pounds = kilograms * 2.2808; System.out.println(kilograms+" kilograms is "+pounds+" pounds"); } public void showGallons(double liters){ double gallons = liters * 3.7854; System.out.println(liters+" liters is "+gallons+" gallons"); } public void showLiters(double gallons){ double liters = gallons * 0.26417; System.out.println(gallons+" gallons is "+liters+" liters"); }   public void menu(){ System.out.print("Unit converter \n"); System.out.println("1 Convert Length"); System.out.println("2 Convert Weight"); System.out.println("3 Convert volume"); System.out.println("Enter in amount to convert: "); Scanner scan=new Scanner(System.in); double m=scan.nextDouble(); boolean quit = false;     do{ System.out.println(); System.out.println("1. Meters to feet"); System.out.println("2. Feet to meters"); System.out.println("3. pounds to kilograms"); System.out.println("4. kilograms to pounds"); System.out.println("5. Gallons to liters"); System.out.println("6. Liters to gallons"); System.out.println("7. I give up, I can't go on!"); System.out.println(); System.out.print("Enter your choice: "); int menu = scan.nextInt(); switch(menu) { case 1: showFeet(m); break; case 2: showMeter(m); break; case 3: showKilograms(m); break; case 4: showPounds(m); break; case 5: showGallons(m); break; case 6: showLiters(m); break; case 7: quit = true; System.out.println("We lost another one."); break; default: System.out.println("Invalid Entry!"); } }     while (!quit);   } public static void main(String[]args){ PojectConvert convert=new PojectConvert(); convert.menu(); } }```

she wants it to be able to do this

1. first you should ask the user what type of conversion he wants (1. length, 2. wight, 3. volume)
int t=scan.nextInt();

2. then you should use a switch statement to display only the units corresponding to that type of conversion:
switch (t) {
case 1:
System.out.println("1. Meters to feet");
System.out.println("2. Feet to meters");
break;
case 2:
System.out.println("1. pounds to kilograms");
System.out.println("2. kilograms to pounds");
break;
case 3:
System.out.println("1. Gallons to liters");
System.out.println("2. Liters to gallons");
break;
}

3. Then you should ask for the conversion direction
int direction= scan.nextInt();

4. And then you should ask for the value to be converted:
int m = scan.nextInt();

5. Now you have all the info you need, you can write another switch statement to do the conversion:
switch (t) {
case 1: if (direction==1) showFeet(m);
else showMeter(m);
break;
case 2:if (direction==1) showKilograms(m);
else showPounds(m);
break;
case 3:....
}

The other thing you need to do is: every time you ask the user to make a selection, you have to make sure he enters a valid value.

For example, when asking for the type of conversion (step 1 above) you have to make sure the value entered is 1, 2 or 3.
So you need to use a loop:
int t;
do {
System.out.println(....);
t=scan.nextInt();
} while (t!=1 && t!=2 && t!=3); // as long as t is different from 1, 2 and 3 the user will be asked to entered a value again

i have no idea how to change my program to make all that work. can anyone help me out?
• February 7th, 2013, 12:08 PM
Norm
Re: Homework for class, teacher's not helping
[code=java]
[/code]
to get highlighting and preserve formatting.
Be sure the code has proper indentations for nested statements. All statements should not start in the first column.

Quote:

how to change my program to make all that work
What does the program currently do?

Take the items in the list one at a time.
What problem do you have with the first one?
• February 7th, 2013, 12:13 PM
DarkHatter
Re: Homework for class, teacher's not helping
It converts measurements.
volume, length, weight.
she wants it to be able to do this:Attachment 1758
• February 7th, 2013, 12:18 PM
Norm
Re: Homework for class, teacher's not helping
You missed this part:
Be sure the code has proper indentations for nested statements. All statements should not start in the first column.
Unformatted code is hard to read and understand.

What items on the list have you done?
What is the next item on the list you are working on? What specific questions do you have about that item?

It's not possible to read the image.
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.
• February 7th, 2013, 12:29 PM
DarkHatter
Re: Homework for class, teacher's not helping
its supposed to ask the user what type of converson does he/she want: 1. length, 2. weight, 3. volume. user chooses one then its suppoed to ask whether or not its should be, lets say user chooses 1, it will ask if the user wants to convert feet to meters or meters to feet. after that it'll ask user to enter how much to convert. enter in amount and then it'll show how much then it'll ask the user if he wants to perform another conversion or not.
ive done the conversion parts and that works fine but i dont have it asking user all these things and i don't know how too. i'm very new at java programming, well programming in general.
• February 7th, 2013, 12:32 PM
Norm
Re: Homework for class, teacher's not helping
Which one item are you working on now? Do them one at a time.

You missed this part: You need to edit the code and fix the formatting.
Be sure the code has proper indentations for nested statements. All statements should not start in the first column.
Unformatted code is hard to read and understand.
• February 7th, 2013, 12:45 PM
DarkHatter
Re: Homework for class, teacher's not helping
im working on setting up the user interface to it. and i dont know what you mean its hard to read. i read it just fine.
• February 7th, 2013, 12:48 PM
Norm
Re: Homework for class, teacher's not helping
If you are the only one that will read it, then you can have it anyway you want.
There are formatting standards that most of us follow and we'd like any code we work with to follow those standards.
Code Conventions for the Java Programming Language: Contents

Quote:

im working on setting up the user interface
What specific questions do you have?
• February 7th, 2013, 02:27 PM
DarkHatter
Re: Homework for class, teacher's not helping
how do i change my format to match hers
• February 7th, 2013, 02:35 PM
Norm
Re: Homework for class, teacher's not helping
Can you post the outputs from both programs so they can be compared? The image you posted was not readable.
If all you have to worry about is making a pretty print out, you must be close to having the program working.
I usually leave making the print out pretty until after I've got the logic and computations worked out.