Question

In: Computer Science

I need to create an exception handler with the following code I have below public class...

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

Solutions

Expert Solution

/**************************************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 :)


Related Solutions

Create a java program that: - Has a class that defines an exception -Have that exception...
Create a java program that: - Has a class that defines an exception -Have that exception throw(n) in one method, and be caught and handled in another one. -Has a program that always continues even if incorrect data is entered by the user -has a minimum of 2 classes in it
I need this code translated to C code. Thank you. public class FourColorTheorem { public static...
I need this code translated to C code. Thank you. public class FourColorTheorem { public static boolean isPrime(int num) { // Corner case if (num <= 1) return false; // Check from 2 to n-1 for (int i = 2; i < num; i++) if (num % i == 0) return false; return true; } public static void main(String[] args) { int squares[] = new int[100]; for (int i = 1; i < squares.length; i++) squares[i-1] = i * i;...
I need a java flowchart diagram for the following code: import java.util.*; public class Main {...
I need a java flowchart diagram for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");   ...
GIVEN THE CODE BELOW Given the following class: /** * Document class. */ public class Document...
GIVEN THE CODE BELOW Given the following class: /** * Document class. */ public class Document { private int words; /** * constructor * pre: none * post: A Document object created. Words initialized to 0. */ public Document() { words = 0; //default words } /** * Changes the number of document words. * pre: none * post: Words has been changed. */ public void setWords(int numWords) { words = numWords; } /** * Calculates the number of pages....
I need to translate my java code into C code. import java.util.Scanner; class CS_Lab3 { public...
I need to translate my java code into C code. import java.util.Scanner; class CS_Lab3 { public static void main( String args[] ) { Scanner input = new Scanner( System.in ); // create array to hold user input int nums[] = new int[10]; int i = 0, truthCount = 0; char result = 'F', result2 = 'F'; // ask user to enter integers System.out.print("Please Enter 10 Different integers: "); // gather input into array for ( i = 0; i <...
I need this Java code transform to Python Code PROGRAM: import java.util.Scanner; public class Main {...
I need this Java code transform to Python Code PROGRAM: import java.util.Scanner; public class Main { static int count=0; int calculate(int row, int column) { count++; if(row==1&&column==1) { return 0; } else if(column==1) { return ((200+calculate(row-1,column))/2); } else if(column==row) { return (200+calculate(row-1,column-1))/2; } else { return ((200+calculate(row-1,column-1))/2)+((200+calculate(row-1,column))/2); }    } public static void main(String[] args) { int row,column,weight; Main m=new Main(); System.out.println("Welcome to the Human pyramid. Select a row column combination and i will tell you how much weight the...
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int...
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int maxRevenue(int[] billboard, int[] revenue, int distance, int milesRes) { int[] MR = new int[distance + 1]; //Next billboard which can be used will start from index 0 in billboard[] int nextBillBoard = 0; //example if milesRes = 5 miles then any 2 bill boards has to be more than //5 miles away so actually we can put at 6th mile so we can add...
I needv pseudocode and a flowchart for the following java code public class AcmePay { public...
I needv pseudocode and a flowchart for the following java code public class AcmePay { public static void main(String[] args) throws Exception { Scanner scanner = new Scanner(System.in); int hours, shift, retirement = 0; do { System.out.print("Enter the number of hours worked (>0): "); hours = scanner.nextInt(); } while (hours <= 0); do { System.out.print("Enter shift [1 2 or 3]: "); shift = scanner.nextInt(); } while (shift < 1 || shift > 3); if (shift == 2 || shift ==...
JAVA CODE BEGINNERS, I already have the DEMO CLASS(NEED YOU TO USE), I need you to...
JAVA CODE BEGINNERS, I already have the DEMO CLASS(NEED YOU TO USE), I need you to use all methods, also switch statements. Write a Temperature class. The class will have three conversion methods: toCelsius(), toKelvin() and toFahrenheit(). These methods will return a Temperature in those three scales equal to the this temperature. Note that the value of this is not changed in these conversions. In addition to these three conversion methods the class will have methods add(Temperature), subtract(Temperature), multiply(Temperature), and...
<<<<<<<<. I need the UML diagram for all classes.java below. >>>>> // Vehicle.java public class Vehicle...
<<<<<<<<. I need the UML diagram for all classes.java below. >>>>> // Vehicle.java public class Vehicle {    // data members declared as private    private String make;    private double weight;    private double height;    private double length;    private int maxSpeed;    private int noOfDoors;    private int numberSeats;    /**    * @param make    * @param weight    * @param height    * @param length    * @param maxSpeed    * @param noOfDoors    *...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT