Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8912377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:11:00+00:00 2026-06-15T04:11:00+00:00

I’ve been banging my head against the wall for a while with this one

  • 0

I’ve been banging my head against the wall for a while with this one –

I have a method to create a list of objects of another class:

    public List <PayrollRecord> processPayroll() {
    List<PayrollRecord> payroll = new ArrayList<PayrollRecord>();
    for (Employee employee : staff) {
        PayrollRecord payRec = new PayrollRecord(employee.getName(), employee.calculatePay());
        payroll.add(payRec);
    }
    return payroll;
}

staff is a list of Employee class objects that can be added to the list by this method:

    public void addEmployee(Employee employee) {
    staff.add(employee);
}

Employee is also an interface implemented by a few other classes – Manager, SalesAssociate and StoreEmployee. (Can you tell I’m trying to get through an assignment yet? Maybe some of you are familiar).

The PayrollRecord class looks like this:

    package my.package.ext;

public class PayrollRecord {

    private String employeeName;
    private double currentPay;

    public PayrollRecord(String employeeName, double currentPay) {

    }

    public double getCurrentPay() {


        return this.currentPay;
        }

        public String getEmployeeName() {
            return this.employeeName;
        }

    }

So within a test class, I should be able to create some variables for employee info, add some employees to staff, and then run the processPayroll method and do some asserts on it. My test class currently looks like this (and will need several more asserts. I’m not going to bother adding those until I can get this basic problem figured out):

    @Test
public void testPayroll() {
    List<PayrollRecord> list = store.processPayroll();
    assertEquals(managerTestPay, list.get(0).getCurrentPay(), 0);
}

Based on the number of employees I have added elsewhere in the test class, there should be five. I can set list.get out of bounds and verify that there are indeed 5. However the problem is that all 5 records are null and 0.0. I should have a name and pay for each employee record. However the assertion error comes back showing me what I expected, but that the actual value is 0.0 (or null when I try list.get(0).getName()).

Thank you in advance for your help and wisdom.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T04:11:01+00:00Added an answer on June 15, 2026 at 4:11 am

    And you set the members how ?

    private String employeeName;
    private double currentPay;
    
    public PayrollRecord(String employeeName, double currentPay) {
    
    }
    

    In your constructor you need to assign to the members e.g.

    this.employeeName = employeeName;
    

    It’s not enough to simply call the constructor with arguments. The arguments need to be used to populate the members of the class (either directly or via some manipulation).

    A useful technique here is to set the member variables to be final. This means:

    1. you can’t change their values post initialisation
    2. you have to initialise the values either immediately or in the constructor

    You might think this is a limitation. However a lot of the time you’ll find you only set this info once (I suspect for your assignment the name and salary will remain constant). If you need to change one or other it’s easy to remove the final qualification.

    private final String employeeName;
    private final double currentPay;
    
    public PayrollRecord(String employeeName, double currentPay) {
       this.employeeName = employeeName;
       // etc...
    }
    

    Ensuring that the members don’t change means the class instance is immutable. This is a good thing generally. It’s easy to reason about and debug immutable classes, and they’re implicitly thread-safe.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
This could be a duplicate question, but I have no idea what search terms
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have been unable to fix a problem with Java Unicode and encoding. The
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.