Exception Handling - Java Implement the following program with exception handlin
ID: 3741236 • Letter: E
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 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
}
}
}
Explanation / Answer
ANSWER :
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);
try{ //put a try block before initialization of variable
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
}
}
catch(InputMismatchException e)
{
System.out.println(ex); //catch exception if wrong type of input is given
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.