Question

In: Computer Science

- Java Programming - Develop JUnit test for a designated program - Perform JUnit testing for...

- Java Programming

- Develop JUnit test for a designated program

- Perform JUnit testing for on method that return both successful and unsuccessful results

- Create a JUnit test class for testing the getAverageScore and the getTotalScore methods.   On class per test.

public class Student
{
  
private String name;

private double totalScore;
  
private int quizCount;
  
public Student()
{
name = "";
totalScore = 0;
quizCount = 0;
}
n the name
*/
public Student(String n)
{
name = n;
totalScore = 0;
quizCount = 0;
}

public void setName(String aName)
{

name = aName;

}

public String getName()
{
return name;
}

public void addQuiz(int score)
{
if(score >= 0 && score <= 100)
{
totalScore = totalScore + score;
quizCount = quizCount + 1;
}
else
{
System.out.println("Score must be between 0 and 100, inclusive");
  
}
}

public double getTotalScore()
{
return totalScore;
}


public double getAverageScore()
{
return totalScore / quizCount;
}


}
----------------------

import java.util.Scanner;

public class StudentTester
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
String repeat = "n";
int currentScore = 0;
  
System.out.print("Enter Student's name: ");
String sName = keyboard.nextLine();
  
Student student1 = new Student(sName);
  
do
{
System.out.print("Enter quiz score: ");
currentScore = keyboard.nextInt();
  
student1.addQuiz(currentScore);

System.out.print("Do you wish to enter another score, (Enter y for Yes, n for no: " );

keyboard.nextLine();
repeat = keyboard.nextLine();
  
}while(repeat.equalsIgnoreCase("y"));
  
String studName = student1.getName();
double totalScore = student1.getTotalScore();
double avgScore = student1.getAverageScore();
  
  
  
System.out.printf("%s total quiz score is: %,2f and average quiz score is: %6.2f \n", studName, totalScore, avgScore);
  
  
Student student2 = new Student();

Solutions

Expert Solution

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class StudentTester {

    private Student student;

    @Before
    public void setUp() {
        this.student = new Student();
        student.setName("User1");
        student.addQuiz(12);
        student.addQuiz(45);
        student.addQuiz(67);
        student.addQuiz(87);

    }

    @Test
    public void positiveTestTotal() {
        assertEquals(211, student.getTotalScore(), 0.0);
    }

    @Test
    public void negativeTestTotal() {
        assertNotSame("Assert", 2411, student.getTotalScore());
    }


    @Test
    public void positiveTestAverage() {
        assertEquals(52.75, student.getAverageScore(), 0.0);
    }

    @Test
    public void negativTestAverage() {
        assertNotSame("Should not Match", 211, student.getTotalScore());
    }

    @Test
    public void testGetName() {
        Student student = new Student();
        student.setName("User2");
        assertSame("Should  Match", "User2", student.getName());
    }

    @Test
    public void testNegativeGetName() {
        Student student = new Student();
        student.setName("User2");
        assertNotSame("Should  Not Match", "User1", student.getName());
    }


    @Test
    public void successfulTestaddQuix() {
        Student student = new Student();
        student.setName("User2");
        student.addQuiz(12);
        assertEquals("Should Match", 12, student.getAverageScore(), 0.0);
        assertEquals("Should Match", 12, student.getTotalScore(), 0.0);
    }
}

Related Solutions

Create a JUNIT test program in Java for this program: package edu.odu.cs.cs350; import java.util.Arrays; /** *...
Create a JUNIT test program in Java for this program: package edu.odu.cs.cs350; import java.util.Arrays; /** * A duration represents a period of elapsed time, e.g., 4 hours, 23 minutes, 2 seconds. * This is differentiated from a point in time (e.g., 4:23:02AM). A meeting that * has a duration of 2 hours has that same duration no matter where it was held. A * starting time (point) for that of 2:00PM EST can be unambiguous only if the time *...
Triangle Testing You are about to test the implementations of triangleType method with JUnit Testing. To...
Triangle Testing You are about to test the implementations of triangleType method with JUnit Testing. To test this Triangle class, you write a JUnit test class named TriangleTest.java. This test class contains multiple test cases in the format of methods. The test runner executes TriangleTest to generate a JUnit test report. In this project, you do not need to write the code for this Triangle class. Instead, you will write JUnit test cases to make sure that the implementation of...
Programming Problem Design a program in java that can perform three different arithmetic operations based on...
Programming Problem Design a program in java that can perform three different arithmetic operations based on the user’s input. The three operations are 1. Summation of integers from 1 to m 2. Factorial of a given number n (n!) 3. Finding the leftmost digit of a given integer (note: you have to use int) This program should prompt the user a menu including four options, ask the user for one option, and perform the corresponding computation. This process will repeat...
Java programming Write the max-heapify code and test it and then write the program to find...
Java programming Write the max-heapify code and test it and then write the program to find the three largest values of the array without sorting the entire array to get values.
I'm pretty new to JUnit testing. Can someone demonstrate how to test one of the tree...
I'm pretty new to JUnit testing. Can someone demonstrate how to test one of the tree traversals and the add() and addrecursive() methods? public class BinaryTree { private MorseNode<Character> root = new MorseNode<Character>(); private MorseNode<Character> left = null; private MorseNode<Character> right = null; public MorseNode<Character> getRoot() { return root; } public void printPostorder(MorseNode<Character> node) { if(node == null) {return;} //System.out.print(node.getLetter()); //traverse left subtree printPostorder(node.getLeft()); //traverse right subtree printPostorder(node.getRight()); //node if(node.getLetter() != null) { System.out.print(node.getLetter() + " ");} } public void...
Java programming extra credit. The following program will be used to test your knowledge on Implementing...
Java programming extra credit. The following program will be used to test your knowledge on Implementing with tester classes. Included is the Details class. Your assignment is to create a class named DetailsTester using info from the two classes(Procedure class is only included to know the details of the procedure, which includes desc, date, and cost) Patient class is included as well.That will properly implement the class. package doctorOffice; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.ArrayList; public class Patient extends Person...
Java programming language Write a Java application that simulates a test. The test contains at least...
Java programming language Write a Java application that simulates a test. The test contains at least five questions. Each question should be a multiple-choice question with 4 options. Design a QuestionBank class. Use programmer-defined methods to implement your solution. For example: - create a method to simulate the questions – simulateQuestion - create a method to check the answer – checkAnswer - create a method to display a random message for the user – generateMessage - create a method to...
I'm really confused Junit test case Here is my code. How can I do Junit test...
I'm really confused Junit test case Here is my code. How can I do Junit test with this code? package pieces; import java.util.ArrayList; import board.Board; public class Knight extends Piece {    public Knight(int positionX, int positionY, boolean isWhite) {        super("N", positionX, positionY, isWhite);    }    @Override    public String getPossibleMoves() {        ArrayList<String> possibleMoves = new ArrayList<>();               // check if squares where knight can go are available for it       ...
It is required to develop an application in Java to perform some operations of a bank....
It is required to develop an application in Java to perform some operations of a bank. The application will have the following classes: Class called Person having following data fields (private): firstName (String), lastName(String), gender(char), cpr(long), mobile(String) and following methods: Constructor having 5 parameters to initialize all data fields, Set and get methods for all data fields, toString method to return String equivalent of all data fields, Abstract class called Account having following data fields(protected): accountHolder(Person), ccountNum(long), balance(double)and following methods:...
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester. The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT