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...
write program in java Create a class named PersonalDetails with the fields name and address. The...
write program in java Create a class named PersonalDetails with the fields name and address. The class should have a parameterized constructor and get method for each field.  Create a class named Student with the fields ID, PersonalDetails object, major and GPA. The class should have a parameterized constructor and get method for each field. Create an application/class named StudentApp that declare Student object. Prompts (GUI input) the user for student details including ID, name, address, major and GPA....
3) Create a Java program that uses NO methods, but use scanner: Write a program where...
3) Create a Java program that uses NO methods, but use scanner: Write a program where you will enter the flying distance from one continent to another, you will take the plane in one country, then you will enter miles per gallon and price of gallon and in the end it will calculate how much gas was spend for that distance in miles. Steps: 1) Prompt user to enter the name of country that you are 2) Declare variable to...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount to represent a bank account according to the following requirements: A bank account has three attributes: accountnumber, balance and customer name. Add a constructor without parameters. In the initialization of the attributes, set the number and the balance to zero and the customer name to an empty string. Add a constructor with three parameters to initialize all the attributes by specific values. Add a...
Create a Project and a Class called “FinalGrade” Write a Java program to compute your final...
Create a Project and a Class called “FinalGrade” Write a Java program to compute your final grade for the course. Assume the following (you can hard-code these values in your program). Assignments are worth 30% Labs are worth 40% Mid-term is worth 15% Final is worth 15% Ask for the grades in each category. Store them in a double variable. Print out the final percentage grade.           For example, Final Grade Calculation Enter percentage grades in the following order. Assignments,...
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...
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.
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT