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 7926655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:51:43+00:00 2026-06-03T18:51:43+00:00

If you want to get straight to the problem, skip this paragraph. As practice,

  • 0

If you want to get straight to the problem, skip this paragraph. As practice, I am trying to write a Java program that simulates an economy, and to that end wrote a company class. The idea was to have, say, a dozen of them, wrap their earnings into a normalvariate function, and that would be the economy.

I wrote a separate class to graph the companies’ outputs using JFreeChart. However, I can’t access the ArrayList that I write the amount of money for each year to from the graphing class. I understand the best way to do this is probably with getters, but it didn’t seem to work, so if that is your advice, could you please provide an example? Thanks!

The company:

public class ServiceProvider implements Company {
    //Variables


    public ArrayList getRecords(){
        return records;
    }

    public ServiceProvider(){
        money = 10000;
        yearID = 0;
        goodYears = 0;badYears = 0;
        records = new ArrayList();
        id++;
    }

    public void year() {
        yearID++;
        if(!getBankrupt()){
            spend();
        }
        writeRecords();
    }

    public void spend() {
        ...
    }

    public void printRecords() {
        for(int i=0;i<records.size();i++){
            String[] tmp = (String[]) records.get(i);
            for(String a:tmp){
                System.out.print(a+" ");
            }
            System.out.print("\n");


        }

    }

    public void writeRecords(){
        String[] toWrite = new String[2];
        toWrite[0] = String.valueOf(yearID);
        toWrite[1] = String.valueOf(money);
        records.add(toWrite);
    }

    public void writeRecords(String toWrite){
        String temp = "\n"+yearID+"   "+toWrite;
        records.add(temp);
    }

    public boolean getBankrupt(){
        boolean result = (money < 0) ? true : false;
        return result;
    }


}

What I am trying to access it from:

public class grapher extends JFrame {
    ArrayList records = s.getRecords();

    public grapher(){
        super("ServiceProvider");
        final XYDataset dataset = getCompanyData();
    }



    private XYDataset getCompanyData(){
        XYSeries series;
        for(int i=0;i<s.getRecords().length;i++){ //S cannot be resolved, it's instantiated in the main class.

        }
    }

}

The main class:

public class start {

    public static void main(String[] args) {
        ServiceProvider s = new ServiceProvider();
        for(int i=0;i<10;i++){
            s.year();
        }
        s.printRecords();

    }

}

P.S. Don’t tell me what a mess Records are. I know.

  • 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-03T18:51:59+00:00Added an answer on June 3, 2026 at 6:51 pm

    Pass the instance of ServiceProvider as an argument to the grapher constructor and then it can pass it as an argument to getCompanyData().

    Since the instance is created outside of the grapher class, there is no way for grapher to have the instance of ServiceProvider to work with unless you hand that instance to grapher.

    BTW, make sure that whatever you do with that ArrayList in grapher that you don’t change it. If you do, you’ll be changing it in the ServiceProvider (since it’s all just references to the same underlying ArrayList). That’s probably not what you want to do. If you do need to change it, make a copy and work with the copy.

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

Sidebar

Related Questions

I want to get this straight. I know what DLL's are. I have done
I want get all of the Geom objects that are related to a certain
I want to get an entity with its sub-properties. How can I do that
Pretty straight forward problem. I simply want to return the affected row created by
This Is a problem that I've been leaving and coming back to for a
I have a fairly straight-forward scenario that I am trying to solve but I'm
Greetings! Lets cut the excessive intro this time and get straight to the point.
I have written some jquery to get around this problem: iPhone "Bookmark to Homescreen"
Having some problem getting my program to sleep What im trying to do is
I can't get a straight forward answer on this. Is Google's V8 engine really

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.