SlideShare a Scribd company logo
1 of 9
Download to read offline
//Clock.java
public class Clock {
//3 private ints to represent hours, minutes, and seconds.
int hours;
int minutes;
int seconds;
//a default constructor that always initializes the hour/minute/second to 0,0,0 (midnight).
public Clock()
{
hours=0;
minutes=0;
seconds=0;
}
/*
a parameterized constructor that receives the hour, minute, second as 3 ints and sets
the values in the instance to whatever is received.
*/
public Clock(int h, int m, int s)
{
/*If the hour is illegal (negative or > 23) or if the minute or the second are illegal (negative or >
59),
use the following statement to throw an exception: throw new IllegalArgumentException*/
if(h>23 || m>59 || s>59)
throw new IllegalArgumentException("Wrong time to set") ;
hours=h;
minutes=m;
seconds=s;
}
//copy constructor
public Clock(Clock C)
{
hours=C.hours;
minutes=C.minutes;
seconds=C.seconds;
}
//adds numSeconds to how many seconds it has (and fixes the hour/min/sec
//so that they “carry” to the next unit if possible)
void addSeconds(int numSeconds)
{
seconds+=numSeconds;
if(seconds>60)
{
minutes+=seconds/60;
seconds%=60;
if(minutes>60)
{
hours+=minutes/60;
minutes%=60;
}
}
}
/*
Tick : increments itself by 1 second (and fixes the hour/min/sec so they “carry” to the next unit
if possible).
*/
void tick()
{
seconds+=1;
if(seconds>60)
{
minutes+=seconds/60;
seconds%=60;
if(minutes>60)
{
hours+=minutes/60;
minutes%=60;
}
}
}
// returns true if it is “night” (between 8PM and 5AM inclusive), returns false otherwise.
boolean isNight()
{
if((hours>=20 && hours <=23)||(hours>=0 && hours<=5))
{
return true;
}
else
return false;
}
//returns a representation of the current instance in the form hh:mm:ss AMorPM
public String toString()
{
String time="";
String ampm="";
if(hours == 0)
{
time=time +"12";
ampm="AM";
}
else if( hours>=1 && hours<=11)
{
time=time+ hours;
ampm="AM";
}
else if(hours == 12)
{
time=time+"12";
ampm="PM";
}
else if(hours>=13 && hours<=23)
{
time=time+(hours-12);
ampm="PM";
}
if(minutes<10)
{
time+=":0"+minutes;
}
else
{
time+=":"+minutes;
}
if(seconds<10)
{
time+=":0"+seconds;
}
else
{
time+=":"+seconds;
}
time+=" "+ampm;
return time;
}
}
///////////////////////////////////////////////////////
//driver
public class ClockDriver {
public static void main(String[] args) {
try{
Clock clock1=new Clock(13,29,45);
System.out.println("clock1: "+clock1.toString());
clock1.addSeconds(34);
System.out.println("clock1 after adding 34 seconds : "+clock1.toString());
clock1.tick();
System.out.println("clock1 after tick : "+clock1.toString());
Clock clock2=new Clock(25,45,50);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
Solution
//Clock.java
public class Clock {
//3 private ints to represent hours, minutes, and seconds.
int hours;
int minutes;
int seconds;
//a default constructor that always initializes the hour/minute/second to 0,0,0 (midnight).
public Clock()
{
hours=0;
minutes=0;
seconds=0;
}
/*
a parameterized constructor that receives the hour, minute, second as 3 ints and sets
the values in the instance to whatever is received.
*/
public Clock(int h, int m, int s)
{
/*If the hour is illegal (negative or > 23) or if the minute or the second are illegal (negative or >
59),
use the following statement to throw an exception: throw new IllegalArgumentException*/
if(h>23 || m>59 || s>59)
throw new IllegalArgumentException("Wrong time to set") ;
hours=h;
minutes=m;
seconds=s;
}
//copy constructor
public Clock(Clock C)
{
hours=C.hours;
minutes=C.minutes;
seconds=C.seconds;
}
//adds numSeconds to how many seconds it has (and fixes the hour/min/sec
//so that they “carry” to the next unit if possible)
void addSeconds(int numSeconds)
{
seconds+=numSeconds;
if(seconds>60)
{
minutes+=seconds/60;
seconds%=60;
if(minutes>60)
{
hours+=minutes/60;
minutes%=60;
}
}
}
/*
Tick : increments itself by 1 second (and fixes the hour/min/sec so they “carry” to the next unit
if possible).
*/
void tick()
{
seconds+=1;
if(seconds>60)
{
minutes+=seconds/60;
seconds%=60;
if(minutes>60)
{
hours+=minutes/60;
minutes%=60;
}
}
}
// returns true if it is “night” (between 8PM and 5AM inclusive), returns false otherwise.
boolean isNight()
{
if((hours>=20 && hours <=23)||(hours>=0 && hours<=5))
{
return true;
}
else
return false;
}
//returns a representation of the current instance in the form hh:mm:ss AMorPM
public String toString()
{
String time="";
String ampm="";
if(hours == 0)
{
time=time +"12";
ampm="AM";
}
else if( hours>=1 && hours<=11)
{
time=time+ hours;
ampm="AM";
}
else if(hours == 12)
{
time=time+"12";
ampm="PM";
}
else if(hours>=13 && hours<=23)
{
time=time+(hours-12);
ampm="PM";
}
if(minutes<10)
{
time+=":0"+minutes;
}
else
{
time+=":"+minutes;
}
if(seconds<10)
{
time+=":0"+seconds;
}
else
{
time+=":"+seconds;
}
time+=" "+ampm;
return time;
}
}
///////////////////////////////////////////////////////
//driver
public class ClockDriver {
public static void main(String[] args) {
try{
Clock clock1=new Clock(13,29,45);
System.out.println("clock1: "+clock1.toString());
clock1.addSeconds(34);
System.out.println("clock1 after adding 34 seconds : "+clock1.toString());
clock1.tick();
System.out.println("clock1 after tick : "+clock1.toString());
Clock clock2=new Clock(25,45,50);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}

More Related Content

Similar to Clock.java public class Clock { 3 private ints to represent .pdf

Modify the Time classattached to be able to work with Date.pdf
Modify the Time classattached to be able to work with Date.pdfModify the Time classattached to be able to work with Date.pdf
Modify the Time classattached to be able to work with Date.pdfaaseletronics2013
 
Csphtp1 08
Csphtp1 08Csphtp1 08
Csphtp1 08HUST
 
C++ C++ C++ In Chapter 1- the class clockType was designed to implem.docx
C++ C++ C++   In Chapter 1- the class clockType was designed to implem.docxC++ C++ C++   In Chapter 1- the class clockType was designed to implem.docx
C++ C++ C++ In Chapter 1- the class clockType was designed to implem.docxCharlesCSZWhitei
 
How to Create a Countdown Timer in Python.pdf
How to Create a Countdown Timer in Python.pdfHow to Create a Countdown Timer in Python.pdf
How to Create a Countdown Timer in Python.pdfabhishekdf3
 
Timers in Unix/Linux
Timers in Unix/LinuxTimers in Unix/Linux
Timers in Unix/Linuxgeeksrik
 
#include iostream #include string #include invalidHr.h.pdf
 #include iostream #include string #include invalidHr.h.pdf #include iostream #include string #include invalidHr.h.pdf
#include iostream #include string #include invalidHr.h.pdfAPMRETAIL
 
Contiki os timer tutorial
Contiki os timer tutorialContiki os timer tutorial
Contiki os timer tutorialSalah Amean
 
Import java
Import javaImport java
Import javaheni2121
 
Integration Project Inspection 3
Integration Project Inspection 3Integration Project Inspection 3
Integration Project Inspection 3Dillon Lee
 

Similar to Clock.java public class Clock { 3 private ints to represent .pdf (12)

Modify the Time classattached to be able to work with Date.pdf
Modify the Time classattached to be able to work with Date.pdfModify the Time classattached to be able to work with Date.pdf
Modify the Time classattached to be able to work with Date.pdf
 
Csphtp1 08
Csphtp1 08Csphtp1 08
Csphtp1 08
 
C++ C++ C++ In Chapter 1- the class clockType was designed to implem.docx
C++ C++ C++   In Chapter 1- the class clockType was designed to implem.docxC++ C++ C++   In Chapter 1- the class clockType was designed to implem.docx
C++ C++ C++ In Chapter 1- the class clockType was designed to implem.docx
 
How to Create a Countdown Timer in Python.pdf
How to Create a Countdown Timer in Python.pdfHow to Create a Countdown Timer in Python.pdf
How to Create a Countdown Timer in Python.pdf
 
Timers in Unix/Linux
Timers in Unix/LinuxTimers in Unix/Linux
Timers in Unix/Linux
 
#include iostream #include string #include invalidHr.h.pdf
 #include iostream #include string #include invalidHr.h.pdf #include iostream #include string #include invalidHr.h.pdf
#include iostream #include string #include invalidHr.h.pdf
 
Contiki os timer tutorial
Contiki os timer tutorialContiki os timer tutorial
Contiki os timer tutorial
 
Object - Based Programming
Object - Based ProgrammingObject - Based Programming
Object - Based Programming
 
Import java
Import javaImport java
Import java
 
Integration Project Inspection 3
Integration Project Inspection 3Integration Project Inspection 3
Integration Project Inspection 3
 
I os 06
I os 06I os 06
I os 06
 
Class ‘increment’
Class ‘increment’Class ‘increment’
Class ‘increment’
 

More from anubhavnigam2608

1.KClO(3) ---- Potassium chlorate 2.H(2)SO(2) ------ sulphurous acid.pdf
  1.KClO(3) ---- Potassium chlorate 2.H(2)SO(2) ------ sulphurous acid.pdf  1.KClO(3) ---- Potassium chlorate 2.H(2)SO(2) ------ sulphurous acid.pdf
1.KClO(3) ---- Potassium chlorate 2.H(2)SO(2) ------ sulphurous acid.pdfanubhavnigam2608
 
Seven Requirements of CTC 1 Age 2 Relationship .pdf
     Seven Requirements of CTC           1 Age     2 Relationship    .pdf     Seven Requirements of CTC           1 Age     2 Relationship    .pdf
Seven Requirements of CTC 1 Age 2 Relationship .pdfanubhavnigam2608
 
the solvents that are used to combine together to.pdf
                     the solvents that are used to combine together to.pdf                     the solvents that are used to combine together to.pdf
the solvents that are used to combine together to.pdfanubhavnigam2608
 
The most simple property to calculate this is Ka.pdf
                     The most simple property to calculate this is Ka.pdf                     The most simple property to calculate this is Ka.pdf
The most simple property to calculate this is Ka.pdfanubhavnigam2608
 
The images of the compounds are not visible. Kin.pdf
                     The images of the compounds are not visible.  Kin.pdf                     The images of the compounds are not visible.  Kin.pdf
The images of the compounds are not visible. Kin.pdfanubhavnigam2608
 
Step1 Moles of water produced =18 Step2 The init.pdf
                     Step1 Moles of water produced =18  Step2 The init.pdf                     Step1 Moles of water produced =18  Step2 The init.pdf
Step1 Moles of water produced =18 Step2 The init.pdfanubhavnigam2608
 
Sorry. SnO2 is amphoteric. This one is a tricky o.pdf
                     Sorry. SnO2 is amphoteric. This one is a tricky o.pdf                     Sorry. SnO2 is amphoteric. This one is a tricky o.pdf
Sorry. SnO2 is amphoteric. This one is a tricky o.pdfanubhavnigam2608
 
scientific synonyms for height may be peak or tal.pdf
                     scientific synonyms for height may be peak or tal.pdf                     scientific synonyms for height may be peak or tal.pdf
scientific synonyms for height may be peak or tal.pdfanubhavnigam2608
 
R = k[NOCl2][NO] Molecularity = 2 .pdf
                     R = k[NOCl2][NO]  Molecularity = 2               .pdf                     R = k[NOCl2][NO]  Molecularity = 2               .pdf
R = k[NOCl2][NO] Molecularity = 2 .pdfanubhavnigam2608
 
In general, they are alkenes attached to electron.pdf
                     In general, they are alkenes attached to electron.pdf                     In general, they are alkenes attached to electron.pdf
In general, they are alkenes attached to electron.pdfanubhavnigam2608
 
Group 11 has three elements namely, Cu, Ag and Au.pdf
                     Group 11 has three elements namely, Cu, Ag and Au.pdf                     Group 11 has three elements namely, Cu, Ag and Au.pdf
Group 11 has three elements namely, Cu, Ag and Au.pdfanubhavnigam2608
 
Cu, (H2), Zn, Mg Activity series basedon hyd.pdf
                     Cu, (H2), Zn, Mg  Activity series basedon hyd.pdf                     Cu, (H2), Zn, Mg  Activity series basedon hyd.pdf
Cu, (H2), Zn, Mg Activity series basedon hyd.pdfanubhavnigam2608
 
answewrSolutionanswewr.pdf
answewrSolutionanswewr.pdfanswewrSolutionanswewr.pdf
answewrSolutionanswewr.pdfanubhavnigam2608
 
When a compound is completely oxidized with oxygen, the products wil.pdf
When a compound is completely oxidized with oxygen, the products wil.pdfWhen a compound is completely oxidized with oxygen, the products wil.pdf
When a compound is completely oxidized with oxygen, the products wil.pdfanubhavnigam2608
 
Calcium metal reacts with liquid water to produce.pdf
                     Calcium metal reacts with liquid water to produce.pdf                     Calcium metal reacts with liquid water to produce.pdf
Calcium metal reacts with liquid water to produce.pdfanubhavnigam2608
 
The mineralogical and structural adjustment of solid rocks to physic.pdf
The mineralogical and structural adjustment of solid rocks to physic.pdfThe mineralogical and structural adjustment of solid rocks to physic.pdf
The mineralogical and structural adjustment of solid rocks to physic.pdfanubhavnigam2608
 

More from anubhavnigam2608 (20)

1.KClO(3) ---- Potassium chlorate 2.H(2)SO(2) ------ sulphurous acid.pdf
  1.KClO(3) ---- Potassium chlorate 2.H(2)SO(2) ------ sulphurous acid.pdf  1.KClO(3) ---- Potassium chlorate 2.H(2)SO(2) ------ sulphurous acid.pdf
1.KClO(3) ---- Potassium chlorate 2.H(2)SO(2) ------ sulphurous acid.pdf
 
Seven Requirements of CTC 1 Age 2 Relationship .pdf
     Seven Requirements of CTC           1 Age     2 Relationship    .pdf     Seven Requirements of CTC           1 Age     2 Relationship    .pdf
Seven Requirements of CTC 1 Age 2 Relationship .pdf
 
the solvents that are used to combine together to.pdf
                     the solvents that are used to combine together to.pdf                     the solvents that are used to combine together to.pdf
the solvents that are used to combine together to.pdf
 
The most simple property to calculate this is Ka.pdf
                     The most simple property to calculate this is Ka.pdf                     The most simple property to calculate this is Ka.pdf
The most simple property to calculate this is Ka.pdf
 
The images of the compounds are not visible. Kin.pdf
                     The images of the compounds are not visible.  Kin.pdf                     The images of the compounds are not visible.  Kin.pdf
The images of the compounds are not visible. Kin.pdf
 
Step1 Moles of water produced =18 Step2 The init.pdf
                     Step1 Moles of water produced =18  Step2 The init.pdf                     Step1 Moles of water produced =18  Step2 The init.pdf
Step1 Moles of water produced =18 Step2 The init.pdf
 
Sorry. SnO2 is amphoteric. This one is a tricky o.pdf
                     Sorry. SnO2 is amphoteric. This one is a tricky o.pdf                     Sorry. SnO2 is amphoteric. This one is a tricky o.pdf
Sorry. SnO2 is amphoteric. This one is a tricky o.pdf
 
SO2 Solu.pdf
                     SO2                                      Solu.pdf                     SO2                                      Solu.pdf
SO2 Solu.pdf
 
scientific synonyms for height may be peak or tal.pdf
                     scientific synonyms for height may be peak or tal.pdf                     scientific synonyms for height may be peak or tal.pdf
scientific synonyms for height may be peak or tal.pdf
 
R = k[NOCl2][NO] Molecularity = 2 .pdf
                     R = k[NOCl2][NO]  Molecularity = 2               .pdf                     R = k[NOCl2][NO]  Molecularity = 2               .pdf
R = k[NOCl2][NO] Molecularity = 2 .pdf
 
In general, they are alkenes attached to electron.pdf
                     In general, they are alkenes attached to electron.pdf                     In general, they are alkenes attached to electron.pdf
In general, they are alkenes attached to electron.pdf
 
I, II S.pdf
                     I, II                                       S.pdf                     I, II                                       S.pdf
I, II S.pdf
 
Group 11 has three elements namely, Cu, Ag and Au.pdf
                     Group 11 has three elements namely, Cu, Ag and Au.pdf                     Group 11 has three elements namely, Cu, Ag and Au.pdf
Group 11 has three elements namely, Cu, Ag and Au.pdf
 
Cu, (H2), Zn, Mg Activity series basedon hyd.pdf
                     Cu, (H2), Zn, Mg  Activity series basedon hyd.pdf                     Cu, (H2), Zn, Mg  Activity series basedon hyd.pdf
Cu, (H2), Zn, Mg Activity series basedon hyd.pdf
 
answewrSolutionanswewr.pdf
answewrSolutionanswewr.pdfanswewrSolutionanswewr.pdf
answewrSolutionanswewr.pdf
 
cis- So.pdf
                     cis-                                       So.pdf                     cis-                                       So.pdf
cis- So.pdf
 
When a compound is completely oxidized with oxygen, the products wil.pdf
When a compound is completely oxidized with oxygen, the products wil.pdfWhen a compound is completely oxidized with oxygen, the products wil.pdf
When a compound is completely oxidized with oxygen, the products wil.pdf
 
Calcium metal reacts with liquid water to produce.pdf
                     Calcium metal reacts with liquid water to produce.pdf                     Calcium metal reacts with liquid water to produce.pdf
Calcium metal reacts with liquid water to produce.pdf
 
two;Solutiontwo;.pdf
two;Solutiontwo;.pdftwo;Solutiontwo;.pdf
two;Solutiontwo;.pdf
 
The mineralogical and structural adjustment of solid rocks to physic.pdf
The mineralogical and structural adjustment of solid rocks to physic.pdfThe mineralogical and structural adjustment of solid rocks to physic.pdf
The mineralogical and structural adjustment of solid rocks to physic.pdf
 

Recently uploaded

TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...Nguyen Thanh Tu Collection
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...Nguyen Thanh Tu Collection
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024Borja Sotomayor
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismDabee Kamal
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17Celine George
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportDenish Jangid
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhleson0603
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSean M. Fox
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxLimon Prince
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxCeline George
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptNishitharanjan Rout
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi RajagopalEADTU
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...Nguyen Thanh Tu Collection
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................MirzaAbrarBaig5
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnershipsexpandedwebsite
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文中 央社
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFVivekanand Anglo Vedic Academy
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxAdelaideRefugio
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...EduSkills OECD
 

Recently uploaded (20)

TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 

Clock.java public class Clock { 3 private ints to represent .pdf

  • 1. //Clock.java public class Clock { //3 private ints to represent hours, minutes, and seconds. int hours; int minutes; int seconds; //a default constructor that always initializes the hour/minute/second to 0,0,0 (midnight). public Clock() { hours=0; minutes=0; seconds=0; } /* a parameterized constructor that receives the hour, minute, second as 3 ints and sets the values in the instance to whatever is received. */ public Clock(int h, int m, int s) { /*If the hour is illegal (negative or > 23) or if the minute or the second are illegal (negative or > 59), use the following statement to throw an exception: throw new IllegalArgumentException*/ if(h>23 || m>59 || s>59) throw new IllegalArgumentException("Wrong time to set") ; hours=h; minutes=m; seconds=s; } //copy constructor public Clock(Clock C) { hours=C.hours; minutes=C.minutes; seconds=C.seconds; }
  • 2. //adds numSeconds to how many seconds it has (and fixes the hour/min/sec //so that they “carry” to the next unit if possible) void addSeconds(int numSeconds) { seconds+=numSeconds; if(seconds>60) { minutes+=seconds/60; seconds%=60; if(minutes>60) { hours+=minutes/60; minutes%=60; } } } /* Tick : increments itself by 1 second (and fixes the hour/min/sec so they “carry” to the next unit if possible). */ void tick() { seconds+=1; if(seconds>60) { minutes+=seconds/60; seconds%=60; if(minutes>60) { hours+=minutes/60; minutes%=60; } } } // returns true if it is “night” (between 8PM and 5AM inclusive), returns false otherwise. boolean isNight()
  • 3. { if((hours>=20 && hours <=23)||(hours>=0 && hours<=5)) { return true; } else return false; } //returns a representation of the current instance in the form hh:mm:ss AMorPM public String toString() { String time=""; String ampm=""; if(hours == 0) { time=time +"12"; ampm="AM"; } else if( hours>=1 && hours<=11) { time=time+ hours; ampm="AM"; } else if(hours == 12) { time=time+"12"; ampm="PM"; } else if(hours>=13 && hours<=23) { time=time+(hours-12); ampm="PM"; } if(minutes<10) { time+=":0"+minutes;
  • 4. } else { time+=":"+minutes; } if(seconds<10) { time+=":0"+seconds; } else { time+=":"+seconds; } time+=" "+ampm; return time; } } /////////////////////////////////////////////////////// //driver public class ClockDriver { public static void main(String[] args) { try{ Clock clock1=new Clock(13,29,45); System.out.println("clock1: "+clock1.toString()); clock1.addSeconds(34); System.out.println("clock1 after adding 34 seconds : "+clock1.toString()); clock1.tick(); System.out.println("clock1 after tick : "+clock1.toString()); Clock clock2=new Clock(25,45,50); } catch(Exception ex) { ex.printStackTrace(); }
  • 5. } } Solution //Clock.java public class Clock { //3 private ints to represent hours, minutes, and seconds. int hours; int minutes; int seconds; //a default constructor that always initializes the hour/minute/second to 0,0,0 (midnight). public Clock() { hours=0; minutes=0; seconds=0; } /* a parameterized constructor that receives the hour, minute, second as 3 ints and sets the values in the instance to whatever is received. */ public Clock(int h, int m, int s) { /*If the hour is illegal (negative or > 23) or if the minute or the second are illegal (negative or > 59), use the following statement to throw an exception: throw new IllegalArgumentException*/ if(h>23 || m>59 || s>59) throw new IllegalArgumentException("Wrong time to set") ; hours=h; minutes=m; seconds=s; } //copy constructor public Clock(Clock C)
  • 6. { hours=C.hours; minutes=C.minutes; seconds=C.seconds; } //adds numSeconds to how many seconds it has (and fixes the hour/min/sec //so that they “carry” to the next unit if possible) void addSeconds(int numSeconds) { seconds+=numSeconds; if(seconds>60) { minutes+=seconds/60; seconds%=60; if(minutes>60) { hours+=minutes/60; minutes%=60; } } } /* Tick : increments itself by 1 second (and fixes the hour/min/sec so they “carry” to the next unit if possible). */ void tick() { seconds+=1; if(seconds>60) { minutes+=seconds/60; seconds%=60; if(minutes>60) { hours+=minutes/60; minutes%=60;
  • 7. } } } // returns true if it is “night” (between 8PM and 5AM inclusive), returns false otherwise. boolean isNight() { if((hours>=20 && hours <=23)||(hours>=0 && hours<=5)) { return true; } else return false; } //returns a representation of the current instance in the form hh:mm:ss AMorPM public String toString() { String time=""; String ampm=""; if(hours == 0) { time=time +"12"; ampm="AM"; } else if( hours>=1 && hours<=11) { time=time+ hours; ampm="AM"; } else if(hours == 12) { time=time+"12"; ampm="PM"; } else if(hours>=13 && hours<=23) { time=time+(hours-12);
  • 8. ampm="PM"; } if(minutes<10) { time+=":0"+minutes; } else { time+=":"+minutes; } if(seconds<10) { time+=":0"+seconds; } else { time+=":"+seconds; } time+=" "+ampm; return time; } } /////////////////////////////////////////////////////// //driver public class ClockDriver { public static void main(String[] args) { try{ Clock clock1=new Clock(13,29,45); System.out.println("clock1: "+clock1.toString()); clock1.addSeconds(34); System.out.println("clock1 after adding 34 seconds : "+clock1.toString()); clock1.tick(); System.out.println("clock1 after tick : "+clock1.toString()); Clock clock2=new Clock(25,45,50);