Write a CS102 class that stores student information and marks, and calculates a
ID: 668799 • Letter: W
Question
Write a CS102 class that stores student information and marks, and calculates a student's final mark. Your class must implement the attached Student interface.
interface Student
{
public void setName ( String name );
public String getName ();
public void setMark ( String category, int mark );
public float getFinal ();
}
The 4 methods in Student are as follows:
setName() sets the name of the student.
getName() returns the name of the student.
setMark() sets one of 4 marks, depending on the category parameter, which is one of {"pracs", "practests", "tests", "exam"}.
getFinal() returns the final mark using the formula from the notes to students. All calculations are done with floats.
There is no test program. Instead a technique called unit testing will be used.
Unit testing is an automatic testing technique for individual modules of a large software system. Tests are defined and executed automatically to rapidly test and re-test a class during development without user intervention. This helps to scale up testing when a program gets very large. It also ensures the correctness of each individual class within the system.
JUnit is one of the most popular unit testing frameworks for Java. The advantage of a framework (rather than writing small programs to test each class) is that there is a standard API, common understanding of the testing approach among Java programmers and integrated support in many IDEs.
Write JUnit tests for your CS102 class in a class called CS102Test, stored in the file CS102Test.java. These 4 tests must be included.
1. A test that getName() returns the value set using setName().
2. A test that, if all marks are 0, the final mark is 0.
3. A test that, if all marks are 100, the final mark is 100.
4. A test that the final mark is correctly calculated for all marks being between 0 and 100.
JUnit is used.In order to compile the JUnit test class from the command-line, a command such as:
javac -cp junit-4.12.jar:hamcrest-core-1.3.jar:. CS102Test.java
And in order to execute the JUnit tests from the command-line, a command such as:
java -cp junit-4.12.jar:hamcrest-core-1.3.jar:. org.junit.runner.JUnitCore CS102Test
Explanation / Answer
//This project is created by netbeans IDE
//student.java
package StudentPackage;
public interface Student {
public void setName ( String name );
public String getName ();
public void setMark ( String category, float mark );
public float getFinal ();
}
//st.java
package StudentPackage;
public class st implements Student{
String Name;
float Practical_mark;
float PracticalTest_mark;
float Test_mark;
float Exam_mark;
String[] Category={"pracs", "practests", "tests", "exam"};
float finalMark;
@SuppressWarnings("empty-statement")
public st()
{}
@Override
public void setName(String name) {
this.Name=name;
}
@Override
public String getName() {
return this.Name;
}
@Override
public void setMark(String category, float mark) {
switch (category) {
case "pracs":
this.Practical_mark=mark;
break;
case "practests":
this.PracticalTest_mark=mark;
break;
case "tests":
this.Test_mark=mark;
break;
case "exam":
this.Exam_mark=mark;
break;
}
}
@Override
public float getFinal() {
this.finalMark=(float) (0.60*this.Exam_mark+0.10*this.PracticalTest_mark+0.15*this.Practical_mark+0.5*this.Test_mark);
return this.finalMark;
}
}
//CS102Test.java
import StudentPackage.st;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
public class CS102Test {
public CS102Test() {
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
//1. A test that getName() returns the value set using setName().
@Test
public void nameTest()
{
st stud=new st();
String S="David";
stud.setName(S);
assertTrue(stud.getName().equals(S));
}
//2. A test that, if all marks are 0, the final mark is 0.
@Test
public void markTest1()
{
st stud=new st();
float mark1=0,mark2=0,mark3=0,mark4=0;
//"pracs", "practests", "tests", "exam"};
stud.setMark("pracs", mark1);
stud.setMark("practests", mark2);
stud.setMark("tests", mark3);
stud.setMark("exam", mark4);
float fmark=stud.getFinal();
assertEquals(0.00,fmark,2);
}
//3. A test that, if all marks are 100, the final mark is 100.
@Test
public void markTest2()
{
st stud=new st();
float mark1=100,mark2=100,mark3=100,mark4=100;
stud.setMark("pracs", mark1);
stud.setMark("practests", mark2);
stud.setMark("tests", mark3);
stud.setMark("exam", mark4);
float fmark=stud.getFinal();
assertTrue(!(fmark==100));
}
//4. A test that the final mark is correctly calculated for all marks being between 0 and 100.
@Test
public void markTest3()
{
st stud=new st();
float mark1=25,mark2=45,mark3=30,mark4=55;
stud.setMark("pracs", mark1);
stud.setMark("practests", mark2);
stud.setMark("tests", mark3);
stud.setMark("exam", mark4);
float fmark=stud.getFinal();
boolean flag=false;
if(fmark>0.0 && fmark<100.)
flag=true;
assertTrue(flag==true);
}
}
// Output: by directly run the CS102Test.java using Run file menu option
All tests are passed
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.