Java class and object 2, java object
Write Java applications. First, define a Clock class-Clock, which includes three int-type member variables to indicate the time, minute, and second, respectively, A constructor is used to construct three member variables (hour, minute, and second)
And the show () member method is used to display the Time of the clock object. Next, define a main class-TestClass, and create multiple clock class objects in the main method of the main class.
Some objects call the show () method to display the clock time.
Class:
1 int hour; 2 int minute; 3 int second; 4 5 // The constructor initializes the variable 6 Clock (int h, int m, int s) {7 hour = h; 8 minute = m; 9 second = s; 10} 11 12 // 13 void show () {14 if (hour> = 0 & hour <24) & (minute> = 0 & minute <60) & (second> = 0 & second <60) {15 System. out. println ("Current Time" + hour + "hour" + minute + "minute" + second + "second. "); 16} else {17 System. out. println (" your input time is incorrect. Please enter it again! "); 18} 19}
Call:
1 public static void main(String[] args) { 2 Clock clock1=new Clock(3, 34, 18); 3 4 Clock clock2=new Clock(23, 34, 18); 5 6 Clock clock3=new Clock(25, 15, 55); 7 8 Clock clock4=new Clock(23, 64, 28); 9 10 Clock clock5=new Clock(23, 54, 78);11 12 Clock clock6=new Clock(0, 0, 0);13 14 clock1.show();15 clock2.show();16 clock3.show();17 clock4.show();18 clock5.show();19 clock6.show();20 21 }
Result: