Question

In: Computer Science

Write a class called Time that represents the time of the day. It has attributes for...

Write a class called Time that represents the time of the day. It has attributes for the hour and minute. The hour value ranges from 0 to 23, where the range 0 to 11 represents a time before noon. The minute value ranges from 0 to 59.

  • Write a default constructor that initializes the time to 0 hours and 0 minutes.
  • Write a private method isValid(hour, minute) that returns true if the given hour and minute values are in the appropriate range.
  • Write a method setTime(hour, minute) that sets the time if the given values are valid.
  • Write another method setTime(hour, minute, isAM) that sets the time if the given values are valid. The given hour should be in the range 1 to 12. The parameter isAM is true if the time is an a.m. time and false otherwise.
  • Add two more constructors that are similar to the setTime methods described.
  • Add a method getTime24 that returns a string that gives the time in 24 hour notation hhmm. For example,
    • If the hour value is 7 and the minute value is 25, return “’0725”.
    • If the hour value is 0 and the minute value is 5, return “0005”.
    • If the hour value is 15 and the minute value is 30, return “1530”.
  • Add a method getTime12 returns a string that gives the time in 12-hour notation h:mm xx. For example
    • If the hour value is 7 and the minute value is 25, return “’7:25 am”.
    • If the hour value is 0 and the minute value is 5, return “12:05 am”.
    • If the hour value is 15 and the minute value is 30, return “3:30 pm”.

Write a driver program to test your class and call it  testTime.. Make sure to test all the methods

Use JavaScript language

Solutions

Expert Solution

class Time{
    private int hour,minute;
    
    private boolean isValid(int hour,int minute){
        return hour>=0 && hour<24 && minute>=0 && minute<60; 
    }
    
    public Time(){
        this.hour=0;
        this.minute=0;
    }
    
    public Time(int hour,int minute){
        if (isValid(hour,minute)){
            this.hour=hour;
            this.minute=minute;
        } 
        else
            System.out.println("Invalid Time");
    }
    
    public Time(int hour,int minute,boolean isAM){
        if(hour>0 && hour<=12 && minute>=0 && minute<60){
            if(hour==12){
                hour=0;
            }
            if(!isAM){
                hour+=12;
            }
            this.hour=hour;
            this.minute=minute;
        }
        else 
            System.out.println("Invalid Time");
    }   
    
    public void setTime(int hour,int minute){
        if (isValid(hour,minute)){
            this.hour=hour;
            this.minute=minute;
        } 
        else
            System.out.println("Invalid Time");
    }   
    
    public void setTime(int hour,int minute,boolean isAM){
        if(hour>0 && hour<=12 && minute>=0 && minute<60){
            if(hour==12){
                hour=0;
            }
            if(!isAM){
                hour+=12;
            }
            this.hour=hour;
            this.minute=minute;
        }
        else 
            System.out.println("Invalid Time");
    }  
    public String getTime24(){
        String s="";
        if(hour<10)
            s+='0';
        s+=hour;
        if(minute<10)
            s+=0;
        s+=minute;
        return s;
    }
    public String getTime12(){
        boolean isAM=true;
        int h=hour;    //declared a variabe h to store hours so that we can change the hours inside the function and not change the value of the object
        if(h==0){
            h=12;
        }
        if(h>12){
            h-=12;
            isAM=false;
        }
        String s="";
        if(h<10)
            s+='0';
        s+=h;
        s+=':';
        if(minute<10)
            s+='0';
        s+=minute;
        if(isAM)
            s+=" AM";
        else
            s+=" PM";
        return s;
    }
}
public class TestTime 
{
        public static void main(String[] args) {
                Time t=new Time();
                t.setTime(16,34);
                System.out.println(t.getTime12());
                System.out.println(t.getTime24());
                t.setTime(4,34,false);
                System.out.println(t.getTime12());
                System.out.println(t.getTime24());
                
                t=new Time(13,43);
                System.out.println(t.getTime24());
                System.out.println(t.getTime12());
                
                t=new Time(7,45,true);
                System.out.println(t.getTime24());
                System.out.println(t.getTime12());
                
        }
}

Related Solutions

Write a class called Time that represents the time of the day. It has attributes for...
Write a class called Time that represents the time of the day. It has attributes for the hour and minute. The hour value ranges from 0 to 23, where the range 0 to 11 represents a time before noon. The minute value ranges from 0 to 59. Write a default constructor that initializes the time to 0 hours and 0 minutes. Write a private method isValid(hour, minute) that returns true if the given hour and minute values are in the...
Task1: Write a class DynArr that represents an array data structure. Here are the attributes (data)...
Task1: Write a class DynArr that represents an array data structure. Here are the attributes (data) of the class (note - fields of the objects should be marked as private)” INITIAL_SIZE: the initial size of the array = 10; _innerArr: an array of integers _growthFactor: the factor by which to resize the array with to become bigger. _lastIndex: an index on the last element in the array. The class will have the constructor public DynArr() that initialzes an array of...
Write the definition for a generic / Template class called time that has hours and minutes...
Write the definition for a generic / Template class called time that has hours and minutes as structure. The class has the following member functions: SetTime to set the specified value in object ShowTime to display time object Sum to sum two time object & return time Write the definitions for each of the above member functions. Write main function to create three time objects. Set the value in two objects and call sum() to calculate sum and assign it...
#Write a class called "Burrito". A Burrito should have the #following attributes (instance variables): # #...
#Write a class called "Burrito". A Burrito should have the #following attributes (instance variables): # # - meat # - to_go # - rice # - beans # - extra_meat (default: False) # - guacamole (default: False) # - cheese (default: False) # - pico (default: False) # - corn (default: False) # #The constructor should let any of these attributes be #changed when the object is instantiated. The attributes #with a default value should be optional. Both positional #and...
1. Write a class called Rectangle that maintains two attributes to represent the length and width...
1. Write a class called Rectangle that maintains two attributes to represent the length and width of a rectangle. Provide suitable get and set methods plus two methods that return the perimeter and area of the rectangle. Include two constructors for this class. One a parameterless constructor that initializes both the length and width to 0, and the second one that takes two parameters to initialize the length and width. 2. Write a java program (a driver application) that tests...
Write a Class called Module with the following attributes: module code, module name, list of lecturers...
Write a Class called Module with the following attributes: module code, module name, list of lecturers for the module (some modules may have more than one lecturer – we only want to store their names), number of lecture hours, and module description. Create a parameterised (with parameters for all of the class attributes) and a non-parameterised constructor, and have the accessor and mutator methods for each attribute including a toString method. Write a class Student with the following attributes: student...
Java Write a class called Car that contains instance data that represents the make, model, and...
Java Write a class called Car that contains instance data that represents the make, model, and year of the car. Define the Car constructor to initialize these values Include getter and setter methods for all instance data and a toString method that returns a one-line description of the car. Add a method called isAntique that returns a boolean indicating if the car is an antique (if it is more than 45 years old). Create a driver class called CarTest, whose...
In java beginner coding language ,Write a class called Sphere that contains instance data that represents...
In java beginner coding language ,Write a class called Sphere that contains instance data that represents the sphere’s diameter. Define the Sphere constructor to accept and initialize the diameter, and include getter and setter methods for the diameter. Include methods that calculate and return the volume and surface area of the sphere. Include a toString method that returns a one-line description of the sphere. Create a driver class called MultiSphere, whose main method instantiates and updates several Sphere objects.
Write a java class called circle that represents a circle. It should have following three fields:...
Write a java class called circle that represents a circle. It should have following three fields: int x (x-coordinate), int y (y-coordinate), double radius (radius of the circle). Your circle object should have following methods: public int getRadius () public int getX () public int getY () public double area () public double perimeter() public String toString() write a client program called CircleClient that creates objects of the circle class called c1 and c2. Assign values to the fields when...
using Java Implement a class called Binomial_Coefficient o Your class has 2 int attributes, namely K...
using Java Implement a class called Binomial_Coefficient o Your class has 2 int attributes, namely K and n o Create an overloaded constructor to initialize the variables into any positive integers such that n > k > 0o Create a method that calculates the value of binomial coefficient, C(n,k) , using the following rule: ▪ For an array of size (n+1) X (k+1) ▪ Array [n][k] = Array[n-1][ k-1]+ Array[n-1] [k]▪ Array[n][0]= Array[n][n] = 1 ▪ Hence, C(n,k) = array...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT