| | |
can anyone find my mistake in TrainingZone.java
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2008
Posts: 1
Reputation:
Solved Threads: 0
hi,i started to learn java nearly 2 weeks ago so i dont know java clearly,i try to write a program about trainingzone.
i must provide an interactive training zonecalculator that prompts a user for age.
i write a program but it gives errors and i dont improve this,if anyone can help me i will be too happy ,maybe most of people know this program and it seems easy but please dont forget im starter of java.Already now thanks to everybody..
im waiting to your help
)
i must provide an interactive training zonecalculator that prompts a user for age.
i write a program but it gives errors and i dont improve this,if anyone can help me i will be too happy ,maybe most of people know this program and it seems easy but please dont forget im starter of java.Already now thanks to everybody..
Java Syntax (Toggle Plain Text)
//Purpose:compute a heart beat training zone for your age of interest import java.util.*; public class TrainingZone{ //main():application entry point public static void main(string[]args){ //defining constants final double LOW_ZONE_MULTIPLIER=0.65; final double HIGH_ZONE_MULTIPLIER=0.80; final double BASE_RATE=220.0; //displaying legend(step1) System.out.println("Heart beat estimator"); //get person's characteristics System.out.print("Enter age(years): "); double age = stdin.nextDouble(); //perform trainingZone double trainingzone=HIGH_ZONE_MULTIPLIER*(BASE_RATE-ageOf Interest); double trainingzone=LOW_ZONE_MULTIPLIER*(BASE_RATE-ageOf Interest); //display result System.out.println("Suggested heart beat training zone for"); System.out.println("a fit person"+"age of interest"+"years with normal"); System.out.println("heart rate is"+"low BeatRate"+"to"); System.out.println ("high BeatRate"+"beats per minute"); } } it gives ')'expected errors on double trainingzone=HIGH_ZONE_MULTIPLIER*(BASE_RATE-ageOf Interest); double trainingzone=LOW_ZONE_MULTIPLIER*(BASE_RATE-ageOf Interest);
) Last edited by ~s.o.s~; Nov 1st, 2008 at 9:36 am. Reason: Added code tags, learn to use them.
•
•
Join Date: Jun 2007
Posts: 59
Reputation:
Solved Threads: 3
well you have a few mistakes.. For ex. ageOf Interest is what? I mean is it a variable (and should be ageOfInterest)? Then, you define twice the variable trainingzone. The second one should be trainingzone1 or something like that. And to read double values from standard input you need first to init your stdin variable like this:
Java Syntax (Toggle Plain Text)
Scanner stdin = new Scanner(System.in);
Last edited by Chaster; Nov 1st, 2008 at 7:56 am.
•
•
Join Date: Sep 2008
Posts: 1,598
Reputation:
Solved Threads: 202
Chaster is correct, you must define your variables before you use them. A variable in computer science is the same as it is in normal algebra. For example, x and y are commonly used variables in algebra. Similarly, in Java programming, you can define your own variables. To define a variable of type double named Age of Interest, do the following.
double age_Of_Interest;
Please note the following things about the declaration above:
double age_Of_Interest;
Please note the following things about the declaration above:
- it is intentionally spelled age_Of_Interest because, by convention, variable names start with a lowercase letter. Each word in the variable name (except the first word) starts with an uppercase letter.
- Java does not allow you to put spaces in your variable names, so if you want to separate words, you have to do so using underscores. ( _ )
- After the declaration above, age_Of_Interest does not have a value. Giving a variable a value is called "initializing the variable". So after we declared the variable, if we had said age_Of_Interest = 3.5, that is initializing the variable.
- A variable can only be declared once. When you first put the type and the name of the variable, that declares it. So double age_Of_Interest; is a variable declaration.
- You should not use a variable before it is declared and initialized. And the compiler will not let you use a variable that is not declared, which is why you got errors.
- The first time you say double age_Of_Interest; you are creating a variable called age_Of_Interest. After you do that, if you say double age_Of_Interest; again, you are trying to create a variable that already exists, which does not make sense and is not allowed. To use the variable after its been declared/created, all you have to do is say age_Of_Interest.
Last edited by BestJewSinceJC; Nov 1st, 2008 at 4:54 pm.
•
•
Join Date: Sep 2008
Posts: 1,598
Reputation:
Solved Threads: 202
A quick example of what I explained in my post above. Comments start with // and are either above the line they refer to, or next to the line they refer to.
Java Syntax (Toggle Plain Text)
public class UsingVariables{ public static void main(String[] args){ int aNewVariable = 0; //creates 'aNewVariable' & initializes it to 0. //creates 'a_Different_Variable' & doesn't initialize it. int a_Different_Variable; System.out.println(aNewVariable); //This will print 0 //We didn't initialize the variable & can't be sure what this prints System.out.println(a_Different_Variable); //Not allowed. a_Different_Variable already exists! int a_Different_Variable = 5; //This initializes a_Different_Variable to 5. a_Different_Variable = 5; System.out.println(a_Different_Variable); //This will print 5. } }
Last edited by BestJewSinceJC; Nov 1st, 2008 at 5:04 pm.
![]() |
Other Threads in the Java Forum
- Previous Thread: Binary Search tree help in Java 98% complete
- Next Thread: Mouse listener
| Thread Tools | Search this Thread |
android api applet application array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) chat class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse editor error errors event eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads time tree unlimited utility webservices windows






