Question

Complete the following Java class. In this class, inside the main() method, user first enters the...

Complete the following Java class. In this class, inside the main() method, user first enters the name of the students in a while loop. Then, in an enhanced for loop, and by using the method getScores(), user enters the grades of each student. Finally, the list of the students and their final scores will be printed out using a for loop. Using the comments provided, complete the code in methods finalScore(), minimum(), and sum().

import java.util.Scanner;

import java.util.ArrayList;

public class GradeBook {

   public static void main(String[] args) {

  Scanner in = new Scanner(System.in);

  ArrayList nameList = new ArrayList ();

  ArrayList scoreList = new ArrayList ();

  boolean done = false;

  /* get student names */

  while(!done) {

     System.out.println("Enter a student name, Q to quit:");

     String name = in.nextLine();

     if (name.equals("Q")) done = true;

     else nameList.add(name);  

  }

  /* get grades for each student */

  for (String n : nameList) {

     scoreList.add(getScores(n, in));

  }

  /* print names of students and their final scores */

  for (int i = 0; i < nameList.size(); i++) {

     System.out.println(nameList.get(i) + ": " + scoreList.get(i));

  }

   }

   /**

  Gets the scores for an individual student, then

  returns the final score for a list of scores.

  @param name - the name of the student being processed

  @return the sum of the scores, with the lowest score dropped if

  there are at least two scores (by calling sum() and minimum()

  methods), or 0 if there are no scores. If there is only one score,

  it just returns that score.

   */

   public static double getScores(String name, Scanner in) {

  ArrayList scores = new ArrayList ();

  System.out.println("Enter scores for " + name + ", Q to quit:");

  while (in.hasNextDouble()) {  

     scores.add(in.nextDouble());

  }

  in.next(); // Read off "Q"

  return finalScore(scores);

   }

   /**

  Gets the final score for a list of scores.

  @param scores - the list of scores for one student to process

  @return the sum of the scores, with the lowest score dropped if

  there are at least two scores (by calling sum() and minimum()

  methods), or 0 if there are no scores. If there is only one score,

  it just returns that score.

   */

   public static double finalScore(ArrayList scores)   {



   */ Complete this method using its comments:

}

   /**

  Gets the minimum score in a list of scores.

  @param scores - the list of scores for one student to process

  @return the minimum score, or 0 if there are no scores.

   */

   public static double minimum(ArrayList scores)  {


   */ Complete this method using its comments:

}

   /**

  Computes the sum of the scores in a list of scores.

  @param scores - the list of scores for one student to process

  @return the sum of the scores

   */

   public static double sum(ArrayList scores)  {


   */ Complete this method using its comments:

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;
import java.util.ArrayList;

public class GradeBook {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        ArrayList<String> nameList = new ArrayList();
        ArrayList<Double> scoreList = new ArrayList();
        boolean done = false;
        /* get student names */
        while (!done) {
            System.out.println("Enter a student name, Q to quit:");
            String name = in.nextLine();
            if (name.equals("Q")) done = true;
            else nameList.add(name);
        }
        /* get grades for each student */
        for (String n : nameList) {
            scoreList.add(getScores(n, in));
        }
        /* print names of students and their final scores */
        for (int i = 0; i < nameList.size(); i++) {
            System.out.println(nameList.get(i) + ": " + scoreList.get(i));
        }
    }

    /**
     * Gets the scores for an individual student, then
     * returns the final score for a list of scores.
     *
     * @param name - the name of the student being processed
     * @return the sum of the scores, with the lowest score dropped if
     * there are at least two scores (by calling sum() and minimum()
     * methods), or 0 if there are no scores. If there is only one score,
     * it just returns that score.
     */
    public static double getScores(String name, Scanner in) {
        ArrayList scores = new ArrayList();
        System.out.println("Enter scores for " + name + ", Q to quit:");
        while (in.hasNextDouble()) {
            scores.add(in.nextDouble());
        }
        in.next(); // Read off "Q"
        return finalScore(scores);
    }

    /**
     * Gets the final score for a list of scores.
     *
     * @param scores - the list of scores for one student to process
     * @return the sum of the scores, with the lowest score dropped if
     * there are at least two scores (by calling sum() and minimum()
     * methods), or 0 if there are no scores. If there is only one score,
     * it just returns that score.
     */
    public static double finalScore(ArrayList scores) {
        return sum(scores) - minimum(scores);
    }

    /**
     * Gets the minimum score in a list of scores.
     *
     * @param scores - the list of scores for one student to process
     * @return the minimum score, or 0 if there are no scores.
     */
    public static double minimum(ArrayList scores) {
        double min = (double) scores.get(0);
        for (int i = 0; i < scores.size(); i++) {
            if ((double)scores.get(i) < min) {
                min = (double)scores.get(i);
            }
        }
        return min;
    }

    /**
     * Computes the sum of the scores in a list of scores.
     *
     * @param scores - the list of scores for one student to process
     * @return the sum of the scores
     */
    public static double sum(ArrayList scores) {
        double sum = 0;
        for (int i = 0; i < scores.size(); i++) {
            sum += (double)scores.get(i);
        }
        return sum;
    }
}
Add a comment
Know the answer?
Add Answer to:
Complete the following Java class. In this class, inside the main() method, user first enters the...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA 3files seprate 1.Classroom.java 2.ClassroomTester.java (provided) 3.Student.java(provided) Use the following files: ClassroomTester.java import java.util.ArrayList; /** *...

    JAVA 3files seprate 1.Classroom.java 2.ClassroomTester.java (provided) 3.Student.java(provided) Use the following files: ClassroomTester.java import java.util.ArrayList; /** * Write a description of class UniversityTester here. * * @author (your name) * @version (a version number or a date) */ public class ClassroomTester { public static void main(String[] args) { ArrayList<Double> grades1 = new ArrayList<>(); grades1.add(82.0); grades1.add(91.5); grades1.add(85.0); Student student1 = new Student("Srivani", grades1); ArrayList<Double> grades2 = new ArrayList<>(); grades2.add(95.0); grades2.add(87.0); grades2.add(99.0); grades2.add(100.0); Student student2 = new Student("Carlos", grades2); ArrayList<Double> grades3 = new...

  • JAVA programming 9.9 Ch 9, Part 2: ArrayList Searching import java.util.ArrayList; public class ArrayListSet {    ...

    JAVA programming 9.9 Ch 9, Part 2: ArrayList Searching import java.util.ArrayList; public class ArrayListSet {     /**      * Searches through the ArrayList arr, from the first index to the last, returning an ArrayList      * containing all the indexes of Strings in arr that match String s. For this method, two Strings,      * p and q, match when p.equals(q) returns true or if both of the compared references are null.      *      * @param s The string...

  • I keep getting an Error after I ask the user for test scores. What is missing?...

    I keep getting an Error after I ask the user for test scores. What is missing? package testscores; import java.util.Scanner; public class TestScores { /** * @param args the command line arguments */ private double[] scores;    public TestScores(double[] score) throws IllegalArgumentException { scores = new double[scores.length]; for (int i = 0; i < scores.length; i++) { if (score[i] < 0 || score[i] > 100){ throw new IllegalArgumentException(); } scores[i]=score[i];    }    } public double getAverage() { int sum=0;...

  • in java coorect this code & screenshoot your output ---------------------------------------------------------------------- public class UNOGame {     /**...

    in java coorect this code & screenshoot your output ---------------------------------------------------------------------- public class UNOGame {     /**      * @param args the command line arguments      */         public static void main(String[] args) {       Scanner input=new Scanner(System.in);          Scanner input2=new Scanner(System.in);                             UNOCard c=new UNOCard ();                UNOCard D=new UNOCard ();                 Queue Q=new Queue();                           listplayer ll=new listplayer();                           System.out.println("Enter Players Name :\n Click STOP To Start Game..");        String Name = input.nextLine();...

  • Activity 2. Complete the code inside the Java file below, ListUtil.java, such that this class supplies...

    Activity 2. Complete the code inside the Java file below, ListUtil.java, such that this class supplies a utility method to reverse the entries in a linked list. Then, test your code using the tester class, Reverse Tester.java, given below. ListUtil.java import java.util.LinkedList; /** This class supplies a utility method to reverse the entries in a linked list. */ public class ListUtil { /** Reverses the elements in a linked list @param strings the linked list to reverse public static void...

  • Prompt: In this stepping stone lab assignment, you will build a Recipe class, getting user input...

    Prompt: In this stepping stone lab assignment, you will build a Recipe class, getting user input to collect the recipe name and serving size, using the ingredient entry code from Stepping Stone Lab Four to add ingredients to the recipe, and calculating the calories per serving. Additionally, you will build your first custom method to print the recipe to the screen. Specifically, you will need to create the following:  The instance variables for the class (recipeName, serving size, and...

  • How to complete this methods: import java.io.FileInputStream; import java.io.FileNotFoundException; import java.time.Loc...

    How to complete this methods: import java.io.FileInputStream; import java.io.FileNotFoundException; import java.time.LocalDate; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Scanner; import java.util.Set; /** * Utility class that deals with all the other classes. * * @author EECS2030 Summer 2019 * */ public final class Registrar {    public static final String COURSES_FILE = "Courses.csv";    public static final String STUDENTS_FILE = "Students.csv";    public static final String PATH = System.getProperty("java.class.path");    /**    * Hash table to store the list of students using their...

  • For the code below write a public static main() method in class Student that: - creates...

    For the code below write a public static main() method in class Student that: - creates an ArrayList<Student> object called students - adds 4 new Student objects to the students list, with some made up names and dates - sort the students list by name and display the sorted collection to System.out. use function getCompByName() - sort the students list by enrollment date and display the sorted collection to System.out. use function getCompByDate() import java.util.Comparator;    import java.util.Date;    public...

  • Solve this using Java for an Intro To Java Class. Provided files: Person.java /** * Models...

    Solve this using Java for an Intro To Java Class. Provided files: Person.java /** * Models a person */ public class Person { private String name; private String gender; private int age; /** * Consructs a Person object * @param name the name of the person * @param gender the gender of the person either * m for male or f for female * @param age the age of the person */ public Person(String name, String gender, int age) {...

  • Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file...

    Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file doesn’t exist. You will process through the file skipping any text or real (double) numbers. You will print the max, min, sum, count, and average of the integers in the file. You will want to create test files that contain integers, doubles, and Strings. HINT: Use hasNextInt() method and while loop. You may also want to use Integer.MAX_VALUE and Integer.MIN_VALUE for the initialization of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT