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 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 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 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...
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...
Using python class: Design a class called Account CLASS NAME:    Account ATTRIBUTES: -nextAcctID: int    #NOTE-...
Using python class: Design a class called Account CLASS NAME:    Account ATTRIBUTES: -nextAcctID: int    #NOTE- class-level attribute initialized to 1000 -acctID: int -bank: String -acctType: String (ex: checking, savings) -balance: Float METHODS: <<Constructor>>Account(id:int, bank: String, type:String, bal:float) +getAcctID(void): int                        NOTE: retrieving a CLASS-LEVEL attribute! -setAcctID(newID: int): void           NOTE: PRIVATE method +getBank(void): String +setBank(newBank: String): void +getBalance(void): float +setBalance(newBal: float): void +str(void): String NOTE: Description: prints the information for the account one item per line. For example: Account #:        ...
Write a class encapsulating a board game. A board game has the following additional attributes: the...
Write a class encapsulating a board game. A board game has the following additional attributes: the number of players and whether the game can end in a tie. Code the constructor and the toString method of the new class. You also need to include a client class(with the main method) to test your code. code in Python
Write in Java the following: A.  A bank account class that has three protected attributes: account number...
Write in Java the following: A.  A bank account class that has three protected attributes: account number (string), customer name (string), and balance (float). The class also has a parameterized constructor and a method public void withDraw (float amount) which subtracts the amount provided as a parameter from the balance. B. A saving account class that is a child class of the bank account class with a private attribute: penality rate (float). This class also has a parameterized constructor and a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT