- If you haven’t completed the previous exercises in this chapter, do so now.
- Open TheTimeClass.java and add the following methods. These are getters and setters.
public void setHour(int hour){ this.hour = hour; } public void setMinute(int minute){ this.minute = minute; } public void setSecond(int second){ this.second = second; } public int getHour(){ return this.hour; } public int getMinute(){ return this.minute; } public int getSecond(){ return this.second; }
- Close TheTimeClass.java
- Create a new class named TimeClient in a new file and add a the following code to a main method.
public class TimeClient { public static void main(String[] args){ TheTimeClass myTime = new TheTimeClass(); myTime.setHour(12); myTime.setMinute(32); myTime.setSecond(45); String timeString = myTime.getHour() + ":" + myTime.getMinute() + ":" + myTime.getSecond(); System.out.println(timeString); } }
- Compile both classes and run the program.
$ javac TheTimeClass.java $ javac TimeClient.java $ java TimeClient 12:32:45