Question

Hello, How would I test this in Junit? Is there anything else I can do with...

Hello, How would I test this in Junit? Is there anything else I can do with my code?

import java.text.DecimalFormat;

public class PayCalculator implements Constants {
  
   private String employeeName;
   private int reportID;
   private double hourlyWage;
   private static int ID = 0;
   private static int reportIDGenerator = 1000;
   public int[] overtimes = HOURS_WORKED;

public PayCalculator(String name) {
   super(); {
   this.reportID = reportIDGenerator;
   reportIDGenerator+=10;
   this.overtimes = HOURS_WORKED;
   }
}
public PayCalculator(String name, double hourlyWage) {
   this.reportID = reportIDGenerator;
reportIDGenerator+=10;
this.employeeName = name;
this.hourlyWage = hourlyWage;
this.overtimes = HOURS_WORKED;
}

public String getEmployeeName() {
return employeeName;
}

public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}

public int getReportID() {
return reportID;
}

public double getHourlyWage() {
return hourlyWage;
}

public void setHourlyWage(double hourlyWage) {
this.hourlyWage = hourlyWage;
}
  
@Override
public String toString() {
return "PayCalculator [employeeName=" + employeeName + ", reportId=" + reportID +
       ", hourlyWage=" + hourlyWage+ "]";
}
public double calculateYearlyGrossPay(){
double totalGross = 0.0;
for(int period = 1; period <= PAY_PERIODS_IN_YEAR; period++){
totalGross += calculateGrossForPeriod(period);
}
return totalGross;
}
public double calculateYearlyNetPay(){
double totalNet = 0.0;
for(int period = 1; period <= PAY_PERIODS_IN_YEAR; period++){
totalNet += calculateNetPayForPeriod(period);
}
return totalNet;
}
public double calculateNetPayForPeriod(int periodNumber){
double gross = calculateGrossForPeriod(periodNumber);
double tax = calculateTax(gross);
double netPay = gross-tax;
return netPay;
}
public double PAY_PERIODS_IN_YEAR(int periodNumber){
double gross = calculateGrossForPeriod(periodNumber);
double tax = calculateTax(gross);
double netPay = gross-tax;
return netPay;
}
public void printNetPayForAllPeriods(){
DecimalFormat df = new DecimalFormat("#.00");
System.out.println("NET PAY for all periods:\n");
for(int period =1; period <= PAY_PERIODS_IN_YEAR; period++){
System.out.println("PERIOD:"+period+" NET PAY:"+df.format(calculateNetPayForPeriod(period)));
}
}
public void increaseWageRate(double percentage){
hourlyWage =hourlyWage+ hourlyWage*(percentage/100);
}

private double calculateGrossForPeriod(int periodNumber){
double regulayPay = FULL_TIME*hourlyWage;
double overtimePay = overtimes[periodNumber-1]*(hourlyWage*OVERTIME_RATE);
double gross= regulayPay+overtimePay;
return gross;
}
private double calculateTax(double gross){
double federalTax = gross*FEDERAL_TAX_RATE;
double stateTax = gross*STATE_TAX_RATE;
return federalTax+stateTax;
}
  
}

public interface Constants {
  
   public final int[] HOURS_WORKED = {89, 80, 19, 73, 44, 99, 77, 0, 80, 70, 80, 87, 84, 82,
           80, 30, 89, 90, 100, 120, 0, 69, 99, 91, 83, 80};
  
   public final int PAY_PERIODS_IN_YEAR = 26;
   public final double FEDERAL_TAX_RATE = 0.2;
   public final double STATE_TAX_RATE = 0.09;
   public final double OVERTIME_RATE = 1.5;
   public final double FULL_TIME = 80;
  
   public final int PAY_PERIOD_1 = 0;
   public final int PAY_PERIOD_2 = 1;
   public final int PAY_PERIOD_3 = 2;
   public final int PAY_PERIOD_4 = 3;
   public final int PAY_PERIOD_5 = 4;
   public final int PAY_PERIOD_6 = 5;
   public final int PAY_PERIOD_7 = 6;
   public final int PAY_PERIOD_8 = 7;
   public final int PAY_PERIOD_9 = 8;
   public final int PAY_PERIOD_10 = 9;
   public final int PAY_PERIOD_11 = 10;
   public final int PAY_PERIOD_12 = 11;
   public final int PAY_PERIOD_13 = 12;
   public final int PAY_PERIOD_14 = 13;
   public final int PAY_PERIOD_15 = 14;
   public final int PAY_PERIOD_16 = 15;
   public final int PAY_PERIOD_17 = 16;
   public final int PAY_PERIOD_18 = 17;
   public final int PAY_PERIOD_19 = 18;
   public final int PAY_PERIOD_20 = 19;
   public final int PAY_PERIOD_21 = 20;
   public final int PAY_PERIOD_22 = 21;
   public final int PAY_PERIOD_23 = 22;
   public final int PAY_PERIOD_24 = 23;
   public final int PAY_PERIOD_25 = 24;
   public final int PAY_PERIOD_26 = 25;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I would share you some snapshots from one of existing project to show how junit can be applied successfully

So this is the general structure of the project and the imports you would need .

E Quick Access - a PO Java EE - shoppingbackend/src/test/java/com/homsil/shoppingbackend/test/Cooklest.java - Eclipse File Ed

You can multiple test cases like


