Answer to Question #321094 in Java | JSP | JSF for dee

Question #321094

consider the time displayed on a digital clock( 07.30.00) with 3 main components hours, minutes and seconds as attributes. Apart from the ordinary instance methods such as getters, setters and constructors, your class should have the following functionality listed below

A) PM) returns true if the hour value is between 12 noon till midnight

B) to 12hourt): converts the provided time to 12 hour format considering the isPM function

C) Add Time() takes in another time and sum them up appropriately considering that there only 24 hours in a day

D) Provide a toString(): to represent the time with AM/PM if it's in 12 hour format 1.1.Provide a simple Class and object diagram for the above scenario

1.2. Implement class modeled above and test its functionality as specified below

Sample Run

Enter time (hh mm):19 24 30

Enter time (hh mm ): 06 15 10

Output:

19.24.30 isPM

06:15:10 isAM

19:24:30 to 12 hour format is 07:24:30 PM

19:24 30 PLUS 06:15:10=01:39:40

The addition result represented with to String: 01:39:40 AM

1
Expert's answer
2022-03-30T15:48:53-0400
import java.util.Scanner;

class TimeClock{
    public int hour, min, sec, hour12;
    public int hourS, minS, secS;
    public String format = "";

    TimeClock(int hour, int min, int sec){
        setHour(hour);
        setMin(min);
        setSec(sec);

        isPM();

        toStrings();
    }

    public void setHour(int hour) {
        if(hour <= 23 & hour >=0){
            this.hour = hour;
        }
        else{
            this.hour = 0;
        }
    }

    public void setMin(int min) {
        if(min <= 60 & min >=0){
            this.min = min;
        }
        else{
            this.min = 0;
        }
    }

    public void setSec(int sec) {
        if(sec <= 60 & sec >=0){
            this.sec = sec;
        }
        else{
            this.sec = 0;
        }
    }

    public boolean isPM(){
        if(hour > 12){
            format = "PM";
            return true;
        } else {
            format = "AM";
        }
        return false;
    }

    public void to12hour(){
        if(isPM()){
            hour12 = hour%12;
        }
    }

    public void AddTime(TimeClock a){
        String form;
        secS = (a.sec + sec)%60;
        minS = ((a.min + min) + ((a.sec + sec)/60))%60;
        hourS = ((a.hour + hour) + ((a.min + min) + ((a.sec + sec)/60))/60)%24;
        if(hourS > 12){
            form = "PM";
        } else {
            form = "AM";
        }
        System.out.println(hour + ":" + min + ":" + sec + " PLUS " + a.hour + ":" + a.min + ":" + a.sec + " = " + hourS + ":" + minS + ":" + secS);
        System.out.printf("The addition result represented with to String: %d:%d:%d", hourS, minS, secS);
        System.out.println(form);
    }

    public void toStrings() {
        System.out.printf("%d.%d.%d", hour, min, sec);
        System.out.println("is" + format);

    }
}


public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        System.out.print("Enter time (hh mm): ");
        String str1 = in.nextLine();
        String[] str2 = str1.split(" ");
        int hour = Integer.parseInt(str2[0]);
        int min = Integer.parseInt(str2[1]);
        int sec = Integer.parseInt(str2[2]);

        System.out.print("Enter time (hh mm): ");
        String str11 = in.nextLine();
        String[] str21 = str11.split(" ");
        int hour1 = Integer.parseInt(str21[0]);
        int min1 = Integer.parseInt(str21[1]);
        int sec1 = Integer.parseInt(str21[2]);

        TimeClock timeClock1 = new TimeClock(hour, min, sec);
        TimeClock timeClock2 = new TimeClock(hour1, min1, sec1);

        timeClock1.to12hour();
        timeClock1.AddTime(timeClock2);

    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS