1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
IgorC [24]
3 years ago
8

Modify class Time2 of fig 8.5, (which is split into four pictures) to include a tick method that increments the time stored in a

Time2 object by one second. Provide method incrementMinute to increment the minute by one and method incrementHour to increment the hour by one. Write a program that tests the tick method, the incrementMinute method and the incrementHour method to ensure that they work correctly. Be sure to test the following cases:
a: incrementing into the next minute
b:incrementing into the next hour
c:incrementing into the next day(ie. 11:59:59 PM to 12:00:00AM
Computers and Technology
1 answer:
ArbitrLikvidat [17]3 years ago
4 0

Explanation:

Complete Program:

NOTE:   The newly added statements are highlighted in bold.

// File: Time2.java

public class Time2

{

private int hour; // 0 - 23

private int minute; // 0 - 59

private int second; // 0 - 59

public Time2()

{

 this(0, 0, 0);

}

public Time2(int hour)

{

 this(hour, 0, 0);

}

public Time2(int hour, int minute)

{

 this(hour, minute, 0);

}

public Time2(int hour, int minute, int second)

{

 if(hour < 0 || hour >= 24)

  throw new IllegalArgumentException("hour must be 0-23");

 if(minute < 0 || minute >= 60)

  throw new IllegalArgumentException("minute must be 0-59");

 if(second < 0 || second >= 60)

  throw new IllegalArgumentException("second must be 0-59");

 this.hour = hour;

 this.minute = minute;

 this.second = second;

}

public Time2(Time2 time)

{

 this(time.getHour(), time.getMinute(), time.getSecond());

}

public void setTime(int hour, int minute, int second)

{

 if(hour < 0 || hour >= 24)

  throw new IllegalArgumentException("hour must be 0-23");

 if(minute < 0 || minute >= 60)

  throw new IllegalArgumentException("minute must be 0-59");

 if(second < 0 || second >= 60)

  throw new IllegalArgumentException("second must be 0-59");

 this.hour = hour;

 this.minute = minute;

 this.second = second;

}

public void setHour(int hour)

{

 if(hour < 0 || hour >= 24)

  throw new IllegalArgumentException("hour must be 0-23");

 

 this.hour = hour;

}

public void setMinute(int minute)

{

 if(minute < 0 || minute >= 60)

  throw new IllegalArgumentException("minute must be 0-59");

 

 this.minute = minute;

}

public void setSecond(int second)

{

 if(second < 0 || second >= 60)

  throw new IllegalArgumentException("second must be 0-59");

 

 this.second = second;

}

public int getHour()

{

 return hour;

}

public int getMinute()

{

 return minute;

}

public int getSecond()

{

 return second;

}

public String toUniversalString()

{

 return String.format("%02d:%02d:%02d", getHour(), getMinute(),

   getSecond());

}

public String toString()

{

 return String.format("%d:%02d:%02d %s",

   ((getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12),

   getMinute(), getSecond(), (getHour() < 12 ? "AM" : "PM"));

}

// increment second

public void tick()

{

 if(second < 23)

 {

  second++;

 }

 else if(minute < 59)

 {

  second = 0;

  minute++;

 }

 else if(hour < 23)

 {

  second = 0;

  minute = 0;

  hour++;

 }

 else

 {

  second = 0;

  minute = 0;

  hour = 0;

 }    

}

// increment minute

public void incrementMinute()

{

 if(minute < 59)

 {

  minute++;

 }

 else if(hour < 23)

 {

  minute = 0;

  hour++;

 }

 else

 {

  minute = 0;

  hour = 0;

 }    

}

public void incrementHour()

{

 if(hour < 23)

 {

  hour++;

 }

 else

 {

  hour = 0;

 }    

}

}

------------------------------------------------------------------------------------------------------------

// File: Time2Test.java

public class Time2Test

{

  public static void main( String args[] )

  {      

     Time2 t = new Time2(22, 58, 59);

     System.out.println("Starting time: ");

     System.out.println("Time in 24 hours format: " + t.toUniversalString());

     System.out.println("Time in 12 hours format: " + t);

     System.out.println();

     

     t.incrementMinute();

     System.out.println("After incrementing one minute: ");

     System.out.println("Time in 24 hours format: " + t.toUniversalString());

     System.out.println("Time in 12 hours format: " + t);

     System.out.println();

     

     t.incrementHour();

     System.out.println("After incrementing one hour: ");

     System.out.println("Time in 24 hours format: " + t.toUniversalString());

     System.out.println("Time in 12 hours format: " + t);

     System.out.println();

     

     t.tick();

     System.out.println("After incrementing one second: ");

     System.out.println("Time in 24 hours format: " + t.toUniversalString());

     System.out.println("Time in 12 hours format: " + t);    

     System.out.println();

  }

}

You might be interested in
Which of the following questions will most likely be answered by displaying data on a line graph?
Artemon [7]
I'm pretty sure the answer is b
4 0
3 years ago
What is the user requirements of an educational app
VARVARA [1.3K]
Ummmmm I don’t know but I wanna know now.....
7 0
3 years ago
State one way the projector can be integrated into teaching and learning ​
koban [17]

Answer:

The projector con be used to show videos or pictures on a particular subject or topic to gain more understanding

4 0
3 years ago
__________ has remained a dominant communication tool, despite the rise in use of newer, alternative media options for message d
maksim [4K]

Answer: E-mails

Explanation:

 An E-mails is one of the type of digital message and also known as the dominant communication tool which is used for in the form of document in an organization where we can used it in different ways are as follows:

  • Communicating with other employees related to rules and regulations of an organization
  • Making various types of recommendations
  • Used for an inquiry purpose
  • providing various types of current status and new updates

According to the given question, Emails is one of the type of tool that is used as the communication medium and it is one of the flexible message delivery options which is widely used by an organizations.

 

6 0
3 years ago
Which of the following need NOT be completed separately if a worksheet is prepared?
Anika [276]
C because you already have you sheet prepared
3 0
3 years ago
Other questions:
  • Terrence smiles at his customers, helps his coworkers, and stays late when needed. What personal skill does Terrence demonstrate
    10·2 answers
  • Which part of a fax cover sheet helps the recipient verify the successful transmission of all the pages? A) REMARKS: be) TOTAL N
    7·2 answers
  • ______ are single numbers or values, which may include integers, floating-point decimals, or strings of characters.
    10·1 answer
  • Given a variable temps that refers to a list, all of whose elements refer to values of type float, representing temperature data
    10·2 answers
  • You are planning to install Windows 10 on a computer in a dual-boot configuration. The computer already has Windows 8 installed.
    14·2 answers
  • Can someone that been helping me answer one more question for please and thx
    13·1 answer
  • Does digital media play a big role in your life?
    13·1 answer
  • Copy and paste is the only way to move text from one place to another.<br><br>True or <br>False​
    15·2 answers
  • You’ve been tossed into an insane asylum. What do you tell the people there to prove to them that you don’t belong inside?
    7·2 answers
  • When working in outline mode, press __________ and click the eye icon to change only one layer back to preview mode.
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!