Site Navigation

Site Search

CS 110: Lab 5

Learning Objectives

After completion of this lab, you should be able to

Work collaboratively as a pair programming team

All labs in CS 110 will be done as pair programming teams. Your partner for today's lab is listed in the table below:

Hebeler 203
Grader: Vinh Tran; Assistant: Daniel Carpenter
Team 1
Aguilar Jr, Jaime
Cuddington, Chris
Team 2
Amezcua Gutierrez, Edson
Field, Sarah
Team 3
Baird, Owen
Hansen, Mitchell
Team 4
Berman, Jake
Hastings, Jake
Team 5
Coudriet, Blake
Hogan, Martin
Team 6
Erickson, Joel
Ling, Nathan
Team 7
Goeke, Maxwell
Nash, Christopher
Team 8
Harris, Alexander
Olivares, Scott
Team 9
Heflick, Liz
Shearer, Stetson
Team 10
McCauley, Rylee
Wagster, Nathan
Team 11
Millard, Ryan
Smith, Stephanie
Team 12
Olden, Gregory
Akana, Chris
Team 13
Quayle, Weston
Millard, Mike
Team 14
Straub-Walden, Andy
Awan, Samara
Team 15
Williamson, Sarah
Barrett Wright, Matthew
Fill in: Bloom, Thomas

Note: Partners will change every week.

You may wish to review basic pair programming guidelines before you begin.

You should change roles every 10 to 15 minutes.

Write a Java class to represent a Car type

Based on Programming Challenge 2 from Chapter 3 of your textbook: Car.java

  1. Create a new Java class named Car that has the following fields
    • year - The year field is an int that holds a car's year model (e.g. 2010)
    • make - The make field is a String object that holds the make of the car (e.g. "Porsche")
    • speed - The speed field is an double that holds a car's current speed (e.g. 25.0)

  2. In addition, the Car class should have the following methods.
    • Constructor - The constructor should accept the car's year, make, and beginning speed as arguments
      • These values should be used to initialize the Car's year, make, and speed fields
    • Getter Methods - Write three accessor (getter) methods to get the values stored in an object's fields
      • getYear(), getMake(), getSpeed()
    • accelerate - Write an accelerate method that has no arguments (parameters) passed to it and adds 1 to the speed field each time it is called
      • For example: if the car was going 3 mph, accelerate would set the speed to 4 mph

Here is a big hint to get you started that you can copy and paste into a file named Car.java

public class Car
{
   private int year;
   private String make;
   private double speed;
     
// Constructor
   public Car(int y, String m, double beginningSpeed)
   {
      year = y;
      // you figure out the next two lines
   }

// Returns the Car's year
   public int getYear()
   {
      return year;
   }

// Returns the Car's make
   public String getMake()
   {
      // you figure out this one line
   }

// Returns the Car's current speed
   public double getSpeed()
   {
      // you figure out this one line
   }

// Adds one to the current speed
   public void accelerate()
   {
      // you figure out this one line
   }
}
 

Change Pair Partner Roles

Change pair partner roles. The driver should become the navigator, the navigator should become the driver.

Write a main method that uses your Car class

  1. Write a separate Java class RaceTrack in file RaceTrack.java with a main method that uses your Car class
    • Save the class in file RaceTrack.java in folder U:\labs\lab5\
  2. Inside RaceTrack class' main method
    • Create a new Car object (using the Car constructor method), passing in the year, make, and speed
    • Display the current status of the car object using the getter methods getYear(), getMake(), and getSpeed()
    • Call the car's accelerate method and then re-display the car's speed using getSpeed()

Her's a really big hint to speed you up that you can copy into your RaceTrack.java file

public class RaceTrack
{
   public static void main(String[] args)
   {
   
   // Create a new car
      Car sportsCar = new Car(2010, "Porsche", 5.0);
   
   // Display the current status.
      System.out.println("Year: " + sportsCar.getYear());
      // You figure out these two lines to display the make and speed of sportsCar
   
   // Accelerate and re-display the speed
      sportsCar.accelerate();
      System.out.println("Accelerate, my speed is now " + sportsCar.getSpeed());
   }
}  

Sample Session

Year: 2010
Make: Porsche
Speed: 5.0
Accelerate, my speed is now 6.0

Change Pair Partner Roles

Change pair partner roles. The driver should become the navigator, the navigator should become the driver.

Compare linear versus exponential growth algorithms

Computer scientists, and mathematicians, would say that the car's accelerate algorithm grows at a linear rate. If we were to graph it, the slope of the growth line would be a straight line (linear).

In this case, the car's speed increases 1 mph with each acceleration. Even if we changed the algorithm to add 100 mph with each acceleration call, we would still say it grows at a linear rate and the slope of the growth line would be a straight line.

  1. On the Lab 5 assignment sheet, determine the car's speed if we begin at a speed of 5.0 and continue to call the accelerate method, adding 1.0 to the car's speed each time.
  2. Fill in the calculated values on the Lab 5 assignment sheet, you will turn this sheet in to receive credit for the lab
Test Case 1
NSpeed + 1Speed Verified on Computer
Beginning5.05.0
16.0
27.0
3
4
5
  1. Modify your main method to call the accelerate method repeatedly to check your speed calculations

Growth by multiplication

Growth by multiplication occurs if we were to double (multiply by 2) the car's speed with each acceleration. For example, if the car was going 5.0 mph and accelerated, the car's new speed would be 10.0 (5.0 times 2.0). Accelerate again and the speed would be 20.0, etc.

  1. On the Lab 5 assignment sheet, assuming the speed were to double with each acceleration, use a calculator to determine the speed if we called accelerate 3 times, 4 times, and 5 times.
    • Note: assume the car starts at a speed of 5.0 mph.
Test Case 2
NSpeed * 2Speed Verified on Computer
Beginning5.05.0
110.0
220.0
3
4
5
  1. Modify your Java Car class so it accelerates by doubling its speed with each acceleration.
    • Change the code inside method accelerate in Car.java
  2. Rerun your main method and check your new speed calculations

Change Pair Partner Roles

Change pair partner roles. The driver should become the navigator, the navigator should become the driver.

Exponential Growth

Imagine acceleration raising the current speed by the power of 2 (speed2).

Java's Math class contains the pow (power) method we can use to raise an exponent to a power

speed = Math.pow(speed, 2);

Math.pow(speed, 2) is the equivalent of speed2

  1. On the Lab 5 assignment sheet, assuming the speed were raised by the power of 2 with each acceleration, use a calculator to determine the speed if we started at 5.0 and squared it each time we called the accelerate method
    • Note: when double values become really large, they are expressed in scientific notation (e.g. 4.294967296 x E9)
Test Case 3
NSpeed2CalculatedSpeed Verified on Computer
Beginning5.05.0
125.0
2625.0
3
4
5
  1. Modify your Java Car class so it accelerates by raising its current speed by the power of 2.
    • Change the code inside method accelerate in Car.java
  2. Rerun your main method and check your new speed calculations
    • Note: you do not need to brake after acceleration (Do not try this in real life)

To Receive Credit

You are allowed to drop one lab grade for the entire quarter.