| Author |
Catch Exception and decimal format?
|
Jason Beck
Greenhorn
Joined: Sep 20, 2003
Posts: 1
|
|
Hi there, This is a sample program which actually demonstrates the OOP on a time display program: // Time abstract data type (ADT) definition public class Time { private int hour; // 0 - 23 (attributes) private int minute; // 0 - 59 (attributes) private int second; // 0 - 59 (attributes) // Time constructor initializes each data member to zero. // Ensures all Time objects start in a consistent state. public Time( ) { hour = 0; minute = 0; second = 0; } // set a new Time value hour, minute and second public void setTime(int h, int m, int s) { hour = h; minute = m; second = s; } // Print time in standard format public void printStandard( ) { System.out.print( hour + ":" + minute + ":" + second); if (hour < 12 ) System.out.println(" AM"); else System.out.println(" PM"); } // Driver to test simple class Time public static void main( String args[ ] ) { Time t = new Time( ); // instantiate object t of class time System.out.print("\nThe initial standard time is "); t.printStandard( ); t.setTime(13, 27, 6); System.out.print("\nStandard time after setTime is "); t.printStandard( ); t.setTime(99, 99, 99); // attempt invalid settings System.out.print("\nAfter attempting invalid settings: "); t.printStandard( ); } } My question is: 1.) The setTime method shown above does not check for error. For example hour must be 0-23, minutes must be 0-59 and finally seconds must be 0-59. I was told that a catch exception method can do the job. However I do not know how to do that. Can anyone please teach me how to do that? 2.) How can I display the time in military format like 9:40:12 am as 094012 and 9:40:12 pm as 214012? 3.) How can I modify the method printStandard to print time in 2 digit format: eg 9:3:5 am should be displayed as 9:03:05 am. I was told that the decimal format method should do the trick. Can anyone help me out here? You assistance would be greatly appreciated as I need to prepare for my upcoming finals. Thank you.
|
 |
Stefan Wagner
Ranch Hand
Joined: Jun 02, 2003
Posts: 1923
|
|
Originally posted by Jason Beck: 1.) The setTime method shown above does not check for error. For example hour must be 0-23, minutes must be 0-59 and finally seconds must be 0-59. I was told that a catch exception method can do the job. However I do not know how to do that. Can anyone please teach me how to do that?
2.) How can I display the time in military format like 9:40:12 am as 094012 and 9:40:12 pm as 214012? 3.) How can I modify the method printStandard to print time in 2 digit format: eg 9:3:5 am should be displayed as 9:03:05 am. I was told that the decimal format method should do the trick. Can anyone help me out here? You assistance would be greatly appreciated as I need to prepare for my upcoming finals. Thank you. I guess in java.text (or was it 'java.util.text'?) there are formatter methods. Have a look at the api-docs. Did I do your homework well?
|
http://home.arcor.de/hirnstrom/bewerbung
|
 |
 |
|
|
subject: Catch Exception and decimal format?
|
|
|