// Scores.java: read scores and calculate average
public class Scores
{
public static void main (String[] args) {
int score;
int sumScores = 0;
int countScores = 0;
int maxScore;
// use a "priming" read just before the loop
score = GetNext.getNextInt();
maxScore = score;
while (score >= 0) { // terminate loop with negative number
countScores++;
sumScores = sumScores + score;
if (score > maxScore) maxScore = score;
score = GetNext.getNextInt();
}
if (countScores%2 == 0)
System.out.println("Even");
else
System.out.println("Odd");
// extra output just to be sure
System.out.println("countScores: " + countScores);
System.out.println("sumScores: " + sumScores);
System.out.println("maxScore: " + maxScore);
} // end of main
}
Also need the class
GetNext.
Input: 80 70 90 60 -1 Output: Even countScores: 4 sumScores: 300 maxScore: 90
// Scores.java: read scores and calculate average
public class Scores
{
public static void main (String[] args) {
int score;
int sumScores = 0;
int countScores = 0;
int maxScore = 0; // make the initial max as small as possible
while (true) { // must terminate elsewhere
score = GetNext.getNextInt();
if (score < 0) break;
countScores++;
sumScores += score;
if (score > maxScore) maxScore = score;
}
if (countScores%2 == 0)
System.out.println("Even");
else
System.out.println("Odd");
// extra output just to be sure
System.out.println("countScores: " + countScores);
System.out.println("sumScores: " + sumScores);
System.out.println("maxScore: " + maxScore);
} // end of main
}
public class Scores
{
public static void main (String[] args) {
int score;
int sumScores = 0;
int countScores = 0;
int maxScore = 0;
// store next score and check for -1 inside while condition
while ((score = GetNext.getNextInt()) >= 0) {
countScores++;
sumScores = sumScores + score;
if (score > maxScore) maxScore = score;
}
if (countScores%2 == 0)
System.out.println("Even");
else
System.out.println("Odd");
// extra output just to be sure
System.out.println("countScores: " + countScores);
System.out.println("sumScores: " + sumScores);
System.out.println("maxScore: " + maxScore);
} // end of main
}
// 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) {
name = n; age = a; temperature = t; // part a
}
public String getName() {
return name;
}
public double getAge() {
return age;
}
public double getTemperature() {
return temperature;
}
public boolean hasFever() {
return temperature >= FEVER; // part b
}
public boolean needsBloodWorkup() {
return hasFever() && age < 0.5; // part c
}
}
// HealthRecordDemo.java: test HealthRecord class
public class HealthRecordDemo {
public static void main (String[] args) {
HealthRecord patient1 =
new HealthRecord("Joe Jones", 2.0, 101.5);
HealthRecord patient2 =
new HealthRecord("John Junior", 0.3, 102.1); // part d
HealthRecord patient3 =
new HealthRecord("Jenny Jamanski", 0.7, 100.3);
if (patient1.hasFever())
System.out.println(patient1.getName() + " has a fever"); // part e
if (patient1.needsBloodWorkup())
System.out.println(patient1.getName() + " needs a blood workup");
if (patient2.hasFever())
System.out.println(patient2.getName() + " has a fever");
if (patient2.needsBloodWorkup())
System.out.println(patient2.getName() + " needs a blood workup"); // part f
if (patient3.hasFever())
System.out.println(patient3.getName() + " has a fever");
if (patient3.needsBloodWorkup())
System.out.println(patient3.getName() + " needs a blood workup");
}
}
Output of a run:
Joe Jones has a fever
John Junior has a fever
John Junior needs a blood workup
Random myRandom = new Random();
int dice = (int)(6.0*myRandom.nextDouble() + 1.0)
(Note: 6.0 and 1.0 above could just be 6 and 1.
// Stars.java: read scores and calculate average
public class Stars
{
public static void main (String[] args) {
int n = 8;
System.out.println("n: " + n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++)
System.out.print(" ");
for (int j = 0; j < n-i; j++)
System.out.print("*");
System.out.println();
}
}
}