Question

Write a static method called averageScores that takes as a parameter a Scanner containing a series...

Write a static method called averageScores that takes as a parameter a Scanner containing a series of student records and that prints a summary of each student record. A student record will begin with a name followed by a sequence of integer homework scores. The name is guaranteed to be one word composed of letters. You may assume that each student has at least one homework score. Your method should produce one lines of output for each student showing the student's name and average score. For example, if a Scanner called records contains the following:

        John 71 83 94 81 Sally 94
        85 Fred 90 95 82 85

and the following call is made:

        averageScores(records);

the following output should be produced:

        John average = 82.25
        Sally average = 89.5
        Fred average = 88.0

You are to exactly reproduce the format of this output. Notice that line breaks in the input are not meaningful and that the average is not rounded. You may not construct any extra data structures to solve this problem.

Homework Answers

Answer #1
import java.util.Scanner;

public class AverageScores {

    public static void averageScores(Scanner in) {
        String name;
        double total, count;
        while (in.hasNext()) {
            name = in.next();
            total = 0;
            count = 0;
            while (in.hasNextInt()) {
                total += in.nextInt();
                ++count;
            }
            System.out.println(name + " average = " + (total/count));
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        averageScores(in);
        in.close();
    }
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Code in Java SAMPLE PROGRAM OUTPUT Because there are several different things your program might do...
Code in Java SAMPLE PROGRAM OUTPUT Because there are several different things your program might do depending upon what the user enters, please refer to the examples below to use to test your program. Run your final program one time for each scenario to make sure that you get the expected output. Be sure to format the output of your program so that it follows what is included in the examples. Remember, in all examples bold items are entered by...
1. Consider the following interface: interface Duty { public String getDuty(); } a. Write a class...
1. Consider the following interface: interface Duty { public String getDuty(); } a. Write a class called Student which implements Duty. Class Student adds 1 data field, id, and 2 methods, getId and setId, along with a 1-argument constructor. The duty of a Student is to study 40 hours a week. b. Write a class called Professor which implements Duty. Class Professor adds 1 data field, name, and 2 methods, getName and setName, along with a 1-argument constructor. The duty...
Do the TODOs in SongFileAccessor.java. It inherits from FileAccessor class. TODO 1: Implement the processLine method....
Do the TODOs in SongFileAccessor.java. It inherits from FileAccessor class. TODO 1: Implement the processLine method. When the text file is processed, each line of text will be passed to processLine . Each line contains 4 fields: title, album, artist, and play time. The album field is optional. Each field is separated by a comma. TODO 2: Implement the songToCSVString method. This method takes a Song object as a parameter and returns a String which is the csv representation of...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT