Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Java Lab 8: RaceCar – Part 1                                                  Thursday, June 24, 2004 
 
Read instructions carefully! Following instructions is part of the grade for this lab.  
 
This lab is due at 5pm today, at which point solutions will be posted at a location to be 
announced to the class. Having the lab due at 5 pm implies that you should save all your 
files and leave your machine by 5 pm. Failure to turn in the lab promptly at 5 pm will be 
penalized by taking off 1 point.  
 
Make sure you read through the entire lab before attempting any of the parts to manage 
your time appropriately. For this lab, we will also be grading the neatness of and 
comments in your code. Please look over the solutions that we give you for guidelines on 
how to present your code. 1 point will be taken off for code that is not neat or has no 
comments. 
 
0. Create a new folder named lab8 in the folder C:\java\lastName (where lastName is the 
variable that stores your last name). This is the folder to which you will be saving all the 
files required for this lab. 
 
1. (2 points) Create a new class called RaceCar.  Add two fields to RaceCar: a field of 
type String to store the name of the car and a field of type Color (from 
java.awt.Color) to store the color of the car. Each car can have a different name and 
color. Should the fields be static or non-static? Compile. 
 
2. (1 point) Every one of our racecars will have the same top speed. Add a private 
constant of type double to the RaceCar class to store the top speed and initialize it to 
any number you want. Should this field be static or non-static? Compile. 
 
3. (1 point) Add a constructor to RaceCar which accepts a name and color argument 
and assigns the arguments to the name and color fields of the class. Compile. 
 
4. (2 points) Add a method 1 called getName that returns the name of the car and a 
method called getColor that returns the color of the car. Should these methods be 
static or non-static? Compile. 
 
5. (2 points) Add a method to RaceCar called race which accepts two RaceCars as 
arguments, simulates a race between the two, and returns the car that won the race, or 
return null if the race is a tie. The method should calculate a random speed for each car 
between 0 and the top speed. The car with the higher speed wins the race, but if they both 
have the same speed they tie. The method random() in java.lang.Math returns a 
random double between 0 and 1. If you multiply this random number by the top speed, 
the product will be a random number between 0 and the top speed. Should this method be 
static or non-static? Compile. 
 
6. (2 points) Add a main method to RaceCar which creates two RaceCars, races 
them against one another, and prints out the winner's name. When instantiating the 
RaceCar objects, pass one of the static fields of the Color class (like RED or BLUE) as 
the color argument to the constructor. Should the main method be static or non-static? 
Compile and run. 
 
7. (Optional – 2 points) Instead of having a constant top speed for all racecars, change the 
RaceCar class so every car has its own top speed. 
 
8. (Optional – max 3 points) Program a better racing simulation in the race method. 
 
9. CheckOff  
Please ensure that you have the file RaceCar.java in the folder named lab8. These files 
should compile successfully. Failure to compile will automatically cost you 1 point.   
  
If you are ready to be checked off before 5pm, call one of the team members who will 
assign you a grade for the lab. Otherwise, you should leave the required files on your 
machine for us to evaluate at our own convenience.  
 
We will not accept modifications to the required files after the lab is officially due.