Testerfile

jrb47
jrb47 used Ask the Experts™
on
I am trying to implement a tester file.
It will test 4 classes that have been written

The files are listed in the uploaded doc

the testcode that i have is......and i know it is wrong
/**
   This program tests the BankAccount class and
   its subclasses. 
*/
public class Tester
{  
   public static void main(String[] args)
   {  
     HourlyWorker h1
            = new HourlyWorker(26,"Smith",11.50);
      
     HourlyWorker h2
            = new HourlyWorker(27,"Brown",10.50);
         
     SalariedWorker s1
	  			= new SalariedWorker(127,"Black",250.00);
		
	 SalariedWorker s2
	  			= new SalariedWorker(129,"Green",650.00); 
				
				
				    
    h2.computePay(30);
	 System.out.println(h2.toString);
	 
	 h1.computePay(50);
	 System.out.println(h1.toString);
	 
	      
    s1.computePay();
	 System.out.println(s1.toString);
 
	 s2.computePay();
   System.out.println(s1.toString);
   }
}

Open in new window

public-abstract-class-Worker.doc
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Kevin CrossChief Technology Officer
Most Valuable Expert 2011

Commented:
jrb47,

As we discussed previously with List / ArrayList, it is best to type your object as its abstraction / interface so you can switch between different implementations.

e.g., instead of this:
HourlyWorker h1
            = new HourlyWorker(26,"Smith",11.50);

you would do this:
Worker h1
            = new HourlyWorker(26,"Smith",11.50);

Remember that methods should have () regardless of if you need to pass parameters or not.

e.g., instead of this:
System.out.println(h2.toString);

you should write this:
System.out.println(h2.toString());

or simply:
System.out.println(h2); // will call toString() implicitly

Remember computePay() takes a double parameter for hours worked.  Even though these are salaried employees, still pass something like computePay(40).

Lastly, you will probably want to test this all through your Workers class, so use that.

e.g., something similar to this:
Workers company = new Workers("JRB47 Incorporated");
company.add(new HourlyWorker(26,"Smith",11.50));

You can then use the getWeeklyPayroll() method as you know that will test all the computePay() calls.  Can test toString() similarly or still from Worker object like so.

Worker w = company.getWorker(1);
System.out.println(w.toString());

Hope that helps.

Please post back if you continue to have issues or need a better explanation.

Regards,
Kevin
Kevin CrossChief Technology Officer
Most Valuable Expert 2011

Commented:
Type-o:
*company.add(new HourlyWorker(26,"Smith",11.50));

Should be:
company.addWorker(new HourlyWorker(26,"Smith",11.50));

Author

Commented:
This is compiling but not working correct
1. I have not called the computePay method correct so no hours are being passes
2. I kept getting errors on trying to loop through the list for printing
3. the figure printing out for getpayroll is way wrong

4. i am sure there is more.



/**
   This program tests the BankAccount class and
   its subclasses. 
*/
public class Tester
{  
   public static void main(String[] args)
   {  
    Workers company = new Workers("JRB47 Incorporated");
    company.addWorker(new HourlyWorker(26,"Smith",10.50));
	 company.addWorker(new HourlyWorker(27,"Brown",11.50));
	 company.addWorker(new SalariedWorker(28,"Black",125.50));
    company.addWorker(new SalariedWorker(29,"Green",235.50));
 
 
   	System.out.println(company);
//	for(int i=0; i< w.lenght; i++)
//	{
	Worker w = company.getWorker(1);
   System.out.println(w.toString());
	System.out.println(w.toString());
	
   	
//	}	 
	 	System.out.println(company.getWeeklyPayroll());	 
	}
}

Open in new window

Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Kevin CrossChief Technology Officer
Most Valuable Expert 2011

Commented:
Remember it assumes 40 hours for getWeeklyPayroll() per the original assignment instructions.  

What is the result you are getting?  What do you think it should be?  Why?

As for the loop issue, it seems the problem is here at first glance:
for(int i=0; i< w.lenght; i++)

Should be looping through number of workers in company, so should look like this:
for (int i=0; i < company.getNumemps(); i++)

Then instead of calling getWorker(1) you would pass the value of i.
Worker w = company.getWorker(i);

Regards,
Kevin

Author

Commented:
Here is the output
 ----jGRASP exec: java Tester

Workers@1f6a7b9
Smith ID=26 (hourly rate = 10.5,hours worked = 0)
Brown ID=27 (hourly rate = 11.5,hours worked = 0)
Black ID=28 (weekly wage = 125.5)
Green ID=29 (weekly wage = 235.5)
383.0

 ----jGRASP: operation complete.


I would expect the total to be the result of each persons compute pay (so i changed it to one)
if i change it to 40 it multiplies each persons weekly wage by 40 or their hourly rate

also i have not passed the number of hours for the hourly employee to computePay.

/**
   This program tests the BankAccount class and
   its subclasses. 
*/
public class Tester
{  
   public static void main(String[] args)
   {  
    Workers company = new Workers("JRB47 Incorporated");
    company.addWorker(new HourlyWorker(26,"Smith",10.50));
	 company.addWorker(new HourlyWorker(27,"Brown",11.50));
	 company.addWorker(new SalariedWorker(28,"Black",125.50));
    company.addWorker(new SalariedWorker(29,"Green",235.50));
 
 
   System.out.println(company);
   for (int i=0; i < company.getNumemps(); i++)
   {
	Worker w = company.getWorker(i);
   System.out.println(w.toString());
   	
	}	 
	 	System.out.println(company.getWeeklyPayroll());	 
	}
}

Open in new window

Author

Commented:
in addition the company name is printing out like an array address or something

Kevin CrossChief Technology Officer
Most Valuable Expert 2011

Commented:
Part of the assignment is to implement toString() on your Workers class.  That will take care of the issue with the print of company being a class identifier.  Think your toString() was supposed to print the company and all the workers, so the loop would be there too.

As for the payroll issue.  I meant what $ value is it returning that you are saying is wrong.  And what $ value should it be instead as I thought it was a calculation problem.

"If i change it to 40 it multiplies each persons weekly wage by 40 or their hourly rate"

That should not be the case as this should be based on your implementation of computePay(double); therefore, you can pass 40 to both the hourly and salaried employees.

HourlyWorker:
public double computePay(double hours) {
            // use hourly rate stored * hours passed as parameter
            return hourlyRate * hours;
      }

SalariedWorker:
public double computePay(double hours) {
            // ignore hours passed as parameter, since salaried
            return weeklyWage;
      }

Passing 40 should yield the correct results per assignment for both here as hourly folks need to get paid for all 40 hours in the week versus just 1 and salaried individuals get paid the same regardless.
Kevin CrossChief Technology Officer
Most Valuable Expert 2011

Commented:
I get this as result of running the code in snippet so you can see the payroll amount that appears to be correct mathmetically; therefore, again, please provide the $ figure you would expect instead if that still seems wrong to you.

Weekly Payroll: 1780.0

The code I posted assumes implementation of toString() within Workers class.


public class Tester {
 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Workers company = new Workers("JRB47 Incorporated");
		company.addWorker(new HourlyWorker(26,"Smith",11.50));
		company.addWorker(new HourlyWorker(27,"Brown",10.50));
		company.addWorker(new SalariedWorker(127,"Black",250.00));
		company.addWorker(new SalariedWorker(129,"Green",650.00));
		
		System.out.println(company);
		System.out.println();
		System.out.println("Weekly Payroll: " + company.getWeeklyPayroll());
		System.out.println();
	}
 
}

Open in new window

Author

Commented:
the payroll is printing out correct - my bad

The hours worked is printing zero
and i need to call the computePay to test greater than 40

and there is not tostring method in the workers class so i need to put that in there

Author

Commented:
I think i need to step back here:

i see some mistakes let me try to fix them!

Author

Commented:
This is the toString that i tried for Workers.....it is wrong


	public String toString()
  {
     return company +
  
	for (int i=0; i < company.getNumemps(); i++)
   {
	Worker w = company.getWorker(i);
   System.out.println(w.toString());
     	
	}	 

Open in new window

Kevin CrossChief Technology Officer
Most Valuable Expert 2011

Commented:
You need to build a string instead of using println().  Since String is immutable, when you append to it you actually create a new instance of String; therefore, I had mocked something up on my end using StringBuilder so hopefully it doesn't confuse things.  You can use regular strings if you want noting that sb.append() is same as "original string" + "another string".

I have to run and catch a flight shortly, so if I go offline you know why although I will try to answer via mobile EE.

Regards,
isa
public String toString() {
		StringBuilder sb = new StringBuilder();
		
		sb.append(company);
		
		for (Worker w : workerList) sb.append("\n" + w);
		
		return sb.toString();
	}

Open in new window

Author

Commented:
one last thing
I need to print out a worker.computePay with over 40 hours to test the overtime pay
Would that be

w.computePay(50);
System.out.println(w.computePay();

Author

Commented:
funny I am trying to get out of here to go from Fl to Ga also

Kevin CrossChief Technology Officer
Most Valuable Expert 2011

Commented:
Put the call to computePay(50) inside the println.

Author

Commented:
System.out.println(w.computePay(50));  

put in the tester.java

gives this error

 ----jGRASP exec: javac -g D:\CEN5990\Workshops\WORKSHOP10\Tester.java

Tester.java:14: cannot find symbol
symbol  : variable w
location: class Tester
    System.out.println(w.computePay(50));  
                       ^
1 error

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.

BTW i did fix the method for hourly computePay to calculate the overtime

Kevin CrossChief Technology Officer
Most Valuable Expert 2011

Commented:
It looks like you didn't get a worker first.
Try company.getWorker(0).computePay(50)
Chief Technology Officer
Most Valuable Expert 2011
Commented:
Or before code you have currently:
Worker w = company.getWorker(0);

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial