Welcome to Tech Support Forum home to more then 136,000 problems solved. Issues have included: Spyware, Malware, Virus Issues, Windows, Microsoft, Linux, Networking, Security, Hardware, and Gaming Getting your problem solved is as easy as:
1. Registering for a free account
2. Asking your question
3. Receiving an answer

Registered members:
* Get free support
* Communicate privately with other members (PM).
* Removal of this message
* See fewer ads.
* And much more..

 



Want to know how to post a question? click here Having problems with spyware and pop-ups? First Steps
Go Back   Tech Support Forum > Design Forum > Web Design & Programming
User Name
Password
Site Map Register Donate Rules Blogs Mark Forums Read


Web Design & Programming Discussion of web design, and server-side & client-side scripting

Reply
 
LinkBack Thread Tools
Old 09-24-2009, 06:27 PM   #1 (permalink)
Registered User
 
Join Date: Dec 2008
Posts: 6
OS: xp


EEK! Need Help on Java Programming - Weight on other planets

Hi, I need help on an java assignment, here's what it needs to do:

Ask the user for weight value on Earth, and asked the user to choose planet and calculate their weight on that planet.

1. Mecury
2. Venus
~ for all planets in the solar system
Reply: ex. 70 kg on Earth is 40kg on the moon.
Ask for another weight
Then let them put in another weight and loop.
Quit the program when 0 is entered.
Complain if invalid number is entered.
Must use 'switch'(case) statements in code


Here's the code I have so far:

import java.util.*;
public class Planetweight
{
public static void main(String[]args)
{
boolean done = false; //boolean = is program done or not
int userChoice; //integer to hold user's choice
//two doubles used in the equation
double d1 , d2; //make multiple declarations with coma
Scanner inputReader = new Scanner(System.in);

System.out.print("Choose a planet to find out how much you weigh on that planet");

//give user choices
System.out.println("Choose from the following options:\n");
System.out.println(" 1. Mercury"); //0.38
System.out.println(" 2. Venus"); //0.91
System.out.println(" 3. Earth"); //1.0
System.out.println(" 4. Mars"); //0.38
System.out.println(" 5. Jupiter"); //2.54
System.out.println(" 6 Saturn."); //107
System.out.println(" 7. Uranus"); //90
System.out.println(" 8. Neptune"); //1.15
System.out.println(" 9. Pluto"); //0.08

userChoice = inputReader.nextInt();

//ask the user for the two numbers
System.out.print("\n Enter your current weight on Earth in kilograms:");
d1 = inputReader.nextDouble();

System.out.print(d1 + "kg on Earth is" );
}
}

the numbers after the // is the comparison of the gravitational pull on that planet compared to Earth's.

any input would be appreciated
bjc92113 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Important Information
Join the #1 Tech Support Forum Today - It's Totally Free!

TechSupportForum.com is a leading support website for your computer needs. We offer free, friendly and personalized computer support. Why pay to have your computer fixed when you can do it for free.

Join TechSupportforum.com Today - Click Here

Old 09-26-2009, 12:14 PM   #2 (permalink)
Moderator: Design
 
dm01's Avatar
 
Join Date: Oct 2006
Location: Richmond, B.C.; Canada
Posts: 1,475
OS: Windows XP [Version 5.1.2600] SP3 | Ubuntu Jaunty Jackalope | Windows 7 Ultimate (MSDNAA I <3 you)

My System

Re: Need Help on Java Programming - Weight on other planets

Which part do you need help with? Are you just plain stuck, or do you have some idea where you need to go with this?
__________________


Validate your Markup Validate your CSS Notepad++
Please use [html], [php], and [code] when posting code or markup.
I do not help by Private Message or e-mail. If for some reason I have over-looked a reply to a thread that I have previously replied to, then send me a message.
dm01 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 09-27-2009, 12:44 AM   #3 (permalink)
Registered User
 
Join Date: Dec 2008
Posts: 6
OS: xp


Re: Need Help on Java Programming - Weight on other planets

program is supposed to calculate the weight of the person on a different planet and respond in this format:
" 'x' kg on Earth is equal to 'y' kg on (selected planet)"

I'm stuck on how to put the name of the planet selected by the user into the response..
bjc92113 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 09-27-2009, 02:29 PM   #4 (permalink)
Moderator: Design
 
dm01's Avatar
 
Join Date: Oct 2006
Location: Richmond, B.C.; Canada
Posts: 1,475
OS: Windows XP [Version 5.1.2600] SP3 | Ubuntu Jaunty Jackalope | Windows 7 Ultimate (MSDNAA I <3 you)

My System

Re: Need Help on Java Programming - Weight on other planets

Looks like you're going to need a string for the planet name.

String planetname = null;

You only need one switch to make this work. Read http://java.sun.com/docs/books/tutor...ts/switch.html for a very good explanation of how the switch statement works.

I'll let you figure out the rest on your own. If you have any questions, please post back. This particular assignment has intrigued me, so I decided to build one as well :). The finicky validation and repeats stymied me, but I think I got it now.
__________________


Validate your Markup Validate your CSS Notepad++
Please use [html], [php], and [code] when posting code or markup.
I do not help by Private Message or e-mail. If for some reason I have over-looked a reply to a thread that I have previously replied to, then send me a message.
dm01 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 09-27-2009, 06:42 PM   #5 (permalink)
Registered User
 
Join Date: Dec 2008
Posts: 6
OS: xp


Re: Need Help on Java Programming - Weight on other planets

Here's my current Code now:

import java.util.*;
public class Planetweight
{
public static void main(String[]args)
{
boolean done = false; //boolean = is program done or not
int userChoice; //integer to hold user's choice
int i = 0;
//two doubles used in the equation
double d1; //declare d1 as a double
Scanner inputReader = new Scanner(System.in);

System.out.print("Choose a planet to find out how much you weigh on that planet.\n");

//give user choices
System.out.println("Choose from the following options:\nType 0 to quit.\n");
System.out.println(" 1. Mercury"); //0.38
System.out.println(" 2. Venus"); //0.91
System.out.println(" 3. Earth"); //1.0
System.out.println(" 4. Mars"); //0.38
System.out.println(" 5. Jupiter"); //2.54
System.out.println(" 6 Saturn."); //1.07
System.out.println(" 7. Uranus"); //0.9
System.out.println(" 8. Neptune"); //1.15
System.out.println(" 9. Pluto"); //0.08
System.out.println(" 10. Moon"); //0.17

userChoice = inputReader.nextInt();

//ask the user for the two numbers
System.out.print("\n Enter your current weight on Earth in kilograms:");
d1 = inputReader.nextDouble();

while (!done)
{
if (i==0)
{
System.out.println("Goodbye.");
done = true;
}
else if (!(i==0))



switch(userChoice)
{
case 1:
System.out.println(d1 + "kg on Earth is " + d1*0.38 + "kg on Mercury.");
break; //tells system to break out

case 2:
System.out.println(d1 + "kg on Earth is " + d1*0.91 + "kg on Venus.");
break;

case 3:
System.out.println(d1 + "kg on Earth is " + d1*1.0 + "kg on Earth.");
break;

case 4:
System.out.println(d1 + "kg on Earth is " + d1*0.38 + "kg on Mars.");
break;

case 5:
System.out.println(d1 + "kg on Earth is " + d1*2.54 + "kg on Jupiter.");
break;

case 6:
System.out.println(d1 + "kg on Earth is " + d1*1.07 + "kg on Saturn.");
break;

case 7:
System.out.println(d1 + "kg on Earth is " + d1*0.9 + "kg on Uranus.");
break;

case 8:
System.out.println(d1 + "kg on Earth is " + d1*1.15 + "kg on Neptune.");
break;

case 9:
System.out.println(d1 + "kg on Earth is " + d1*0.8 + "kg on Pluto.");
break;

case 10:
System.out.println(d1 + "kg on Earth is " + d1*0.17 + "kg on the Moon.");
break;

default: //user picked a bogus value
System.out.println("invalid input");
break;
}

}


}
}

but I'm having problems getting the program to loop using WhileLoops
btw, also needs to loop when user enters a invalid input
bjc92113 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 09-27-2009, 08:18 PM   #6 (permalink)
Moderator: Design
 
dm01's Avatar
 
Join Date: Oct 2006
Location: Richmond, B.C.; Canada
Posts: 1,475
OS: Windows XP [Version 5.1.2600] SP3 | Ubuntu Jaunty Jackalope | Windows 7 Ultimate (MSDNAA I <3 you)

My System

Re: Need Help on Java Programming - Weight on other planets

For validation, you are going to need several boolean variables and while statements. You could use do while statements, but they are not considered good practice. The while loop operates on a condition, just like any other loop statement. This condition can be as simple as x==1 or planet_Flag==false; more complex conditions can be created using operators, but this is unnecessary.

while statements are fairly simple to implement singly. Once you get into stacked loop statements, things become tricky.
Code:
byte x == 1;

while (x==1)
{
x=2; while (x==2) {
x=3 while (x==1) {
x=4 while (x==4) {
x=16
}
} while (x==3) x=6
}
}
What number is x assigned to? x==6

As always, if you have any questions, post back :)
__________________


Validate your Markup Validate your CSS Notepad++
Please use [html], [php], and [code] when posting code or markup.
I do not help by Private Message or e-mail. If for some reason I have over-looked a reply to a thread that I have previously replied to, then send me a message.
dm01 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 09-28-2009, 09:19 AM   #7 (permalink)
Registered User
 
Join Date: Dec 2008
Posts: 6
OS: xp


Re: Need Help on Java Programming - Weight on other planets

The code above is mainly composed of while loops, but I'm required by my teacher to use switchcases, so here's what I tried:

/*
Ask the user for weight value on Earth, and asked the user to choose planet and calculate their weight on that planet.

1. Mecury
2. Venus
~ for all planets in the solar system

Reply: ex. 70 kg on Earth is 40kg on the moon.

Ask for another weight
Then let them put in another weight and loop.
Quit the program when 0 is entered.

Complain if invalid number is entered.

Must use 'switch'(case) statements in code

Due: next Friday

*/
import java.util.*;
public class Planetweight
{
public static void main(String[]args)
{
boolean done = false; //boolean = is program done or not
int userChoice; //integer to hold user's choice
//two doubles used in the equation
double d1; //declare d1 as a double
Scanner inputReader = new Scanner(System.in);

System.out.print("Choose a planet to find out how much you weigh on that planet.\nType 0 to quit.\n \n");

//give user choices
System.out.println(" 1. Mercury"); //0.38
System.out.println(" 2. Venus"); //0.91
System.out.println(" 3. Earth"); //1.0
System.out.println(" 4. Mars"); //0.38
System.out.println(" 5. Jupiter"); //2.54
System.out.println(" 6 Saturn."); //1.07
System.out.println(" 7. Uranus"); //0.9
System.out.println(" 8. Neptune"); //1.15
System.out.println(" 9. Pluto"); //0.08
System.out.println(" 10. Moon"); //0.17

userChoice = inputReader.nextInt();



while (userChoice = inputReader.nextInt()) //if the user did not type 0, the calculator will begin
{

//ask the user for weight
System.out.print("\n Enter your current weight on Earth in kilograms:");
d1 = inputReader.nextDouble();

switch(userChoice) //takes the case chooses the action to perform
{
case 1:
System.out.println(d1 + "kg on Earth is " + d1*0.38 + "kg on Mercury.");
break; //tells system to break out

case 2:
System.out.println(d1 + "kg on Earth is " + d1*0.91 + "kg on Venus.");
break;

case 3:
System.out.println(d1 + "kg on Earth is " + d1*1.0 + "kg on Earth.");
break;

case 4:
System.out.println(d1 + "kg on Earth is " + d1*0.38 + "kg on Mars.");
break;

case 5:
System.out.println(d1 + "kg on Earth is " + d1*2.54 + "kg on Jupiter.");
break;

case 6:
System.out.println(d1 + "kg on Earth is " + d1*1.07 + "kg on Saturn.");
break;

case 7:
System.out.println(d1 + "kg on Earth is " + d1*0.9 + "kg on Uranus.");
break;

case 8:
System.out.println(d1 + "kg on Earth is " + d1*1.15 + "kg on Neptune.");
break;

case 9:
System.out.println(d1 + "kg on Earth is " + d1*0.8 + "kg on Pluto.");
break;

case 10:
System.out.println(d1 + "kg on Earth is " + d1*0.17 + "kg on the Moon.");
break;

case 0:
System.out.println("Goodbye!");
System.exit(0);
break;

default: //user picked a bogus value
System.out.println("Pick a valid number, retard");
break;
}

}
if (userChoice==0) //tells the user program is done
{
System.out.println("Goodbye!!");
}


}
}

but when I compile this, I get this error:

---------- Capture Output ----------
> "C:\Program Files\Java\jdk1.6.0_16\bin\javac.exe" Planetweight.java
Planetweight.java:50: incompatible types
found : int
required: boolean
while (userChoice = inputReader.nextInt()) //if the user did not type 0, the calculator will begin
^
1 error
> Terminated with exit code 1.

but I don't know how to change it into boolean, cuz whatever I change will only return me with more errors..
bjc92113 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 09-28-2009, 09:15 PM   #8 (permalink)
Moderator: Design
 
dm01's Avatar
 
Join Date: Oct 2006
Location: Richmond, B.C.; Canada
Posts: 1,475
OS: Windows XP [Version 5.1.2600] SP3 | Ubuntu Jaunty Jackalope | Windows 7 Ultimate (MSDNAA I <3 you)

My System

Re: Need Help on Java Programming - Weight on other planets

Code:
userChoice = inputReader.nextInt();

while (userChoice = inputReader.nextInt())
This reads as: "the integer that the user entered is assigned to userChoice. While the integer that the user entered is assigned to userChoice, the statements in the squirly brackets underneath me may execute." Although this looks legal in English, it is in fact an impossible condition, and the compiler is very confused as a result.

Operating conditions almost invariably consist of a variable, one or more operands, and a number or other variable(s). In this case, you want userChoice to be greater than or equal to zero. I would initialize userChoice to -1 at the start of each iteration to clear this condition.

Code:
while (userChoice >= 0)
You don't really need this if you have a case 0:
Code:
if (userChoice==0) //tells the user program is done
{
System.out.println("Goodbye!!");
}
Just to help clean things up a bit.

Good luck, and remember: sometimes the compiler does not have the best solution.

PS: insulting the user will not win you any favours with the instructor. I learned this the hard way :S.
__________________


Validate your Markup Validate your CSS Notepad++
Please use [html], [php], and [code] when posting code or markup.
I do not help by Private Message or e-mail. If for some reason I have over-looked a reply to a thread that I have previously replied to, then send me a message.
dm01 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 09-29-2009, 06:03 PM   #9 (permalink)
Registered User
 
Join Date: Dec 2008
Posts: 6
OS: xp


Re: Need Help on Java Programming - Weight on other planets

thx for the helps, I managed to get the code to work.
bjc92113 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 09-29-2009, 06:19 PM   #10 (permalink)
Moderator: Design
 
dm01's Avatar
 
Join Date: Oct 2006
Location: Richmond, B.C.; Canada
Posts: 1,475
OS: Windows XP [Version 5.1.2600] SP3 | Ubuntu Jaunty Jackalope | Windows 7 Ultimate (MSDNAA I <3 you)

My System

Re: Need Help on Java Programming - Weight on other planets

Glad to hear it. Drop by any time if you're having trouble.
__________________


Validate your Markup Validate your CSS Notepad++
Please use [html], [php], and [code] when posting code or markup.
I do not help by Private Message or e-mail. If for some reason I have over-looked a reply to a thread that I have previously replied to, then send me a message.
dm01 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -7. The time now is 04:46 AM.



Copyright 2001 - 2009, Tech Support Forum
Home Tips Plus | Outdoor Basecamp | Automotive Support Forum

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85