Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Single Task: I need help writting test cases on a single program called \"P

ID: 3707553 • Letter: J

Question

Java Single Task:

I need help writting test cases on a single program called "Payroll" which extends a generic class "Dict" based on JavaDoc (no actual codes are given). Please read and help me if you can. Thank you.

I have finished the test cases for class Dict. But I do not know how to begin on class Payroll.

Plese help me writting test cases for all methods for class Payroll (8 methods total)

Below is the demo and Javadoc of 2 classes

Demo of Dict

The Dict class provides a quite simple dictionary implementation. Below is a demonstration of creating one and calling some of its methods.

> import payroll.*;

> Dict d = new Dict();

> d.put("one",1);

> d

{(one:1)}

> d.size()

1

> d.put("two",2);

> d.put("three",3);

> d.size()

3

> d.get("one")

1

> d.get("350")

java.util.NoSuchElementException: "350" key not in dict.

at payroll.Dict.complainNoKey(Dict.java:147)

at payroll.Dict.get(Dict.java:70)

> d.pop("two")

2

> d.toString()

"{(one:1),(three:3)}"

> d.keys()

[one, three]

> d.clear()

> d

{}

>

Demo of Payroll

The Payroll class uses the Dict class in its implementation of some salary-related calculations. Below is a demonstration of some basic uses of Payroll.

> import payroll.*;

> Payroll p = new Payroll()

> p.hire("A",120);

> p.hire("A",120)

> p.hire("B",240)

> p.employees()

[A, B]

> p.topSalary()

240

> p.hire("C",1500)

> p.topSalary()

1500

> p.fire("C")

true

> p.employees()

[A, B]

> p.fire("C")

false

> p.giveRaise("A",0.5)

> p.getSalary("A")

180

> p.giveRaise(1.0) // everyone gets 100% raise!

> p.getSalary("A")

360

> p.getSalary("B")

480

> p.monthlyExpense() // for one month of twelve.

70

>

-----------Javadoc for Dict--------------

Class Dict

java.lang.Object

payroll.Dict

public class Dict

extends java.lang.Object

The Dict class provides an arraylist-based dictionary implementation. It provides a minimal set of operations to be generally useful.

Constructor Summary

Constructors

Constructor and Description

Dict()

Create an empty Dict.

Method Summary

All Methods Instance Methods Concrete Methods

Modifier and Type Method and Description

void clear()

removes all key-value pairs from the Dict.

V get(K key)

Given a key, find it in the Dict and return its associated value.

boolean has(K key)

Checks if the given key is in the Dict or not.

java.util.List keys()

Gives a List of all keys currently in the Dict.

V pop(K key)

Given a key to seek, find and remove that key-value pair (returning the associated value).

void put(K key, V val)

Given a key and value, put them in the Dict.

int size()

Answers how many key-value pairs are currently in the Dict.

java.lang.String toString()

Gives a simple representation of the Dict, e.g.

Methods inherited from class java.lang.Object

clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

Constructor Detail

Dict

public Dict()

Create an empty Dict. (No key-value pairs exist yet).

Method Detail

size

public int size()

Answers how many key-value pairs are currently in the Dict.

Returns:

the number of key-value pairs in this Dict.

has

public boolean has(K key)

Checks if the given key is in the Dict or not. null is never in the Dict.

Parameters:

key - the key to search for in this Dict.

Returns:

whether the given key is in this Dict or not.

get

public V get(K key)

Given a key, find it in the Dict and return its associated value.

Parameters:

key - the key to find in the Dict.

Returns:

the value associated with the search key.

Throws:

java.util.NoSuchElementException - when the key isn't present in the Dict (including when key is null).

put

public void put(K key,

V val)

Given a key and value, put them in the Dict. When the key is already present, this replaces the current key-value pair with that key. Does not allow putting a null key into the Dict.

Parameters:

key - the key to put in the Dict.

val - the value to put in the Dict.

Throws:

java.lang.UnsupportedOperationException - when we attempt to use a null key.

pop

public V pop(K key)

Given a key to seek, find and remove that key-value pair (returning the associated value).

Parameters:

key - the key to seek.

Returns:

the associated value.

Throws:

java.util.NoSuchElementException - when the key isn't present in this Dict.

java.lang.NullPointerException - when the requested key is null.

keys

public java.util.List keys()

Gives a List of all keys currently in the Dict. No ordering is implied (though it may correlate with put-order).

Returns:

the List containing all keys currently in the Dict.

clear

public void clear()

removes all key-value pairs from the Dict.

toString

public java.lang.String toString()

Gives a simple representation of the Dict, e.g. "{(k1:v1),(k2:v2),....}"

Overrides:

toString in class java.lang.Object

Returns:

the informative String as described.

------------------Javadoc for class Payroll----------------

Class Payroll

java.lang.Object

payroll.Payroll

public class Payroll

extends java.lang.Object

class Payroll keeps track of all employees and their yearly salary. It also provides a few useful functionalities. This class is implemented using the Dict class internally.

Constructor Summary

Constructors

Constructor and Description

Payroll()

Creates an empty Payroll.

Payroll(Dict salaries)

Accepts an initial Dict and populates the Payroll.

Method Summary

All Methods Instance Methods Concrete Methods

Modifier and Type Method and Description

java.util.List employees()

Returns a List of employee names.

boolean fire(java.lang.String name)

Fire the named employee (remove from the Payroll), if present.

int getSalary(java.lang.String name)

finds the named employee and returns their salary.

void giveRaise(double raise)

Give everyone the indicated raise.

void giveRaise(java.lang.String name, double raise)

Give only the named person the indicated raise.

void hire(java.lang.String name, int salary)

Hires the named person at the given yearly salary.

int monthlyExpense()

Reports on the monthly cost of paying all employees.

int topSalary()

Finds and returns the biggest salary in this Payroll.

Methods inherited from class java.lang.Object

clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Detail

Payroll

public Payroll()

Creates an empty Payroll.

Payroll

public Payroll(Dict salaries)

Accepts an initial Dict and populates the Payroll.

Parameters:

salaries - the initial Dict of name-salary items.

Throws:

java.lang.NullPointerException - when the parameter, or any of the names in it, is null.

Method Detail

employees

public java.util.List employees()

Returns a List of employee names. Order based on order of last insertion-when-not-present.

Returns:

List of employee names.

getSalary

public int getSalary(java.lang.String name)

finds the named employee and returns their salary.

Parameters:

name - name of employee to report upon.

Returns:

the employee's salary.

Throws:

java.lang.NullPointerException - when name is null.

java.util.NoSuchElementException - when the name isn't in this Payroll.

topSalary

public int topSalary()

Finds and returns the biggest salary in this Payroll. Returns zero when no employees are present.

Returns:

the highest salary, or zero when no employees are present.

hire

public void hire(java.lang.String name,

int salary)

Hires the named person at the given yearly salary. Silently replaces existing entries when that name is already present.

Parameters:

name - hired employee's name

salary - hired employee's yearly salary

Throws:

java.lang.NullPointerException - when the given name is null.

java.lang.RuntimeException - when a negative salary is attempted.

fire

public boolean fire(java.lang.String name)

Fire the named employee (remove from the Payroll), if present. (null names are never present by definition of Dicts). Returns whether anyone was fired.

Parameters:

name - the person to fire.

Returns:

whether or not name was found&fired.

monthlyExpense

public int monthlyExpense()

Reports on the monthly cost of paying all employees. Since given (yearly) salaries are whole numbers, this divides the yearly sum by twelve, and rounds up to the nearest whole number.

Returns:

the monthly salary cost for all employees.

giveRaise

public void giveRaise(double raise)

Give everyone the indicated raise. For example, if an employee's salary is 10000, and raise==0.05 (a 5% raise), the employee's salary becomes 10500. Round up the nearest whole value.

Parameters:

raise - the raise amount.

Throws:

java.lang.RuntimeException - when a negative raise is attempted.

giveRaise

public void giveRaise(java.lang.String name,

double raise)

Give only the named person the indicated raise. For example, if name's salary is 10000, and raise==0.05 (a 5% raise), the employee's salary becomes 10500. Round up the nearest whole value.

Parameters:

name - the name of the employee to give the raise to.

raise - the raise amount.

Throws:

java.lang.RuntimeException - when a negative raise is attempted.

java.lang.NullPointerException - when the name is null.

java.util.NoSuchElementException - when no employee has that name.

----Here is the java test that must be added--

import org.junit.*; // Rule, Test

import org.junit.rules.Timeout;

import static org.junit.Assert.*;

import java.util.*;

import payroll.*;

public class PayrollTest {

public static void main(String args[]){

org.junit.runner.JUnitCore.main("PayrollTest");

}

  

// 1 second max per method tested

@Rule public Timeout globalTimeout = Timeout.seconds(1);

// BEGIN TESTS HERE.

// Eliminate this test after you get the hang of things

@Test public void example(){

assertEquals(5,5);

assertFalse(5==6);

assertTrue(6==6);

}

  

}

Explanation / Answer

Solution:

Please note below are the 8 methods mentioned in the Payroll class for which the test methods are coded.

Please find comments in the code.

java.util.List employees()
boolean fire(java.lang.String name)
int getSalary(java.lang.String name)
void giveRaise(double raise)
void giveRaise(java.lang.String name, double raise)
void hire(java.lang.String name, int salary)
int monthlyExpense()
int topSalary()

PayrollTest.java

import org.junit.*; // Rule, Test
import org.junit.rules.Timeout;
import static org.junit.Assert.*;
import java.util.*;
import payroll.*;

public class PayrollTest {
public static void main(String args[]){
org.junit.runner.JUnitCore.main("PayrollTest");
}

// 1 second max per method tested
@Rule public Timeout globalTimeout = Timeout.seconds(1);

// BEGIN TESTS HERE.
static Payroll payrollObj = null; //creating a reference of Payroll class

/*
* This is a setUp method which is
* by convention static.
* Here we perform operations
* which are required before any of the tests run.
* The BeforeClass annotation performs that part.
*/
@BeforeClass
public static void setUp{
payrollObj = new Payroll();
payrollObj.hire("A",120);
payrollObj.hire("A",120);
payrollObj.hire("B",240);
}

/*
* test method for employees()
* here as expected only two people should be hired
*/
@Test
public void testEmployees(){
Assert.assertNotNull(payrollObj.employees());
Assert.assertEquals(2, payrollObj.employees().size());
}

/*
* test method for hire()
* here as expected only two people should be hired
*/
@Test
public void testHire(){
Assert.assertEquals(2, payrollObj.employees().size());
}

/*
* test method for topSalary()
* here the expected result is 240
*/
@Test
public void testTopSalary(){
Assert.assertEquals(240, payrollObj.topSalary());
payrollObj.hire("C", 1500);
//now the expected is 1500
Assert.assertEquals(1500, payrollObj.topSalary());
}

/*
* test method for fire()
* here the expected result is true
*/
@Test
public void testFire(){
Assert.assertTrue(payrollObj.fire("C"));
Assert.assertEquals(2, payrollObj.employees().size());
Assert.assertFalse(payrollObj.fire("C"));
}

/*
* test method for giveSalary()
*/
public void testGetSalary(){
Assert.assertEquals(120, payrollObj.getSalary("A"));
}

/*
* test method for giveRaise(java.lang.String name, double raise)
*/
@Test
public void testGiveRaise(){
payrollObj.giveRaise("A",0.5);
Assert.assertEquals(180, payrollObj.getSalary("A"));
}

/*
* test method for giveRaise(double raise)
*/
@Test
public void testGiveRaiseAll(){
payrollObj.giveRaise(1.0);
Assert.assertEquals(360, payrollObj.getSalary("A"));
Assert.assertEquals(480, payrollObj.getSalary("B"));
}

/*
* test method for monthlyExpense()
*/
@Test
public void testGiveRaiseAll(){
Assert.assertEquals(70, payrollObj.monthlyExpense());
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote