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

Hi,

Please find the below output with code:

<!DOCTYPE html>
<html>
<body>

    <h2>JavaScript Class Method</h2>

    <p>How to define and use a Class method.</p>
    <h1> Demo For 24 Hours Time Format</h1>
    =======================================================
    <p id="demo1"></p>
    =======================================================
    <p id="demo2"></p>
    ======================================================
    <p id="demo3"></p>
    =======================================================

    <h1> Demo For 12 Hours Time Format</h1>
    ======================================================
    <p id="demo4"></p>
    =======================================================
    <p id="demo5"></p>
    ======================================================
    <p id="demo6"></p>
    =======================================================

    <script>
        classObject = [];
        //The hour value ranges from 0 to 23
        //0 to 11 represents a time before noon
        //minute value ranges from 0 to 59

        //Class Time
        class Time {

            constructor() {
                this.hour = 0;
                this.minte = 0;
                this.isAM = false;
                classObject = this;
            }

            //Is Valid Method
            isValid(hour, minute) {
                this.hour = hour;
                this.minte = minute;

            }

            //convert time into 12 hour format
            getTime12(hour, minute) {
                var xx = "";
                //Check am and pm
                if (hour >= 1 && hour < 12) {
                    xx = " am";
                }
                else {
                    xx = " pm";
                }

                //Convert 24 hours time into 12 hours
                if (hour == 0) {
                    hour = 12;
                }
                else if (hour >= 12) {
                    hour = hour - 12;
                }

                //If minute digit is 1 i.e. < 9 then prepend 0 in the minute
                if (minute <= 9) {
                    minute = "0" + minute;
                }

                //Return contactinated hours and minute
                return hour + ":" + minute + xx;


            }

            //convert time into 24 hour format
            getTime24(hour, minute) {
                //If hour digit is single ditit the and prepend 0
                if (hour < 10) {
                    hour = "0" + hour;
                }

                //If minute digit is single digit then prepend 0
                if (minute < 10) {
                    minute = "0" + minute;
                }

                //Return contactinated hours and minute
                return hour + "" + minute;

            }


            //Set time method
            setTime(hour, minute, isAM) {
                this.hour = hour;
                this.minte = minute;
                //set am and pm
                if (hour >= 1 && hour < 12) {
                    this.isAM = true;
                }
                else {
                    this.isAM = false;
                }
            }

        }

        //instantiate Class
        let myTime = new Time();

        //declare variable
        var formateDateTime;

        //Check here 24 hours times
        formateDateTime = myTime.getTime24(7, 25);
        document.getElementById("demo1").innerHTML = "Input is - 7,25 for the 24 Hour Format is: Output is -" + formateDateTime + " ";

        formateDateTime = myTime.getTime24(0, 5);
        document.getElementById("demo2").innerHTML = "Input is - 0,5  for the 24 Hour Format is: Output is " + formateDateTime + " ";

        formateDateTime = myTime.getTime24(15, 30);
        document.getElementById("demo3").innerHTML = "Input is - 15,30  for the 24 Hour Format is: Output is " + formateDateTime + " ";

        //Check here 12 hours times
        formateDateTime = myTime.getTime12(7, 25);
        document.getElementById("demo4").innerHTML = "Input is - 7,25  for the 12 Hour Format is: Output is -" + formateDateTime + " ";

        formateDateTime = myTime.getTime12(0, 5);
        document.getElementById("demo5").innerHTML = "Input is - 0,5  for the 12 Hour Format is: Output is " + formateDateTime + " ";

        formateDateTime = myTime.getTime12(15, 30);
        document.getElementById("demo6").innerHTML = "Input is - 15,30  for the 12 Hour Format is: Output is " + formateDateTime + " ";


    </script>

</body>
</html>

Thanks.


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...
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.
How to write a Java program that has a base class with methods and attributes, subclasses...
How to write a Java program that has a base class with methods and attributes, subclasses with methods and attributes. Please use any example you'd like.
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 #:        ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT