Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Nov 2009
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Help with method argument/variable.

    Ok, im having a little trouble again. Here is my code.
    Code:
    public class clsAutos {
    	public static boolean isCarStarted = false;
    	public static double gas;
    	public static double efficiency;
    	public static String color;
    	public static boolean isCarStoped = true;
    	public static double drive;
    	public static double distance = ;
    	public clsAutos(double eff){
    		efficiency = eff;
    		eff = 20;
    	}
    	
    	public void addGas(double ammount){
    		gas = gas + ammount;
    		System.out.println("You've added " + ammount + " gallons of gas, bringing your total gas to " + gas + " gallons.");
    		return;
    	}
    	
    	public void drive(double distance){
    	
    		if(isCarStarted && gas > gas - distance/efficiency){
    				gas = gas - (distance/efficiency);
    				System.out.println("You have driven " + distance + " miles. You have " + gas + " gallons of gas left.");
    				
    		}else{
    			System.out.println("You cannot drive.");
    		}
    		return;
    		}
    		
    	public double getGas(){
    		
    		return(gas);
    	}
    	public double getDrive(){
    		
    		return(drive);
    	}
    	public boolean getStart(){
    	
    		return(isCarStarted);
    	}
    	public boolean getStop(){
    		
    		return(isCarStoped);
    	}
    	public void setColor(String col){
    		color = col;
    		System.out.println("You're car is the color " + color);
    		
    		return;
    	}
    	
    	
    	public void startCar(boolean carStarted){
    		boolean wasStarted = isCarStarted;
    		
    		
    			if(wasStarted){
    				System.out.println("The car is already started.");
    				
    				return;
    			}else{
    				System.out.println("The car has been started.");
    				isCarStoped = false;
    				isCarStarted = true;
    				return;
    			}
    	}
    	public void stopCar(boolean carStoped){
    	
    		
    		
    			if(isCarStoped){
    				
    				
    				System.out.println("The car is already off.");
    				return;
    			}else{
    				System.out.println("The car has been stoped.");
    				isCarStoped = true;
    				isCarStarted = false;
    				return;
    			}
    	}
    	}
    number 1, i dont know how to set what my distance is. i dont know why i can print how far i went, but how do i track that as a variable. Also, whenever i perform the method drive, it sets isCarStarted to true. Any ideas anyone?

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    Change
    Code:
    	public static double distance = ;
    to
    Code:
    	public static double totalDistance = 0.0;
    And then:
    Code:
    	public void drive(double distance){
    	
    		if( ! isCarStarted )
    		{
    		    System.out.println("You haven't started the car yet!");
    		    return;
    		}
    		double gasNeeded = distance/efficiency;
    		if ( gasNeeded > gas )
    		{
    		    System.out.println("You don't have enough gas to drive that far!");
    		    return;
    		}
    		totalDistance += distance;
    		gas -= gasNeeded;
    		System.out.println("You just drove " + distance + " miles.");
    		System.out.println("You have now driven a total of " + totalDistance + " miles.");
    		System.out.println("You have " + gas + " gallons of gas left.");
    	}
    Untested! Off the top of my head.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    And by the way, there is NO REASON to have BOTH isCarStarted and isCarStoped. I would GET RID of isCarStoped. (And it is spelled wrong, anyway: the word is "Stopped".)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Regular Coder cs_student's Avatar
    Join Date
    Oct 2009
    Location
    ~/
    Posts
    195
    Thanks
    2
    Thanked 22 Times in 22 Posts
    I think you should have a look at the top two links in my signature.

    First, I don't even know what your question is. Maybe try thinking about your question before writing it down. After you have written it down, re-read it to make sure that it will make sense to someone who is totally oblivious to what your trying to accomplish (ie me).

    First, maybe you should work on figuring out why the program isn't even compiling. Your referencing static members in non-static methods. You have also made all of your members public, which is a very bad design. If you need to change your members, create public mutator methods to do so. If you need to retrieve values of the private members, create getter methods which return the corresponding values.


    Also, whenever i perform the method drive, it sets isCarStarted to true.
    Ok .... What is wrong. How is the drive method supposed to act? Should the car drive even if it isn't started? Should it throw an exception or flag if the car isn't started when the drive method is called?

    You need to be specific about what you are trying to accomplish and how you are trying to accomplish it if you want any help.

    And by the way, there is NO REASON to have BOTH isCarStarted and isCarStoped. I would GET RID of isCarStoped. (And it is spelled wrong, anyway: the word is "Stopped".)
    Maybe he is planning for the future when he calculates the amount of fuel consumption during idle (when the car is stopped, but still running). =P

    Regards,


    cs_student
    Last edited by cs_student; 11-12-2009 at 10:02 PM.

  • #5
    New Coder
    Join Date
    Nov 2009
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts
    lol, ok, i have an isCarStoped (only this way cause i always forget how to spell it, and so i just do it this way, and remember one p) because i have a button that has to stop the car. Again, i have only been coding for about 4 weeks in an IB HL computer science class in school. Basically, I have 4 classes atm, an autos class (the one i just posted) then i have my Start Game which just says whether i want to run my GameGui(another of my class) or my MainMenu(another class) and i have it set ATM to gamegui, but im trying to completely debug the program before i start adding more buttons to my gui. The reason i made it all public static is because i have to use them in my gamegui class, and idk how else to access them from over there. I want it to use distance in my gamegui class to calculate the gas-distance/efficiency, but i had to make a variable for that, but i didn't know what to set distance = to too calculate what the distance i drove was. Also, my drive method is just supposed to drive the car. Its not supposed to drive if there is not enough gas (gas-distance/efficiency) AND the car is not started, isCarStarted = true. If its not true, its not supposed to drive. I dont completely know everything im doing, and im trying to keep it as simple as possible.

  • #6
    Regular Coder cs_student's Avatar
    Join Date
    Oct 2009
    Location
    ~/
    Posts
    195
    Thanks
    2
    Thanked 22 Times in 22 Posts
    I suggest that you read the textbook you have been assigned or take a good look at the Java Trail. I took a computer science class my junior year in high school (which was last year). You are using the same tactics as many of my class mates (who eventually dropped out). Their idea was to try and get something that compiled or worked, then work from there. They didn't care how it got done, just that it did (ends to a mean). However, when the professor asked them to modify or extend the code it was a nightmare for them as even they couldn't understand what they had done, let alone extend it.

    I suggest that you plan out your entire program before you decide to code it.

    if there is not enough gas (gas-distance/efficiency) AND the car is not started, isCarStarted = true.
    Do you want to check if there is not enough gas to get to the destination, or just if there is gas to start the car and start going towards the destination?
    Also isCarStarted = true only sets the value of isCarStarted to true. It doesn't test if isCarStarted is true or not. Since the variable is a boolean primitive itself you don't have to compare it with anything. Just isCarStarted will suffice to evaluate the conditional.

    The reason i made it all public static is because i have to use them in my gamegui class, and idk how else to access them from over there
    Pass a reference of the Auto class to the GameUI class.

    ie

    Code:
    ....
    Auto car = new Auto(0.65);
    ....
    GameUI = new GameUI(car);
    ...

    Hope that gives you a little more insight of how you can do it. Try to be modular and OOP as possible. You don't want other classes accessing other class members. Create appropriate functions for them to do so and pass an instance of the class instead.
    Last edited by cs_student; 11-13-2009 at 12:52 AM.

  • #7
    New Coder
    Join Date
    Nov 2009
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ok, here is a quick example of what i am using. We dont have textbooks or anything, our teacher did a small example of class(showed us his) and whenever we ask a question, he says its just logical, and figure it out. Alot of the time it is, but there are coding problems we have also. Anway, here is my drive from GameGui.
    Code:
    butDrive.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e)
    			{
    				objCar.drive(Double.parseDouble(txtDrive.getText()));
    				labDrive.setText(Double.toString(objCar.getDrive()));
    				labGas.setText(Double.toString(objCar.getGas()));
    				if(clsAutos.isCarStarted = true && clsAutos.gas > clsAutos.gas - clsAutos.totalDistance/clsAutos.efficiency){
    					car.setLocation(car.getX() + Integer.parseInt(txtDrive.getText()),100);
    				}else{
    				
    			}
    		}
    		}
    		);
    again, im only turning to here, cause some stuff i just dont know how to do.

    /*it doesn't show it all, but thats the meat of the method.*/

  • #8
    Regular Coder cs_student's Avatar
    Join Date
    Oct 2009
    Location
    ~/
    Posts
    195
    Thanks
    2
    Thanked 22 Times in 22 Posts
    I would check to see if the car can drive before calling the drive method. ie

    Code:
    butDrive.addActionListener( 
            new ActionListener(){
                    public void actionPerformed(ActionEvent e)
                    {
                            /** 
                                Have a method to test if car can drive.
                                This way if you later want to also check 
                                if the seat belt is buckled as well, you could
                                put it in the method rather than adding another
                                conditional to ever if statement that checks if
                                the car can drive.
                            **/
                            if( objCar.canDrive() ) 
                            {
                            // Drive
                            objCar.drive(Double.parseDouble(txtDrive.getText()));
                            labDrive.setText(Double.toString(objCar.getDrive()));
                            labGas.setText(Double.toString(objCar.getGas()));
    
                            } else {
                                    // What should happen if you can't drive?
                            }
                    }
            }       
    );

  • #9
    New Coder
    Join Date
    Nov 2009
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Alright, my program is almost finally fully debugged! i only have one last question before i take a rest and sleep. Here is my clsGameGui.
    Code:
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    
    @SuppressWarnings("serial")
    public class clsGameGui extends JFrame {
    	
    	private Toolkit tools;
    	
    	clsAutos objCar = new clsAutos(20);
    
    	public clsGameGui(){
    		setSize(800,600);
    		setTitle("Car Game GUI EDITION!");
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		tools = getToolkit();
    		Dimension size = tools.getScreenSize();
    		setLocation(size.width/2 - getWidth()/2, size.height/2 - getHeight()/2);
    		
    		JPanel panel = new JPanel();
    		getContentPane().add(panel);
    		panel.setLayout(null);
    		
    		final JLabel car = new JLabel();
    		
    		car.setIcon(new ImageIcon("Images/car.png"));
    		car.setBounds(1,100,80,30);
    
    		final JLabel labGas = new JLabel(Double.toString(objCar.getGas()));
    		labGas.setBounds(125,5,50,20);
    		final JLabel labDrive = new JLabel(Double.toString(objCar.getDrive()));
    		labDrive.setBounds(125,25,50,20);
    		final JLabel labStart = new JLabel(Boolean.toString(objCar.getStart()));
    		
    		final JLabel labStop = new JLabel(Boolean.toString(objCar.getStop()));
    		
    		final JTextField txtGas = new JTextField();
    		txtGas.setBounds(1, 1, 30,20);
    				
    		JButton butGas = new JButton("Add Gas");
    		butGas.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				objCar.addGas(Double.parseDouble(txtGas.getText()));
    				labGas.setText(Double.toString(objCar.getGas()));
    				
    			}
    		});
    		
    		butGas.setBounds(35, 1, 90,20);
    		
    		JButton butStart = new JButton("Start Car");
    		butStart.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				objCar.startCar(Boolean.parseBoolean(labStart.getText()));
    				
    				
    			}
    		});
    		
    		butStart.setBounds(35, 50, 90,20);
    		
    		JButton butStop = new JButton("Stop Car");
    		butStop.addActionListener(new ActionListener()
    			{
    			public void actionPerformed(ActionEvent e)
    			{
    				objCar.stopCar(Boolean.parseBoolean(labStop.getText()));
    			}
    		});
    		butStop.setBounds(35, 75, 90,20);
    		final JTextField txtDrive = new JTextField();
    		txtDrive.setBounds(1, 25, 30, 20);
    		
    		JButton butDrive = new JButton("Drive");
    		
    		butDrive.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e)
    			{
    				objCar.drive(Double.parseDouble(txtDrive.getText()));
    				labDrive.setText(Double.toString(objCar.getDrive()));
    				labGas.setText(Double.toString(objCar.getGas()));
    				if(clsAutos.isCarStarted && clsAutos.gas > clsAutos.gas - clsAutos.totalDistance/clsAutos.efficiency){
    					car.setLocation(car.getX() + Integer.parseInt(txtDrive.getText()),100);
    				}else{
    				
    			}
    		}
    		}
    		);
    		butDrive.setBounds(35, 25, 90, 20);
    		
    		
    		//Remember that panel.add has to happen after everything else.
    		
    		panel.add(butStop);
    		panel.add(butStart);
    		
    		panel.add(car);
    		panel.add(labGas);
    		panel.add(txtGas);
    		panel.add(butGas);
    		panel.add(txtDrive);
    		panel.add(butDrive);
    	}
    			
    		
    		
    	}
    What comes up, if u can't already tell. Is two text boxes, 4 buttons, and a label. Also under, is a little car. If i can drive, everything we went over before, that now works, then it moves my car
    Code:
    car.setLocation(car.getX() + Integer.parseInt(txtDrive.getText()),100);
    is the code i used to do that. If i dont have enough gas to drive the full ammount, then it doesn't do anything, because it cannot drive. Is there any way i can make it so that if i add in a drive ammount that uses more gas than is currently in the car, it uses all the gass i have, then stops, instead of not doing anything. I dont have a timer on it or anything yet, but im just wondering if its simple enough to do.

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    I'm not going to mess with your code.

    I'll just show you how to change the code I gave you, before:
    Code:
    	public void drive(double distance){
    	
    		if( ! isCarStarted )
    		{
    		    System.out.println("You haven't started the car yet!");
    		    return;
    		}
    		double gasNeeded = distance/efficiency;
    		if ( gasNeeded > gas )
    		{
    		    System.out.println("You don't have enough gas to drive " + distance + " miles!");
    		    System.out.println("So you will only drive the indicated distance:");
    		    distance = gas * efficency; // only enough gas to go this far
    		    gasNeeded = gas; // and need all that is left to drive that distance
     
    		}
    		totalDistance += distance;
    		gas -= gasNeeded;
    		System.out.println("You just drove " + distance + " miles.");
    		System.out.println("You have now driven a total of " + totalDistance + " miles.");
    		System.out.println("You have " + gas + " gallons of gas left.");
    	}
    See? Dirt simple. Not enough gas? Change the distance instead of refusing to go at all.

    LOGIC, as your instructor says.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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