Results 21 to 28 of 28
- 08-09-2011, 08:37 AM #21
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 08-09-2011, 08:50 AM #22
Member
- Join Date
- Aug 2011
- Location
- West Virginia
- Posts
- 38
- Rep Power
- 0
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?
- 08-09-2011, 08:52 AM #23
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 08-09-2011, 09:01 AM #24
Member
- Join Date
- Aug 2011
- Location
- West Virginia
- Posts
- 38
- Rep Power
- 0
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 09:12 AM.
- 08-09-2011, 09:10 AM #25
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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
- 08-09-2011, 09:21 AM #26
Member
- Join Date
- Aug 2011
- Location
- West Virginia
- Posts
- 38
- Rep Power
- 0
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 09:26 AM.
- 08-09-2011, 09:32 AM #27
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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)
orJava Code:Scanner in = new Scanner(System.in); String line = in.nextLine();
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.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); } }
Your code will do something extremely similar to Card.
- 08-09-2011, 09:50 AM #28
Member
- Join Date
- Aug 2011
- Location
- West Virginia
- Posts
- 38
- Rep Power
- 0
Victory! I finally got it to compile and output correctly. Here was the final code:
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 :)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); } }
Similar Threads
-
How to inherit "Object" class in Java
By Ipsita in forum Advanced JavaReplies: 5Last Post: 04-08-2011, 12:37 PM -
Java, Military Format using "/" and "%" Operator!!
By sk8rsam77 in forum New To JavaReplies: 11Last Post: 02-26-2010, 03:03 AM -
How to use "Class convexHull.GrahamScan" in Java
By Mazharul in forum Java 2DReplies: 1Last Post: 04-18-2009, 05:14 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


14Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks