12 Replies - 966 Views - Last Post: 06 April 2016 - 10:32 AM Rate Topic: -----

#1 Balthazar   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 164
  • Joined: 16-November 15

Creating an object that constructs an array of objects?

Posted 06 April 2016 - 07:07 AM

I have tried looking this up but all I find is the basic "Creating an array of objects".

A bit of background before I ask the question. This is for my Java final. My school doesn't seem to focus much on objects and how they interlink (they mention it and we use them in lessons, but usually at most it's a single object and if there's more than one for the lesson it's usually already written and we're not writing it).

My final is to write a GUI booking system for a sheriff's office. So I have multiple interlinked objects. I am trying to learn this as I go (which is always what you want to do while working on your *final project*). In the prison there are 4 cellblocks, each cellblock has a set number of cells (200 for minimum, 50 for maximum) in those cell blocks each cell has 2 bunks and a prisoner is assigned to a bunk. The other two cellblocks are Isolation and Infirmary each of those hold 10 cells with 1 bunk each).

So my thought process is this(using just the "minimum security" for my example). I would create a Prison object that will have a constructor that will create 4 cellblock objects (one of which being called "minimum") and passing it the value of the number of cells (200). The cellblock object would have a constructor that creates an array of Cells with the value passed to it. The cell object would create two bunk objects (a topBunk and a botBunk) and the bunk object will hold a prisoner.... whew... ok so the path would, in theory, look like:

prison().getMinimum().getCell(154).getbunk(topBunk).getPrisoner().getFirstName();

However. This doesn't seem to work. I have had zero experience in programming objects that construct arrays of other objects, let alone trying to access the data within them.

Any thoughts? Or am I making this WAY to complicated?

Is This A Good Question/Topic? 0
  • +

Replies To: Creating an object that constructs an array of objects?

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15268
  • View blog
  • Posts: 61,212
  • Joined: 12-June 08

Re: Creating an object that constructs an array of objects?

Posted 06 April 2016 - 07:20 AM

I am not clear on why you would reference a prisoner by the bunk level. Wouldn't they just occupy a cell?

Dial it back then. Start with working on the prison object and block objects. Can you make those two work together?
Was This Post Helpful? 0
  • +
  • -

#3 Balthazar   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 164
  • Joined: 16-November 15

Re: Creating an object that constructs an array of objects?

Posted 06 April 2016 - 07:59 AM

View Postmodi123_1, on 06 April 2016 - 02:20 PM, said:

I am not clear on why you would reference a prisoner by the bunk level. Wouldn't they just occupy a cell?

Ya, originally I thought this too, however in our discussions our instructor said that we needed to assign a prisoner to a bunk.

View Postmodi123_1, on 06 April 2016 - 02:20 PM, said:

Dial it back then. Start with working on the prison object and block objects. Can you make those two work together?

I believe so. When I create my Prison object and try to use it I can do prison().getMinimum() and when I try to go further I do see the methods within the Minimum (Cellblock) object, but that's where it kind of falls apart. I have a method called "getCell" that returns a Cell[element], however nothing I do seems to work. I have included a rough prototype of my classes below. I'm hoping that I just made a simple mistake.

Prison Class:
public class Prison {
    private Cellblock minimum;
    
    public Prison(){
        minimum = new Cellblock(200);
    }
}



Cellblock Class:
public class Cellblock {

    private Cell[] cell;
    
 public Cellblock(int num){
        cell = new Cell[num];
    }

  public Cell[] getCell(int num) { 
        return cell[num];
    }

}



Cell Class:
public class Cell {
    private Bunk topBunk;
    private Bunk botBunk;
getters and setters
}



Bunk Class:
public class Bunk {
    private Prisoner prisoner;
    
    prisoner attributes
    getters and setters        
 }



So that's the idea I have

This post has been edited by Balthazar: 06 April 2016 - 08:00 AM

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15268
  • View blog
  • Posts: 61,212
  • Joined: 12-June 08

Re: Creating an object that constructs an array of objects?

Posted 06 April 2016 - 08:09 AM

What you have doesn't jive.

A prison has one to many prison blocks.
Each prison block has one to many cells.

 class Prison
    {
        public Block[] _oBlock; // block array
        public Prison()// constructor
        {
            _oBlock = new Block[2];// define

            for (int i = 0; i < _oBlock.Length; i++) //instantiate
            {
                _oBlock[i] = new Block();
            }
        }
    }
    class Block
    {
        public Cell[] _oCells; // cell array
        public Block() // constructor
        {
            _oCells = new Cell[5];// define

            for (int i = 0; i < _oCells.Length; i++) //instantiate
            {
                _oCells[i] = new Cell();
            }
        }
    }

    class Cell
    { 
        public Cell()// constructor.
        {
     
        }
    }

Was This Post Helpful? 1
  • +
  • -

#5 Balthazar   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 164
  • Joined: 16-November 15

Re: Creating an object that constructs an array of objects?

Posted 06 April 2016 - 08:10 AM

Actually I noticed an error. For the "getCell" method NetBeans said the return value is an array instead of an object (I used the auto getter/setter). So I took the [] out of the return and it seems to work. I am now able to see all of the classes. However I am now getting a InvocationTargetException when I try to access it. I'm *thinking* it's because not all of the variables are set in my prisoner file with my test. So here's my test code:

Prison prison = new Prison();
        Prisoner z = new Prisoner();
        z.setfName("Lord");
        z.setlName("Balthazar");
        z.setAge(36);
        
        prison.getMinimum().getCell(150).getBotBunk().setPrisoner(z);
        
        System.out.println("Prisoner in cell is " + prison.getMinimum().getCell(150).getBotBunk().getPrisoner().getfName());



