In: Computer Science
I need to create an exception handler with the following code I have below
public class Time {
/**
* Properties Declared;
*/
private int hours;
private int minutes;
private int seconds;
/*
* Default Constructor
*/
public Time()
{
hours = 0;
minutes = 0;
seconds = 0;
}
/**
* Parameterized Contructor throws BadTimeException in any case that
* there should be an error
* @param h
* @param m
* @param s
*/
public Time(int h, int m, int s) throws BadTimeException
{
hours = h;
minutes = m;
seconds = s;
if (h < 0 || m < 0 || s < 0 )
{
throw new BadTimeException("Error time can not be negatiive");
}
if(h > 24 || m > 60 || s > 60 )
{
}
}
public void helperMethod()
{
if(this.seconds > 59)
{
this.seconds = (this.seconds % 60);
this.minutes += 1;
}
if(this.minutes > 59)
{
this.minutes = (this.minutes % 60);
this.hours += 1;
}
}
/**
*
* @return
*/
public int getHours() {
return hours;
}
/**
*
* @return
*/
public int getMinutes()
{
return minutes;
}
/**
*
* @return
*/
public int getSeconds()
{
return seconds;
}
/**
* Copy Constructor
* @param originalObject
*/
public Time(Time originalObject)
{
hours = originalObject.hours;
minutes = originalObject.minutes;
seconds = originalObject.seconds;
}
/**
*
*/
public String toString()
{
String time = "";
String h, m, s;
if(hours < 10)
h = "0" + hours;
else
h = hours + "";
if(minutes < 10)
m = "0" + minutes;
else
m = minutes + "";
if(seconds < 10)
s= "0" + seconds;
else
s= seconds + "";
time = h + ":" + m + ":" + s;
return time;
}
/**
*
* @param hours
* @return
* @throws BadTimeException
*/
public Time later(int newHours) throws BadTimeException
{
return new Time(hours + newHours, this.minutes, this.seconds);
}
/**
*
* @param hours
* @param minutes
* @return
* @throws BadTimeException
*/
public Time later(int newHours, int newMinutes) throws BadTimeException
{
return new Time(hours + newHours, minutes + newMinutes, seconds);
}
/**
*
* @param hours
* @param minutes
* @param seconds
* @return
* @throws BadTimeException
*/
public Time later(int newHours, int newMinutes, int newSeconds) throws BadTimeException
{
return new Time(hours + newHours, minutes + newMinutes, seconds + newSeconds);
}
}
For example if I have 189 secs it should recognize it as an error and fix it by calling the helper method and instead should be 3 minutes and 9 seconds and the same thing with minutes ex 120 mins is 2 hours, but for hours it should just be dropped after the 1 day that is made for example 37 hours just becomes 13 hours. the exceptions handler should call a helper method that will fix the properties to make sense
/**************************************Time.java***************************/
public class Time {
/**
*
* Properties Declared;
*
*/
private int days;
private int hours;
private int minutes;
private int seconds;
/*
*
* Default Constructor
*
*/
public Time()
{
this.days = 0;
this.hours = 0;
this.minutes = 0;
this.seconds = 0;
}
/**
*
* Parameterized Contructor throws BadTimeException in
any case that
*
* there should be an error
*
* @param h
*
* @param m
*
* @param s
*
*/
public Time(int h, int m, int s) throws BadTimeException
{
this.hours = h;
this.minutes = m;
this.seconds = s;
if (h < 0 || m < 0 || s < 0)
{
throw new BadTimeException("Error time can not be negatiive");
}
if (h > 24 || m > 60 || s > 60)
{
helperMethod();
}
}
public void helperMethod()
{
if (this.seconds > 59)
{
this.minutes
+= seconds / 60;
this.seconds =
(this.seconds % 60);
}
if (this.minutes > 59)
{
this.hours +=
minutes / 60;
this.minutes =
(this.minutes % 60);
}
if (this.hours > 23) {
this.days +=
hours / 24;
this.hours =
(this.hours % 24);
}
}
/**
*
*
*
* @return
*
*/
public int getHours() {
return hours;
}
/**
*
*
*
* @return
*
*/
public int getMinutes()
{
return minutes;
}
/**
*
*
*
* @return
*
*/
public int getSeconds()
{
return seconds;
}
public int getDays() {
return days;
}
/**
*
* Copy Constructor
*
* @param originalObject
*
*/
public Time(Time originalObject)
{
hours = originalObject.hours;
minutes = originalObject.minutes;
seconds = originalObject.seconds;
}
/**
*
*/
public String toString()
{
String time = "";
String d, h, m, s;
if (days < 10)
d = "0" +
days;
else
d = days + "";
if (hours < 10)
h = "0" + hours;
else
h = hours + "";
if (minutes < 10)
m = "0" + minutes;
else
m = minutes + "";
if (seconds < 10)
s = "0" + seconds;
else
s = seconds + "";
time = d + ":" + h + ":" + m + ":" + s;
return time;
}
/**
*
*
*
* @param hours
*
* @return
*
* @throws BadTimeException
*
*/
public Time later(int newHours) throws BadTimeException
{
return new Time(hours + newHours, this.minutes, this.seconds);
}
/**
*
*
*
* @param hours
*
* @param minutes
*
* @return
*
* @throws BadTimeException
*
*/
public Time later(int newHours, int newMinutes) throws BadTimeException
{
return new Time(hours + newHours, minutes + newMinutes, seconds);
}
/**
*
*
*
* @param hours
*
* @param minutes
*
* @param seconds
*
* @return
*
* @throws BadTimeException
*
*/
public Time later(int newHours, int newMinutes, int newSeconds) throws BadTimeException
{
return new Time(hours + newHours, minutes + newMinutes, seconds + newSeconds);
}
}
/*******************************TestTime.java************************/
public class TestTime {
public static void main(String[] args) throws
BadTimeException {
Time t = new Time(37, 120,
189);
System.out.println(t.toString());
}
}
/*************************************BadTimeException.java************************/
public class BadTimeException extends Exception {
public BadTimeException(String s) {
super(s);
}
}
/*********************output***************************/
01:15:03:09

Please let me know if you have any doubt or modify the answer, Thanks :)