The Artima Developer Community
Sponsored Link

Java Answers Forum
I am new and need help with objects and classes

3 replies on 1 page. Most recent reply: Oct 14, 2006 3:56 PM by ash gholami

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
ash gholami

Posts: 3
Nickname: ashgx47
Registered: Oct, 2006

I am new and need help with objects and classes Posted: Oct 13, 2006 11:18 PM
Reply to this message Reply
Advertisement
Hi , I am a computer science pre-major and am in a program methodology class. I am having trouble understanding the basic concept of object and classes. For example we have this problem:

Write a class name Car that has the following fields:
yearModel : field is an int that holds the cars year model
make: String object that holds make of the car
speed: int that holds cars current speed

in addition , class should have folliwng methos:
constructor
accessor
accelerate: method that should add 5 to the current speed each time it is called
brake: method which subracts 5 from current speed each time it is called.

now my professor made the following modifications to that original problem:
create car class exactly as mentioned above, then write the application program to demonstrate the car class as follows:
ask user for year and make of a car, creates car object using users input
asks the user for the year and make of a second car and created a second car object using these new inputs

only one variable in the program to stroe the year model of a car and only one variable in the program to store make of a car

call the accelerate method 3 times for the first car and 2 times for the second car
display the year make and speed of each car
call the beake method 2 times for each of the cars
dispplay the year and make and speed of each car

if it is unclear what i am tryin to get done this sums it up:
- Ask user in terminal window to input the model and year for two seperate cars
- Apply accelerate method 3 times for the first car and 2 times for the second
- Display the current speed of both cars.. should be 15mph and 10mph
- Apply brake method 2 times for each of the cars.
- Display speed of both cars.. should be 5mph and 0mph

hope that makes it more clear, once again i appreciate all the help and i am learning ! :)

my code is below:
- Ask user in terminal window to input the model and year for two seperate cars
- Apply accelerate method 3 times for the first car and 2 times for the second
- Display the current speed of both cars.. should be 15mph and 10mph
- Apply brake method 2 times for each of the cars.
- Display speed of both cars.. should be 5mph and 0mph

hope that makes it more clear, once again i appreciate all the help and i am learning ! :)


ash gholami

Posts: 3
Nickname: ashgx47
Registered: Oct, 2006

Re: I am new and need help with objects and classes Posted: Oct 13, 2006 11:20 PM
Reply to this message Reply
- Ask user in terminal window to input the model and year for two seperate cars
- Apply accelerate method 3 times for the first car and 2 times for the second
- Display the current speed of both cars.. should be 15mph and 10mph
- Apply brake method 2 times for each of the cars.
- Display speed of both cars.. should be 5mph and 0mph

hope that makes it more clear, once again i appreciate all the help and i am learning ! :)

public class Car
{
    //*************Instance Variables*************************************
    public int yearModel;
    public String make;
    public int speed;
    
    //************Constructors*******************************************
    /* Instantiates Car and makes all variables zero*/
    public Car()
    { 
        yearModel=0;
        speed=0;
        make="";
    }
    //***********Explicit value constructor*****************************
    //********Instantiates new Car and initializes make, model and speed*
    
    public Car(int yearModel, int speed, String make)
    {
        this.yearModel=yearModel;
 
        this.make= make;
    }
    
    //*************Accessor Methods***************************************
    //*The getYear method returns the year for the car********************
    
    public int getYear()
    {
        return yearModel;
    }
    
    //*The getSpeed method returns the speed for the car******************
    
    public int getSpeed()
    {
        return speed;
    }
    
    //*The getMake method returns the make of the car*********************
    
    public String getMake()
    {
        return make;
    }
    
    //*the accelerate method adss 5 to the current speed every time it is called
    
    public int accelerate()
    {
        speed=speed+5;
        return speed;
    }
    
    //*the brake method subtracts 5 to the current speed every time it is called
    public int brake()
    {
        speed=speed-5;
        return speed;
    }
    //*************Mutator Methods****************************************
    //*The setYear sets the year of the car to the value passed in the year parameter
    
    public void setYear(int yearValue)
    {
        this.yearModel=yearValue;
    }
    
    //*The setMake sets the make of the car to the value passed in the make paramtere
    
    public void setMake(String make)
    {
        this.make=make;
    }
    
    
    //*************Input Output Methods***********************************
    //*The toString method returns a string representation of the car info
     
    public String toString()
      {
          return "The "+yearModel+" "+make+" is currently going "+speed+" MPH."; 
      } // end method toString
    //************* MAIN METHOD ******************************************
    
    
    
    public static void main(String[]args)
    {
      Car car1= new Car(yearModel,make,speed);
    }
} // end class Car

Abhijit Akhawe

Posts: 4
Nickname: abhi27
Registered: Oct, 2006

Re: I am new and need help with objects and classes Posted: Oct 14, 2006 2:20 PM
Reply to this message Reply
Using the forums to solve your homework ;)

Well anyways, you dont need the main method on the Car class that you have posted. Basically you will have a Car class without the main. And a ApplicationProgram class that has the main method. The main method will ask the user for input and create the car objects using the constructor you rightly defined. Of course to the constructor you will pass the make and year that the user has entered. Then its all a question of calling the appropriate methods like accelerate and brake on the car objects.

One thing that I dont understand is the line 'only one variable in the program to stroe the year model of a car and only one variable in the program to store make of a car'. If you have two different makes and years how can u use one variable to store them.

Well anyways hope this helps.

ash gholami

Posts: 3
Nickname: ashgx47
Registered: Oct, 2006

Re: I am new and need help with objects and classes Posted: Oct 14, 2006 3:56 PM
Reply to this message Reply
thank you that helped, i am going to begin working on that application program that has the main and will see if it works out. once again thank you.

Flat View: This topic has 3 replies on 1 page
Topic: Saving Arrays Previous Topic   Next Topic Topic: i question about JDBC

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use