Question: I am compiling Figure 8.2 in Java How to Program (early objects) (10th Edition). But when I compile, I get no errors, but my output

I am compiling Figure 8.2 in Java How to Program (early objects) (10th Edition). But when I compile, I get no errors, but my output never shows the time in After calling setTime. Here are the results I am getting "C:\Program Files\Java\jdk1.8.0_121\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.1.6\lib\idea_rt.jar=50969:C:\Program Files\JetBrains\IntelliJ IDEA 2018.1.6\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_121\lib\ant-javafx.jar;C:\Program Files\Java\jdk1.8.0_121\lib\dt.jar;C:\Program Files\Java\jdk1.8.0_121\lib\javafx-mx.jar;C:\Program Files\Java\jdk1.8.0_121\lib\jconsole.jar;C:\Program Files\Java\jdk1.8.0_121\lib\packager.jar;C:\Program Files\Java\jdk1.8.0_121\lib\sa-jdi.jar;C:\Program Files\Java\jdk1.8.0_121\lib\tools.jar;C:\Users\crobbins\IdeaProjects\Time1\out\production\Time1" Time1Test After time object is created Universal time: 00:00:00 Standard time: 12:00:00 AM After calling setTime Universal time: 00:00:00 Standard time: 12:00:00 AM Exception: hour, minute and/or second was out of range After calling setTime with invalid values Universal time: 00:00:00 Standard time: 12:00:00 AM Process finished with exit code 0

// Fig 8.2: Time1Test.java // Time1 object used in an app public class Time1Test { public static void main(String[] args){ //create and initialize a Time1 object Time1 time = new Time1(); //invoice Time1 constructor //output string representations of the time displayTime("After time object is created", time); System.out.println(); //change time and output updated time time.setTime(13,27,6); displayTime("After calling setTime", time); System.out.println(); //attempt to set time with invalid values try { time.setTime(99, 99, 99); //all values out of range } catch (IllegalArgumentException e){ System.out.printf("Exception: %s%n%n", e.getMessage()); } //display time after attem to set invalid values displayTime("After calling setTime with invalid values", time); } //displays a Time1 object in 24-hour and 12-hour formats private static void displayTime(String header, Time1 t){ System.out.printf("%s%nUniversal time: %s%nStandard time: %s%n", header, t.toUniversalString(), t.toString()); } }
//Time1.Java //Time 1 class declaration maintains the time in 24-hour format public class Time1 { private int hour; // 0-23 private int minute; // 0-59 private int second; // 0-59 //set a new time value using universal time; throw an exception //exception if the hour, minute or second is invalid public void setTime(int hour, int minute, int second) { //validate hour, minute and second if (hour < 0 || hour >= 24 || minute < 0 || minute >= 60 || second < 0 || second >= 60) { throw new IllegalArgumentException("hour, minute and/or second was out of range"); } } //convert to String in universal-time format (HH:MM:SS) public String toUniversalString() { return String.format("%02d:%02d:%02d", hour, minute, second); } // convert to String in standard-time format (H:MM:SS AM or PM) public String toString(){ return String.format("%d:%02d:%02d %s", ((hour == 0 || hour == 12) ? 12 : hour % 12), minute, second, (hour <12 ? "AM" : "PM")); } } 

I don't know how to attach the file for you. I don't see an option for doing that. I am using IntelliJ

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!