Question
Write a JUnit test case @Before @Test - popGradeData() - testAvg() throws Exception @After class Grade { private int id; private String name; private double
Write a JUnit test case
@Before
@Test
- popGradeData()
- testAvg() throws Exception
@After
class Grade {
private int id;
private String name;
private double point;
// Parameterized constructor
public Grade(int id, String name, double point) {
super();
this.id = id;
this.name = name;
this.point = point;
}
// getters and setters
public int getid() {
return id;
}
public void setid(int id) {
this.id = id;
}
public String getname() {
return name;
}
public void setname(String name) {
this.name = name;
}
public double getpoint() {
return point;
}
public void setpoint(double point) {
this.point = point;
}
void popGradeData(){
System.out.println("id="+id+", name="+name+", point=$"+point+"");
}
}
/* ********************************************************************** */
public class testGrade {
Grade[] grade = new Grade[3];
void popGradeData() {
int ngrades = 3;
for (int i = 0; i < ngrades; i++) {
grade[0] = new Grade(101, "Andy", 95.00);
grade[1] = new Grade(102, "Bob", 85.00);
grade[2] = new Grade(103, "Collin", 75.00);
grade[i].popGradeData();
}
}
// throw Exception if no Grade found
double AvgPoint() throws Exception {
double sum = 0.0;
if (grade.length == 0) {
throw new Exception("** No Grades found **");
} else {
for (int i = 0; i < grade.length; i++) {
sum = sum + grade[i].getpoint();
}
}
return (sum / grade.length);
}
public static void main(String args[]) throws Exception {
double AvgPoint;
testGrade g = new testGrade();
g.popGradeData();
AvgPoint = g.AvgPoint();
System.out.println("Average Point:" + AvgPoint);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started