Results 1 to 2 of 2
  1. #1
    TANKDS is offline Member
    Join Date
    Feb 2012
    Posts
    54
    Rep Power
    0

    Default Help Me, load in car number

    Rather than CarDemo to give back just the Speed Gear and RPM I want it to also say before it what Car it is referring to. Below is my Car.java code and CarDemo.java.

    Java Code:
    class Car {
    
    int Speed = 0;
    int Gear = 1;
    int RPM = 0;
    
    void speedup(int increment) {
    Speed = Speed + increment;
    }
    
    void applybrakes(int decrement) {
    Speed = Speed - decrement;
    }
    
    void changegear(int newValue) {
    Gear = newValue;
    }
    
    void increaserpm(int increment) {
    RPM = RPM + increment;
    }
    
    void decreaserpm(int decrement) {
    RPM = RPM - decrement;
    }
    void printStates() {
    System.out.println("Speed:" +
     Speed + " Gear:" + 
    Gear + " RPM:" + RPM);
    }
    }
    Java Code:
    class CarDemo {
    public static void main(String[] args) {
    
    Car car1 = new Car();
    Car car2 = new Car();
    
    car1.increaserpm(7000);
    car1.speedup(15);
    car1.increaserpm(6000);
    car1.changegear(2);
    car1.decreaserpm(9000);
    car1.speedup(25);
    System.out.println("Car 1 = ");
    car1.printStates();
    
    car2.increaserpm(14000);
    car2.speedup(10);
    car2.changegear(2);
    car2.decreaserpm(14000);
    car2.applybrakes(10);
    car2.changegear(0);
    System.out.println("Car 2 = ");
    car2.printStates();
    
    System.out.println("Car 1 - Status = Functioning");
    System.out.println("Car 2 - Status = Engine Blown");
    }
    }
    I basically want it to express Car (car number) = Speed:(Speed) Gear:(Gear) RPM:(RPM)

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,573
    Rep Power
    12

    Default Re: Help Me, load in car number

    I basically want it to express Car (car number) = ...
    What are these "car numbers" of which you speak?

    As it is, cars have a speed, gear and rpm (*). And that's it: a number is not part of their state.

    One option you have is to make a car's number part of its state. Give Car an int variable number and use the constructor and/or some other mechanism to ensure that a car is set up with a sensible number. (By "sensible" I am thinking that you might want these numbers to be unique.) If you follow this approach you could override the Car toString() method to provide the string you are looking for, or have create a Car method specifically to return this string.

    A second option is for the caller (currently main() in CarDemo) to maintain its own notion of what the number of a car is. If main() cares to regard car2 as having the number 2, then it can do so. Or it could put the cars into an array of Car and use the index of a car within this array as its number. Either way it would be handy for Car to have a method that returns car information.

    A third way of associating a number with a car might be to use the hash code associated with it. Within main() you can say things like car1.hashCode() to obtain an int that is unique for this car. Within the Car class an instance can just say hashCode() to get that same int. Cars get a number this way, but it is only good for the lifetime of the program. Once again, having associated a number with a car you still need to obtain a string with the other car information.

    -----

    There is a common theme running through all three approaches to giving a car a number: the printStates() method is inflexible. It would be better for this method to return a String rather than print it. The caller can print it if the caller wants. Or the caller can prefix the string with the car number and print that.

    -----

    (*) It would be a very good idea to follow Java coding conventions and begin variables with a lowercase letter: speed, gear etc.

Similar Threads

  1. Replies: 8
    Last Post: 01-21-2012, 02:14 AM
  2. Dice help. posting the number of times a number is rolled.
    By cookiejarvus in forum New To Java
    Replies: 13
    Last Post: 12-05-2011, 12:08 AM
  3. Replies: 1
    Last Post: 10-28-2011, 09:18 AM
  4. Finding a number in array close to another number
    By SteroidalPsycho in forum New To Java
    Replies: 2
    Last Post: 02-15-2010, 01:37 AM
  5. Printing the Number of Times a Number in a Range Shows up
    By space4rent00 in forum New To Java
    Replies: 1
    Last Post: 02-05-2010, 11:42 PM

Tags for this Thread

Posting Permissions

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