Welcome to Dream.In.Code
Become a Java Expert!

Join 150,183 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,129 people online right now. Registration is fast and FREE... Join Now!




passign Class pointers to another class

 
Reply to this topicStart new topic

passign Class pointers to another class

BetaWar
25 Aug, 2008 - 02:58 PM
Post #1

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 2,304



Thanked: 101 times
Dream Kudos: 1275
My Contributions
So, this is for school, I don't know when it is due or any of that, just that I want it to work a certain way and it isn't.

Here is my code:

SwitchRunner.java:
CODE
public class SwitchRunner
{
    public static void main(String[] args)
    {
        SwitchX mySwitch = new SwitchX(false);
        SwitchX mySwitch2 = new SwitchX(false);
        System.out.print(mySwitch.getState());
        System.out.print("\n");
        mySwitch.setState(true);
        System.out.print(mySwitch.getState());
        System.out.print("\n");
        mySwitch.toggle();
        System.out.print(mySwitch.getState());
        System.out.print("\n");
        mySwitch.toggle();
        System.out.print(mySwitch.getState());
        System.out.print("\n");

        Bulb myBulb = new Bulb(mySwitch, mySwitch2);
        System.out.println(myBulb.getState());
    }
}


SwitchX.java:
CODE
public class SwitchX
{
    private boolean isOn;
    public SwitchX(boolean startOn)
    {
        isOn = startOn;
    }
    public void toggle(){
        if(isOn){
            isOn = false;
        }
        else{
            isOn = true;
        }
    }
    public boolean getState(){
        return isOn;
    }
    public void setState(boolean state){
        isOn = state;
    }
    public String toString(){
        return "The switch is on = "+isOn;
    }
}


Bulb.java:
CODE
public class Bulb
{
    private boolean isOn;
    public Bulb(switch1, switch2)
    {
        if((switch1.getState() == false && switch2.getState() == true) || (switch1.getState() == true && switch2.getState() == false)){
                isOn = true;
        }
        else{
                isOn = false;
        }
    }
    public boolean getState(){
        return isOn;
    }
}


The problem I am having is sending the pointers to the SwitchX class to Bulb. It doesn't like the attempt, and when I place SwitchX in front of switch1 and switch2 it doesn't like it (this is the type of thing you do in C++). So I am nondering how you get things to work by passing in the pointers to an object? Or is this even possible in Java?

Additionally I am wondering if there is any way to add multiple classes into a single file without getting compilation errors?

This post has been edited by BetaWar: 25 Aug, 2008 - 03:21 PM
User is offlineProfile CardPM
+Quote Post

KYA
RE: Passign Class Pointers To Another Class
25 Aug, 2008 - 03:08 PM
Post #2

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,910



Thanked: 159 times
Dream Kudos: 1375
My Contributions
Java does not have explicit pointers like C/C++. It gives them up for a garbage collector.
User is offlineProfile CardPM
+Quote Post

BetaWar
RE: Passign Class Pointers To Another Class
25 Aug, 2008 - 03:20 PM
Post #3

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 2,304



Thanked: 101 times
Dream Kudos: 1275
My Contributions
So is there any way to do implicit pointers?
User is offlineProfile CardPM
+Quote Post

KYA
RE: Passign Class Pointers To Another Class
25 Aug, 2008 - 03:26 PM
Post #4

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,910



Thanked: 159 times
Dream Kudos: 1375
My Contributions
Uh....(its been a looong time since I've worked with java).

If i recall correctly, java classes have this

Which is essentially a pointer to the class itself.
User is offlineProfile CardPM
+Quote Post

pbl
RE: Passign Class Pointers To Another Class
25 Aug, 2008 - 05:08 PM
Post #5

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(KYA @ 25 Aug, 2008 - 04:08 PM) *

Java does not have explicit pointers like C/C++. It gives them up for a garbage collector.

Let me rephrase that correctly

Java only have explicit pointers but as you cannot perform arithmetic operations on them, we just don't call them pointer. And you won't be welcome in any discussion talking about pointers in Java, this is a forbiden word but:

SwitchX x = new Switch(true);

just creates an area in memory wher the instance of the object Switch is located and have "x" to POINT to it (oups !!! I said the forbidden word).

If you have an array of Switch and

Switch x = switches[0];

you cannot in Java write x++ to have x to point (the forbidden word again) to switches[1]




User is offlineProfile CardPM
+Quote Post

pbl
RE: Passign Class Pointers To Another Class
25 Aug, 2008 - 05:19 PM
Post #6

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
The problem you have (nothing to do with pointers) is that you did not specify what type are the parameters to the constructor of Bulb

CODE

public class Bulb
{
    private boolean isOn;
    public Bulb(switch1, switch2)      // <-------- here
    {


This will work... and by the way, the println() method adds the \n for you
Remove some boolean obvious useless operations

CODE

public class SwitchRunner
{
    SwitchRunner() {
        SwitchX mySwitch = new SwitchX(false);
        SwitchX mySwitch2 = new SwitchX(false);
        System.out.println(mySwitch.getState());

        mySwitch.setState(true);
        System.out.println(mySwitch.getState());

        mySwitch.toggle();
        System.out.println(mySwitch.getState());

        mySwitch.toggle();
        System.out.println(mySwitch.getState());

        Bulb myBulb = new Bulb(mySwitch, mySwitch2);
        System.out.println(myBulb.getState());

    }
    public static void main(String[] args)
    {
        new SwitchRunner();
    }

    class SwitchX
    {
        private boolean isOn;
        public SwitchX(boolean startOn)
        {
            isOn = startOn;
        }
        public void toggle(){
            isOn = !isOn;
        }
        public boolean getState(){
            return isOn;
        }
        public void setState(boolean state){
            isOn = state;
        }
        public String toString(){
            return "The switch is on = "+isOn;
        }
    }

    class Bulb
    {
        private boolean isOn;
        public Bulb(SwitchX switch1, SwitchX switch2)
        {
            isOn = switch1.getState() != switch2.getState());
        }
        public boolean getState(){
            return isOn;
        }
    }

}


This post has been edited by pbl: 25 Aug, 2008 - 05:28 PM
User is offlineProfile CardPM
+Quote Post

BetaWar
RE: Passign Class Pointers To Another Class
25 Aug, 2008 - 05:47 PM
Post #7

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 2,304



Thanked: 101 times
Dream Kudos: 1275
My Contributions
Okay, thanks that helped out a lot smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 03:56AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month