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

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);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!