   @Test
   public void testGetCook() {
      
       cook = cookDAO.get(3);
      
      
       assertEquals("Successfully fetched a single cook from the table!","Television",cook.getName());
      
      
   }
  
  
   @Test
   public void testUpdateCook() {
      
       cook = cookDAO.get(3);
      
       cook.setName("TV");
      
       assertEquals("Successfully updated a single cook in the table!",true,cookDAO.update(cook));
      
      
   }
  

This code is from the same file . the important line is the assertEquals for you . So if the update has been successfull then the test case pass .

74 750 /* 76 77 @Test public void testDeleteCook() { cook = cookDAO.get (3); assertEquals(Successfully deleted a single cook

After that you need to write click on the file and Run As --> Junit .

So for your example , you need to run the test case on your methods for example .

private double calculateTax(double gross){
double federalTax = gross*FEDERAL_TAX_RATE;
double stateTax = gross*STATE_TAX_RATE;
return federalTax+stateTax;
}

assertEquals("Successfully calculated test",expectedvalue,calculateTax(100.00));

@Test
   public void testUpdateCook() {
      
// Create the PayCalculator object here.
  assertEquals("Successfully calculated test",expectedvalue,obj.calculateTax(100.00));
      
      
   }

Hope this helps.

Add a comment
Know the answer?
Add Answer to:
Hello, How would I test this in Junit? Is there anything else I can do with...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Must be written in java. Modularize the following code. //CIS 183 Lab 4 //Joseph Yousef import...

    Must be written in java. Modularize the following code. //CIS 183 Lab 4 //Joseph Yousef import java.util.*; public class SalaryCalc { public static void main(String [] args) { Scanner input = new Scanner(System.in); int sentinelValue = 1; //initializes sentinelValue value to 1 to be used in while loop while (sentinelValue == 1) { System.out.println("Enter 1 to enter employee, or enter 2 to end process: ");    sentinelValue = input.nextInt(); input.nextLine(); System.out.println("enter employee name: "); String employeeName = input.nextLine(); System.out.println("enter day...

  • This is my code but my overtime calculation is wrong and I cant figure out how...

    This is my code but my overtime calculation is wrong and I cant figure out how to round to two decimal places import java.util.Scanner; public class HourlyWage { public static void main(String[] args) { //Variables double totalWage; double totalOvertimePay; double totalPay; String name; double overtimeHours = 0.0; double hoursWorked = 0.0; double hourlyWage = 0.0; Scanner keyboard = new Scanner(System.in); System.out.println("Please enter your name "); name = keyboard.next(); System.out.println("Please enter your hourly wage "); hourlyWage = keyboard.nextDouble(); System.out.println("How many hours...

  • Can someone help me with my code.. I cant get an output. It says I do...

    Can someone help me with my code.. I cant get an output. It says I do not have a main method. HELP PLEASE. The instruction are in bold on the bottom of the code. package SteppingStones; //Denisse.Carbo import java.util.Scanner; import java.util.ArrayList; import java.util.List; public class SteppingStone5_Recipe { private String recipeName; private int servings; private List<String> recipeIngredients; private double totalRecipeCalories; public String getRecipeName() { return recipeName; } public void setRecipeName (string recipeName){ this.recipeName = recipeName; } public int getServings() { return...

  • Cant figure out how to fix error Code- import java.io.File; import java.io.IOException; import java.util.*; public class Program8 {    public static void main(String[] args)throws IOException{       ...

    Cant figure out how to fix error Code- import java.io.File; import java.io.IOException; import java.util.*; public class Program8 {    public static void main(String[] args)throws IOException{        File prg8 = new File("program8.txt");        Scanner reader = new Scanner(prg8);        String cName = "";        int cID = 0;        double bill = 0.0;        String email = "";        double nExempt = 0.0;        String tExempt = "";        int x = 0;        int j = 1;        while(reader.hasNextInt()) {            x = reader.nextInt();}        Customers c1 [] = new Customers [x];        for (int...

  • import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {   ...

    import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {        Student s1 = new Student();         Student s2 = new Student("Smith", "123-45-6789", 3.2);         Student s3 = new Student("Jones", "987-65-4321", 3.7);         System.out.println("The name of student #1 is ");         System.out.println("The social security number of student #1 is " + s1.toString());         System.out.println("Student #2 is " + s2);         System.out.println("the name of student #3 is " + s3.getName());         System.out.println("The social security number...

  • Hello, How can I make the program print out "Invalid data!!" and nothing else if the...

    Hello, How can I make the program print out "Invalid data!!" and nothing else if the file has a grade that is not an A, B, C, D, E, or F or a percentage below zero? I need this done by tomorrow if possible please. The program should sort a file containing data about students like this for five columns: one for last name, one for first name, one for student ID, one for student grade percentage, one for student...

  • I'm getting a few errors still under RecipeBox. Would you be able to see what's wrong?...

    I'm getting a few errors still under RecipeBox. Would you be able to see what's wrong? Thanks ------------ package recipecollection; import java.util.ArrayList; public class SteppingStone5_RecipeTest { public static void main(String[] args) { ArrayList<String> recipeIngredients = new ArrayList<>(); recipeIngredients.add("Peanut butter"); recipeIngredients.add("Jelly"); recipeIngredients.add("Bread"); SteppingStone5_Recipe recipe1 = new SteppingStone5_Recipe("Peanut butter & jelly sandwich", 2, recipeIngredients, 300, 10.0); System.out.println("RECIPE 1"); recipe1.printRecipe(); System.out.println(); recipe1.setRecipeName("Turkey sandwich"); recipe1.setServings(5); recipe1.setTotalRecipeCalories(500); System.out.println(); System.out.println("RECIPE 1 (Modified)"); recipe1.printRecipe(); System.out.println(); System.out.println("RECIPE 2"); SteppingStone5_Recipe recipe2 = SteppingStone5_Recipe.createNewRecipe(); recipe2.printRecipe(); } } ///////////////// package recipecollection;...

  • How would I test this code thoroughly in a main.cpp file. Locomotive is an abstract base...

    How would I test this code thoroughly in a main.cpp file. Locomotive is an abstract base class. steam and dieselElectric are the derived classes. #ifndef COMMODITY_H #define COMMODITY_H #include <iostream> #include <string> using namespace std; class commodity { private:    string name;    int quantity; public:    commodity(commodity *c);    commodity(string name,int quantity);    ~commodity();    string getName() const;    int getQuantity() const; }; #endif #include "commodity.h" commodity::commodity(commodity *c) {       this->name=c->getName();    this->quantity=c->getQuantity(); } commodity::commodity(string name,int quantity) {       this->name=name;   ...

  • JAVA: How do I output all the data included for each employee? I can only get...

    JAVA: How do I output all the data included for each employee? I can only get it to output the name, monthly salary and annual salary, but only from the Employee.java file, not Salesman.java or Executive.java. Employee.java package project1; public class Employee { private String name; private int monthlySalary; public Employee(String name, int monthlySalary) { this.name = name; this.monthlySalary = monthlySalary; } public int getAnnualSalary() { int totalPay = 0; totalPay = 12 * monthlySalary; return totalPay; } public String...

  • I need help in getting the second output to show both the commission rate and the...

    I need help in getting the second output to show both the commission rate and the earnings for the String method output package CompemsationTypes; public class BasePlusCommissionEmployee extends CommissionEmployee { public void setGrossSales(double grossSales) { super.setGrossSales(grossSales); } public double getGrossSales() { return super.getGrossSales(); } public void setCommissionRate(double commissionRate) { super.setCommissionRate(commissionRate); } public double getCommissionRate() { return super.getCommissionRate(); } public String getFirstName() { return super.getFirstName(); } public String getLastName() { return super.getLastName(); } public String getSocialSecurityNumber() { return super.getSocialSecurityNumber(); } private...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT