Java homework help

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 2433 Views - Last Post: 08 December 2011 - 12:05 AM Rate Topic: -----

#1 ambrosial921  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Enter your choice: 1 [Enter]
500 meters is 0.5 kilometers
1. Convert to kilometers
2. Convert o inches
3. Convert to feet
4. Quit the program

Enter your choice: 3 [Enter]
500 meters is 1640.5 feet
1. Convert to kilometers
2. Convert o inches
3. Convert to feet
4. Quit the program

Enter your choice: 4 [Enter]
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();

while (menu)

showMenu();
System.out.println("Enter enter your choice: ");
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);
}
}
public static void showMenu()
{
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  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Java homework help

Posted 07 December 2011 - 08:15 PM

What are those lots of errors?
What is menu here?
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?
Was This Post Helpful? 0
  • +
  • -

#3 lilVaratep  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 207
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 ambrosial921  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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);
}
public static void showMenu()
{
		System.out.println(" Please enter your 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 main (String [] args)
{

		double meters;
		int choice;

		Scanner keyboard = new Scanner (System.in);

		System.out.println("Enter a distance in meters: ");
		meters = keyboard.nextDouble();
		
		showMenu();
		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

Was This Post Helpful? 0
  • +
  • -

#5 ambrosial921  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Please enter your choice:
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:

Enter your choice: 1 [Enter]
500 meters is 0.5 kilometers
1. Convert to kilometers
2. Convert o inches
3. Convert to feet
4. Quit the program
Was This Post Helpful? 0
  • +
  • -

#6 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • 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!!!!
Was This Post Helpful? 0
  • +
  • -

#7 ambrosial921  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#8 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • 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..
Was This Post Helpful? 0
  • +
  • -

#9 ambrosial921  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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)    
{
                  
      System.out.println("Select your choice: ");
      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.");
      }
}		  
}


Mentor Note: Please :code:

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

Was This Post Helpful? 0
  • +
  • -

#10 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • 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

Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Java homework help

Posted 07 December 2011 - 10:34 PM

View Postsmohd, 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 :)
Was This Post Helpful? 0
  • +
  • -

#12 ambrosial921  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#13 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • 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?
Was This Post Helpful? 0
  • +
  • -

#14 ambrosial921  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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)
{
System.out.println("Select your choice: ");
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.
Was This Post Helpful? 0
  • +
  • -

#15 Phorty Ounce  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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) 
{
System.out.println("Select your choice: ");
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"?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2