Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Class whose objects are point in time represented by hours, minutes and seconds.
- * The class has getters that return the hours, minutes and seconds for a given time stamp.
- * The class has no setters due to the implementation of the method void skip(TimeStamp time).
- * This method can change a timestamp if that is what one want to do.
- */
- public class TimeStamp{
- //Attributes
- private int hours;
- private int minutes;
- private int seconds;
- public static void main(String[] args){
- System.out.println("This is a class that can manage time stamps.");
- System.out.println("The timestamps are given in hours:minutes:seconds.");
- System.out.println("Hours are in the interval from 0 to 23, minutes in ");
- System.out.println("the interval 0 to 59 and seconds in the interval from 0 to 59.");
- System.out.print("\n");
- TimeStamp timeStamp1 = new TimeStamp(23, 59, 59);
- TimeStamp timeStamp2 = new TimeStamp(1, 23, 45);
- System.out.println(timeStamp1.toString());
- timeStamp1.skipMinute();
- System.out.println(timeStamp1.toString());
- timeStamp1.skipSecond();
- System.out.println(timeStamp1.toString());
- timeStamp1.skip(timeStamp2);
- System.out.println(timeStamp1.toString());
- System.out.println(timeStamp1.equals(timeStamp2));
- System.out.println(timeStamp1.copy().toString());
- }
- //Getters
- public int getHours(){
- return this.hours;
- }
- public int getMinutes(){
- return this.minutes;
- }
- public int getSeconds(){
- return this.seconds;
- }
- //Default constructor i.e. hours, minutes and seconds are set to 0.
- public TimeStamp(){
- this.hours = 0;
- this.minutes = 0;
- this.seconds = 0;
- }
- //Constructor where you set the hours. Minutes and seconds are set to 0.
- public TimeStamp(int hours){
- if(valid(hours, 0, 0)){
- this.hours = hours;
- this.minutes = 0;
- this.seconds = 0;
- } else {
- System.out.println("Please enter valid amount of hours");
- }
- }
- //Constructor where you set the hours and minutes. Seconds are set to 0.
- public TimeStamp(int hours, int minutes){
- if(valid(hours, minutes, 0)){
- this.hours = hours;
- this.minutes = minutes;
- this.seconds = 0;
- } else {
- System.out.println("Please enter valid amount of hours and/or minutes");
- }
- }
- //Constructor where you set the hours, minutes and seconds.
- public TimeStamp(int hours, int minutes, int seconds){
- if(valid(hours, minutes, seconds)){
- this.hours = hours;
- this.minutes = minutes;
- this.seconds = seconds;
- } else {
- System.out.println("Please enter valid amount of hours, minutes and/or seconds");
- }
- }
- //Method that checks whether the given time stamp is valid
- public boolean valid(int hours, int minutes, int seconds){
- boolean valid;
- if(0 <= hours && hours < 24 && 0 <= minutes && minutes < 60 && 0 <= seconds && seconds < 60){
- valid = true;
- } else {
- valid = false;
- }
- return valid;
- }
- //Method that adds one second to a time stamp
- public void skipSecond(){
- if(seconds < 59){
- seconds = seconds + 1;
- } else {
- seconds = 0;
- skipMinute();
- }
- }
- //Method that adds one minute to a time stamp
- public void skipMinute(){
- if(minutes < 59){
- minutes = minutes + 1;
- } else {
- minutes = 0;
- skipHour();
- }
- }
- //Method that adds one hour to a time stamo
- public void skipHour(){
- if(hours < 23){
- hours = hours + 1;
- } else {
- hours = 0;
- }
- }
- //Adds the amount of time described in time to the time stamp.
- public void skip(TimeStamp time){
- for(int i = 0; i < time.getSeconds(); i++){
- skipSecond();
- }
- for(int i = 0; i < time.getMinutes(); i++){
- skipMinute();
- }
- for(int i = 0; i < time.getHours(); i++){
- skipHour();
- }
- }
- //Checks whether two time stamps are equal.
- public boolean equals(Object other){
- TimeStamp otherTimeStamp = (TimeStamp)other;
- return this.seconds == otherTimeStamp.seconds && this.minutes == otherTimeStamp.minutes && this.hours == otherTimeStamp.hours;
- }
- //Produces a copy of a time stamp.
- public TimeStamp copy(){
- TimeStamp copy = new TimeStamp();
- copy.seconds = this.seconds;
- copy.minutes = this.minutes;
- copy.hours = this.hours;
- return copy;
- }
- //toString method that returns textual representation of a time stamp
- public String toString(){
- return getHours() + ":" + getMinutes() + ":" + getSeconds();
- }
- }
- _______________________________________________________________________________________________________________________________
- /*
- * Class with objects that represent a date as a year, month and day.
- * We have getters for the year, month and day. Setters are omitted since
- * if we want to change a date, we do so by using void skipTime().
- */
- public class Date extends TimeStamp{
- private int year;
- private int month;
- private int day;
- //Constructor that creates a date corresponding to the given year, month and day. The time is set to midnight.
- public Date(int year, int month, int day){
- super();
- this.year = year;
- this.month = month;
- this.day = day;
- }
- //Constructor that creates a date corresponding to the given year, month, date and time stamp.
- public Date(int year, int month, int day, TimeStamp time(hours, minutes, seconds)){
- super(hours, minutes, seconds);
- this.year = year;
- this.month = month;
- this.day = day;
- }
- //Checks whether the given date is a valid date
- public boolean valid(int year, int month, int day){
- boolean valid;
- return valid;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement