Creating Car Class in Java

Problem:

Design and implement a class called Car that contains instance data that represents the make, model, and year of the car. Define the Car constructor to initialize these values. Include getter and setter methods for all instance data, and a toString method that returns a one-line description of the car. Create a driver class called CarTest, whose main method instantiates and updates several Car objects.




Solution:

public class Car
{
 private String make;
 private String model;
 private int year;
 public Car(String make,String model,int year)
 {
  this.make = make;
  this.model= model;
  this.year = year;
 }
 
 public String setCarMake(String make)
 {
  this.make = make;
  return make;
 }
 
 public String getCarMake()
 {
  return make;
 }
 
 public String setCarModel(String model)
 {
  this.model = model;
  return model;
 }
 
 public String getCarModel()
 {
  return model;
 }
 
 public int setCarYear(int year)
 {
  this.year = year;
  return year;
 }
 
 public int getCarYear()
 {
  return year;
 }
 
 public String toString()
 {
  return "Car make: " + make + "      
  Car model: " + model + "      Car Year:" + year;
 }
 
 public static void main (String[] args)
 {
  Car car1 = new Car("Toyota", "Corolla" , 1996);
  Car car2 = new Car("Nissan", "Murano" , 2004);
  Car car3 = new Car("Infinity", "Mazda" , 2013);
  
  car1.setCarMake("BMW");
  System.out.println(car1);
  System.out.println(car1.getCarYear());

  car2.setCarModel("Altima");
  System.out.println(car2);
  System.out.println(car2.getCarMake());
  
  car3.setCarYear(2012);
  System.out.println(car3);
  System.out.println(car3.getCarModel());


 }
}


17 comments :

  1. Excellent .. Amazing .. I’ll bookmark your blog and take the feeds also…I’m happy to find so many useful info here in the post, we need work out more techniques in this regard, thanks for sharing. Revs check

    ReplyDelete
  2. Your post has those facts which are not accessible from anywhere else. It’s my humble request to u please keep writing such remarkable articles Toronto Limousine Service

    ReplyDelete
  3. We are really grateful for your blog. Many things are here to learn. Extended Car Warranty

    ReplyDelete
  4. Extremely pleasant article, I appreciated perusing your post, exceptionally decent share, I need to twit this to my adherents. Much appreciated!.  Hamilton Wedding Limo

    ReplyDelete
  5. This is very interesting content! I have thoroughly enjoyed reading your points and have come to the conclusion that you are right about many of them. You are great. Party Bus Toronto

    ReplyDelete
  6. A few experts offer versatile enumerating administrations to give their clients accommodation and genuine feelings of serenity, as the last can watch their car getting expertly cleaned. car detailing north york

    ReplyDelete
  7. Cars and minivans went under the Dodge nameplate, while its trucks, the Dodge Ram and Dodge Dakota, became Ram trucks. If you're looking at a used truck that comes from an earlier model year, it's a Dodge Ram. jaxcjdrwestside.com

    ReplyDelete
  8. The issue was the more seasoned set up instructor, great or impassive, continued being allotted the better classes. Best Dissertation Writing Service UK

    ReplyDelete
  9. The electric car is presently generally well known with organizations who need to develop an eco-accommodating picture. The primary Peugeot iOn was conveyed to the UK for the EDF Energy Company. ev charging point installation

    ReplyDelete
  10. I like to add about 20% to my max limit to allow for cars that can be negotiated within the budget. fuel consumption calculator

    ReplyDelete
  11. Thanks so much for sharing this awesome info! I am looking forward to see more postsby you! Car Accessories

    ReplyDelete
  12. site promotion will always be a tedious job but you can outsource site promotion on some indian or pakistani guy` أشهر المعالم في مدينة طرابزون

    ReplyDelete
  13. This post is so useful and relevant. Too easy to understand. Thanks for your post. Keep blogging

    cheap car rental auckland

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact