CSCI 161 - Lab 10: Car Class

Objectives

The objectives of this lab are to reinforce and help teach:

Overview

In this lab, you will develop a class that is the OOP representation of a car.

You will not write a program but instead only the Car class and that will be developed in a Car.java file. The main program is give here and should be copied into your Lab10 project as Lab10.java

Again, you will have to create your own Car.java class according to the specification below.

Specification

Develop your Car class as follows

  1. Your class should have the following Class state (fields) that describe the make, model, year of the car, along with the car's mpg rating, how long the car accelerates from 0 to 60 mpg, as well as the car's current speed, as follows:
    
            private String make;
            private String model;
            private int year;
            private double mpg;
            private double zeroToSixty;
            private double speed;
  2. Your car needs a default constructor that initializes state as follows:
    
            this.make = "undefined";
            this.model = "undefined";
            this.year = 1900;
            this.mpg = 0.0;
            this.zeroToSixty = 0.0;
            this.speed = 0;
  3. Your car also needs a parameterized Constructor that sets make, model, year, mpg, and zeroToSixty according to the values passed into the constructor.
  4. Include a toString accessor method using the code provide below (unchanged):
    
    	/*
    	 * toString - returns a string representation of the Car
    	 */
    	public String toString() {
    		return String.format("Make=%s, Model=%s, Year=%d, MPG=%.2f, Zero to Sixty in %.2f seconds, Current Speed=%.2f mph.",
                                        this.make,
                                        this.model,
                                        this.year,
                                        this.mpg,
                                        this.zeroToSixty,
                                        this.speed);		
    	}
  5. Include an accelerate mutator method with the prototype (name, returns, params) starting with the code below and finish the method so that it updates the speed as expected. You will have to do math and only change the code where the comment is:
    
                /*
                * accelerate - accelerates the car for a supplied number of seconds
                */
                public void accelerate(int seconds) {
                    System.out.printf("\tAccelarating %s %s for %d seconds.\n", this.make, this.model, seconds);
                    this.speed += /* put your code here */;
                }
                
  6. Include a brake mutator method with the prototype (name, returns, params) as below and finish the method so that it updates the speed as expected. You will have to do math and only change the code where the comment is:
    
                /*
                * brakes - brakes (decelerates) the car for a supplied number of seconds limiting the
                *          lowest speed to be 0.00 mph.
                */
                public void brake(int seconds) {
                    System.out.printf("\tBraking %s %s for %d seconds.\n", this.make, this.model, seconds);
                    this.speed = /* put your code here */;
                }
                

Instructions

Following are the instructions and you should follow them carefully:

  1. Using Eclipse create a new Java Project and name it: Lab10
  2. As with all your labs, add a class of the same name, Lab10 and in that class file copy in the code supplied in the link above.
  3. Create a Car class in the same directory as Lab10.java. The class should be contained with Car.java and and should adhere to the Specification above.
  4. Your Car class should be properly commented, indented and use whitespace appropriately.
  5. Your Lab10.java main program class should not be updated. You will not be submitting that file but it is the class that you run to test your Car class.
  6. Your program should output *exactly* according to the Output Specification below which should not be a problem as the main method code has been supplied to you, as have all other methods withing the Car class that output things or generate strings that are output. The only thing that could be "off" in your output is your speed calculations (accelerate and brake methods), so fix those if needed.
  7. When the lab is complete and working submit Car.java via AutoLab.

Output Specification

Following is the desired output:

        
Car c1: Make=undefined, Model=undefined, Year=1900, MPG=0.00, Zero to Sixty in 0.00 seconds, Current Speed=0.00 mph.

Car c2: Make=Chevrolet, Model=Corvette, Year=1953, MPG=13.70, Zero to Sixty in 11.20 seconds, Current Speed=0.00 mph.

Car c3: Make=Ford, Model=Mustang, Year=1967, MPG=13.90, Zero to Sixty in 7.50 seconds, Current Speed=0.00 mph.
	Accelarating Ford Mustang for 10 seconds.
Car c3: Make=Ford, Model=Mustang, Year=1967, MPG=13.90, Zero to Sixty in 7.50 seconds, Current Speed=80.00 mph.
	Braking Ford Mustang for 5 seconds.
Car c3: Make=Ford, Model=Mustang, Year=1967, MPG=13.90, Zero to Sixty in 7.50 seconds, Current Speed=40.00 mph.
	Braking Ford Mustang for 6 seconds.
Car c3: Make=Ford, Model=Mustang, Year=1967, MPG=13.90, Zero to Sixty in 7.50 seconds, Current Speed=0.00 mph.        
        
    

Deadline

This lab be submitted to the instructor by 11:59pm on Monday, November 27th.