Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Obtain fixed array from different class

Posted on 2009-05-20
6
261 Views
Last Modified: 2013-11-23
Hi,
I'd like to know how to access a fixed array from different class.
I'm trying to create a visit method in the CoffeeCart class that checking if the office in that array exist than the CoffeeCart will visit that office and automatically will increase the amount of the cup which will be given to the employees in that particular office.

Below here is the code that I've done so far but it always says cannot resolve symbol numOffice which is the name of the array in the company class.

public class CoffeeCart
{
    private Company visitOffice;
    private Office emp;  
     private int cups=0;

public void visit(Office myOffice)
    {
        Company theVisit = visitOffice.addOffice(numOffice[office]);
        int index =0;
        while(index < theVisit.length)
        {
            if(theVisit[index] == null)
            {
                theVisit[index] = myOffice;  
                cups += emp.getNumEmployees();
                break;
            }
            index++;
        }
    }

public class Company
{
    // instance variables
    private CoffeeCart theCoffeeCart;
    private Office[] numOffice = new Office[4];
 
  public void addOffice(Office newOffice)
    {   
       int index = 0;
       while (index<numOffice.length)
       {
           if(numOffice[index] ==null)
           {
               numOffice[index] = newOffice;
               index = numOffice.length;
           }
           index++;
        }
        
    }
 
_______________________________________________________
public class Office
{
     private int numEmployees;
 
    public int getNumEmployees()
    {
        return numEmployees;
    }
 
   
   public void setNumEmployees(int newEmployees)
    {
        numEmployees = newEmployees;
    }

Open in new window

0
Comment
Question by:VanJava
  • 2
  • 2
  • 2
6 Comments
 
LVL 3

Expert Comment

by:hazgoduk
ID: 24434392
In your Company object you need a getArray() function that returns the array
0
 
LVL 3

Expert Comment

by:avdej
ID: 24434846
Look at the code snipped I've prepared for you. It should explain you the issue.

1. Store class A into file A.java
2. Store class B into file B.java
3. Compile and run:
    javac B.java
    java B

You can access any PUBLIC field (no matter which type it has, e.g. Office[]) of some another class in the same way.

Avdej

public class A
{
    static public int i = 11;   // i belongs to class A
    public int j;               // j belongs to an INSTANCE of class A
    
    // CONSTRUCTOR
    public A(int j)
    {
        this.j = j;
    }
}
 
 
public class B
{
    static public void main(String[] args)
    {
        A a = new A(22);
        
        System.out.println("Accessing static field A.i --> " + A.i);    // --> 11
        System.out.println("Accessing dynamic field A.j --> " + a.j);   // --> 22
    }
}

Open in new window

0
 

Author Comment

by:VanJava
ID: 24436340
The thing is I can't change my field status into public, it would make my life easier if i can do that
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 3

Expert Comment

by:hazgoduk
ID: 24436865
You don't change the field to public, you create a public get function to it.

Change your Company class to include the getNumOffice function as below, then you can call theVisit.getNumOffice[i]
public class Company
{
    // instance variables
    private CoffeeCart theCoffeeCart;
    private Office[] numOffice = new Office[4];
 
    public void addOffice(Office newOffice)
    {   
       int index = 0;
       while (index<numOffice.length)
       {
          if(numOffice[index] ==null)
          {
             numOffice[index] = newOffice;
             index = numOffice.length;
          }
          index++;
       }
   }
 
   public Office[] getNumOffice()
   {
      return numOffice();
   }
}

Open in new window

0
 
LVL 3

Expert Comment

by:avdej
ID: 24441941
Hi, VanJava,

Unfortunately it seems that besides of that particular issue your program has a lot of another design inconsistencies and bugs.

For example the following lines have no healthy meaning and will never work:
Company theVisit = visitOffice.addOffice(numOffice[office]);
int index =0;
while(index < theVisit.length)
...
1. visitOffice.addOffice(...) returns 'void' -> whereas you try to get Company from it.
2. numOffice belongs to Company -> you try to access it locally
3. Company has no field named 'length' -> you try to use it in ' theVisit.length'
etc.

I suggest you post all the code you have written so far. I'll bring it into runnable state. Then you can study it and modify it to meet your particular needs.

Avdej
0
 

Accepted Solution

by:
VanJava earned 0 total points
ID: 24511702
Thank you all, but I found my own solution for this problem. Cheers
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jdbc error in jsp application 20 63
configure dependency in POM for new database 3 34
Glassfish admin console not working 1 32
passing enum to a method 3 18
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question