Question: Exception Handling - Java Implement the following program with exception handling. -----------------------Time.java---------------------------------------------------------- public class Time { private int hours; //Instance variable for hours private int

Exception Handling - Java

Implement the following program with exception handling.

-----------------------Time.java----------------------------------------------------------

public class Time { private int hours; //Instance variable for hours private int minutes; //Instance variable for minutes private int seconds; //Instance variable for seconds public Time(int hours, int minutes, int seconds) //Constructor { this.hours = hours; //Initialized this.minutes = minutes; //Initialized this.seconds = seconds; //Initialized }

public int getHours() { return hours; } public void setHours(int hours) { this.hours = hours; } public int getMinutes() { return minutes; } public void setMinutes(int minutes) { this.minutes = minutes; } public int getSeconds() { return seconds; } public void setSeconds(int seconds) { this.seconds = seconds; } public void displayTime() //Function to produce the numbers entered into a clock format { System.out.println(hours + ":" + minutes + ":" + seconds); } }

---------------------------------TimeTest.java------------------------------------------------------

import java.util.Scanner; public class timeTest {

/** * @param args the command line arguments */ public static void main(String[] args) { //Main method Scanner in = new Scanner(System.in); int h, m, s; System.out.print("Enter the number of hours: "); //Collecting number of hours from user h = in.nextInt(); System.out.print("Enter the number of minutes: "); //Collecting number of minutes from user m = in.nextInt(); System.out.print("Enter the number of seconds: "); //Collecting number of seconds from user s = in.nextInt(); if(h > 23 || m > 59 || s > 59 || h < 0 || m < 0 || s < 0) //Parameters to make sure the numbers fit 24 hour clock format { System.out.println("Error, recheck entered time."); //Numbers are too big or too small and an error is received } else { Time t = new Time(h ,m ,s); t.displayTime(); //PRinting of the clock } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!