Need help outputting sub-list of an ArrayList
Here are my Two classes and a tester. I need help outputting a sub-list of all the Highly Paid employees.
Code :
public class Pay2
{
private String name;
private char gender;
private double hours;
private double payRate;
/**
* Constructor for objects of class Pay
*/
public Pay2(String n, char g, double h, double r)
{
// initialise instance variables
name = n;
gender = g;
hours = h;
payRate = r;
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public double computeGrossPay ()
{
return hours * payRate;
}
public char getGender ()
{
return gender;
}
public double getRate ()
{
return payRate;
}
}
_____________________________
Code :
import java.util.*;
public class Salary
{
private ArrayList theSalary;
public Salary()
{
ArrayList theSalary = new ArrayList ();
}
public void addSalary (Pay2 p)
{
theSalary.add (p);
}
public double computeSalary()
{
double total = 0;
for (int i = 0; i < theSalary.size(); i++)
{
Pay2 p =(Pay2) theSalary.get (i);
total = total + p.computeGrossPay();
}
return total;
}
public double computeMale()
{
double total = 0;
for (int i = 0; i < theSalary.size();i++)
{
Pay2 p =(Pay2) theSalary.get (i);
if (p.getGender() == 'm')
total = total + p.computeGrossPay();
}
return total;
}
public double computeFemale()
{
double total = 0;
for (int i = 0; i < theSalary.size();i++)
{
Pay2 p =(Pay2) theSalary.get (i);
if (p.getGender() == 'f')
total = total + p.computeGrossPay();
}
return total;
}
public int highPay(double i)
{
double pay = i;
int number = 0;
for( int s = 0; s < theSalary.size(); s++)
{
Pay2 p =(Pay2) theSalary.get (s);
if (p.getRate() >= pay)
number++;
}
return number;
}
public ArrayList highPaid(double b)
{
double pay = b;
ArrayList a = new ArrayList();
for (int i = 0; i < theSalary.size(); i++)
{
Pay2 p =(Pay2) theSalary.get (i);
if (p.getRate() >= pay)
a.add(p);
}
return a;
}
}
_____________________________________
Code :
import java.util.*;
public class Tester
{
public static void main (String [] args)
{
Salary s = new Salary();
Pay2 p = new Pay2("Bob", 'm', 30, 10.50);
s.addSalary(p);
p = new Pay2("Jen", 'f', 30, 12.00);
s.addSalary(p);
p = new Pay2("Tom", 'm', 20, 9.50);
s.addSalary(p);
p = new Pay2("Pat", 'f', 40, 15.65);
s.addSalary(p);
p = new Pay2("Nick", 'm', 45, 12.00);
s.addSalary(p);
p = new Pay2("Mike", 'm', 35, 15.00);
s.addSalary(p);
p = new Pay2("Barb", 'f', 20, 25.00);
s.addSalary(p);
p = new Pay2("Katie", 'f', 30, 14.00);
s.addSalary(p);
p = new Pay2("John", 'm', 45, 12.00);
s.addSalary(p);
p = new Pay2("Mark", 'm', 40, 7.75);
s.addSalary(p);
Scanner sc = new Scanner(System.in);
System.out.println("What is the threshold for high Pay?");
double a = sc.nextDouble ();
double w = s.computeSalary();
double m = s.computeMale();
double f = s.computeFemale();
int h = s.highPay(a);
System.out.println("The weekly salarys for all employees is $" + w);
System.out.println("The weekly salarys for all males is $" + m );
System.out.println("The weekly salarys for all females is $" + f);
}
}
Any feedback will help.
Thanks, Allusive
Re: Need help outputting sub-list of an ArrayList
Highly paid employees means what???
Tell the ranges of highly paid employee??
There can only be one employee who has the highest pay in the whole, so make it more clear please. Thanks.