Gregory Desrosiers Montreal

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Friday, 14 December 2012

JAVA Program: Car Class

Posted on 17:03 by Unknown
Hello, people.

Sorry if I'm late with my Bloggers, but at least I know for sure that I need to upload one!

Anyway, let me show you one of the initial exercises I had in my JAVA Programming classes for creating classes instead of inputting those already from loaded packages.

There is no description of this, unfortunately, but this exercise comes from two JAVA files because the Car class is in a class file of its own.

Here it is:

public class Car
{
    private int yearModel;
    private String make;
    private int speed;

    public Car(int modelAndYear, String manufacturer)
    {
        yearModel = modelAndYear;
        make = manufacturer;
        speed = 0;
    }

    public void accelerate()
    {
        speed += 5;
    }

    public void brake()
    { 
        speed -= 5;
    }

    public int getYearModel()
    {
        return yearModel;
    }

    public String getMake()
    {
        return make;
    }

    public int getSpeed()
    {
       return speed;
    }

}

Below is the demonstration code for the Car class. In this case, the created car accelerates five times and have its speed displayed, then decelerates five times with its speed displayed. To make this simple, two for loops were used.

public class CarClassProgram
{
    public static void main(String[] args)
    {
        Car car1 = new Car(87, "Chevrolet");
       
        for (int x = 0; x < 5; x++)
        {
            car1.accelerate();
            System.out.println(car1.getSpeed());
        }
       
        for (int y = 0; y < 5; y++)
        {
            car1.brake();
            System.out.println(car1.getSpeed());
        }
    }
}


Well, I hope you've enjoyed checking this out! Later!

Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • The Quebec Ministerial Exam of College English (English Exit Exam)
    Hello, everyone. How you doing? I'm good. There is some unfortunate news for me to deliver, though I am planning to try to work myself a...
  • String Letter Counter (JAVA Program)
    Hello, guys. This is another JAVA Program I want to share with you guys. It's a counter for a specific letter in an entered string strai...
  • The Asperger Computer: A Self-Published Guide on Asperger's Syndrome and Multimedia
    The Asperger Computer - A Self-Published Guide on Asperger's Syndrome and Multimedia by Gregory Desrosiers How is Asperger’s syndrome li...
  • The Asperger Computer (e-book) - Sample Paragraph
    This is the sample paragraph for my e-book adaptation of a speech I've delivered at my college, Champlain College Saint-Lambert, on May ...
  • The Asperger Computer - Table des matières et une sample
    Excusez-moi si il ya beaucoup d'erreurs avec l'écriture , j'ai dû utiliser un traducteur pour obtenir ce parce que je su...
  • Arcade Games to check out
    Hello, everyone. How you people doing? I'm okay, though I am feeling mightily frustrated and lazy to try to work on a lot of things befo...
  • JAVA Program: Number Loop (Experimental Program of the Timer class)
    Hello there. Here's another program I want to present to you: it's a self-running experiment that tests the Timer class provided in ...
  • Fundraising Project (To Autism Society Canada)
    Hi, everyone! How are you people doing? I am feeling tired and stuff, but there are a couple of things I want to bring up with you. Tomorrow...
  • JAVA Program: Command-based String Remover in Text Files
    Hello, everyone. Let me share with you the code on one of the programs I did back in March 2013, while doing my Data Structures class. This ...
  • JAVA Program: Military Time to Real Time
    Hello, people. How you doing? I'm really busy with my college homework, but I am working so hard to pass my courses and continue in my s...

Blog Archive

  • ►  2013 (38)
    • ►  August (2)
    • ►  July (9)
    • ►  June (12)
    • ►  May (12)
    • ►  April (2)
    • ►  January (1)
  • ▼  2012 (24)
    • ▼  December (13)
      • Need some help for other holidays to come
      • JAVA Program: Coin Toss
      • JAVA Program: Lottery Class
      • JAVA Program: Car Class
      • String Letter Counter (JAVA Program)
      • Fun with Music, Drama and Comedy
      • JAVA Program - City Water Company Bill [The use of...
      • Inequality in Canadian Women through Politics and ...
      • Super Mario Bros: The Fighter Attack [Game Idea ma...
      • Princess Peach In India [Story Idea]
      • The Black Glove [Short Poem by me]
      • Longueuil RTL #83 (Sec. 5 English Short Story)
      • Super Mario Script 1 (v 2.3) [Super Mario Galaxy f...
    • ►  November (3)
    • ►  October (5)
    • ►  September (1)
    • ►  August (2)
Powered by Blogger.

About Me

Unknown
View my complete profile