Welcome to Dream.In.Code
Become a Java Expert!

Join 150,198 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,992 people online right now. Registration is fast and FREE... Join Now!




Demonstrate a class the creates an object

 
Reply to this topicStart new topic

Demonstrate a class the creates an object

camckee316
23 Sep, 2008 - 12:53 PM
Post #1

New D.I.C Head
*

Joined: 29 Aug, 2008
Posts: 48


My Contributions
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



CODE

// 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.
CODE

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;
    }
}









User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Demonstrate A Class The Creates An Object
23 Sep, 2008 - 01:09 PM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
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 Sep, 2008 - 01:11 PM
User is offlineProfile CardPM
+Quote Post

Gloin
RE: Demonstrate A Class The Creates An Object
23 Sep, 2008 - 01:15 PM
Post #3

Expert Schmexpert...
Group Icon

Joined: 4 Aug, 2008
Posts: 934



Thanked: 54 times
My Contributions
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.
User is online!Profile CardPM
+Quote Post

camckee316
RE: Demonstrate A Class The Creates An Object
18 Oct, 2008 - 07:23 AM
Post #4

New D.I.C Head
*

Joined: 29 Aug, 2008
Posts: 48


My Contributions
thank you for helping. what an oversite on my part. biggrin.gif once again thanks
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:53AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month