Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

NOTE: DO NOT USE FLOAT DATA TYPE, OR USE THE EXPRESSION \"?\" WHILE DOING THE CO

ID: 3836040 • Letter: N

Question

NOTE: DO NOT USE FLOAT DATA TYPE, OR USE THE EXPRESSION "?" WHILE DOING THE CODE!!

NOTE2: CREATE A CLASS (you can use only import java.util.Scanner;) !!!

Problem 2: (7 marks) public class Time Three instance variables: 1) hour: value of a hours as integer 2) min': value of a minutes as integer 3) sec' value of a seconds as integer A three-argument constructor to initialize all instance variables with values given as parameters if they are valid values using 24 hours format A mutator method that sets the 'sec' field to a value given as a parameter, but only if the given value is valid AtoString method that returns time as 12 hours format An method that increments time by a positive number of seconds given as a parameter only if value is less than or equal 59

Explanation / Answer

Below the the Class Time which has all the specified functions

Time :


public class Time {
  
int hour=-1;
int min=-1;
int sec=-1;
  
Time(int _hour , int _min , int _sec){
if((hour <24) && (min < 60) && (sec <60)){
this.hour = _hour;
this.min = _min;
this.sec = _sec;
}else{
System.out.println("Please Entre Valid Time");
}
  
}
  
public boolean setSeconds(int seconds){
if((hour <24) && (min < 60) && (sec <60)){
if( seconds >=0 && seconds <= 59 ){
sec = seconds;
return true;
}else{
System.out.println("Please entre a value seconds Value");
return false;
}
}
System.out.println("Please Set a Valid Time before setting new Seconds value");
return false;
}
  
public String toString(){
  
if(hour < 0 || min < 0 || sec < 0){
  
return "Valid time Not Set";
}
String meridiem = "AM";
if(hour>=12){
meridiem = "PM";
}
return String.format("%2d:%2d:%2d %s",hour%12,min,sec,meridiem);
}
  
public boolean incrementTime(int seconds){
if(sec < 0 ){
System.out.println("Please Set the time before incrementing");
return false;
}
if( seconds >=0 && seconds <= 59 ){
sec = sec + seconds;
if(sec > 59){
sec = sec%60;
min++;
if(min > 59){
min=0;
hour++;
if(hour > 23){
hour = 0;
}
}
}
return true;
}
System.out.println("Please enter a value between 1 and 59");
return false;
}
  
}

Below The class Main which invokes all the method the Time Class

Main :

import java.util.Scanner;


public class Main {
  
static Time time ;
  
public static void main(String[] args){
  
while(true){
System.out.println("Select any one option");
System.out.println("1. Set new Time");
System.out.println("2. Set Seconds Value");
System.out.println("3. View the Time");
System.out.println("4. Increment the Time with Seconds");
System.out.println("5. Exit");
  
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
  
switch(i){
  
case 1 :
System.out.println("Enter he Hours Value:");
sc = new Scanner(System.in);
int tempHour = sc.nextInt();
System.out.println("Enter the Minutes Value:");
sc = new Scanner(System.in);
int tempMin = sc.nextInt();
System.out.println("Enter the Seconds Value:");
sc = new Scanner(System.in);
int tempSec = sc.nextInt();
time = new Time(tempHour,tempMin,tempSec);
break;
case 2 :
if(time == null){
System.out.println("Please Set a Valid Time before setting new Seconds value");
continue;
}
  
System.out.println("Enter the Seconds Value to be set");
sc = new Scanner(System.in);
i = sc.nextInt();
  
time.setSeconds(i);
break;
case 3 :
if(time == null){
System.out.println("Please Set a Valid Time ");
continue;
}
System.out.println(time.toString());
break;
case 4 :
if(time == null){
System.out.println("Please Set a Valid Time");
continue;
}
System.out.println("Enter the Seconds Value to be increment");
sc = new Scanner(System.in);
i = sc.nextInt();
time.incrementTime(i);
break;
case 5 :
System.out.println("Thank You ...!");
System.exit(0);
break;
default :
System.out.println("Invalid Input");
break;
  
}
}
}
}

Output :

Select any one option
1. Set new Time
2. Set Seconds Value
3. View the Time
4. Increment the Time with Seconds
5. Exit
1
Enter he Hours Value:
12
Enter the Minutes Value:
35
Enter the Seconds Value:
26
Select any one option
1. Set new Time
2. Set Seconds Value
3. View the Time
4. Increment the Time with Seconds
5. Exit
3
0:35:26 PM
Select any one option
1. Set new Time
2. Set Seconds Value
3. View the Time
4. Increment the Time with Seconds
5. Exit
2
Enter the Seconds Value to be set
12
Select any one option
1. Set new Time
2. Set Seconds Value
3. View the Time
4. Increment the Time with Seconds
5. Exit
3
0:35:12 PM
Select any one option
1. Set new Time
2. Set Seconds Value
3. View the Time
4. Increment the Time with Seconds
5. Exit
4
Enter the Seconds Value to be increment
50
Select any one option
1. Set new Time
2. Set Seconds Value
3. View the Time
4. Increment the Time with Seconds
5. Exit
3
0:36: 2 PM
Select any one option
1. Set new Time
2. Set Seconds Value
3. View the Time
4. Increment the Time with Seconds
5. Exit
5
Thank You ...!