- Assume your program is reading a sequence of positive
integers ("scores") with a -1 to mark the end.
Assume GetNext.getNextInt() will read and return
each new integer. Stop reading when you read the final -1.
You must use a loop of some kind, and you should not use an array.
- Store the sum of the numbers (but not the final -1)
in a variable sumScores.
- Count the numbers as you read them (but not the final -1)
and store the count in a variable numScores.
- Store the largest integer read in a variable maxScores.
- If the number of scores read is even, print Even
and otherwise print Odd.
(You do not need to print anything else.)
[For example, if scores 80 70 90 60 -1
are read, sumScores in 300,
numScores is 4,
maxScores is 90, and the
program should print Even.]
- Consider the following partially completed class
HealthRecord:
// HealthRecord.java: store health information
public class HealthRecord
{
public static final double FEVER = 100.4;
private String name;
private double age;
private double temperature;
public HealthRecord(String n, double a, double t) {
// fill in for part a.
}
public String getName() {
return name;
}
public double getAge() {
return age;
}
public double getTemperature() {
return temperature;
}
public boolean hasFever() {
// fill in for part b.
}
public boolean needsBloodWorkup() {
// fill in for part c.
}
}
// HealthRecordDemo.java: test HealthRecord class
public class HealthRecordDemo {
public static void main (String[] args) {
// fill in code for parts d, e, and f.
}
}
- Fill in the code for the constructor of the
HealthRecord class.
- By definition, a patient has a fever if his temperature is
greater than or equal to 100.4. Fill in the code for the
method hasFever(). (Be sure to use the
final constant FEVER.)
- A patient with a fever under 1/2 year old needs a blood
workup. Fill in the code for the method
needsBloodWorkup.
- In the main function of
HealthRecordDemo class, create two instances
of the HealthRecord class, one
with identifier patient1 named
"Joe Jones" of age 2.0 with temperature 101.5,
and the second with identifier patient2
named "John Junior" of age 0.3 with temperature 102.1.
- In the main function of the
HealthRecordDemo class, write a statement
that will check if paitent1 has a fever
and will print his name along with " has a fever" in case he does.
- In the main function of the
HealthRecordDemo class, write a statement
that will check if paitent2 needs a blood workup
and will print his name along with " needs a blood workup"
in case he does.
- The Math class has a method random
that is described in your text as follows:
public static double random()
Returns a random number between 0.0 and 1.0.
Similarly the Random class has a method
nextDouble
that is described in your text as follows:
public double nextDouble()
Returns a random number between 0.0 and 1.0.
Using either method, give an expression that will return
a random integer between 1 and
6, inclusive, to use in throwing dice, say.
(Your answer should just be a single expression.)
- Write a Java program segment (no need for the enclosing
main function) that uses whatever value is in
the integer variable n to print the following
pattern of stars: n rows, where the first row
has n stars,
and each subsequent row has one fewer star, and the rows are
lined up at the right (so there are initial blanks). Here is an example
with n equal 6
******
*****
****
***
**
*
[Hint:
Use a for loop to let row i
go from 0 to
n - 1.
Then use for loops inside the main loop
so that on row i, you
print i blanks followed by n - i
stars.]
Points for each problem: 1-30, 2-40, 3-10, 4-20.