Instructions
Objective
Write a program to implement inheritance in java language.
Requirements and Specifications
Assignment: Write a java assignment program to practice with the use of inheritance in Java. Constraints: • Do not use break/continue in your loops. • Do not use the System.exit() method to exit the program. • All member attributes must be defined as private. • All member methods must be defined as public. Process:
Define the utility Class ExamScore:
The class should have 2 private instance fields to store the name of an exam and the score for an exam. Also provide the following methods: • A no-arg constructor to initialize the instance variables to values of your choice. • A Constructor with 2 parameters to initialize the instance variables to the arguments indicated in the parameter list of the constructor. • Appropriate methods to set the value of each of the private instance fields. • Appropriate methods to get the value of each of the private instance fields. • A toString() method used to display the values of the object using the format: “Exam Name: “ followed by the name, followed by “ Score: “, followed by the score.
Define the utility Class StudentScore that inherits from the ExamScore and has one attribute to store the identification number for the student. • A no-arg constructor to initialize the instance variable to a value of your choice. • A Constructor with 1 parameter to initialize the instance variables to the argument indicated in the parameter list of the constructor. • Appropriate methods to set the value of each of the private instance field. • Appropriate methods to get the value of each of the private instance field. • A toString() method used to display the identification number along with the Exam score information inherited from the ExamScore information. 3. Write a main program (test class) to test various operations on the objects of the ExamScore and StudentScore classes. Perform a series of operations to test each of the methods and the constructors. In addition, show how to define an array of N (ask the user for number of objects to create) StudentScore objects from the StudentScore class and show the functionality of the class with your array. Show how to populate the array with name: FINAL PROJECT, a random value between 1 and 100 to score, and a random number between 100 and 1000 to identification number. Make sure to use loops for processing arrays.
Write a java assignment program (test class) to test various operations on the objects of the ExamScore and StudentScore classes. Perform a series of operations to test each of the methods and the constructors. In addition, show how to define an array of N (ask the user for number of objects to create) StudentScore objects from the StudentScore class and show the functionality of the class with your array. Show how to populate the array with name: FINAL PROJECT, a random value between 1 and 100 to score, and a random number between 100 and 1000 to identification number. Make sure to use loops for processing arrays.
Source Code
EXAM SCORE
public class ExamScore {
private String examName;
private double score;
public ExamScore() {
this("Exam 1", 100.0);
}
public ExamScore(String examName, double score) {
this.examName = examName;
this.score = score;
}
public String getExamName() {
return examName;
}
public void setExamName(String examName) {
this.examName = examName;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String toString() {
return "Exam Name: " + examName + " Score: " + score;
}
}
STUDENT
public class StudentScore extends ExamScore {
private int studentNumber;
public StudentScore() {
this(1000);
}
public StudentScore(int studentNumber) {
this.studentNumber = studentNumber;
}
public int getStudentNumber() {
return studentNumber;
}
public void setStudentNumber(int studentNumber) {
this.studentNumber = studentNumber;
}
@Override
public String toString() {
return super.toString() + " Identification Number: " + studentNumber;
}
}
TEST
import java.util.Random;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
ExamScore e1 = new ExamScore();
System.out.println(e1);
StudentScore s1 = new StudentScore();
System.out.println(s1);
try(Scanner scanner = new Scanner(System.in)) {
System.out.println("Enter the name of an exam and the corresponding score");
String examName = scanner.nextLine();
double score = Double.parseDouble(scanner.nextLine());
e1.setExamName(examName);
e1.setScore(score);
System.out.println("just updated information for an exam");
System.out.println("updated exam name: " + e1.getExamName());
System.out.println("updated score: " + e1.getScore());
System.out.println(e1);
System.out.println("Enter the identification number for a student");
int studentId = Integer.parseInt(scanner.nextLine());
s1.setStudentNumber(studentId);
System.out.println("Enter the name of an exam and the corresponding score for: " + s1.getStudentNumber());
examName = scanner.nextLine();
score = Double.parseDouble(scanner.nextLine());
s1.setExamName(examName);
s1.setScore(score);
System.out.println(s1);
System.out.println();
System.out.println("Enter size of test data array");
int N = Integer.parseInt(scanner.nextLine());
StudentScore[] data = new StudentScore[N];
Random random = new Random();
for (int i = 0; i
data[i] = new StudentScore(random.nextInt(1000));
data[i].setExamName("FINAL PROJECT");
data[i].setScore(random.nextDouble() * 100);
}
for (int i = 0; i
System.out.println(data[i]);
}
}
}
}
Similar Samples
Explore our diverse range of programming homework samples at ProgrammingHomeworkHelp.com. From fundamental coding exercises to complex algorithm implementations in languages like Python, Java, and C++, our samples showcase expertise and precision. Each example serves as a valuable resource to aid in understanding and mastering programming concepts for academic success.
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java