Needs to be coded in Java part 1 Make a class to simulate a
Needs to be coded in Java :
part 1:
Make a class to simulate a 12-hour clock (unfortunately you must name this your UMN ID rather than “Clock”). For this class, you must implement three methods:
1. A constructor that takes in 3 arguments: hours, minutes and am/pm (as a string). You may assume hours <= 12 and minutes <= 60 when starting the clock.
2. A getTime() method that returns a String telling the current time.
3. An advance() method that moves the clock forward a specified amount of hours and minutes. For this, hours and minutes can be any positive number.
After making these methods, ask the user to enter the current time, display the time, then ask how much to move the time forward and finally display the new time. You may assume the user will only enter integers for hours/minutes and either “am” or “pm” to specify which part of the day.
Hint: Just because we want to show hours/minutes/am or pm, doesn\'t mean you need to store the information in this way. You can store the actual time however you feel is easiest to use.
Example 1 (user input is bolded):
Enter current time: 12 30 am
12:30am
How much do you wish to advance the time by?
6 20
6:50am
Example 2 (user input is underlined): Enter current time: 12 00 pm
12:0pm
How much do you wish to advance the time by?
1 5
1:5pm
Part 2:
Extend the clock from part 1 to have an additional “alarm” feature. Add to your class the following:
1. A “Clock” variable (the class you built for part A) to store the alarm time (Hint: you can re-use your constructor).
2. A boolean to tell whether or not the alarm is set.
Modify your main() to ask to set the alarm between the initial clock setup and the time advance. If the time advance goes over alarm time, you should show the alarm going off. The alarm cannot go off more than once.
Hint: it might be easier to rewrite your advance() method differently to be easier for the alarm (don\'t try to get too fancy).
Example : (user input is bolded):
Enter current time:
12 30 am
12:30am
What time do you want to set the alarm?
3 30 am
How much do you wish to advance the time by?
3 0
*Bzzt* *Bzzt* at 3:30am
3:30am
Solution
Solution for Part 1
import java.util.*;
import java.lang.*;
public class clock
{
public clock(String HH,String MM,String AMPM)
{
System.out,println(\"Enter Current time:\ \");
Scanner Sc =new Scanner(System.in):
String time =null;
time = Sc.nextLine();
String[] Time = time.split(\"\\\\s+\");
HH=Integer.parseInt(Time[0]);
MM=Integer.parseInt(Time[1]);
AMPM=Time[2];
String CurrTime = getTime();
System.out.println(CurrTime);
}
public static String getTime()
{
String tme = HH+\":\"+MM+AMPM;
return tme;
}
public static void advane()
{
System.out.println(\"How much do you wish to advance time by : \");
int hh,mm; String[] s;
Scanner sc = new Scanner(System.in);
s=.sc.nextLine().split(\"\\\\s+\");
hh=Integer.parseInt(s[0]);
mm=Integer.parseInt(s[1]);
if(hh<=24 && mm <=60)
{
hh=HH+hh;
mm=MM+mm;
if(mm>60)
{
mm=mm-60;
}
if(hh>12)
{
hh=hh-12;
System.out.println(hh+mm+\"PM\")
}
else if(hh<=12)
{
System.out.println(hh+MM+\"AM\")
} }
else
{ System.out.println(\"Please enter proper range of time to advance\ \"); return; }
}
public static void main(String[] args)
{
static int HH=0,MM=0;
static String AMPM=null;
clock TIME = new clock(HH,MM,AMPM);
}
}
Solution for Part 2 using above class
import java.util.*;
import java.lang.*;
public class clock
{
public clock(String HH,String MM,String AMPM)
{
System.out,println(\"Enter Current time:\ \");
Scanner Sc =new Scanner(System.in):
String time =null;
time = Sc.nextLine();
String[] Time = time.split(\"\\\\s+\");
HH=Integer.parseInt(Time[0]);
MM=Integer.parseInt(Time[1]);
AMPM=Time[2];
String CurrTime = getTime();
System.out.println(CurrTime);
System.out.println(\"What time you want to set the alarm: \");
String[] Clock = Sc.nextLine().split(\"\\\\s+\"); // Using new String object to store the Clock alarm data
int alarmhh=Integer.parseInt(Clock[0]); // Parsing the string and converting to Integer
int alarmmm=Integer.parseInt(Clock[1]);
String ampm = Clock[2];
boolean value = advance(alarmhh,alarmmm,ampm);//Calling the advance method with entered values as //arguments to set the alarm.
System.out.println(\"Alarm Set : \"+ value);
}
public static String getTime() // Method to Get the current time
{
String tme = HH+\":\"+MM+AMPM;
return tme;
}
public boolean void advane(int alarmhh,int alarmmm,int ampm)
{
System.out.println(\"How much do you wish to advance time by : \");
int hh,mm; String[] s;
Scanner sc = new Scanner(System.in);
s=.sc.nextLine().split(\"\\\\s+\");
hh=Integer.parseInt(s[0]);
mm=Integer.parseInt(s[1]);
if(hh<=24 && mm <=60)
{
hh=HH+hh;
mm=MM+mm;
if(mm>60)
{
mm=mm-60;
}
if(hh>12)
{
hh=hh-12;
System.out.println(hh+mm+\"PM\")
}
else if(hh<=12)
{
System.out.println(hh+MM+\"AM\")
} }
else
{ System.out.println(\"Please enter proper range of time to advance\ \"); return; }
if(alarmhh <= hh && alarmmm <=mm )
{
System.out.println(\"*Bzzt* *Bzzt* at \"+alarmhh+\":\"+alarmmm+amapm);
return true;
}
else return false;
}
}
public static void main(String[] args)
{
static int HH=0,MM=0;
static String AMPM=null;
clock TIME = new clock(HH,MM,AMPM);
}
}





