3 Replies - 840 Views - Last Post: 18 October 2008 - 08:23 AM Rate Topic: -----

#1 camckee316  Icon User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 277
  • Joined: 29-August 08

Demonstrate a class the creates an object

Posted 23 September 2008 - 01:53 PM

I am to create a class that creates an object (car). As it speeds up I am to display the speed. there is no problem that part works fine. The problem is when it slows down I am to display the speed just as I did when it speed up. However for reason or another It does not go into this loop. Any ideas on what I am doing wrong? Thanks



// demonstrate the class in a program that creates a car

import java.util.Scanner;

public class classDemoMcKee
{
	public static void main(String[] args)
	{
	carMckee carCreate;
	int yr;
	String make;
	int speed;
	int current = 0;
	
	Scanner keyboard = new Scanner(System.in);
	
	// get year
	System.out.println("What year is your vehicle?");
	yr = keyboard.nextInt();
	keyboard.nextLine();
	
	// get make
	System.out.println("What is the make of your vehicle?");
	make = keyboard.nextLine();
	
	System.out.println();
	
	carCreate = new carMckee(yr, make);

	System.out.println("We will now accelerate to 25 mph. Displaying the "
	+ "speed every 5 mph.");
	System.out.println();
	
   //loop to speed up 
	for (speed = 0; speed < 5; speed++)
	{
		System.out.println("Your car's speed is " + carCreate.getSpeed() + " mph.");
		System.out.println();
 	   carCreate.accelerate();
	}
	
		current = carCreate.getSpeed();
	
	System.out.println("You are currently going " + current + " mph.");
	System.out.println();
	System.out.println("Let's slow down. I will display your speed as you slow down just "
	+ "as I did when you speed up.");
	
	System.out.println();
	
	//loop to slow down
	for (speed = 5; speed > 0; speed--)
	{
		System.out.println("Your car's speed is " + carCreate.getBrake() + " mph.");
		System.out.println();
		  carCreate.getBrake();
	}
	
	current = carCreate.getBrake();
	
	System.out.println("Your speed is " + speed + " you should put the vehicle in park.");
	System.out.println();	
	System.out.println("How did you like driving your " 
	+ yr
	+ " "
	+ make);
 }
}	



Here is on file, this is the other.
public class carMckee
{
	private int speed;
	private int yearModel;
	private int make;
	
	public carMckee(int yr, String mk)
	{
		yearModel = yr; // year of car
		String make = mk; // make of car
		speed = 0; // speed of car
	}
	
	public int getSpeed()
	{
		return speed;
	}
	
	public int getBrake()
	{
		return speed;
	}
	public int yearModel()
	{
		return yearModel;
	}
	
	public int make()
	{
		return make;
	}
	
	public void accelerate()
	{
		speed += 5;
	}
	
	public void slow()
	{
		speed -= 5;
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Demonstrate a class the creates an object

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Demonstrate a class the creates an object

Posted 23 September 2008 - 02:09 PM

you're calling: carCreate.getBrake();, but your slowing function is called slow()

I assume your getBrake function should be calling slow, instead of returning the current speed.

This post has been edited by William_Wilson: 23 September 2008 - 02:11 PM

Was This Post Helpful? 0
  • +
  • -

#3 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Demonstrate a class the creates an object

Posted 23 September 2008 - 02:15 PM

You have two methods that return the speed of the car, getSpeed() and getBrake().
You have an accelerate-method and one that you call slow that you don't seem to use. I think it's called retardate or perhaps this ought to be your brake-method.
Was This Post Helpful? 0
  • +
  • -

#4 camckee316  Icon User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 277
  • Joined: 29-August 08

Re: Demonstrate a class the creates an object

Posted 18 October 2008 - 08:23 AM

thank you for helping. what an oversite on my part. :D once again thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1