V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
chenhui7373
V2EX  ›  问与答

[速测 JAVA 功底系列] Java 程序 - Odometer ...

  •  
  •   chenhui7373 · 2015-09-08 08:59:51 +08:00 · 1555 次点击
    这是一个创建于 3360 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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.

    2 条回复    2015-09-08 09:13:43 +08:00
    Comdex
        1
    Comdex  
       2015-09-08 09:05:53 +08:00
    啥玩意?
    qping
        2
    qping  
       2015-09-08 09:13:43 +08:00
    速测英文功底
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2996 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 14:43 · PVG 22:43 · LAX 06:43 · JFK 09:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.