Question

In: Computer Science

Consider that time could be internally represented as 3 integers. hour (between 0 - 23) minute...

Consider that time could be internally represented as 3 integers.

  • hour (between 0 - 23)
  • minute (between 0 - 59)
  • second (between 0 - 59)

MyTime class consists of the following members:

  • Default constructor: set the time to 00:00:00
  • Constructor taking 3 integers i.e., hour, minute and second respectively. This constructor sets this object's Time based on the given parameters.
  • Constructor taking 2 integers i.e., hour and minute respectively. This constructor sets this object's hour and minute based on the given parameters. Second will be 0 by default.
  • Constructor taking 1 integer i.e., hour. This constructor sets this object's hour based on the given parameter. Minute and second will be 0 by default.
  • Accessors (getter) for each field.
  • Mutators (setter) for each field. Make sure that each field is set within its proper ranges.
  • to12HourFormat() method
    Returns the string representation of this Time object in 12-hour format.
    For instance, if the time is 13:05:58 in 24-hour format, it returns "01:05:58 PM".
    If the time is "12:05:14", it returns "12:05:14 PM".
    If the time is "00:00:00", it returns "12:00:00 AM".
  • toString() method
    Returns the string representation of time in 24-hour format i.e., HH:MM:SS
    For instance, 13:05:58

Complete the MyTime class definition and write a main program to test your class in Java.

Solutions

Expert Solution


Output:

Code:

import java.io.*;

class MyTime {
  
    //instance variables
    private int hour;
    private int minute;
    private int second;
  
    //default constructor
    MyTime(){
        hour = 0;
        minute = 0;
        second = 0;
    }
  
    //parametrized constructors
    MyTime(int hour, int minute, int second){
        this.setHour(hour);
        this.setMinute(minute);
        this.setSecond(second);
    }
    MyTime(int hour, int minute){
        this.setHour(hour);
        this.setMinute(minute);
        second = 0;
    }
    MyTime(int hour){
        this.setHour(hour);
        minute = 0;
        second = 0;
    }
  
    //getters
    int getHour(){
        return hour;
    }
    int getMinute(){
        return minute;
    }
    int getSecond(){
        return second;
    }
  
    //setters
    //if there is error in value default value is set
    void setHour(int hour){
        if(hour<0 || hour>23)
            this.hour = 0;
        else
            this.hour = hour;
    }
    void setMinute(int minute){
        if(minute<0 || minute>59)
            this.minute = 0;
        else
            this.minute = minute;
    }
    void setSecond(int second){
        if(second<0 || second>59)
            this.second = 0;
        else
            this.second = second;
    }
  
    //12-hour Format
    String to12HourFormat(){
        int h = hour; //modified hour
        boolean am = (hour>=12) ? false : true; //am or pm
        if(hour==0)
            h=12;
        else if(hour>12){
            h = hour - 12;
        }
        MyTime newTime = new MyTime(h, minute, second);
        String result = newTime.toString();
        if(am==true)
            result += " AM";
        else
            result += " PM";
        return result;
    }
  
    public String toString(){
        String time = "";
        if(hour<10) time += "0";
        time += hour+":";
        if(minute<10) time += "0";
        time += minute+":";
        if(second<10) time += "0";
        time += second;
        return time;
    }
  
    //main function to test
   public static void main (String[] args) {
      
       MyTime ob1 = new MyTime(5,36,52);
       MyTime ob2 = new MyTime(23,65,46);
       MyTime ob3 = new MyTime(12,56,23);
       MyTime ob4 = new MyTime(0,46,28);
       MyTime ob5 = new MyTime(13);
       MyTime ob6 = new MyTime(6,28);
      
       System.out.println(ob1);
       System.out.println(ob2.to12HourFormat());
       System.out.println(ob3.to12HourFormat());
       System.out.println(ob4.to12HourFormat());
       System.out.println(ob5);
       System.out.println(ob6);
   }
}


Related Solutions

Consider two sets of integers represented in arrays, X = [x1, x2, . . . ,...
Consider two sets of integers represented in arrays, X = [x1, x2, . . . , xn] and Y = [y1, y2, . . . , yn]. Write two versions of a FindSetUnion(X, Y ) algorithm to find the union of X and Y as an array. An element is in the union of X and Y if it appears in at least one of X and Y . You may make use any algorithm introduced in the lectures to...
At what time past 6:00 do the minute and hour hands overlap ?
At what time past 6:00 do the minute and hour hands overlap ?
At 3:00 the hour hand and the minute hand of a clock point in directions that...
At 3:00 the hour hand and the minute hand of a clock point in directions that are 90° apart. What is the first time after 3:00 that the angle between the two hands has decreased to 26.0°?
The score of a student on a certain exam is represented by a number between 0...
The score of a student on a certain exam is represented by a number between 0 and 1. Suppose that the student passes the exam if this number is at least 0.55. Suppose we model this experiment by a continuous random variable X, the score, whose probability density function is given by f(x) = { x if 0 <= x < 0.5 5x - 2 if 0.5 <= x <= 1 0 otherwise } a. What is the probability that...
Python: Write the function pixelLuminance that takes 3 integers r, g, and b, each between 0...
Python: Write the function pixelLuminance that takes 3 integers r, g, and b, each between 0 and 255 (inclusive), representing the red, green, and blue intensity of a pixel and returns the luminance of this pixel as an integer. The function expects each parameter r, g, and b to be an integer in the interval [0,255]. You should use assertions to enforce this constraint.
2. i. Consider a binary source alphabet where a symbol 0 is represented by 0 volt...
2. i. Consider a binary source alphabet where a symbol 0 is represented by 0 volt and a symbol 1 is represented by 1 volt. Assume these symbols are transmitted over a baseband channel having uniformly distributed noise with a probability density function:         px= {18 for-4≤x≤4 0 Assume that the single decision threshold T is in the range of 0 and l volt. If the symbols 0 and 1 are sent with probabilities p0 and 1- p0 respectively, derive...
At what time past 6:00 do the minute and hour hands overlap? Please show work.
At what time past 6:00 do the minute and hour hands overlap? Please show work.
Q3 Develop a Time class which encapsulates hour ,minute and second data members. Provide overloaded +,...
Q3 Develop a Time class which encapsulates hour ,minute and second data members. Provide overloaded +, -, ++ and - - overloaded operators for Time.
We consider a monocentric city represented by a segment x 2 [0,xf ] where 0 stands...
We consider a monocentric city represented by a segment x 2 [0,xf ] where 0 stands for the CBD where everybody works and earns a uniform (exogenous) urban wage w >0 and xf is the city fringe. The rural wage a is normalized to zero. Agents located at a distance x from the CBD pay a rent R(x). The utility of an agent is her disposable income. 1 Fixed commuting cost 1.1 Private transportation Agents drive to the CBD and...
1. The relation between gram and amu is represented by ____________________________(use NA = 6 x 10*23)...
1. The relation between gram and amu is represented by ____________________________(use NA = 6 x 10*23) 2. The atomic mass of Rubidium is 85, so the mass of one mole of Rubidium is______________________. 3. The atomic mass of silver (Ag) is 108. find the mass of two moles of silver atoms.___________________ 4. The atomic mass of lithium (Li) is 7. the mass of one atom of lithium is ________________________. 5. Given the following atomic masses: N=14; O=16. tha is the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT