I am attempting to create a digital clock. However, I have run into an issue I am trying to create a method name findNormalizedHour which converts from a 24-hour format to a 12-hour format. This code is passing all of my tests except for it is allowing hours greater than 23 (it is supposed to throw an error) and it is allowing negative numbers (it is supposed to throw an error). How do I fix these two things? I would appreciate any assitance! So far this is what I have: public  int findNormalizedHour(int hour) {         // if hour is less than 12 but not equal to 0          if(hour<12 && hour != 0){                 return hour;             }          if(hour>12) {              return hour - 12;          }          if(hour == 12) {              return hour;          }          if (hour>23 ) {         throw new IllegalArgumentException("Invalid input caused by an hour greater than 23.");          }          if (hour < 0) {         throw new IllegalArgumentException("Invalid input caused by a negative hour.");          }                       // otherwise             else{                 return 12 - hour;             }     }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

I am attempting to create a digital clock. However, I have run into an issue I am trying to create a method name findNormalizedHour which converts from a 24-hour format to a 12-hour format. This code is passing all of my tests except for it is allowing hours greater than 23 (it is supposed to throw an error) and it is allowing negative numbers (it is supposed to throw an error). How do I fix these two things? I would appreciate any assitance!

So far this is what I have:
public  int findNormalizedHour(int hour) {
        // if hour is less than 12 but not equal to 0
         if(hour<12 && hour != 0){
                return hour;
            }
         if(hour>12) {
             return hour - 12;
         }
         if(hour == 12) {
             return hour;
         }
         if (hour>23 ) {
        throw new IllegalArgumentException("Invalid input caused by an hour greater than 23.");
         }
         if (hour < 0) {
        throw new IllegalArgumentException("Invalid input caused by a negative hour.");
         }
         
            // otherwise
            else{
                return 12 - hour;
            }
    }


Expert Solution
Step 1

You need to keep them in try and catch block and last else is not needed all conditions you have already mentioned. 

And i have some of lines of you for one case if the hour==12 you can include in first if condition

public  int findNormalizedHour(int hour) {
        // if hour is less than 12 but not equal to 0
         if(hour<=12 && hour != 0){
                return hour;
            }
         if(hour>12) {
             return hour - 12;
         }
         if (hour>23 ) {

           try{
        throw new IllegalArgumentException("Invalid input caused by an hour greater than 23.");

       }

          catch(MyException e){

                 System.outr.println(e.getMessage());

           }


         }
         if (hour < 0) {

           try{
        throw new IllegalArgumentException("Invalid input caused by a negative hour.");

          }

          catch(MyException e){

                 System.outr.println(e.getMessage());

           }
         }
         
            // otherwise
            else{
                return 12 - hour;
            }
    }

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Math class and its different methods
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education