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
What is the role of the W3C? Group of answer choices oversee research and set standards for many areas of the Internet supervise
Murljashka [212]

Answer:

oversee research and set standards for many areas of the Internet

Explanation:

World Wide Web Consortium was created to maintain a standard order in the cyber world. It is an international community formed by the organizations as a member. W3C sets the standards of the websites and enables them to function and appear the same in every web browser. A specific standard of guidelines, rules, and protocols are fixed so that the World Wide Web can function and grow respectively.

5 0
3 years ago
Along with an ip address, a subnet mask, and a name server, the other thing required for a computer to operate on a network is a
laiz [17]

Along with an IP address, a subnet mask, and a name server, the other thing required for a computer to operate on a network is a primary gateway. The primary gateway acts as an access point which forwards information to another network or the internet.

6 0
3 years ago
If you design your pages using a font that your user does not have installed, the browser defaults to ____ on a macintosh.
sveticcg [70]
The browser should default to Calibri (body)
3 0
2 years ago
What is the meaning of web browser
anastassius [24]
Is that a joke... if ain't cheatin you repeatin
8 0
2 years ago
The right of workers to seek safety and health on the job without fear of punishment is spelled out in:
mr Goodwill [35]
The answer to the question is C.
3 0
3 years ago
Other questions:
  • Which online text source would include a review of a new TV show?
    9·2 answers
  • Given that two int variables, total and amount, have been declared, write a sequence of statements that: initializes total to 0
    14·1 answer
  • A customer in a store is purchasing 5 items. Write a python program that asks for the price of each item and display the subtota
    14·1 answer
  • I'm 11, except my profile says I'm 15.
    15·1 answer
  • list the six external parts or "peripherals" of a computer system and identify which are output and which are input devices.
    15·1 answer
  • A(n) __________ describes a set of well-defined logical steps that must be taken to perform a task
    5·1 answer
  • What can your employer do to protect you from overhead power lines?
    13·1 answer
  • What steps might a company or organization need take in order to keep its digital data secure online?
    8·1 answer
  • Here is the problem specification: An Internet service provider has three different subscription packages for its customers: Pac
    5·1 answer
  • A(n) ____________________ key is a key that is not reused, but rather is only used once, thus improving security by reducing the
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!