Code 29: Time Class
Code a:
public class Test{
public static void main(String args[]){
Class2 Class2Object = new Class2();
System.out.println(Class2Object.toMilitary());
Class2Object.getTime(3, 59, 9);
System.out.println(Class2Object.toMilitary());
}
}
Code b:
public class Class2 {
private int hour;
private int minute;
private int second;
public void getTime(int h, int m, int s){
hour = ((h>=0 && h<=24) ? h : 0);
minute = ((m>=0 && m<=60) ? m : 0);
second = ((s>=0 && s<=60) ? s : 0);
}
public String toMilitary(){
return String.format("%02d:%02d:%02d", hour, minute, second);
}
}
Code 30: Display regular Time
Code a:
public class Test{
public static void main(String args[]){
Class2 Class2Object = new Class2();
System.out.println(Class2Object.toMilitary());
Class2Object.getTime(2, 59, 9);
System.out.println(Class2Object.toMilitary());
System.out.println(Class2Object.toString());
}
}
Code b:
public class Class2 {
private int hour;
private int minute;
private int second;
public void getTime(int h, int m, int s){
hour = ((h>=0 && h<=24) ? h : 0);
minute = ((m>=0 && m<=60) ? m : 0);
second = ((s>=0 && s<=60) ? s : 0);
}
public String toMilitary(){
return String.format("%02d:%02d:%02d", hour, minute, second);
}
public String toString(){
return String.format("%d:%02d:%02d %s", ((hour==0 || hour==12)? 12 : hour%12), minute, second , (hour<12)? "AM":"PM");
}
}
Code 31: Private public and this
public class Test{
public static void main(String args[]){
Class2 Class2Object = new Class2();
System.out.println(Class2Object.toMilitary());
System.out.println(Class2Object.toString());
Class2Object.getTime(2, 59, 9);
System.out.println(Class2Object.toMilitary());
System.out.println(Class2Object.toString());
}
}
Code b:
public class Class2 {
private int hour = 1;
private int minute = 2;;
private int second = 3;
public void getTime(int hour, int minute, int second){
this.hour = 5;
this.minute = 6;
this.second = 7;
}
public String toMilitary(){
return String.format("%02d:%02d:%02d", hour, minute, second);
}
public String toString(){
return String.format("%d:%02d:%02d %s", ((hour==0 || hour==12)? 12 : hour%12), minute, second , (hour<12)? "AM":"PM");
}
}
Code 32: Overloaded Constructors & Objects
Code a:
public class Test{
public static void main(String args[]){
Class2 Class2Object = new Class2();
Class2 Class2Object1= new Class2(12);
Class2 Class2Object2= new Class2(12,57);
Class2 Class2Object3= new Class2(12,57,59);
System.out.printf("%s\n", Class2Object.toMilitary() );
System.out.printf("%s\n", Class2Object1.toMilitary() );
System.out.printf("%s\n", Class2Object2.toMilitary() );
System.out.printf("%s\n", Class2Object3.toMilitary() );
}
}
Code b:
public class Class2 {
private int hour = 1;
private int minute = 2;;
private int second = 3;
public Class2(){
this(0,0,0);
}
public Class2(int h){
this(h,0,0);
}
public Class2(int h, int m){
this(h,m,0);
}
public Class2(int h, int m, int s){
setTime(h,m,s);
}
public void setTime(int h, int m, int s){
setHour(h);
setMinute(m);
setSecond(s);
}
public void setHour(int h){
hour = ((h>=0 || h<=24)?h:0);
}
public void setMinute(int m){
minute = ((m>=0 || m<=60)?m:0);
}
public void setSecond(int s){
second = ((s>=0 || s<=60)?s:0);
}
public int getHour(){
return hour;
}
public int getMinute(){
return minute;
}
public int getSecond(){
return second;
}
public String toMilitary(){
return String.format("%02d:%02d:%02d", getHour(), getMinute(), getSecond() );
}
}
Code 33 toString Function
Code a:
public class Test{
public static void main(String args[]){
Class3 Class3Object = new Class3(4,5,6);
}
}
Code b:
public class Class3 {
private int month;
private int day;
private int year;
public Class3(int m, int d, int y){
month = m;
day = d;
year = y;
System.out.printf("Thsi Constructir for this is %s\n",this);
}
public String toString(){
return String.format("%d/%d/%d", month, day, year);
}
}
No comments:
Post a Comment