Page 2 of 2 FirstFirst 12
Results 21 to 28 of 28
Like Tree14Likes

Thread: Help with Java coding assignment "the fan class"

  1. #21
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Yes, if you know certain values will be appended to it no matter what the value of on is, you should append them prior. Whether the fan is on or off, you are still gonna be returning the color, etc. in the String returned.
    broo7198 likes this.

  2. #22
    broo7198 is offline Member
    Join Date
    Aug 2011
    Location
    West Virginia
    Posts
    38
    Rep Power
    0

    Default

    so to get my printouts do I just need to create another method in the Fan class and completely get rid of the FanTest.java?

  3. #23
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    No, you can create the fan class objects in any other class, then simply call methods on them. Try printing the fan objects and see what happens.
    broo7198 likes this.

  4. #24
    broo7198 is offline Member
    Join Date
    Aug 2011
    Location
    West Virginia
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    No, you can create the fan class objects in any other class, then simply call methods on them. Try printing the fan objects and see what happens.
    I'm struggling to figure out where in the code to print the objects. Am I using the correct syntax?

    Java Code:
    System.out.println(new Fan(true, 10, "Yellow", Fan.Fast));
    
    		System.out.println(new Fan(false,5, "Blue", Fan.SLOW));

    I am putting that under the main method .. here is the entire code:

    Java Code:
    public class Fan  {
    		//Variables
    		private int speed;
    		private boolean on;
    		private double radius;
    		private String color;
    		// static variable speeds
    		public static final int SLOW = 1;
    		public static final int MEDIUM = 2;
    		public static final int FAST = 3;
    
    			// toString method
    			public String toString(){
    
    
    
    				StringBuilder builder = new StringBuilder();
    
    					// append constant variables
    					builder.append(" Color: ");
    					builder.append(color);
    					builder.append(" Fan Speed: ");
    					builder.append(speed);
    
    
    				// determine if fan is on
    				if(on){
    					builder.append(" Radius: ");
    					builder.append(radius);
    				}
    				else {
    					builder.append(" The Fan is OFF!!");
    					}
    
    				return builder.toString();
    
    			}
    
    			//Fan object
    			public Fan() {
    				//initial parameters
    				on = false;
    				speed = SLOW;
    				radius = 5;
    				color = "Yellow";
    			}
    
    
    					// accessors and mutators
    					void setspeed(int s) {
    						speed = s;
    					}
    
    					void seton(boolean open) {
    						on = open;
    					}
    
    					void setradius(double r) {
    						radius = r;
    					}
    
    					void setcolor(String c) {
    						color = c;
    					}
    
    					int getspeed() {
    						return speed;
    					}
    
    					boolean ison() {
    						return on;
    					}
    
    					double getradius() {
    						return radius;
    					}
    
    					String getcolor() {
    						return color;
    				}
    				public static void main(String[] aargs) {
    
    					System.out.println(new Fan(true, 10, "Yellow", 3));
    
    					System.out.println(new Fan(false,5, "Blue", 1));
    				}
    }
    Last edited by broo7198; 08-09-2011 at 10:12 AM.

  5. #25
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Close, but you haven't created a three item constructor(nor should you for this assignment).

    Pseud-code:
    Java Code:
    declare and initialize fan1
    set to proper values
    print fan 1 to string
    
    declare and initialize fan 2
    set to proper values
    print fan 2 to string
    broo7198 likes this.

  6. #26
    broo7198 is offline Member
    Join Date
    Aug 2011
    Location
    West Virginia
    Posts
    38
    Rep Power
    0

    Default

    I get a bunch of "non-static variable cannot be referenced from a static context" , but this is where I am at now:

    I'm also gettin errors with the setSpeed method and Fan.FAST/SLOW "cannot find symbol"

    Java Code:
    				public static void main(String[] aargs) {
    
    					Fan fan1 = new Fan();
    					on = true;
    					fan1.setSpeed(Fan.FAST);
    					radius = 10;
    					color = "Yellow";
    
    						System.out.println(fan1.toString());
    
    					Fan fan2 = new Fan();
    					on = false;
    					fan2.setSpeed(Fan.SLOW);
    					radius = 5;
    					color = "Blue";
    
    						System.out.println(fan2.toString());
    Last edited by broo7198; 08-09-2011 at 10:26 AM.

  7. #27
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Now that we are working outside of the class you can't access the variables directly. You have to access them as part of an objet(fan1, or fan2). This is where you use the setters.

    Take this as an example(it works very similarly)
    Java Code:
    Scanner in = new Scanner(System.in);
    String line = in.nextLine();
    or

    Java Code:
    public class Card{
      private String suit;
      private int rank;
      public void setRank(int rank){ this.rank = rank; }
      public void setSuit(String suit){ this.suit = suit; }
      @Override public String toString(){
        return rank + " of " + suit;
      }
      public static void main(String[] args){
        Card c = new Card();
        c.setRank(10);
        c.setSuit("Jack");
        System.out.println(c.toString()); //the toString can(and should, for the most part) be omitted
        System.out.println(c);
      }
    }
    This snippet is a fully functional class(which you can run if you'd like), it is not a necessity that the main method be in the same class however.

    Your code will do something extremely similar to Card.
    broo7198 likes this.

  8. #28
    broo7198 is offline Member
    Join Date
    Aug 2011
    Location
    West Virginia
    Posts
    38
    Rep Power
    0

    Default

    Victory! I finally got it to compile and output correctly. Here was the final code:

    Java Code:
     public class Fan  {
    		//Variables
    		private int speed;
    		private boolean on;
    		private double radius;
    		private String color;
    		// static variable speeds
    		public static final int SLOW = 1;
    		public static final int MEDIUM = 2;
    		public static final int FAST = 3;
    
    			// toString method
    			public String toString(){
    
    
    
    				StringBuilder builder = new StringBuilder();
    
    					// append constant variables
    					builder.append(" Color: ");
    					builder.append(color);
    					builder.append(" Fan Speed: ");
    					builder.append(speed);
    
    
    				// determine if fan is on
    				if(on){
    					builder.append(" Radius: ");
    					builder.append(radius);
    				}
    				else {
    					builder.append(" The Fan is OFF!!");
    					}
    
    				return builder.toString();
    
    			}
    
    			//Fan object constructor
    			public Fan() {
    				//initial parameters
    				on = false;
    				speed = SLOW;
    				radius = 5;
    				color = "Yellow";
    			}
    
    
    					// accessors and mutators
    					void setspeed(int s) {
    						speed = s;
    					}
    
    					void seton(boolean open) {
    						on = open;
    					}
    
    					void setradius(double r) {
    						radius = r;
    					}
    
    					void setcolor(String c) {
    						color = c;
    					}
    
    					int getspeed() {
    						return speed;
    					}
    
    					boolean ison() {
    						return on;
    					}
    
    					double getradius() {
    						return radius;
    					}
    
    					String getcolor() {
    						return color;
    				}
    
    
    				// Main Method
    				public static void main(String[] aargs) {
    					// Create fan1 object
    					Fan fan1 = new Fan();
    					fan1.seton(true);
    					fan1.setspeed(Fan.FAST);
    					fan1.setradius(10);
    					fan1.setcolor("Yellow");
    						// output object
    						System.out.println(fan1);
    					//create fan2 object
    					Fan fan2 = new Fan();
    					fan2.seton(false);
    					fan2.setspeed(Fan.MEDIUM);
    					fan2.setradius(5);
    					fan2.setcolor("Blue");
    						//output object
    						System.out.println(fan2);
    
    
    				}
    }
    thanks so much for all of your help. I've really learned a lot with this assignment and your assistance. I expect you'll see me here again soon :)

Page 2 of 2 FirstFirst 12

Similar Threads

  1. How to inherit "Object" class in Java
    By Ipsita in forum Advanced Java
    Replies: 5
    Last Post: 04-08-2011, 01:37 PM
  2. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 04:03 AM
  3. Replies: 1
    Last Post: 04-18-2009, 06:14 AM
  4. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

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