943,781 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 683
  • Java RSS
Nov 1st, 2008
0

can anyone find my mistake in TrainingZone.java

Expand Post »
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..
Java Syntax (Toggle Plain Text)
  1. //Purpose:compute a heart beat training zone for your age of interest
  2. import java.util.*;
  3. public class TrainingZone{
  4. //main():application entry point
  5. public static void main(string[]args){
  6. //defining constants
  7. final double LOW_ZONE_MULTIPLIER=0.65;
  8. final double HIGH_ZONE_MULTIPLIER=0.80;
  9. final double BASE_RATE=220.0;
  10.  
  11. //displaying legend(step1)
  12. System.out.println("Heart beat estimator");
  13. //get person's characteristics
  14. System.out.print("Enter age(years): ");
  15. double age = stdin.nextDouble();
  16.  
  17. //perform trainingZone
  18. double trainingzone=HIGH_ZONE_MULTIPLIER*(BASE_RATE-ageOf Interest);
  19. double trainingzone=LOW_ZONE_MULTIPLIER*(BASE_RATE-ageOf Interest); //display result
  20. System.out.println("Suggested heart beat training zone for");
  21. System.out.println("a fit person"+"age of interest"+"years with normal");
  22. System.out.println("heart rate is"+"low BeatRate"+"to");
  23. System.out.println ("high BeatRate"+"beats per minute");
  24. }
  25. }
  26.  
  27.  
  28. it gives ')'expected errors on
  29. double trainingzone=HIGH_ZONE_MULTIPLIER*(BASE_RATE-ageOf Interest);
  30. double trainingzone=LOW_ZONE_MULTIPLIER*(BASE_RATE-ageOf Interest);
im waiting to your help)
Last edited by ~s.o.s~; Nov 1st, 2008 at 9:36 am. Reason: Added code tags, learn to use them.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chengineer is offline Offline
1 posts
since Nov 2008
Nov 1st, 2008
0

Re: can anyone find my mistake in TrainingZone.java

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)
  1. Scanner stdin = new Scanner(System.in);
Last edited by Chaster; Nov 1st, 2008 at 7:56 am.
Reputation Points: 12
Solved Threads: 3
Junior Poster in Training
Chaster is offline Offline
68 posts
since Jun 2007
Nov 1st, 2008
0

Re: can anyone find my mistake in TrainingZone.java

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:
  • 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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 1st, 2008
0

Re: can anyone find my mistake in TrainingZone.java

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)
  1. public class UsingVariables{
  2.  
  3. public static void main(String[] args){
  4. int aNewVariable = 0; //creates 'aNewVariable' & initializes it to 0.
  5. //creates 'a_Different_Variable' & doesn't initialize it.
  6. int a_Different_Variable;
  7.  
  8. System.out.println(aNewVariable); //This will print 0
  9.  
  10. //We didn't initialize the variable & can't be sure what this prints
  11. System.out.println(a_Different_Variable);
  12.  
  13. //Not allowed. a_Different_Variable already exists!
  14. int a_Different_Variable = 5;
  15.  
  16. //This initializes a_Different_Variable to 5.
  17. a_Different_Variable = 5;
  18.  
  19. System.out.println(a_Different_Variable); //This will print 5.
  20. }
  21. }
Last edited by BestJewSinceJC; Nov 1st, 2008 at 5:04 pm.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 1st, 2008
1

Re: can anyone find my mistake in TrainingZone.java

Click to Expand / Collapse  Quote originally posted by chengineer ...
can anyone find my mistake in TrainingZone.java
Yes.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Binary Search tree help in Java 98% complete
Next Thread in Java Forum Timeline: Mouse listener





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC