Results 1 to 18 of 18
  1. #1
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default How to run java class in ECLIPSE(CS106A Stanford)

    Hi there,
    I have no clue how to run java class in Eclipse.This class is from the book " Art and science of JAVA" by Eric Robert.

    Here is the code
    import java.awt.*;
    /**
    * This class represents a simple implementation of a stoplight.
    * The client can determine the current state of the stoplight by
    * calling stoplight.getState() and change it to the next color
    * in the sequence (GREEN -> YELLOW -> RED -> GREEN) by calling
    * stoplight.advance().
    */
    public class Stoplight {
    /** Constant indicating the color GREEN */
    public static final Color GREEN = Color.GREEN;
    /** Constant indicating the color YELLOW */
    public static final Color YELLOW = Color.YELLOW;
    /** Constant indicating the color RED */
    public static final Color RED = Color.RED;
    /**
    * Creates a new Stoplight object, which is initially GREEN.
    */
    public Stoplight() {
    state = 0;
    }
    /**
    * Returns the current state of the stoplight.
    * @return The state of the stoplight (GREEN, YELLOW, or RED)
    */
    public Color getState() {
    switch (state) {
    case 0: return GREEN;
    case 1: return YELLOW;
    case 2: return RED;
    default: return null; /* Can't occur but required by Java */
    }
    }
    /**
    * Advances the stoplight to the next state.
    */
    public void advance() {
    state = (state + 1) % 3;
    }
    /* Private instance variable */
    private int state;
    }

  2. #2
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    When run it on eclipse, it does not work.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Please define "Does not work" for us. And how do you run this as I don't see a main.

    Also, you've posted over 50 times here and so by now you should know not to use quote tags where code tags belong. Please edit your first post to change the quote tags to code tags so we can all read your code and understand it. Thanks.

  4. #4
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Your question makes no sense whatsover you want to run a class with no implementation of said class, like fubarable said, you got no main, all java progs run from main, surely you know this after 50+ posts

  5. #5
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by ccie007 View Post
    Hi there,
    I have no clue how to run java class in Eclipse.This class is from the book " Art and science of JAVA" by Eric Robert.

    Here is the code

    i just opened the pdf-version of the book you mentioned, the version i used is a preliminary draft and i read the implementation of the Stoplight, there is a sentence like

    The simplest example consists of creating a single stoplight, which you can do by invoking new Stoplight, as follows:

    Stoplight myStoplight = new Stoplight();

    so, your problem is not a eclipse issue. even if you compile it with javac Stoplight.java and start it with java Stoplight nothing will happen, because like the previous posters already mentionned, there is no main method in your class. so, add the main method to your class and add the code above to the main method. i've tried this with your code, but the advance-method produced a wrong result. here is my code

    Java Code:
    	public static void main(String[] args) {
    		Stoplight stoplight = new Stoplight();
    		System.out.println((Color)stoplight.getState());
    		stoplight.advance();
    		System.out.println((Color)stoplight.getState());
    	}

    and the output:

    java.awt.Color[r=0,g=255,b=0]
    java.awt.Color[r=255,g=255,b=0]

    i don't think the previous color should stay active when you advance.
    Last edited by j2me64; 09-05-2010 at 10:52 AM.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,431
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by j2me64 View Post
    and the output:

    Java Code:
    java.awt.Color[r=0,g=255,b=0]
    java.awt.Color[r=255,g=255,b=0]

    i don't think the previous color should stay active when you advance.
    The traffic light goes from state #0 (green) to state #1 (yellow). I don't see any problem so far ...

    kind regards,

    Jos

  7. #7
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    I added the main code and another code as I have been told by the poster.But still not working.

    Java Code:
    import java.awt.*;
    
    import acm.graphics.*;
    
    import acm.program.*;
    /**
    * This class represents a simple implementation of a stoplight.
    * The client can determine the current state of the stoplight by
    * calling stoplight.getState() and change it to the next color
    * in the sequence (GREEN -> YELLOW -> RED -> GREEN) by calling
    * stoplight.advance().
    */
    public class stoplight extends GraphicsProgram{
    /** Constant indicating the color GREEN */
    public static final Color GREEN = Color.GREEN;
    /** Constant indicating the color YELLOW */
    public static final Color YELLOW = Color.YELLOW;
    /** Constant indicating the color RED */
    public static final Color RED = Color.RED;
    /**
    * Creates a new Stoplight object, which is initially GREEN.
    * 
    * 
    */
    
    public stoplight() {
    state = GREEN;
    }
    /**
    * Returns the current state of the stoplight.
    * @return The state of the stoplight (GREEN, YELLOW, or RED)
    */
    public Color getState() {
    return state;
    }
    
    /*This is the main method I have added*/
    
    public static void main(String[] args) {
    	Stoplight stoplight = new Stoplight();
    	System.out.println((Color)stoplight.getState());
    	stoplight.advance();
    	System.out.println((Color)stoplight.getState());
    	Stoplight myStoplight = new Stoplight(); 
    }
    /**
    * Advances the stoplight to the next state.
    */
    public void advance() {
    if (state == RED) {
    state = GREEN;
    } else if (state == YELLOW) {
    state = RED;
    } else if (state == GREEN) {
    state = YELLOW;
    }
    }
    /* Private instance variable */
    private Color state;
    
    }

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ccie007 View Post
    I added the main code and another code as I have been told by the poster.But still not working.
    Can you elaborate for us? What exactly do you mean by "not working"?

    Best of luck

  9. #9
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by ccie007 View Post
    I added the main code and another code as I have been told by the poster.But still not working.

    do you get error(s)?

  10. #10
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    j2me64 is my above new code,ok?

  11. #11
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by ccie007 View Post
    j2me64 is my above new code,ok?

    yes, as long as your imports of acm are founded in the classpath.

    @josAH: i was just thinking in terms of enum and not in awt.awt.Color. so red+green = yellow. no problems.

  12. #12
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    yes, as long as your imports of acm are founded in the classpath.
    I do not get this bit.

    There are red underlines below stoplight words in the main method.I have posted the pic of my eclipse screen to explain.Plz check it out.class | Flickr - Photo Sharing!

    may need to zoom in to see the red underlin errors.

    Thank you.

  13. #13
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by ccie007 View Post
    I do not get this bit.

    There are red underlines below stoplight words in the main method.I have posted the pic of my eclipse screen to explain.Plz check it out.class | Flickr - Photo Sharing!

    may need to zoom in to see the red underlin errors.

    Thank you.

    java is case sensitive and if you declare your class with "public class stoplight " then your class is named stoplight. To correct the bug right click the stoplight.java in your eclipse project and then call the refactor to rename your class Stoplight.

  14. #14
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    Now its working thank you guys.

  15. #15
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    I am thinking of reading another java book once I finish this book before I start CS106B(C++) Stanford university course.How does that sound?I think its a good idea isn't by making my programming foundation strong.

  16. #16
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    Java Code:
    java.awt.Color[r=0,g=255,b=0]
    java.awt.Color[r=255,g=255,b=0]
    By the way is there any way we can display the actual color i.e green, yellow,red rather than RGB value.

  17. #17
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    Am I right on saying main method part is client and rest is implementation?

  18. #18
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Quote Originally Posted by ccie007 View Post
    Am I right on saying main method part is client and rest is implementation?
    Client?? you need to expand further.

Similar Threads

  1. Java Checkerboad question CS106A Stanford univerity
    By ccie007 in forum New To Java
    Replies: 71
    Last Post: 09-20-2013, 04:02 AM
  2. CS106A Stanford University
    By Learning Java in forum New To Java
    Replies: 116
    Last Post: 07-09-2011, 04:43 PM
  3. .class fille needed from eclipse
    By perplexingtrax in forum New To Java
    Replies: 4
    Last Post: 03-27-2008, 05:36 PM
  4. Renaming a class in Eclipse
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-04-2007, 10:54 AM
  5. Eclipse adding $1.class files
    By eboraks in forum Eclipse
    Replies: 1
    Last Post: 11-28-2007, 04:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •