5.1 Write a Java program that divides the number 74.3 by 12.6 and reports the result of the division. Store
the dividend and divisor in variables named dividend and divisor before performing the division.
What will be the type of these variables? What will be the type of the result? What is the quotient?
5.2 Write a Java program to compute the area of a circle whose radius is 5. For the value of PI, use 3.14.
Now rewrite your program so that it uses the very precise value of PI available as a static constant in
the Math class that comes with Java. Here is how you use the Math class constant:
double pi = Math.PI;
How much does your result change?
5.3 Write a Java program that prompts the user for a number, and then tells the user whether the number is
an even multiple of 5. Use Scanner to read the number from the user, and use the modulo operator
(%) to decide whether the number is a multiple of 5.
5.4 Write a Java program that asks a user to enter five Strings, one at a time. Have it save the Strings
in an array of strings. Then have the program display the words in reverse order. Use a for, or a
while, or a do while loop to read in the Strings, and another for, while, or do while loop
to print them out.
5.5 Write a Java program that can categorize vehicles based on the number of wheels the vehicle has.
Your program should prompt the user for the number of wheels on the vehicle, and then read the number
into an int variable. If the user says the vehicle has 2 or 3 wheels, the program will report that it is a
motorcycle, if it has 4 wheels the vehicle will be labeled a “car or light truck,” if it has 6, 8, 10, 12, 14,
16, or 18 wheels, it will be categorized as a truck. Any other number of wheels will be reported as an
error. Use a switch statement to compute the decision.
5.6 Write a Java class called Vehicle. The Vehicle class will have instance attributes for color, make,
model, speed, number of occupants, and maximum number of occupants. The Vehicle class will also
have a static variable called vehicleCount that can be used to track the number of vehicles in the
application. The constructor for Vehicle should expect values for make, model, maximum number of
occupants, and color, and it should set the vehicle speed to zero, the number of occupants to 1, and
increment the count of vehicles each time the constructor is called. Each of the instance and static variables
should have an accessor (get) method that will return the appropriate value, and all except the
vehicleCount variable should also have a mutator (set) method so that the value can be modified.
You should also give the Vehicle class an instance method called changeSpeed. The
changeSpeed method should expect a floating-point value for the new speed, and it should return a
floating-point value representing the difference between the new speed and the previous speed of the
vehicle. Include a public static void main(String[] args) method that creates a few
vehicles, sets some speeds, and reads some variable values, so that you can test your code by launching
the class from the command line.
5.7 Write a Skateboard class that inherits from Vehicle. Override the changeSpeed method for the
Skateboard class, so that instances of the Skateboard class can never exceed 10 mph. If a larger
value is supplied, the method will simply set the speed of the Skateboard to 10.
5.8 Write a Bus class that inherits from Vehicle. An instance of the Bus class must always have a
named driver. In the constructor for a Bus, make sure that your code expects and stores the name of the
driver. Also, the Bus class should have accessor and mutator methods for returning and changing the
name of the driver.
5.9 To the class Vehicle, add a refuel method that expects two parameters, fuelQuantity and
milesSinceLastFueling. Also add instance variables to the Vehicle class for totalMileage
and totalFuelConsumed. Further, add an accessor method called fuelEconomy that will return
the total miles per gallon of the vehicle.
What will you do to make the refuel method work properly when invoked on an instance of
Skateboard? Write a test class called ManyVehicles that creates a variety of different
Vehicles, exercises all the methods you have created, and checks for proper execution. Try to set the
speed of a Skateboard to 60, for example, or to refuel a Skateboard. Check that the fuel economy
calculations are being performed correctly.
5.10 Write a class that extends Exception and is called TooManyOccupantsException. Have the
Vehicle class mutator for number of occupants throw such an exception if the numberOfOccupants
would exceed the maximum number of occupants for the vehicle. What will you need to change in your
ManyVehicles test class?
5.11 Change your ManyVehicles class so that it reads from a text file called Vehicles.txt the specifications
for the Vehicles to create. Use a BufferedReader or a Scanner to read the file. Using a
Scanner is probably easier in this case. Here is a sample Vehicles.txt file. The first word in a
line is the color, the second word in a line is the make, the third word is the model, and the fourth word
is the maximum number of occupants:
red Ford F-150 3
silver BMW 328i 4
blue GM bus 32
gold Chrysler PTCruiser 4
orange WorldIndustries ProBoard 1
5.12 Write a Java program that iterates through the integers from 1 to 20, computing the square of each
number and writing the information to a file called squares.txt. Use a PrintWriter to write the
file of the first 20 integers and their squares. Arrange for two columns with a line of column headings at
the top. You will find this easy to do using the println() method of the PrintWriter.
No comments:
Post a Comment