Question

In: Computer Science

Write a java program StudentDriver class to test the methods of the Student class. Use data:...

Write a java program StudentDriver class to test the methods of the Student class.

Use data:
Mary Brown 1234
John Jones 5678
Maty Jones 1234

Note: we are including Mary Jones twice so we can test the "equals" method.
We can test "compareTo", by test student1 and student2, then student2 and student3.

Just submit your driver class as text file.

Make sure you also test addQuiz, getTotalScore, getAverage, and toString. Don't worry about testing all of the sets and gets...

public class Student implements Comparable
{
        //instance variables
        private String lastName;
        private String firstName;
        private String ID;
        private double totalScore;
        private int numQ;
        
        //Constructor
        public Student(String lastName, String firstName, String ID)
        {
                this.lastName = lastName;
                this.firstName = firstName;
                this.ID = ID;
                totalScore = 0;
                numQ = 0;
        }
        
        //instance methods
        
        public void setLast(String lastName)
        {this.lastName = lastName;}
        
        public void setFirst(String firstName)
        {this.firstName = firstName;}
        
        public void setID(String ID)
        { this.ID = ID;}
        
        public void setTotalScore(double total)
        {this.totalScore = total;}
        
        public void setNumQ(int numQ)
        {this.numQ = numQ;}
        
        public String getLast()
        {return this.lastName;}
        
        public String getFirst()
        {return this.firstName;}
        
        public String getID()
        {return this.ID;}
        
        public double getTotalScore()
        {return this.totalScore;}

        public int getNumQ()
        {return this.numQ;}
        
        public void addQuiz(int score)
        { totalScore += score;
          numQ++;}
        
        public double getAverage()
        {return  totalScore/numQ;}
        
        public boolean equals(Student other)
        {return this.ID.equals(other.ID);}
        
        public int compareTo(Student other)
        {return this.lastName.compareTo(other.lastName);}
        
        public String toString()
        {return this.lastName + ", " + this.firstName + "  " + "\n" + this.ID + "\n";}
        
        
        
        
}
        

Solutions

Expert Solution

Java code:

public class StudentDriver {
public static void main(String args[])
{
System.out.println("Student driver class..");
Student s1 = new Student("Mary","Brown","1234");
Student s2 = new Student("John","Jones","5678");
Student s3 = new Student("Mary","Jones","1234");
Student s4 = new Student("Mary","Jones","1234");
System.out.println("Testing compareTo method with s1 and s2:");
System.out.println(s1.compareTo(s2));
System.out.println("Testing compareTo method with s2 and s3:");
System.out.println(s2.compareTo(s3));
System.out.println("Testing equals method with s3 and s4:");
System.out.println(s3.compareTo(s4));
System.out.println("Testing addQuiz method:");
s1.addQuiz(50);
System.out.println("After calling addQuiz. TotalScore = "+s1.totalScore+" numQ = "+s1.numQ);
System.out.println("Testing setTotalScore and getTotalScore:");
s1.setTotalScore(40);
System.out.println("After calling getTotalScore: "+s1.getTotalScore());
s2.setTotalScore(30);
System.out.println("calling getAverage method:");
System.out.println(s1.getAverage());
System.out.println("calling toString method ");
System.out.println(s1.toString());
}
}

Execution screenshot;


Related Solutions

write program that develop a Java class Dictionary to support the following public methods of an...
write program that develop a Java class Dictionary to support the following public methods of an abstract data type: public class Dictionary { // insert a (key, value) pair into the Dictionary, key value must be unique, the value is associated with the key; only the last value inserted for a key will be kept public void insert(String key, String value); // return the value associated with the key value public String lookup(String key); // delete the (key, value) pair...
How to write a Java program that has a base class with methods and attributes, subclasses...
How to write a Java program that has a base class with methods and attributes, subclasses with methods and attributes. Please use any example you'd like.
3) Create a Java program that uses NO methods, but use scanner: Write a program where...
3) Create a Java program that uses NO methods, but use scanner: Write a program where you will enter the flying distance from one continent to another, you will take the plane in one country, then you will enter miles per gallon and price of gallon and in the end it will calculate how much gas was spend for that distance in miles. Steps: 1) Prompt user to enter the name of country that you are 2) Declare variable to...
Write a program to determine if the student has passed the test or not. Please use...
Write a program to determine if the student has passed the test or not. Please use two if statements to display the results. For example, if the user has entered ‘true’, display ‘the student has passed the test’, and if the user has entered ‘false’, display ‘the student has failed the test’. Use the scanner class to get the Boolean datatype answer from the user if the student has passed the test or not. Use two if statements to determine...
(JAVA) Write a program that maintains student test scores in a two-dimesnional array, with the students...
(JAVA) Write a program that maintains student test scores in a two-dimesnional array, with the students identified by rows and test scores identified by columns. Ask the user for the number of students and for the number of tests (which will be the size of the two-dimensional array). Populate the array with user input (with numbers in {0, 100} for test scores). Assume that a score >= 60 is a pass for the below. Then, write methods for: Computing the...
You do not need to import the Math class into your Java program to use methods...
You do not need to import the Math class into your Java program to use methods or constants in it. Group of answer choices True False
Write a program in java that uses methods to input data for calculation, calculate, and display...
Write a program in java that uses methods to input data for calculation, calculate, and display the results of the calculations. That is, there are at least three methods. The problem is to write a program that calculates the area of a rectangle. This action should be repeatable.
CIT 149 JAVA 1 program question? Write a program that will use static methods as described...
CIT 149 JAVA 1 program question? Write a program that will use static methods as described in the specifications below. Utilizing the if and else statements, write a program which will calculate a commission based on a two-tiered commission plan of 3% and 7% paid on amounts of $15,000 or less, or over $15,000 in monthly sales by a sales force paid on a commission basis. Use the output specifications below. ? Specifications There will be two classes (separate files)...
***IN JAVA*** Write a program contained a class Student which has firstName, lastName, mark, grade. The...
***IN JAVA*** Write a program contained a class Student which has firstName, lastName, mark, grade. The program should allow creation an array of object instances to assign firstName, lastName and mark from input user and perform and assign grade based on mark’s criteria displayed below. MARKS INTERVAL 95 - 100 90 - <95 85 - <90 80 - <85 75 - <80 70 - <75 65 - <70 60 - <65 0 - <60 LETTER GRADE A+ A B+ B...
Assignment Purpose Write a well commented java program that demonstrates the use and re-use of methods...
Assignment Purpose Write a well commented java program that demonstrates the use and re-use of methods with input validation. Instructions It is quite interesting that most of us are likely to be able to read and comprehend words, even if the alphabets of these words are scrambled (two of them) given the fact that the first and last alphabets remain the same. For example, “I dn'ot gvie a dman for a man taht can olny sepll a wrod one way.”...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT