Program Target One
Define a class called Odometer that will be used to track fuel and mileage for an automobile.
The class should have instance variables to track the miles driven and the fuel efficiency of the vehicle in miles per gallon.
Include a mutator method to reset the odometer to zero miles, a mutator method to set the fuel efficiency, a mutator method that accepts miles driven for a trip and adds it to the odometer ’ s total, and an accessor method that returns the number of gallons of gasoline that the vehicle has consumed since the odometer was last reset.
Use your class with a test program that creates several trips with different fuel efficiencies.
You should decide which variables should be public, if any.
Program Target Two
Define a class called BlogEntry that could be used to store an entry for a Web
log. The class should have instance variables to store the poster ’ s username,
text of the entry, and the date of the entry using the Date class from this
chapter. Add a constructor that allows the user of the class to set all instance
variables. Also add a method, DisplayEntry , that outputs all of the instance
variables, and another method called getSummary that returns the first 10
words from the text (or the entire text if it is less than 10 words ). Test your class
from your main method.
Program Target Three
Define a class called Counter whose objects count things. An object of this class
records a count that is a nonnegative integer. Include methods to set the
counter to 0, to increase the count by 1, and to decrease the count by 1. Be sure
that no method allows the value of the counter to become negative. Include an
accessor method that returns the current count value and a method that
outputs the count to the screen. There should be no input method or other
mutator methods. The only method that can set the counter is the one that sets
it to 0. Also, include a toString method and an equals method. Write a program
(or programs ) to test all the methods in your class definition.
Program Target Four
Write a Temperature class that has two instance variables: a temperature value
(a floating-point number ) and a character for the scale, either C for Celsius or F
for Fahrenheit. The class should have four constructor methods: one for each
instance variable (assume zero degrees if no value is specified and Celsius if no
scale is specified ), one with two parameters for the two instance variables, and
a no-argument constructor (set to zero degrees Celsius ).
Include the following:
(1 ) two accessor methods to return the temperature — one to return the
degrees Celsius, the other to return the degrees Fahrenheit — use the following
formulas to write the two methods, and round to the nearest tenth of a degree:
DegreesC = 5*(degreesF - 32 )/9
DegreesF = (9*degreesC )/5 + 32;
(2 ) three mutator methods: one to set the value, one to set the scale ( F or C ),
andone to set both;
(3 ) three comparison methods: an equals method to test whether
two temperatures are equal, one method to test whether one temperature is
greaterthan another, and one method to test whether one temperature is less
than another (note that a Celsius temperature can be equal to a Fahrenheit
temperature as indicated by the above formulas );
(4 ) a suitable toString method. Then write a driver program (or programs ) that
tests all the methods. Be sure to use each of the constructors, to include at least
one true and one false case for each of the comparison methods, and to test at
least the following temperature equalities:
0.0 degrees C = 32.0 degrees F
– 40.0 degrees C = – 40.0 degrees F
100.0 degrees C = 212.0 degrees F.
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.