In: Computer Science
Java
Create a Project named Chap4b
1. Create a Student class with instance data as follows: student id, test1, test2, and test3.
2. Create one constructor with parameter values for all instance data fields.
3. Create getters and setters for all instance data fields.
4. Provide a method called calcAverage that computes and returns the average test score for an object to the driver program.
5. Create a displayInfo method that receives the average from the driver program and displays student ID, all test scores, and the average test score. Create a driver program named TestScores
1. Create 2 student objects as follows: Student FC123 is created with test scores: 100, 80, 94 Student FC456 is created with test scores: 78, 92, 80
2. Call calcAverage() and displayInfo() for both student objects from the driver program.
Expected Output:
Student ID: FC123
Test 1 score: 100
Test 2 score: 80
Test 3 score: 94
Average test score: 91.33
Student ID: FC456
Test 1 score: 78
Test 2 score: 92
Test 3 score: 80
Average test score: 83.33
Project named Chap4b
public class Student {
private static final String FC123 = "FC123"; //
student id's
private static final String FC456 = "FC456"; //
student id's
private static String Student_id;
private int test1;
private int test2;
private int test3;
public Student(String id) {
// TODO Auto-generated constructor
stub
this.setStudent_id(id); //
constructor where student id's will come
}
public double calcAverage(String id, int i, int j,int
k) {
double sum = (i + j + k);
//System.out.println("Sum:
"+sum);
double Avg = sum/3;
String student_id = (String)
id;
displayInfo(student_id, i, j,
k,Avg);
return Avg;
}
public double displayInfo(String student_id,int i,int
j,int k,double avg) {
System.out.println("Student ID:
"+student_id);
System.out.println("Test 1 score:
"+i);
System.out.println("Test 2 score:
"+j);
System.out.println("Test 3 score:
"+k);
System.out.println("Average
:"+avg);
//System.out.println("Student ID:
"+student_id2);
return 0;
}
public static void main(String[] args) {
// TODO Auto-generated method
stub
String std1 = (String) FC123;
String std2 = (String) FC456;
Student FC123 = new
Student(std1);
FC123.calcAverage(std1,100,80,94);
Student FC456 = new
Student(std2);
FC456.calcAverage(std2,78,92,80);
}
public int getTest3() {
return test3;
}
public void setTest3(int test3) {
this.test3 = test3;
}
public int getTest1() {
return test1;
}
public void setTest1(int test1) {
this.test1 = test1;
}
public int getTest2() {
return test2;
}
public void setTest2(int test2) {
this.test2 = test2;
}
public String getStudent_id() {
return Student_id;
}
public void setStudent_id(String id) {
Student_id = (String) id;
}
}
my output :
Student ID: FC123
Test 1 score: 100
Test 2 score: 80
Test 3 score: 94
Average :91.33333333333333
Student ID: FC456
Test 1 score: 78
Test 2 score: 92
Test 3 score: 80
Average :83.33333333333333