Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Need help outputting sub-list of an ArrayList

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    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;
    }
     
     
    }
    _____________________________
    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;
    }
    }
    _____________________________________
    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
    Last edited by Allusive; April 21st, 2010 at 04:48 AM.


  2. #2
    Junior Member
    Join Date
    Mar 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  2. list in JSP
    By smackdown90 in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: November 13th, 2011, 01:08 PM
  3. Keep it simple for the retard- Outputting audio on a trigger.
    By andypooz in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 17th, 2010, 08:14 AM
  4. Arraylist or Arraylist Object?
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: September 11th, 2009, 02:08 AM
  5. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM