Results 1 to 2 of 2

Thread: Little Help ---

  1. #1
    SitnOnGreen is offline Member
    Join Date
    Nov 2007
    Posts
    1
    Rep Power
    0

    Wink Little Help ---

    Heres this homework assignment -- I'm pretty confused on it -- anyone know how to do it?

    Define a class called Time to represent 24-hour clock time values consisting of hour and minute components. The class should define the following private instance variables:
    1. hour (integer), and
    2. minute (integer).

    Define the following public instance methods for the class
    1. Mutator method setTime(): accepts 2 input parameters representing an hour and a minute values. The method should verify that the passed values are valid, i.e. the hour is between 0 and 23, and the minute is between 0 and 59. If the values are valid, they should be assigned to the corresponding instance variables of the calling object and the method should return true. Otherwise, the method should return false, leaving the instance variables unchanged.
    2. Accessor method getHour(): returns the value of the hour instance variable of the calling object.
    3. Accessor method getMinute(): returns the value of the minute instance variable of the calling object.
    4. getAMPMtime(): returns a String, representing the time value stored in a calling object in the AM/PM format. For example, if the calling objectís hour is 14 and minute is 45, the method should return "2:45 PM". If the calling objectís hour is 10 and minute is 14, the method should return "10:14 AM".

    Your class and method names should be exactly as specified above. Be sure to match capitalization precisely.

    Furthermore, create a separate class called UseTime that will be used for testing the functionality of the Time class. The UseTime class will consist of a single method - main(). The main method must
    1. Prompt the user to enter a time string in the format "hh:mm".
    2. Extract the hour and the minute values from the time string.
    3. Create a new object of class Time and call the setTime() method to set its instance variables to the hour and minute values obtained from the user. If the value returned by the setTime() method is false, display an appropriate message and continue asking the user to enter time strings, until setTime() returns true.
    4. Call methods getHour() and getMinute() and print their return values, as shown in line 7 of the sample interaction below.
    5. Print out the string representing the time value in the AM/PM format, using the result returned by getAMPMtime() method.

    Here is a sample interaction.

    Please enter a time string in the format "hh:mm": 17:70
    This time string contains an invalid hour or minute component.
    Please enter a time string in the format "hh:mm": 27:07
    This time string contains an invalid hour or minute component.
    Please enter a time string in the format "hh:mm": 17:20
    Hour is 17, minute is 20.
    Thatís 5:20 PM.

    Be sure to have your Time and UseTime classes defined in the same project. When running your program in Eclipse, pick UseTime as the main class.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Step one:
    Java Code:
    public class UseTime {
        public static void main(String[] args) {
            Time time = new Time();
            // When you get the Time class built up start
            // working on the user input part and associated
            // logic for/in this class.
            // Add it step-by-step and compile after each
            // step/addition.
        }
    }
    
    class Time {
        // Build this up one step at a time.
        // Go down the list in the assignment step-by-step.
        // Compile after each step.
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •