Question

In: Computer Science

write the program in java. Demonstrate that you understand how to use create a class and...

write the program in java.

Demonstrate that you understand how to use create a class and test it using JUnit

Let’s create a new Project HoursWorked

Under your src folder, create package edu.cincinnatistate.pay

Now, create a new class HoursWorked in package edu.cincinnatistate.pay

This class needs to do the following:

Have a constructor that receives intHours which will be stored in totalHrs

Have a method addHours to add hours to totalHrs

Have a method subHours to subtract hours from totalHrs

Have a method that returns the totalHrs

This class must have JavaDoc comments above the class name and above every method.

Once you create the HoursWorked class, a Junit Test HoursWorkedTest.

This should use @BeforeEach to setup the default HoursWorked to 8 hours.

Create @Test testAddHours to test adding hours

Create @test testSubHours to test subtracting hours

Be sure to generate an error in your test and provide comments on why this is an error

Solutions

Expert Solution

Java Code:

package edu.cincinnatistate.pay;

/**
* Class to store the number of hours worked
* Contains method to add and subtract hours
* Has a constructor to populate initial value
*/
public class HoursWorked {
   private int totalHrs;
  
   /**
* Constructor to initialize total hours
* @return none
* @param int hours
*/
   public HoursWorked(int hours) {
       totalHrs = hours;
   }
  
   /**
* Adds the hours
* @return void
* @param int hours
*/
   public void addHours (int hours) {
       totalHrs += hours;
   }
  
   /**
* Subtract the hours
* @return void
* @param int hours
*/
   public void subHours (int hours) {
       totalHrs -= hours;
   }
  
   /**
* Returns the total hours
* @return int total hours
*/
   public int returnTotalHours() {
       return totalHrs;
   }
}

JUnit Code:

package edu.cincinnatistate.pay;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class HoursWorkedTest {
  
   int defaultHoursWorked; //to store default hours worked
   @BeforeEach
   void setupMethod() {
       defaultHoursWorked = 8;
   }
   //positive test case for add hours
   @Test
   void testAddHours() {
       HoursWorked hwObj = new HoursWorked(defaultHoursWorked);
       hwObj.addHours(5);
       assertEquals(hwObj.returnTotalHours(), 13, "Hours are not equal");
   }
  
   //positive test case for subtract hours
   @Test
   void testSubHours() {
       HoursWorked hwObj = new HoursWorked(defaultHoursWorked);
       hwObj.subHours(5);
       assertEquals(hwObj.returnTotalHours(), 3, "Hours are not equal");
   }
  
   //negative test case for subtract hours
   @Test
   void testSubHoursError() {
       HoursWorked hwObj = new HoursWorked(defaultHoursWorked);
       hwObj.subHours(5);
       //8-5 will result in 3 but we are checking for 8 so this will throw an error
       assertEquals(hwObj.returnTotalHours(), 8, "Error case");
   }

}

Sample Output:


Related Solutions

Write the program in Java. Demonstrate that you know how to define variables, create conditional statements,...
Write the program in Java. Demonstrate that you know how to define variables, create conditional statements, write a loop, use arrays, obtain input from the user, display output, test and create a JavaDoc. Create project PaystubTest2 that is a Paystub calculator with program Paystub##.java (where ## is your initials) based on user input. (10 points) You must use the ReadStringFromUser and ReadFloatFromUser methods in the program to obtain user input. (20 points) This program will calculate the Gross Earnings, FICA...
Write a program in java that does the following: Create a StudentRecord class that keeps the...
Write a program in java that does the following: Create a StudentRecord class that keeps the following information for a student: first name (String), last name (String), and balance (integer). Provide proper constructor, setter and getter methods. Read the student information (one student per line) from the input file “csc272input.txt”. The information in the file is listed below. You can use it to generate the input file yourself, or use the original input file that is available alone with this...
Write a Java program to process the information for a bank customer.  Create a class to manage...
Write a Java program to process the information for a bank customer.  Create a class to manage an account, include the necessary data members and methods as necessary.  Develop a tester class to create an object and test all methods and print the info for 1 customer.  Your program must be able to read a record from keyboard, calculate the bonus and print the details to the monitor.  Bonus is 2% per year of deposit, if the amount is on deposit for 5 years...
In this question, you are asked to write a simple java program to understand natural language....
In this question, you are asked to write a simple java program to understand natural language. The user will enter the input following the format: Name came to City, Country in Year. For example: Robin came to Montreal, Canada in 2009. Assume a perfect user will follow the exactly above formats for the inputs. Your program should be able to analyze the key words (Name, City, Country and Year) from the inputs and reorganize the outputs following format: Name stay...
Create a new Java program named AllAboutMe (For JAVA we use blue J) Write code to...
Create a new Java program named AllAboutMe (For JAVA we use blue J) Write code to have the program print your name, favorite color, and three hobbies to a new text file called “AllAboutMe” using PrintStream. Submit code.
Can you write a small program in c++ demonstrate the use of pointer (*) & (&)....
Can you write a small program in c++ demonstrate the use of pointer (*) & (&). Can you also explain the pointer system and how to use them. Thank you.
write the program in java. Develop a class RentCabin that does the following: (use JavaDoc comments)...
write the program in java. Develop a class RentCabin that does the following: (use JavaDoc comments) // declare the constructor that sets the type and rate based in the sqft parm        // set values based on sqft <1000 is small with $100 per night, // sqft between 1000 and 2000, mid-sized $200 per night, and // over 2000 as a large cabin with $300 per night        //declare getRate        //declare getType        //declare setRate with int rate parm...
Write a java program with the following classes: Class Player Method Explanation: play : will use...
Write a java program with the following classes: Class Player Method Explanation: play : will use a loop to generate a series of random numbers and add them to a total, which will be assigned to the variable score. decideRank: will set the instance variable rank to “Level 1”, “Level 2”, “Level 3”, “Level 4” based on the value of score, and return that string. getScore : will return score. toString: will return a string of name, score and rank....
Use LinkedList build-in class (java.util.LinkedList) to write a Java program that has: A. Method to print...
Use LinkedList build-in class (java.util.LinkedList) to write a Java program that has: A. Method to print all elements of a linked list in order. B. Method to print all elements of a linked list in reverse order. C. Method to print all elements of a linked list in order starting from specific position. D. Method to join two linked lists into the first list in the parameters. E. Method to clone a linked list. The copy list has to be...
program language: JAVA For this project, you get to design and write a WeightedCourseGrade class to...
program language: JAVA For this project, you get to design and write a WeightedCourseGrade class to keep track of a student's current grade. You also get to design and write WeightedCourseGradeDriver class that requests input from the user and interacts with the WeightedCourseGrade class. Your WeightedCourseGrade class should store the following information: Weighted subtotal (the sum of all of the categories multiplied by the grade category weight) Total category weights (the sum of all the grade category weights) Provide the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT