a) write an exception class for a time of day not represented in the correct 24-hour format. For example, when a –2, or 24 or 27 are given as times, exceptions should be generated.
b) Write a method to print the time in 24-hour format on screen and throw an exception for the time that is in error. You may take the time as an integer argument.
thanks for the question, here are the two classes - TimeException that extends the exception class and the TimePrinter that represents an object of 24hr formatted time.
The problem does not mention the language in which it needs to be coded, it looked familiar to Java so i coded it in Java.
Let me know in case of any question or changes needed to the code.
===================================================================================
public class TimeException
extends Exception {
public TimeException() {
}
public TimeException(String s)
{
super(s);
}
}
public class TimePrinter {
private int hour;
private int minute;
private int seconds;
public TimePrinter() {
hour=0;
minute=0;
seconds=0;
}
public TimePrinter(int hour, int minute, int seconds) throws TimeException {
if(0<=hour && hour<24 && 0<=minute && minute<60 && 0<=seconds && seconds<60){
this.hour = hour;
this.minute = minute;
this.seconds = seconds;
}else{
throw new TimeException("Invalid Time Entered.");
}
}
@Override
public String toString() {
String time="";
time=hour<10?"0"+hour: String.valueOf(hour);time+=":";
time+=minute<10?"0"+minute: String.valueOf(minute);time+=":";
time+=seconds<10?"0"+seconds: String.valueOf(seconds);
return time;
}
public static void main(String[] args) throws TimeException {
TimePrinter time = new TimePrinter(24,59,59);
System.out.println(time);
}
}
a) write an exception class for a time of day not represented in the correct 24-hour...
Write a program that prompts the user to enter a person's birth date in numeric form (e.g. 8-27-1980) and outputs the date of birth in the format of month day, year (e.g. August 27, 1980). Your program must work for three types of exceptions - invalid day, invalid month and invalid year (year must be between 1915 and 2015). Make these as class driven exceptions. Use one try-catch block with separate catches for the exception classes to handle errors for...
PLEASE I NEED C# Objectives Learn the basics of exception handling. Background Exceptions are essentially unexpected situations. It is difficult to write a program that can handle all possible situations, as we have found out through the many programs we have written. For example, should your program accept accidental input? Does it use the metric system or the empirical system? Do users of your program know which system it uses? In order to deal with all these possibilities, exceptions were...
its about in C++
You will implement a simple calendar application The implementation should include a class named Calendar, an abstract class named Event and two concrete classes named Task and Appointment which inherit from the Event class The Event Class This will be an abstract class. It will have four private integer members called year, month, day and hour which designate the time of the event It should also have an integer member called id which should be a...
1. Create a MyTime class which is designed to contain objects representing times in 12-hour clock format. Eg: 7:53am, 10:20pm, 12:00am (= midnight), 12:00pm (= noon). You can assume the user will enter times in the format given by the previous examples. Provide a default constructor, a set method which is given a String (eg: “7:53am”), a getHours method, a getMinutes method and a boolean isAm method. Also provide a method for returning a string “morning”, “afternoon”, “evening” or “night”...
Please use Java only. Write a class, ZeroException, which is an Exception, and is used to signal that something is zero when it shouldn't be. -- Not needed Write the class ArrayManipulator which creates an array and provides several methods to manipulate values taken from an array. --needed ZeroException Task: -- Not needed Exceptions are used to signal many types of problems in a program. We can write our own as well to describe specific exceptional conditions which may arise....
JAVA 1. Create a MyTime class which is designed to contain objects representing times in 12-hour clock like 7:53am, 10:20pm, 12:00am (= midnight), 12:00pm (= noon). Provide a default constructor, a set method which is given a String (eg, “7:53am”), a getHours method, a getMinutes method and a boolean isAm method. Also provide a method for returning a string “morning”, “afternoon”, “evening” or “night” appropriate (in your opinion) to the time. Please see the NOTE below concerning input validation and...
1. Create a Time class which is designed to contain objects representing times in 12-hour clock format. Eg: 6:58am, 11:13pm, 12:00am (= midnight), 12:00pm (= noon). You can assume the user will enter times in the format given by the previous examples.Provide a default constructor, a set method which is given a String (eg: “9:42am”), a getHours method, a getMinutes method and a boolean isAm method. Also provide a method for returning a string “morning”, “afternoon”, “evening” or “night” appropriate...
File Factorials.java contains a program that calls the factorial method of the MathUtils class to compute the factorials of integers entered by the user. In this program, two things can possibly occur: The program always returns 1 if negative integers are entered by the user. Returning 1 as the factorial of any negative integer is not correct. The program also returns negative numbers when the user enters integers greater than 16. Returning a negative number for values over 16 also...
There is a white space in between but nothing is missing it's
just the way screenshot is taken Can someone help me to solve this
question
Get Homework Help C PART 1: (7 Marks) Cre PDAssignment 2 Q BOStart Assignment New tab X file:///C:/Users/ksimr/AppData/Local/Packages/Microsoft.MicrosoftEdge_8wekyb3d8bbwe/TempState/Downloads/Assignment%202%20Question%20 PART I: (7 Marks) Create an exception hierarchy of ExceptionA, ExceptionB and ExceptionC such that ExceptionB inherits from ExceptionA and ExceptionC inherits from ExceptionB. 6 Write a test program to show that the catch block for...
Instructions: Refer to the Timel class provided in Figure 8.1 in your Deitel book to complete the exercise. Make sure that you follow the Program Style and Readability guidelines found in the Start Here Folder. 1. Write a Point class. The Point class should be written as an abstract data type. 2. Include the following instance variables: a. an integer representing the x coordinate b. an integer representing the y coordinate c. The instance variables in your program should only...