Results 1 to 4 of 4
  1. #1
    Hyperrr is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default 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.

    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;
    			}
    		}
    			
    		
    	}
    }
    Appreciate the help!
    -Hyper

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    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.
    Java Code:
    public Battery bat, removeBattery;
    public boolean ok, putBattery;
    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:
    if(occupied = true)
    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( bool ) {
    if( ! bool ) {

  3. #3
    Hyperrr is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default

    appreciate the speedy reply! The reason I have
    Java Code:
    if(occupied = true)
    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( bool ) {
    if( ! bool ) {
    -Mike

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    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

  1. Replies: 6
    Last Post: 11-19-2010, 07:44 PM
  2. Creating a jpeg file: "no preview available"
    By ScottVal in forum Advanced Java
    Replies: 1
    Last Post: 10-24-2009, 03:31 AM
  3. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  4. Replies: 0
    Last Post: 11-22-2008, 01:49 AM
  5. Replies: 1
    Last Post: 10-20-2008, 07: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
  •