Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Two class program help

  1. #1
    Member
    Join Date
    Feb 2010
    Posts
    30
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Two class program help

    So I have been having some trouble in my Java 1 class lately, so I wanted to go back and try to understand classes and methods more better. (I promise I am not a liar) I just want to learn. I picked out a programming challenge for me to do. The one I picked out is:

    make a class named car that has 3 fields, yearModel as in int, make as a String, and speed as an int. Then it says to make a Constructor that accepts the cars year model and make as arguments. It says these values should be assigned to the objects year model and make fields. and assign speed to 0. It says to make appropriate accessors. and a method of accelerate that adds 5 to the speed field everytime its called. and a method called brake that declines 5 everytime its called.

    I wrote my class, which is:
    public class Car
    {
    	private int yearModel;
    	private String make;
    	private int speed;
     
    	public Car(int model, String m)
    	{
    		yearModel = model;
    		make = m;
    		speed = 0;
    	}
     
    	public int getYearModel()
    	{
    		return yearModel;
    	}
     
    	public String getMake()
    	{
    		return make;
    	}
     
    	public int getSpeed()
    	{
    		return speed;
    	}
     
    	public int accelerate(int ac)
    	{
    		ac = ac + 5;
     
    		return ac;
    	}
     
    	public int brake(int b)
    	{
    		b = b - 5;
     
    		return b;
    	}
    }

    and my driver for the class is:

    import javax.swing.JOptionPane;
     
    public class CarDemo
    {
    	public static void main(String[] args)
    	{
    		Car c = new Car(1992, "Mustang");
    		int s = 0;
    		s = c.getSpeed();
    		for(int i = 0; i < 5; i++)
    		{
    			System.out.println("The " + c.getYearModel() + " " + c.getMake()
    			+ "\n is going ");
     
    			c.accelerate(s);
     
    			System.out.println("Now the " + c.getYearModel() + " " + c.getMake()
    			+ "\n is going " + s);			
     
    		}
    	}
    }

    I am having trouble getting the accelerate method to add 5 to the speed, can someone help? It just says the 1992 mustang is going 0 over and over.
    Last edited by BuhRock; April 16th, 2010 at 11:04 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Two class program help

    You're accelerate method is broken.

    Instead of re-assigning the acceleration with acceleration + 5, you should be increasing the speed by 5.

    something like this:

    public void accelerate()
    {
         speed = speed + 5;
    }

    The brake method should be something similar to the above method.

    Also, you need to be polling the speed each time through the loop, updating the value since Java passes primitives by Value rather than by Reference (note: objects are passed by reference rather than by value)

    for (int i = 0; i < 5; i++)
    {
         // other code to accelerate or decelerate
         int speed = c.getSpeed();
         System.out.println("The car's current speed is " + speed);
    }

  3. #3
    Member
    Join Date
    Feb 2010
    Posts
    30
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Two class program help

    alright, I got the accelerate method fixed, and I tried to do the same thing with the brake method but i cant get the for loop to decrease the speed.

    	public void brake()
    	{
    		int brake = 0;
    		brake = brake - 5;
    	}

    and then

    		for(int i = 0; i < 5; i++)
    		{
    			s = c.getSpeed();
    			System.out.println("The speed is now " + s);
    			c.brake();
    		}

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Two class program help

    You're trying to do the same thing with the brake method as you originally had with your acceleration method. You need to be updating speed, not the variable brake.

    Just copy/paste the acceleration method, change the name to brake and change one other thing (think: what is the difference between acceleration and braking?)

  5. #5
    Junior Member
    Join Date
    Apr 2010
    Posts
    10
    My Mood
    Nerdy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Two class program help

    Old thread but I want to share my work with others it might be useful.

    The problem was here:

    public int accelerate(int ac)
    	{
    		ac = ac + 5;
     
    		return ac;
    	}

    Because if we already have private int speed we don't need to use another variable int ac, but we should use private int speed, so that method would be implemented like this:

    public int accelerate() {
    		speed += 5;
    		return speed;
              }

    Full code:

    public class Car {
    	private int yearModel;
    	private String make;
    	private int speed;
     
    	public Car(int model, String m) {
    		yearModel = model;
    		make = m;
    		speed = 0;
    	}
     
    	public int getYearModel() {
    		return yearModel;
    	}
     
    	public String getMake() {
    		return make;
    	}
     
    	public int getSpeed() {
    		return speed;
    	}
     
    	public int accelerate() {
    		speed += 5;
    		return speed;
    	}
     
    	public int brake(int b) {
    		b = b - 5;
    		return b;
    	}
    }

    public class CarDemo {
    	public static void main(String[] args) {
    		Car c = new Car(1992, "Mustang");
    		int s = 0;
    		s = c.getSpeed();
     
    		for(int i = 0; i < 5; i++) {
    			System.out.println("The " + c.getYearModel() + " " + c.getMake() + "is going: " + s);
     
    			s = c.accelerate();
     
    			System.out.println("Now the " + c.getYearModel() + " " + c.getMake() + "is going: " + s);
    		}
    	}
    }

Similar Threads

  1. Replies: 0
    Last Post: April 11th, 2010, 08:56 AM
  2. problem with data access when a class call another class
    By ea09530 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 4th, 2010, 05:20 PM
  3. Accessing a method of one class in another class
    By Sai in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 23rd, 2010, 04:06 PM
  4. how to load a class and type cast to that class?
    By chinni in forum Object Oriented Programming
    Replies: 2
    Last Post: November 9th, 2009, 10:18 AM
  5. new program (lotto.class)
    By james137 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 3rd, 2009, 05:22 PM