both of the calls cause the exception:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$156(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at sheriff.Sheriff.start(Sheriff.java:56)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
... 1 more
Exception running application sheriff.Sheriff
Java Result: 1
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15268
  • View blog
  • Posts: 61,212
  • Joined: 12-June 08

Re: Creating an object that constructs an array of objects?

Posted 06 April 2016 - 08:26 AM

Is each array actually initialized, or just declared?
Was This Post Helpful? 1
  • +
  • -

#7 Balthazar   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 164
  • Joined: 16-November 15

Re: Creating an object that constructs an array of objects?

Posted 06 April 2016 - 09:12 AM

View Postmodi123_1, on 06 April 2016 - 03:26 PM, said:

Is each array actually initialized, or just declared?


So far, there is only one array. My Prison object creates 4 cellblock objects, and each of those cellblock objects creates an array of cells. I did add the below to my cellblock constructor so when it creates the array it goes ahead and initializes all of the elements with Cell objects. But I am still getting the exception.

    public Cellblock(int num){
        cell = new Cell[num];
        
        for(int count = 0; count < cell.length; count++){
            cell[count] = new Cell();
        }
    }


This post has been edited by Balthazar: 06 April 2016 - 09:13 AM

Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15268
  • View blog
  • Posts: 61,212
  • Joined: 12-June 08

Re: Creating an object that constructs an array of objects?

Posted 06 April 2016 - 09:23 AM

Okay then work through it. Stop trying to bite off the whole enchilada and work in increments.

Is this valid?
prison.getMinimum()


If so, is this valid?
prison.getMinimum().getCell(150)


If so, is this valid?
prison.getMinimum().getCell(150).getBotBunk()


If so then 'SET' what ever value you want here.
prison.getMinimum().getCell(150).getBotBunk().setPrisoner(z)

Was This Post Helpful? 1
  • +
  • -

#9 Balthazar   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 164
  • Joined: 16-November 15

Re: Creating an object that constructs an array of objects?

Posted 06 April 2016 - 10:03 AM

View Postmodi123_1, on 06 April 2016 - 04:23 PM, said:

Okay then work through it. Stop trying to bite off the whole enchilada and work in increments.

Is this valid?
prison.getMinimum()


If so, is this valid?
prison.getMinimum().getCell(150)


If so, is this valid?
prison.getMinimum().getCell(150).getBotBunk()


If so then 'SET' what ever value you want here.
prison.getMinimum().getCell(150).getBotBunk().setPrisoner(z)


I thought that's what I was doing. I am using NetBeans, so when I type prison. it will pop up all of the functions associated with that so then I add the getMinimum(). and choose from that list. Now I know these are only a list of the function headers but basically all these functions do is return the associated object. The only one with any real complexity is getCell(150) because it has to return the element from an array. but I have that set to return cell[num] where num is the number sent during the call. So I either don't have something I need, or I have something set wrong... here's a break down of what my program is doing at each function:

prison.getMinimum().
public class Prison {
    private Cellblock minimum;
    
    public Prison(){
        minimum = new Cellblock(200);
     
    }
public Cellblock getMinimum() {
        return minimum;
    }




getcell(150.)
public class Cellblock {

    private Cell[] cell;

    public Cellblock() {

    }

    public Cellblock(int num) {
        cell = new Cell[num];

        for (int count = 0; count < cell.length; count++) {
            cell[count] = new Cell();
        }
    }

    public Cell getCell(int num) {
        return cell[num];
    }

}


getBotBunk().
public class Cell {

    private Bunk topBunk;
    private Bunk botBunk;

    public Cell() {

    }

    public Bunk getBotBunk() {
        return botBunk;
    }
//**rest of functions ommited**



setPrisoner(z);
public class Bunk {

    private Prisoner prisoner;

    public Bunk() {

    }

    public Prisoner getPrisoner() {
        return prisoner;
    }

    public void setPrisoner(Prisoner prisoner) {
        this.prisoner = prisoner;
    }

}





So that's the path it's taking. I'm hoping if I can get it figured out for this example, hopefully the rest will come easier.

After I "set" prisoner z to the bottom bunk of cell 150 I am trying to output just their first name for testing purposes. This is where I get the exception.

BTW: Thank you for all the help so far.
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15268
  • View blog
  • Posts: 61,212
  • Joined: 12-June 08

Re: Creating an object that constructs an array of objects?

Posted 06 April 2016 - 10:08 AM

Again.. I am asking if those are valid objects down that list. If you need to put a break point at the top and step through each one. Is one a null object as it it hasn't been instantiated? Etc.
Was This Post Helpful? 1
  • +
  • -

#11 Balthazar   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 164
  • Joined: 16-November 15

Re: Creating an object that constructs an array of objects?

Posted 06 April 2016 - 10:13 AM

AHHH! I hadn't created the Bunk objects in the Cell constructor!!! whew... See, if I get the right person to ask the right questions, I can then explain things in a way where I find the problems :P/>

I'm sure I'm not even close to having this figured out, but my program does run and it does output the first name of the prisoner associated with that bunk. So I feel like I'm on the right track.

This post has been edited by Balthazar: 06 April 2016 - 10:14 AM

Was This Post Helpful? 0
  • +
  • -

#12 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15268
  • View blog
  • Posts: 61,212
  • Joined: 12-June 08

Re: Creating an object that constructs an array of objects?

Posted 06 April 2016 - 10:21 AM

Indeed. Again - don't eat the whole enchilada at once. You'll get there in due time, but break up complex parts into manageable parts to debug. Debug, debug, DEBUG! You may assume an object is instantiated when it wasn't so having ordered steps through is a good thing.
Was This Post Helpful? 1
  • +
  • -

#13 Balthazar   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 164
  • Joined: 16-November 15

Re: Creating an object that constructs an array of objects?

Posted 06 April 2016 - 10:32 AM

Ya... Also having someone else (you) look at it with fresh eyes can help. I was looking at all of that and seeing the variable declaration and completely overlooking the fact that those variables were just that... variables. I do appreciate you leading my down the path and letting me find it for myself too rather than just pointing out my mistake. I'm sure I'll have more questions soon once I start to implement this into the GUI.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1