Question

(In Java) Implement a class named Student. For this exercise, a student has a name, an...

(In Java) Implement a class named Student. For this exercise, a student has a name, an id number and a list of the quiz scores they have taken. Supply an appropriate constructor and methods getName(), getId(), addQuiz(int score), getTotalScore(), getNumQuizzes() and getAverageScore(). The scores should be stored in an array or ArrayList. You should compute the total score and the average score when needed. The id number should be implemented with the assistance of a static instance variable. The first id assigned should be 50.

Write a separate class TestStudent, which includes a main method that fully tests your Student class.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.ArrayList;
import java.util.List;

class Student {
   private int id;
   private String name;
   private List<Integer> scores;
   // static variable to maintain the ID count
   // starting from 0
   private static int count = 100;

   /**
   * @param n
   * @param l
   */
   Student(String n, List<Integer> l) {
       name = n;
       id = count++;
       scores = l;
   }

   public String getName() {
       return name;
   }

   public int getId() {
       return id;
   }

   // add score to arraylis
   public void addQuiz(int s) {
       scores.add(s);
   }

   // returns total score
   public int getTotalScore() {
       int sum = 0;
       for (int a : scores)
           sum += a;
       return sum;
   }

   // returns average score
   public double getAverageScore() {
       return ((double) getTotalScore()) / scores.size();
   }
}

public class TestStudent {
   public static void main(String[] args) {
       List<Integer> list = new ArrayList<Integer>();
       list.add(90);
       list.add(80);
       list.add(86);
       Student s1 = new Student("Uday", list);
       System.out.println("Name : " + s1.getName());
       System.out.println("Id : " + s1.getId());
       System.out.println("Total : " + s1.getTotalScore());
       System.out.println("Average: " + s1.getAverageScore());

   }
}

Add a comment
Know the answer?
Add Answer to:
(In Java) Implement a class named Student. For this exercise, a student has a name, an...
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
  • Implement a class Student. For the purpose of this exercise the student has a name and...

    Implement a class Student. For the purpose of this exercise the student has a name and a total quiz score. The appropriate constructors and methods are: getName(), addQuiz(int Score), getTotalScore(), getAverageScore(). Include a toString that prints the students name, the quiz average and the number of quizzes taken. Override the equals method in a meaningful way. You must compare more than one variable in the equals method.

  • Write a class called Student. The specification for a Student is: Three Instance fields name -...

    Write a class called Student. The specification for a Student is: Three Instance fields name - a String of the student's full name totalQuizScore - double numQuizesTaken - int Constructor Default construtor that sets the instance fields to a default value Parameterized constructor that sets the name instance field to a parameter value and set the other instance fields to a default value. Methods setName - sets or changes the student name by taking in a parameter getName - returns...

  • Student class: Instance variables name id Constructors: Default constructor Constructor that has id and name passed...

    Student class: Instance variables name id Constructors: Default constructor Constructor that has id and name passed to the constructor Methods: Accessors int getID( ) String getName( ) Class Roster: This class will implement the functionality of all roster for school. Instance Variables a final int MAX_NUM representing the maximum number of students allowed on the roster an ArrayList storing students Constructors a default constructor should initialize the list to empty strings a single parameter constructor that takes an ArrayList<Student> Both...

  • Implement an abstract class named Person and two subclasses named Student and Staff in Java. A person has a name, address, phone number and e-mail address. A student has a credit hour status (freshman...

    Implement an abstract class named Person and two subclasses named Student and Staff in Java. A person has a name, address, phone number and e-mail address. A student has a credit hour status (freshman, sophomore, junior, or senior). Define the possible status values using an enum. Staff has an office, salaray, and date-hired. Implement the above classes in Java. Provide Constructors for classes to initialize private variables. Getters and setters should only be provided if needed. Override the toString() method...

  • Write you code in a class named HighScores, in file named HighScores.java. Write a program that...

    Write you code in a class named HighScores, in file named HighScores.java. Write a program that records high-score data for a fictitious game. The program will ask the user to enter five names, and five scores. It will store the data in memory, and print it back out sorted by score. The output from your program should look approximately like this: Enter the name for score #1: Suzy Enter the score for score #1: 600 Enter the name for score...

  • By Python 3 please 2. (a) Implement a class Student. For the purpose of this exercise,...

    By Python 3 please 2. (a) Implement a class Student. For the purpose of this exercise, a student has a name and a total quiz score. Supply an appropriate constructor and methods get Name, addQuiz(score), getTotalScore(), and getAverageScore(). To compute the latter, you also need to store the number of quizzes that the student took. (b) Modify the Student class to compute grade point averages. Methods are needed to add a grade and get the current GPA. Specify grades as...

  • PART I: Create an abstract Java class named “Student” in a package named “STUDENTS”. This class...

    PART I: Create an abstract Java class named “Student” in a package named “STUDENTS”. This class has 4 attributes: (1) student ID: protected, an integer of 10 digits (2) student name: protected (3) student group code: protected, an integer (1 for undergraduates, 2 for graduate students) (4) student major: protected (e.g.: Business, Computer Sciences, Engineering) Class Student declaration provides a default constructor, get-methods and set-methods for the attributes, a public abstract method (displayStudentData()). PART II: Create a Java class named...

  • CompSci 251: Intermediate Computer Programming Spring 2017 -Java Lab 5 For this lab we will be...

    CompSci 251: Intermediate Computer Programming Spring 2017 -Java Lab 5 For this lab we will be looking at static, or class variables. Remember that where instances variables are associated with a particular instance, static variables are associated with the particular class. Specifically, if there are n instances of a class, there are n copies of an instance variable, but exactly one copy of a static variable (even when n = 0). Student - uniqueId : int - id: int -...

  • Write a class named TestScores. The class constructor should accept an array test scores as its...

    Write a class named TestScores. The class constructor should accept an array test scores as its argument. The class should a method that returns the average of the test scores. If any test score is negative or greater than 100, the class should throw an IllegalArgumentException. Demonstrate this with an array of five items. This is JAVA program.

  • Implement the following class in Java: Class name: People Constructor Summary: public People (Str...

    Implement the following class in Java: Class name: People Constructor Summary: public People (String name) Methods: public People(String name) public void setName(String name) public String getName() Class name: Truck Constructor Summary: public Truck (int licensePlate, int onBoard, People people) Initially, the truck does not contain any on board the vehicle. If the number of people on board the vehicle is a negative number, the value of onBoard should be stored as zero. Methods: public int getLicensePlate() Returns license plate 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