Write JUnit test cases with Exception for popGradeData() and AvgPoint() methods
ID: 3889926 • Letter: W
Question
Write JUnit test cases with Exception for popGradeData() and AvgPoint() methods below:
JUnitTest {
@Before or @AfterClass
@Test
popGradeData()
AvgPoint()
@After// or @AfterClass
public static void main (…) {}
}
/*****************************************/
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;
}
}
/***************************************/
public class testGrade {
int ngrades = 3;
Grade[] grade = new Grade[ngrades];
void popGradeData() throws Exception {
if (grade.length < ngrades ) {
throw new Exception("**ArrayIndexOutOfBoundsException **");
} else {
for (int i = 0; i < ngrades; i++) {
grade[0] = new Grade(101, "Andy", 95.0);
grade[1] = new Grade(102, "Bob", 85.00);
grade[2] = new Grade(103, "Collin",75.00);
}
}
}
double AvgPoint() throws Exception {
double sum =0;
for (int i = 0; i < grade.length; i++) {
sum = sum + grade[i].getpoint();
if (sum/grade.length == 0) {
throw new Exception("** Zero Grades**");
}
}
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)
}
}
Explanation / Answer
import junit.framework.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
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;
}
public void popGradeData() {
System.out.println("id=" + id + ", name=" + name + ", point=$" + point
+ "");
}
}
/* ********************************************************************** */
public class testGrade {
Grade[] grade = new Grade[3];
// 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);
}
@Before
public 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();
}
}
@Test
public void testAvg() throws Exception {
double AvgPoint;
testGrade g = new testGrade();
g.popGradeData();
AvgPoint = g.AvgPoint();
Assert.assertEquals(85.0, AvgPoint);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.