Objects made from objects - how to manipulate?

Hello all - my first post here and new to OOP from procedural
I am trying to construct an object ("Box") from 2 Things. The Things have a colour and can be on or off.
I can construct the Box from a RED Thing and a BLUE Thing, and initialise them as being on, but....
How do I manipulate the Things in the Box once constructed?
I don't know how to address the Thing methods from the Box Class... please could someone help as I am sure this is really simple - I just can't find the answer in any of my books!
public class Thing {
     boolean on;
     String colour;
public Thing(String s) {
     colour = s;
     on = false;
// this main is just to test this class
public static void main (String[] args){
     Thing test = new Thing("TEST");
     test.turnOn();
     test.getState();
     try {
          System.out.println ("Pause.....");
          Thread.sleep(1000);
     catch (Exception e) {
          System.out.println ("CarCrash!");
     test.turnOff();
     test.getState();
public void turnOn() {
     on = true;
public void turnOff() {
     on = false;
public void getState() {
     if (on) {
          System.out.println (colour + ": ON");
     else {
          System.out.println (colour + ": OFF");
and here is the box class...
public class Box {
public Box() {
     Thing thingOne = new Thing ("RED");
     Thing thingTwo = new Thing ("BLUE");
     thingOne.turnOn();
     thingTwo.turnOn();
public void getBoxState() {
     System.out.println (thingOne.getState());
     System.out.println (thingTwo.getState());
public static void main (String[] args) {
     Box boxOne = new Box();
     boxOne.getBoxState(); // does not compile - this is the crux of the matter...
}

NickM999 wrote:
I've simplified Box to get to the root of the problem:
Nice.
public class Box {
    public Box() {
         Thing thingOne = new Thing ("RED");
         Thing thingTwo = new Thing ("BLUE");
         thingOne.turnOn();
         thingTwo.turnOn();
    public static void main (String[] args) {
         Box boxOne = new Box();
         thingOne.getState();   <--- this is the problem...(line 22)
}Box.java:22: cannot find symbol
symbol : variable thingOne
location: class Box
thingOne.getState();
^
1 error
You've got much worse problems than you think.
If you look at your constructor, you create two instances of Thing. The variables that point to them are local to the constructor, so as so as you leave that method those objects are eligible for garbage collection.
I'd like to create a number of Box instances (all with the same Things (Red/Blue) in them), then manipuate the Things within each box
example: Box 1 - Red Thing ON, Blue Thing OFF... Box 2 - Red Thing OFF, Blue Thing OFF... Box 2 - Red Thing ON, Blue Thing Off... and so on.
I also tried to address the methods in Thing as boxOne.thingOne.turnOff();
Is there a better design?
Thanks for your time!Here's what I'd recommend: don't expose those objects in public. Give clients an interface that they can use to get at them in a way that YOU can control internally.
You should follow the Java Beans conventions for Thing:
public class Thing
    boolean on;
    public boolean isOn() { return on; }
    public void setOn(boolean state) { on = state; }
}Since the state is really two flags, not one, I'd create a State object that would hold the variables. Less atomic that way. The more you can encapsulate in objects the better.
public class Box {
    private Thing thingOne;
    private Thing thingTwo;
    public Box() {
         thingOne = new Thing ("RED");
         thingTwo = new Thing ("BLUE");
         thingOne.turnOn();
         thingTwo.turnOn();
    public boolean getThingOne() { return thingOne.getState(); }
    // same thing for thing two.
    public setThingOne(boolean isOn)
        if (isOn)
            thingOne.turnOn();  //  I assume you have these methods.
        else
            thingOne.turnOff();
    public static void main (String[] args) {
         Box boxOne = new Box();
         thingOne.getState();   <--- this is the problem...(line 22)
}

Similar Messages

  • As 2.0 class objects- how to swap depths of a movie clip

    How do you bring an object to the top? if it's just a movie
    clip, I could do a swapdepths, but if it's a movieclip that's part
    of an AS 2.0 object, how do you swap depths of the whole object?
    I create 2 objects (same class) which each have a movieclip
    within them. The movie clip is created on a unique level with
    getNextHighestDepth().
    I have a button which tries to swapDepths of the 2 objects,
    but I can't get it to work. Can anyone help?
    here's the detail:
    1. create a symbol in the library called "someShape_mc" and
    put some shape in it - a circle, a square, whatever - this symbol
    is exported for action script, and has an AS 2.0 Class of
    "ClassObject" ( I also put a dynamic text field in the shape to
    display the current depth - it's called "depth_txt")
    2. create a button called "swap_btn" on the stage.
    Frame 1 has the following actionscript:
    var BottomObject:ClassObject = new ClassObject(this,100,150);
    var topObject:ClassObject = new ClassObject(this,110,160);
    // for the button add this:
    Swap_btn.onRelease=function() {
    // try it with the full path:
    _root.BottomObject.__LocalMovieClip.swapDepths(_root.topObject.__LocalMovieClip);
    // try it with with just the objects:
    BottomObject.__LocalMovieClip.swapDepths(topObject.__LocalMovieClip);
    // try it with the object as a movieclip
    BottomObject.swapDepths(topObject);
    trace("Did it Swap?");
    // try it with a method in the class....
    BottomObject.swapIt(topObject.__LocalMovieClip);
    BottomObject.swapIt(topObject);
    trace("nope... no swapping going on...");
    ================================
    here's the AS file: "ClassObject.as"
    class ClassObject extends MovieClip{
    var __LocalMovieClip;
    var __Depth;
    function ClassObject(passedIn_mc:MovieClip,x:Number,y:Number)
    __Depth = passedIn_mc.getNextHighestDepth();
    __LocalMovieClip =
    passedIn_mc.attachMovie("someShape_mc","__LocalMovieClip",__Depth);
    trace("made a shape at " + __Depth);
    __LocalMovieClip._x = x;
    __LocalMovieClip._y = y;
    __LocalMovieClip.depth_txt.text = __Depth;
    public function swapIt(targetMc) {
    __LocalMovieClip.swapDepths(targetMc);
    __LocalMovieClip.depth_txt.text =
    __LocalMovieClip.getDepth(); // no difference.
    trace("Tried to swap from within the class...");
    ========================
    so- the goal is to bring the "bottom" Class object on top of
    the "top" object. The button tries various methods of swapping the
    depths of the movie clips - but there is not one that works. What
    am I missing?
    tia
    ferd

    Thank you for your response - and here I have included the
    code I reworked to show how it works, and doesn't work. you're
    right about not needing the extra containers, but this example is
    part of a bigger thing...
    I'm confused - it works ONLY if I attach the movie outside
    the class, even though the "attachment" occurs, I'm thinking, at
    the same scope level, that is, _root.holder_mc, in both examples.
    it seems that the advantage of having a class is defeated
    since I have to do the extra coding for each object that will be
    created. It's like the class can only have a reference to the
    movieclip outside itself, and not have a clip INSIDE that is fully
    functioning. am I right about this? Is there someplace good I can
    learn more about class objects and movieclip usage?
    also, my class object IS a movieclip, but " this.getDepth() "
    is meaningless inside the class object. hmmm...
    This one works..... attaching the movies at the root level
    (to a holder_mc)
    // Frame 1
    tmp1 =
    holder_mc.attachMovie("someShape_mc","tmp1",holder_mc.getNextHighestDepth());
    var BottomObject:ClassObject3 = new
    ClassObject3(tmp1,100,150);
    tmp2 =
    holder_mc.attachMovie("someShape_mc","tmp2",holder_mc.getNextHighestDepth());
    var topObject:ClassObject3 = new ClassObject3(tmp2,110,160);
    // for the button add this:
    Swap_btn.onRelease=function() {
    BottomObject.swapIt(topObject);
    trace("clicked button");
    // ClassObject3.as
    class ClassObject3 extends MovieClip{
    var __LocalMovieClip:MovieClip;
    function
    ClassObject3(passedInMovieClip:MovieClip,x:Number,y:Number) {
    trace(" this class object is at ["+this.getDepth()+"]");
    __LocalMovieClip = passedInMovieClip;
    __LocalMovieClip._x = x;
    __LocalMovieClip._y = y;
    public function swapIt(targetMc:MovieClip):Void {
    trace("do the swap in the class");
    trace("===========================");
    trace("target type :" + typeof(targetMc));
    trace("__LocalMovieClip type :" + typeof(__LocalMovieClip));
    __LocalMovieClip.swapDepths(targetMc.__LocalMovieClip);
    This one does NOT work..... attaching the movies within the
    class object...
    // Frame 1
    var BottomObject:ClassObject2 = new
    ClassObject2(holder_mc,100,150);
    var topObject:ClassObject2 = new
    ClassObject2(holder_mc,110,160);
    // for the button add this:
    Swap_btn.onRelease=function() {
    BottomObject.swapIt(topObject);
    trace("clicked button");
    // ClassObject2.as
    class ClassObject2 extends MovieClip{
    var __LocalMovieClip:MovieClip;
    function
    ClassObject2(passedInMovieClip:MovieClip,x:Number,y:Number) {
    __LocalMovieClip =
    passedInMovieClip.attachMovie("someShape_mc","stuff1",passedInMovieClip.getNextHighestDep th());
    __LocalMovieClip._x = x;
    __LocalMovieClip._y = y;
    public function swapIt(targetMc:MovieClip):Void {
    trace("do the swap in the class");
    trace("===========================");
    trace("target type :" + typeof(targetMc));
    trace("__LocalMovieClip type :" + typeof(__LocalMovieClip));
    __LocalMovieClip.swapDepths(targetMc.__LocalMovieClip);

  • With the new version of itunes, how do I transfer ringtones I have made from my phone into my account so I can download them?

    I just downloaded the new version of itunes to my computer. Now I cannot load my ringtones that I have made from garage band on my iphone to my itunes library to save them on my iphone. Anyone know how to do this yet on the new itunes?

    Missing "how" in the first sentence...

  • My mini died. bought one used from a friend... all of my songs and books are on my iPhone, and I cant figure out how to get them onto this new computer. this is frustrating. the things i really want are purchases I've made from itunes store... help

    My mini died. bought one used from a friend... all of my songs and books are on my iPhone, and I cant figure out how to get them onto this new computer. this is frustrating. the things i really want are purchases I've made from itunes store... help
    !!!!!

    Hey kevyg3,
    I was able to find an article that I believe will help you move your iTunes purchases from your iPhone over to your new computer:
    iTunes Store: Transferring purchases from your iOS device or iPod to a computer
    http://support.apple.com/kb/HT1848
    Hope this helps,
    David

  • How can i view a list of previous purchases i have made from the appstore on my iphone that i have now deleted?

    how can i view a list of previous purchases i have made from the appstore on my iphone that i have now deleted?

    Launch iTunes on your computer. Select iTunes Store in the source list on the left then click Sign In right side of the iTunes window just above QUICK LINKS.
    In the Account Information window click See All to the right of Purchase History.

  • I made an album just now. I went back to delete the pics from my camera roll but my only option is to "delete everywhere." I want to leave them in the new album I made..  How i can delete them from camera roll without deleting from other Album i made

    I made an album just now. I went back to delete the pics from my camera roll but my only option is to "delete everywhere." I want to leave them in the new album I made..  How i can delete them from camera roll without deleting from other Album i made

    The way that I understand that it works, is that the photos are not copied into the new albums, instead it just points to the photos - the number of photos on the iPad in Settings > General > About doesn't increase when you create new album so I assume from that that it isn't copying the photo, just pointing to it. So if you delete the photo from the camera roll you will therefore also be deleting all the pointers to it

  • If we want to transfer data to multiple receiver using context object, how

    If we want to transfer data to multiple receiver using context object, how many receiver determinations we need to create?

    Hi Chiru,
    Just go thro' the below links for sending data to multiple receivers:
    /people/venkataramanan.parameswaran/blog/2006/03/17/illustration-of-enhanced-receiver-determination--sp16
    /people/shabarish.vijayakumar/blog/2006/04/03/xi-in-the-role-of-a-ftp
    Conditions in Receiver Determination
    I hope this helps.
    Regards.
    Praveen

  • HT1391 Hi can anyone tell me how to find out what and when my last purchases were made from the App Store ? I think my kids used all my credit

    Hi can anyone tell me how to find out what and when my last purchases were made from the App Store ? I think my kids used all my credit

    .from iTunes, go to View > Show sidebar
    From sidebar > Itunes Store > Click top left on your Apple ID email address > Account > ...scroll down > Purchase History > View All

  • How to tell which device in-app purchases were made from?

    When looking at account purchase history, how can I tell which device purchases were made from for in-app purchases?

    Hey HeardItHere,
    You can view your purchase history by checking iTunes. It will include in-app purchases. You can find out how to do so here:
    iTunes Store & Mac App Store: Seeing your purchase history and order numbers
    http://support.apple.com/kb/HT2727
    Also, you'll receive an invoice by email each time your credit card is charged. An invoice is sent approximately every $20 or 12 hours, whichever occurs first.
    Sincerely,
    Delgadoh

  • After completion of Integration Directory Objects How to monitor my message

    Hi,
       After creation of configuratin objects how to check my XML message flow in runtime work bench.
    Can u please explain in me step-by-step procedure.
    Thanks & regards,
    Pushparaju.B

    Hi,
    You can do this using:
    1. Runtime Workbench
    The adapters can be checked using Component Montoring>Display ALL button>Adapter Engine--> Communication channel
    http://help.sap.com/saphelp_nw04/helpdata/en/25/9c2f3ffed33d67e10000000a114084/content.htm
    Or message monitoring
    http://help.sap.com/saphelp_nw04/helpdata/en/2f/4e313f8815d036e10000000a114084/content.htm
    2. Transaction SXMB_MONI in the ABAP Stack
    Regards
    Vijaya

  • Im switching email accounts on my itunes account am the computer isnt recognizing purchases made from my other account on the new one (apps). How can I sign in with my new account and get the purchases apps on both accounts?

    Im switching email accounts on my itunes account am the computer isnt recognizing purchases made from my other account on the new one (apps). How can I sign in with my new account and get the purchases apps on both accounts?

    This was EXACTLY what I needed about the purchases I made from my device. However, is there a way to re-download other ones you've made from a computer? Because I realized some of them were not just purchased from my device.
    This is a picture of what it looks like now:
    http://tinypic.com/r/107quxu/7
    As you can see, the stuff circled in red doesn't give me an option to download from Cloud Beta because it already says "downloaded".
    any way to get around that?

  • How do I render my edited project made from .VOB files?

    I have DVDs  that were commercially made from my own 8mm family films and I have edited the .VOB files with Adobe Premiere Elements 11. The edited project is on the time line and I have added background music to the silent 8mm films. The entire project is in .VOB files. When I try to render the project, hitting the enter button simply starts the project playing, but Adobe Premiere Elements 11 does not render the project. There is no colored line above the work area. The files are VTS_01_1.VOB.  I can't get Windows Media Player to burn the project to a DVD so I can send copies to my family. How can I burn the project to a DVD that will play for my family in a normal DVD player? Will it be necessary to render the project? If yes, how do I do that? If I purchase Adobe Creative Cloud will it have something that will help me get my project burned to a DVD? Thanks.

    jedokie
    When you are ready to burn to DVD-VIDEO on DVD disc, click on Publish+Share at the top right of the workspace.
    After you click on Publish+Share, click on the Disc choice. After you click on Disc category, click on DVD in the burn dialog
    which has the header "Disc:: Choose location and settings: Then you set the Burn to: field to Folder (4.7 GB) or Folder (8.5 GB).
    Set your save location for the Folder that is going to be produced from Burn to:
    In the folder from the saved Folder, take only the VIDEO_TS Folder from that saved folder into the free ImgBurn program
    to produce your DVD-VIDEO on DVD disc. Please see
    How to burn a DVD Folder with ImgBurn - AfterDawn: Guides
    After reading those instructions, if you have any questions about them, please let me know and I will fine tune any areas that
    need fine tuning.
    Please let us know if this is the specific information that you were seeking.
    I am not clear what you mean by
    When I look at the normal choices I don't find any choice that will burn my project to a DVD that will play in a normal DVD player. Can you tell me which of the choices will do that?
    This is what I would consider the "normal way"
    1. Burn to: = Disc
    Is Premiere Elements 11 showing your DVD burner in the Burner Location: of the burn dialog?
    Does the Status: show Ready when you have you DVD disc in the burner tray?
    Have you select Presets = whichever corresponds to your source -
    NTSC_Dolby DVD
    or
    NTSC_Widescreen_DVD Dolby
    or
    PAL counterparts if you are in a PAL region
    In the Quality Area of the burn dialog, is Space Required 4.38 GB or less and Bitrate 8.00 Mbps.
    If Yes to all of the above, then hit the Burn button. You should be on your way to producing a DVD-VIDEO on DVD disc
    that should be playable on the computer or TV's DVD player.
    Looking forward to your follow up.
    ATR
    Add On...There is another angle to all this which I will add as a just in case measure...you have DVD-VIDEO files on your Timeline, and I have assumed that you want to burn them to DVD-VIDEO on DVD disc. By any chance is your intent to burn them to AVCHD format on DVD disc?

  • I have purchased a new laptop and my purchases made from new laptop does not show on my account here ... how do I fix it?

    I have purchased a new laptop and my purchases made from new laptop does not show on my account here ... how do I fix it? My old laptop is on windows 7 and my new onw is windows 8

    Copy everything from your old computer to your new one

  • HT204088 How can i view purchases made with an itunes gift card?  All purchases were made from my phone.

    How can i view purchases made with an itunes gift card?  All purchases were made from my phone.

    The article from which you came has instructions on how to view your purchase history. Where in those instructions are you encountering difficulty?

  • TS1389 How do I find what purchases were made from my account?

    How do I find what purchases were made from my account? I have charges on my Credit card and did not make any purchases

    See:
    http://support.apple.com/kb/HT2727
    Regards.

Maybe you are looking for

  • FSCM DM -Auto Closing cases - where work is still required and duplications

    Hi, We have linked SAP4.6c to ECC6 to implement FSCM Dispute management. We have a scenario when we have created a Dispute case linked to an open item in 4.6c ( IDOC accross to ECC6 creates a dispute case),  the BIZ can be working on the Dispute case

  • How to get the line type of a table type

    Hi,   i want to indetify the line type of a table type dynamically in my program. how to do? For example: data: lv_ttype type type ROLLNAME value 'PRXCTRLTAB'. how to identify line type of lv_ttype in the program. Thanks, johnney.

  • Can't convert Word 2003 documents to PDF

    I purchased Adobe Acrobat XI Pro and it will not convert any of my Word documents (.doc Word 2003). I get an error message saying "Attempt failed. Please correct the error and try again." there is no description as to what the error is, or how I migh

  • WDS Server Service won't start - 'An error occurred trying to start the Windows Deployment Services Server.'

    I'm having issues installing starting the WDS server service on Win 2008 R2,  this is happening on multiple (all 2008 R2) systems on this domain so I'm beginning to think it's either AD or network related, in event viewer I get the following error. '

  • Opendocument can't open web i document

    dear all, need your help please i am trying to create hyperlink to open other webi report using opendocument ( my report create with java report panel, so i use opendocument.jsp ). my link is -> "http://<server>/businessobjects/enterprise115/desktopl