Results 1 to 4 of 4
Thread: Creating a "Battery Charger"
- 03-06-2011, 11:11 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
Creating a "Battery Charger"
Hey everyone! First post here at java forums! As im sure you have all been as frustrated as I am right now, lets get into it.
What I am doing is I have a "battery" class and a "batterycharger" class. The idea is to be able to put these battery objects into the batterycharger. My question is, is the way i use the method "putBattery" correct? And how would I create the method "removeBattery" to hold the reference of what is it battery charger. Oh and the battery charger can only hold one battery at a time.
Appreciate the help!Java Code:public class BatteryCharger { public Battery bat, removeBattery; public boolean ok, putBattery; public boolean occupied; public static int hours; public int numberOfHours; public BatteryCharger(){ int inUse; occupied = false; if(occupied = true){ bat.currentLevel = bat.currentLevel + 50; inUse = 1;} else{ inUse = 0; bat = null; } } public static void passTime(int numberOfHours){ String input = JOptionPane.showInputDialog(null, "How many hours should pass?").trim(); numberOfHours = Integer.parseInt(input); numberOfHours = hours; } public Battery removeBattery(){ boolean ok; if(bat == ){ } } public boolean putBattery(Battery theBat){ boolean ok; if(bat == null){ bat = theBat; ok = true; } else{ ok = false;} return ok; } public String getDescription(){ System.out.println("This batter charger"); } public void charge(){ if(occupied = false){ ;} else{ if(occupied = true){ numberOfHours = numberOfHours + 1; } } } }
-Hyper
- 03-06-2011, 11:28 PM #2
The removeBattery method should return the Battery object (actually a reference to the Battery object) and set the instance variable to null. However, you cannot set the instance variable to null after the return statement as it will not be reachable (think about that). You also cannot set the instance variable to null before the return statement otherwise it will return null. So how do you fix this conundrum? Use a temp variable.
Now for some nitpicks.
removeBattery and putBattery are methods. Why have you declared them as instance variables? Do you plan on using these variables later in your code? If so then use different names as it leads to confusion if you give classes, methods and variabels the same name.Java Code:public Battery bat, removeBattery; public boolean ok, putBattery;
Contrast this if statement to the one where you compare bat to null. In any case when dealing with booleans there is no need to use comparison.Java Code:if(occupied = true)
Java Code:if( bool ) { if( ! bool ) {
- 03-06-2011, 11:46 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
appreciate the speedy reply! The reason I have
is because the charger can be in use or not, and if it is in use, it adds +50 power to the battery in it. Am I following correctly? I am very new to java so still learning. Also, what is this?Java Code:if(occupied = true)
-MikeJava Code:if( bool ) { if( ! bool ) {
- 03-07-2011, 12:46 AM #4
And the reason I mentioned it was because you are using assignment (one equals) and not comparison (two equals). Also the code snippet I posted is the preferred way of using booleans in conditional statements. A boolean is already true or false so there is no need to use them in a conditional statement, just use them.
Similar Threads
-
JTable vs JTextField - which to use for creating "Search" screen
By tashimoto in forum AWT / SwingReplies: 6Last Post: 11-19-2010, 07:44 PM -
Creating a jpeg file: "no preview available"
By ScottVal in forum Advanced JavaReplies: 1Last Post: 10-24-2009, 03:31 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 -
"source not found" at debug time when creating new class
By rafamd11 in forum New To JavaReplies: 0Last Post: 11-22-2008, 01:49 AM -
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


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks