Homework 10 is due this Sunday at 11:59 PM.
You will find the assignment here.
If you have a problem or a question, make a post on the Class Discussion Area.
for loop outside the loop
public class Greeting {
String greeting = "Hello";
public void say_something(){
String greeting = "Goodbye";
System.out.println(greeting);
}
}
this to access the object's fields
static DATA_TYPE VARIABLE_NAME;
public static final DATA_TYPE VARIABLE_NAME = VALUE;
Point12() Point12(int x, int y)
p.x = 7; p.y = 2;
public class Time1 {
int hours;
int minutes;
int seconds;
public Time1 (){
this.hours = 0;
this.minutes = 0;
this.seconds = 0;
}
public String toString(){
return hours + ":" + minutes + ":" + seconds;
}
}
public class Time1Driver {
public static void main (String[] args){
Time1 t = new Time1();
t.hours = 30;
t.minutes = 90;
t.seconds = 100;
System.out.println(t);
}
}
$ java Time1Driver
30:90:100
private
private DATA_TYPE FIELD_NAME;
private can only be changed by instance methods
inside the class
public class Time2 {
private int hours;
private int minutes;
private int seconds;
public Time2(){
this.hours = 0;
this.minutes = 0;
this.seconds = 0;
}
public Time2(int hours, int minutes, int seconds){
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
}
public String toString(){
return hours + ":" + minutes + ":" + seconds;
}
}
public class Time2Driver {
public static void main (String[] args){
Time2 t = new Time2();
t.hours = 30;
t.minutes = 90;
t.seconds = 100;
System.out.println(t);
}
}
we will get errors
$ javac Time2Driver.java
Time2Driver.java:4: error: hours has private access in Time2
t.hours = 30;
^
Time2Driver.java:5: error: minutes has private access in Time2
t.minutes = 90;
^
Time2Driver.java:6: error: seconds has private access in Time2
t.seconds = 100;
^
3 errors
public class Time2Driver2 {
public static void main (String[] args){
Time2 t = new Time2(30, 90, 100);
System.out.println(t);
}
}
$ java Time2Driver2
30:90:100
public Time3(int hours, int minutes, int seconds){
if (hours < 0 || hours > 23){
System.out.println("Hours must be between 0 and 23");
} else if ( minutes < 0 || minutes > 59){
System.out.println("Minutes must be between 0 and 59");
} else if ( seconds < 0 || seconds > 59){
System.out.println("seconds must be between 0 and 59");
} else {
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
}
}
public class Time3Driver {
public static void main (String[] args){
Time3 t = new Time3(30, 90, 100);
System.out.println(t);
}
}
$ java Time3Driver Hours must be between 0 and 23 0:0:0
throw new EXCEPTION_TYPE()
throws clauseIllegalArgumentException
public Time4(int hours, int minutes, int seconds)
throws IllegalArgumentException {
if (hours < 0 || hours > 23){
throw new IllegalArgumentException ("Hours must be between 0 and 23");
} else if ( minutes < 0 || minutes > 59){
throw new IllegalArgumentException ("Minutes must be between 0 and 59");
} else if ( seconds < 0 || seconds >s 59){
throw new IllegalArgumentException ("Minutes must be between 0 and 59");
} else {
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
}
}
public class Time4Driver {
public static void main (String[] args){
try {
Time4 t = new Time4(30, 90, 100);
System.out.println(t);
} catch (Exception e){
System.out.println(e);
}
}
$ java Time4Driver
Hours must be between 0 and 23
0:0:0
public Time5(int hours, int minutes, int seconds)
throws IllegalArgumentException {
if (entry_invalid(hours, 23)){
throw exception_create("Hours", 23);
} else if (entry_invalid(minutes, 59)){
throw exception_create("Minutes", 59);
} else if (entry_invalid(seconds, 59)){
throw exception_create("Seconds", 59);
} else {
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
}
}
public boolean entry_invalid (int value, int max) {
return value < 0 || value > max;
}
public IllegalArgumentException exception_create(String label, int max){
String err_msg = label + " must be between 0 and " + max;
return new IllegalArgumentException(err_msg);
}
public class Time5Driver {
public static void main (String[] args){
try {
Time5 t = new Time5(30, 90, 100);
System.out.println(t);
} catch (Exception e){
System.out.println(e);
}
}
}
$ java Time5Driver
java.lang.IllegalArgumentException: Hours must be between 0 and 23
private RETURN_TYPE METHOD_NAME(PARAMETER)
private boolean entry_invalid (int value, int max) {
return value < 0 || value > max;
}
private IllegalArgumentException exception_create(String label, int max){
String err_msg = label + " must be between 0 and " + max;
return new IllegalArgumentException(err_msg);
}
public int get_hours(){
return hours;
}
public int get_minutes(){
return minutes;
}
public int get_seconds(){
return seconds;
}
public class Time7Driver {
public static void main (String[] args){
try {
Time7 t = new Time7(9, 30, 0);
System.out.println(t);
System.out.println("Hours: " + t.get_hours());
System.out.println("Minutes: " + t.get_minutes());
System.out.println("Seconds: " + t.get_seconds());
} catch (Exception e){
System.out.println(e);
}
}
}
$ java Time7Driver
9:30:0
Hours: 9
Minutes: 30
Seconds: 0
public void set_hours(int hours){
this.hours = hours;
}
public void set_minutes(int minutes){
this.minutes = minutes;
}
public void set_seconds(int seconds){
this.seconds = seconds;
}
public class Time8Driver {
public static void main (String[] args){
try {
Time8 t = new Time8(9, 30, 0);
System.out.println(t);
t.set_hours(10);
t.set_minutes(15);
t.set_seconds(30);
System.out.println(t);
} catch (Exception e){
System.out.println(e);
}
}
}
$ java Time8Driver
9:30:0
10:15:30
private int total_seconds;
private int hours_minutes_seconds_to_total_seconds(int hours, int minutes, int seconds){
return hours * 3600 + minutes * 60 + seconds;
}
private int total_seconds_to_hours(){
return this.total_seconds / 3600;
}
private int total_seconds_to_minutes(){
int total = this.total_seconds;
total -= total_seconds_to_hours() * 3600;
return total / 60;
}
public Time9(){
this.total_seconds = 0;
}
public Time9(int hours, int minutes, int seconds)
throws IllegalArgumentException {
if (entry_invalid(hours, 23)){
throw exception_create("Hours", 23);
} else if (entry_invalid(minutes, 59)){
throw exception_create("Minutes", 59);
} else if (entry_invalid(seconds, 59)){
throw exception_create("Seconds", 59);
} else {
this.total_seconds = hours_minutes_seconds_to_total_seconds(hours, minutes, seconds);
}
}
public int get_hours(){
return total_seconds_to_hours();
}
public int get_minutes(){
return total_seconds_to_minutes();
}
public int get_seconds(){
return total_seconds_to_seconds();
}
public String toString(){
//return hours + ":" + minutes + ":" + seconds;
return get_hours() + ":" + get_minutes() + ":" + get_seconds();
}
public class Time9Driver {
public static void main (String[] args){
try {
Time9 t = new Time9(9, 30, 15);
System.out.println(t);
System.out.println(t.get_hours());;
System.out.println(t.get_minutes());;
System.out.println(t.get_seconds());;
t.set_hours(10);
t.set_minutes(5);
t.set_seconds(45);
System.out.println(t);
} catch (Exception e){
System.out.println(e);
}
}
}
$ java Time9Driver
9:30:15
9
30
15
10:5:45