These are the instructions, I am not asking anyone to code for me but would like to hear some ideas on how to start, this just trying to figure it out thing isn't working in this case!
Write a program, HeartRates.java, that takes as input a series of name and age pairs, prints out heart rates for exercising, and continues until either the user types "q" or 4 iterations have been executed.
Computing heart rates for exercising (press q to exit)
-----------------------------------------------
Name: John
Enter age: 20
John for your age: 20
Your aerobic zone is: 150
Your fat Burning zone is: 130
Computing heart rates for exercising (press q to exit)
-----------------------------------------------
Name: Jack
Enter age: aaa
Error! Check age again.
Computing heart rates for exercising (press q to exit)
-----------------------------------------------
Name: Joe
Enter age: 21
Joe for your age: 21
Your aerobic zone is: 149
Your fat Burning zone is: 129
Program terminating. Bye!
if you have any ideas would love to hear them, yes I need to learn this on my own but my professor acts like we already should know how to program.
15 6038
are you working in C or C++ ?
start by writing a program to read data on one person, e.g.
(1) prompt user to enter his/her name;
(2) read the name into a char array if working in C or a string if in C++
(3) display name to check it was read OK
(4) prompt use to enter age
(5) read age into a float
(6) display age to check it is OK
(7) calculate results
when that is working you can add error echecking, e.g. if a numeric value is not entered for age, then add a loop to deal with more than one person
are you working in C or C++ ?
Seeing as this is the Java forum, I'd assume he's using Java, horace...XD
What version of Java are you running? What algorithm will you be using to calculate the output?
What code have you written so far?
Seeing as this is the Java forum, I'd assume he's using Java, horace...XD
guess you a right there Gannon11 !
I code in both all the time so I often find I am writing C++ in a java program, etc etc!
my idea is now
start by writing a program to read data on one person, e.g.
(1) prompt user to enter his/her name;
(2) read the name into a String
(3) display name to check it was read OK
(4) prompt use to enter age
(5) read age into a float
(6) display age to check it is OK
(7) calculate results
when that is working you can add error echecking, e.g. if a numeric value is not entered for age, then add a loop to deal with more than one person
-
public class HeartRates
-
{
-
static final int MAX_HEART_RATE = 220; // cannot change
-
-
public static void main(String[] args)
-
{
-
boolean notQuit = true;
-
System.out.println("Computing heart rates for exercising (press 'q' to exit)");
-
System.out.println("--------------------------------------------------------");
-
-
int i = 0;
-
-
Scanner read = new Scanner(System.in);
-
-
System.out.print("Enter your name: ");
-
-
while(notQuit && i < 4){
-
-
-
-
String name = read.next();
-
System.out.println();
-
-
if( name.charAt(0) == 'q'){
-
notQuit = false;
-
System.out.print("Program terminating. Bye!");
-
} else{
-
System.out.print(name + " enter your age: ");
-
-
double age = read.nextDouble();
-
if (age = true){
-
System.out.println("Error! Check age again.");
-
}
-
-
System.out.println();
-
double newZone = calcAerobicZone(age);
-
double newFat = calcFatBurningZone(age);
-
System.out.println( name + " your age is " + age);
-
System.out.println("Your aerobic zone is: " + newZone);
-
System.out.println("Your fat burning zone is: " + newFat);
-
-
-
System.out.println("Computing heart rates for exercising (press 'q' to exit)");
-
System.out.println("--------------------------------------------------------");
-
System.out.print("Name: ");
-
i++;
-
-
}
-
-
}
-
}
-
-
-
public static double calcAerobicZone(double age)
-
{
-
double aerobicZone = (MAX_HEART_RATE - age) * .75;
-
return aerobicZone;
-
-
}
-
-
public static double calcFatBurningZone(double age)
-
{
-
double fatBurningZone = (MAX_HEART_RATE - age) * .65;
-
return fatBurningZone;
-
-
}
-
}
-
This is about all I have but I am having problems debugging.
fixed a couple of errors - try now -
import java.util.Scanner; // ** for Scanner
-
-
public class HeartRates
-
{
-
static final int MAX_HEART_RATE = 220; // cannot change
-
-
public static void main(String[] args)
-
{
-
boolean notQuit = true;
-
System.out.println("Computing heart rates for exercising (press 'q' to exit)");
-
System.out.println("--------------------------------------------------------");
-
-
int i = 0;
-
-
Scanner read = new Scanner(System.in);
-
-
System.out.print("Enter your name: ");
-
-
while(notQuit && i < 4){
-
-
-
-
String name = read.next();
-
System.out.println();
-
-
if( name.charAt(0) == 'q'){
-
notQuit = false;
-
System.out.print("Program terminating. Bye!");
-
} else{
-
System.out.print(name + " enter your age: ");
-
-
double age = read.nextDouble();
-
if (age > 0){ // ** what test do you want here?
-
System.out.println("Error! Check age again.");
-
}
-
-
System.out.println();
-
double newZone = calcAerobicZone(age);
-
double newFat = calcFatBurningZone(age);
-
System.out.println( name + " your age is " + age);
-
System.out.println("Your aerobic zone is: " + newZone);
-
System.out.println("Your fat burning zone is: " + newFat);
-
-
-
System.out.println("Computing heart rates for exercising (press 'q' to exit)");
-
System.out.println("--------------------------------------------------------");
-
System.out.print("Name: ");
-
i++;
-
-
}
-
-
}
-
}
-
-
-
public static double calcAerobicZone(double age)
-
{
-
double aerobicZone = (MAX_HEART_RATE - age) * .75;
-
return aerobicZone;
-
-
}
-
-
public static double calcFatBurningZone(double age)
-
{
-
double fatBurningZone = (MAX_HEART_RATE - age) * .65;
-
return fatBurningZone;
-
-
}
-
}
actually I need the program to show the ERROR message when someone inputs something other than an number.
Purpose: In this assignment, you will learn how to get input from users with the scanner class, print output with slightly more sophistication, use while loops with boolean logic, and use methods to eliminate unnecessary redundancy. When you finish this lab, you will have the basic skills for input and output to your program, and thus a start on being able to more easily debug your programming errors.
You will write a program, HeartRates.java, that takes as input a series of name and age pairs, prints out heart rates for exercising, and continues until either the user types "q" or 4 iterations have been executed. Below is a sample output from an execution of the program:
Computing heart rates for exercising (press q to exit)
-----------------------------------------------
Name: John
Enter age: 20
John for your age: 20
Your aerobic zone is: 150
Your fat Burning zone is: 130
Computing heart rates for exercising (press q to exit)
-----------------------------------------------
Name: Jack
Enter age: aaa
Error! Check age again.
Computing heart rates for exercising (press q to exit)
-----------------------------------------------
Name: Joe
Enter age: 21
Joe for your age: 21
Your aerobic zone is: 149
Your fat Burning zone is: 129
Program terminating. Bye!
In oder to compute the aerobic and fat burning zone you will use the formulas:
aerobic zone = MAX_HEART_RATE - age) * .75
fat burning zone = MAX_HEART_RATE - age) * .65
where MAX_HEART_RATE is 220 and aerobic zone is and fat burning zone are integer numbers.
The calculation of each zone should be enclosed in a method, which will be called at the appropriate place in your program. The method signatures are the following:
int calcAerobicZone(int age)
int calcFatBurningZone(int age)
You should also try to minimize the number of println() method calls in your source code. In order to do that you can use another method.
Note that only "q" should terminate the execution, all other strings are considered valid names. Your program should also contain explicit checks whether the age inserted by the user is indeed a valid number. If not, the program should print an appropriate message informing the user and should not proceed to the calculation of the two zones. If the user does not terminate the iterative process by inserting "q", the program loop should terminate after 4 tries, regardless of whether some of them are successful or not.
Tip 1: You need to put the following statement at the beginning of your class in order to tell the Java compiler where to find the Scanner class.
import java.util.*;
Tip 2: To compare two strings, you should use:
String h = "hello"
String s = "hello"
String t = "later";
h.equals(s) --> resolves to true
h.equals(t) --> resolves to false
the simplest thing is to use Scanner.hasNextDouble() to check if the next token is a double, if so you read it if not discard it, e.g. -
// read doubles from keyboard and form sum
-
// exit on a negative value
-
-
import java.util.Scanner;
-
-
public class ScanSum {
-
public static void main(String[] args) {
-
Scanner s = new Scanner(System.in);
-
double sum = 0;
-
while (s.hasNext())
-
{
-
// if next token is a double read it and add to sum
-
if (s.hasNextDouble())
-
{
-
double value = s.nextDouble();
-
if(value < 0) break; // if token < 0 finished
-
sum += value;
-
}
-
else s.next(); // not double, discard token
-
}
-
System.out.println(sum);
-
}
-
}
-
how would that fit into the program, can you have two classes?
how would that fit into the program, can you have two classes?
have alook at this simplified version -
// read doubles from keyboard and form sum
-
// exit on a negative value
-
-
import java.util.Scanner;
-
-
public class ScanSum {
-
public static void main(String[] args) {
-
Scanner s = new Scanner(System.in);
-
double sum = 0;
-
while (s.hasNext())
-
{
-
// if next token is a double read it and add to sum
-
if (!s.hasNextDouble()) s.next(); // not double, discard token
-
double value = s.nextDouble();
-
if(value < 0) break; // if token < 0 finished
-
sum += value;
-
}
-
System.out.println(sum);
-
}
-
}
-
just insert a statement such as -
if (!s.hasNextDouble()) s.next(); // not double, discard token
before you try to read a double - it will skip anything else
ok this is what I have so far but when I try to put 'q' in when prompted to enter age I get the error message intended for entering anything other than a number.
This code is starting to hurt my head anyone have any more ideas???? -
import java.util.*
-
-
public class HeartRates
-
{
-
static final int MAX_HEART_RATE = 220; // cannot change
-
-
public static void main(String[] args)
-
{
-
boolean notQuit = true;
-
int i = 0;
-
Scanner read = new Scanner(System.in); //
-
-
while(notQuit && i < 4){ //looping a max of 4 times and stopping when q is entered
-
System.out.println("Computing heart rates for exercising (press 'q' to exit)");
-
System.out.println("--------------------------------------------------------");
-
System.out.print("Name: ");
-
String name = read.next();
-
-
if( name.charAt(0) == 'q'){
-
notQuit = false; //check to see if they entered q to stop loop
-
} else { //
-
System.out.print("Enter age: ");
-
try {//attempting to read a double value and catching exception that occurs
-
double age = read.nextDouble();
-
double newZone = calcAerobicZone(age);
-
double newFat = calcFatBurningZone(age);
-
System.out.println( name + " for your age: " + age);
-
System.out.println("Your aerobic zone is: " + newZone);
-
System.out.println("Your fat burning zone is: " + newFat + "\n");
-
-
} catch (Exception e){
-
System.out.println("Error! Check age again.\n");
-
String garbage = read.next();
-
}
-
finally{
-
i++;
-
}
-
}
-
}
-
System.out.print("Program terminating. Bye!");
-
}
-
public static double calcAerobicZone(double age)
-
{
-
double aerobicZone = (MAX_HEART_RATE - age) * .75;
-
return aerobicZone;
-
-
}
-
-
public static double calcFatBurningZone(double age)
-
{
-
double fatBurningZone = (MAX_HEART_RATE - age) * .65;
-
return fatBurningZone;
-
-
}
-
}
you have to read the faulty number as a string and check if the first character is a q, e.g. -
import java.util.*; // ** added ;
-
-
public class HeartRates
-
{
-
static final int MAX_HEART_RATE = 220; // cannot change
-
-
public static void main(String[] args)
-
{
-
boolean notQuit = true;
-
int i = 0;
-
Scanner read = new Scanner(System.in); //
-
-
while(notQuit && i < 4){ //looping a max of 4 times and stopping when q is entered
-
System.out.println("Computing heart rates for exercising (press 'q' to exit)");
-
System.out.println("--------------------------------------------------------");
-
System.out.print("Name: ");
-
String name = read.next();
-
-
if( name.charAt(0) == 'q'){
-
notQuit = false; //check to see if they entered q to stop loop
-
} else { //
-
System.out.print("Enter age: ");
-
//attempting to read a double value and catching exception that occurs
-
while (!read.hasNextDouble())
-
{
-
String temp = read.next(); // not double, discard token
-
if( temp.charAt(0) == 'q')
-
{ System.out.print("Program terminating. Bye!"); System.exit(0); }
-
System.out.print("Error! Enter age: ");
-
}
-
double age = read.nextDouble();
-
double newZone = calcAerobicZone(age);
-
double newFat = calcFatBurningZone(age);
-
System.out.println( name + " for your age: " + age);
-
System.out.println("Your aerobic zone is: " + newZone);
-
System.out.println("Your fat burning zone is: " + newFat + "\n");
-
i++;
-
}
-
}
-
System.out.print("Program terminating. Bye!");
-
}
-
public static double calcAerobicZone(double age)
-
{
-
double aerobicZone = (MAX_HEART_RATE - age) * .75;
-
return aerobicZone;
-
-
}
-
-
public static double calcFatBurningZone(double age)
-
{
-
double fatBurningZone = (MAX_HEART_RATE - age) * .65;
-
return fatBurningZone;
-
-
}
-
}
When I get my output I am getting numbers like 129.000000001. How could I go about fixing this????
When I get my output I am getting numbers like 129.000000001. How could I go about fixing this????
you can use the System.out.printf() method (similar to C) to set the number of digits after the decimal point, see
http://java.sun.com/developer/technicalArticles/Programming/sprintf/
actually I just set:
double newZone = (int) calcAerobicZone(age);
double newFat = (int) calcFatBurningZone(age);
this truncated the decimal places which works cause when your target heart rate is 127.75 the .75 is not really necessary.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: abcd |
last post by:
kutthaense Secretary Djetvedehald H. Rumsfeld legai predicted eventual
vicmadhlary in Iraq mariyu Afghmadhlaistmadhla, kaani jetvedehly after "a
ljetvedehg, hard slog," mariyu vede legai pressed...
|
by: Darryll Petrancuri |
last post by:
Greetings!
I could really use some suggestions on how to improve on the
following, it at all possible:
Table 'Customer'
---------------------
ID GUID PK
....
|
by: Dj_TRuST |
last post by:
This is my homework and i couldn't do it,please help me.
I wait your helpsss
On the planet Zephod, which orbits the star Betelgeuse, the sharks
increase at a rate of 5% of the guppy population...
|
by: abatesf |
last post by:
I need to write a tax calculation program in C for a store chain. There 4 stores and 4 different tax rates .0700, .0725, .07500 and .0775
This is what i have so far...
any suggestions
#include...
|
by: aljamala |
last post by:
Hi,
I keep getting this warning on a page, but I do not know what the
problem is...does anyone have an idea about what could be wrong?
line 88 column 7 - Warning: missing </formbefore <td>
it...
|
by: Boris Ozegovic |
last post by:
Hi,
I am working on some system, and the communication will take place through
the chatterbot which will be written in AIML (interpreter is written in
Python). English is not my mother tongue,...
|
by: normbrc |
last post by:
I need to create a program in Java that:
1. Asks the user to enter the name of the input file containing heart rate measurements, and input the file name. .txt format
2. Ask the user to enter...
|
by: milov |
last post by:
Project to do simulation testing (me teacher). Page one writes in real time to page two...both displayed at sam time with frames. Code below. Problem...I want to keep score. Each choice needs a...
|
by: imran akhtar |
last post by:
Hi. I have created a program which works correctly, and was just wondering if someone could help me in writing another program which works in the same way, but if the code is different. Below is what...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |