Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Time.java
- ---------
- public class Time {
- int hours;
- int minutes;
- Time() {
- this.hours = 0;
- this.minutes = 0;
- }
- Time(int hours, int minutes) {
- setHours(hours);
- setMinutes(minutes);
- }
- void setHours(int hours) {
- if (hours >= 0 && hours <= 23) {
- this.hours = hours;
- }
- }
- void setMinutes(int minutes) {
- if (minutes >= 0 && minutes <= 59) {
- this.minutes = minutes;
- }
- }
- int getHours() {
- return this.hours;
- }
- int getMinutes() {
- return this.minutes;
- }
- void add(int minutes) {
- this.minutes = this.minutes + minutes;
- if (this.minutes >= 60) {
- this.hours += this.minutes / 60;
- this.minutes = this.minutes % 60;
- if (this.hours >= 24) {
- this.hours = this.hours % 24;
- }
- }
- }
- boolean isAfter(Time t) {
- if (this.hours > t.hours) {
- return true;
- } else if (this.hours < t.hours) {
- return false;
- } else if (this.minutes > t.minutes) {
- return true;
- } else {
- return false;
- }
- }
- boolean isBefore(Time t) {
- if (this.hours == t.hours) {
- return this.minutes < t.minutes;
- }
- return this.hours < t.hours;
- }
- public String toString() {
- String res = "";
- if (this.hours < 10)
- res = "0" + this.hours + ":";
- else
- res = this.hours + ":";
- if (this.minutes < 10)
- res += "0" + this.minutes;
- else
- res += this.minutes + "";
- return res;
- }
- }
- RunTume.java
- ------------
- import java.util.Scanner;
- public class RunTime {
- public static void main(String[] args) {
- Time t;
- t = new Time();
- System.out.println(t);
- t.setHours(8);
- t.setMinutes(25);
- System.out.println(t);
- t.setHours(120);
- t.setMinutes(151);
- System.out.println(t);
- System.out.println("-------------------");
- Time t1 = new Time(22, 12);
- System.out.println(t1);
- t1.add(23);
- System.out.println(t1);
- t1.add(23);
- System.out.println(t1);
- t1.add(24 * 60 * 3 + 10);
- System.out.println(t1);
- System.out.println("-------------------");
- Scanner in = new Scanner(System.in);
- System.out.println("enter time: ");
- int h = in.nextInt();
- int m = in.nextInt();
- Time start = new Time(h, m);
- System.out.println("enter minutes to add: ");
- int min = in.nextInt();
- start.add(min);
- System.out.println("The time will be " + start);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement