Created
January 25, 2021 04:36
-
-
Save BT-ICD/87418986df3530a8797bba0f1e8c1173 to your computer and use it in GitHub Desktop.
Example of method overloading. MyTime Class with add method and Object argument.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Example of method overloading | |
*Create a class name MyTime as follows: | |
* | |
* List of data member | |
* 1. hours | |
* 2. minutes | |
* 3. seconds | |
* | |
* List of methods | |
* 1. setTime – to initialize data member of Time class | |
* 2. printTime – to print current time such as HH: MI : SS | |
* 3. add(int) – add number of seconds to the current object of Time class | |
* 4. add(int , int ) – add specified number of minutes as well as seconds to current object of time | |
* 5. add(Time) – to add specified object of time as an argument to the current object of time | |
* | |
* Try to learn and implement - DRY Principle | |
* Reference: https://en.wikipedia.org/wiki/Don%27t_repeat_yourself | |
* | |
* */ | |
class MyTime{ | |
private int hours, minutes, seconds; | |
/** | |
* To initialize private data member from hours, minutes and seconds | |
*/ | |
void setTime(int hours, int minutes, int seconds){ | |
int totalSeconds = getSecondsFrom(hours,minutes,seconds); | |
initializeTimeFromSeconds(totalSeconds); | |
} | |
void setTime( int minutes, int seconds){ | |
int totalSeconds = getSecondsFrom(0,minutes,seconds); | |
initializeTimeFromSeconds(totalSeconds); | |
} | |
void setTime( int seconds){ | |
int totalSeconds = getSecondsFrom(0,0,seconds); | |
initializeTimeFromSeconds(totalSeconds); | |
} | |
void printTime(){ | |
System.out.println("Hours: " + hours + " Minutes: " + minutes + " Seconds: " + seconds); | |
} | |
void add(int seconds){ | |
add(0,seconds); | |
} | |
void add(int minutes, int seconds){ | |
int totalseconds = getTimeInSeconds() + seconds + (minutes*60); | |
initializeTimeFromSeconds(totalseconds); | |
} | |
void add(MyTime t){ | |
int totalSeconds = this.getTimeInSeconds() +t.getTimeInSeconds(); | |
this.initializeTimeFromSeconds(totalSeconds); | |
} | |
private int getSecondsFrom(int hours, int minutes, int seconds){ | |
int result; | |
result = seconds; | |
result += minutes*60; | |
result += hours * 3600; | |
return result; | |
} | |
private int getSecondsFrom(int minutes, int seconds){ | |
return getSecondsFrom(0,minutes,seconds); | |
} | |
int getTimeInSeconds(){ | |
return ((hours*3600) + (minutes*60) + seconds); | |
} | |
/** | |
* @param seconds | |
* To convert seconds into Hours, Minutes and Seconds | |
*/ | |
private void initializeTimeFromSeconds(int seconds){ | |
int totalSeconds; | |
this.hours = seconds/3600; | |
totalSeconds = seconds%3600; | |
this.minutes = totalSeconds/60; | |
this.seconds = totalSeconds%60; | |
} | |
} | |
public class TimeDemo { | |
public static void main(String[] args) { | |
MyTime t1 = new MyTime(); | |
t1.setTime(1,66,3680); | |
t1.printTime(); | |
MyTime t2= new MyTime(); | |
t2.setTime(0,0,40); | |
t2.printTime(); | |
t2.add(80); | |
t2.printTime(); | |
t1.add(t2); | |
System.out.println("After t1 + t2"); | |
t1.printTime(); | |
t2.printTime(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample output:
Hours: 3 Minutes: 7 Seconds: 20
Hours: 0 Minutes: 0 Seconds: 40
Hours: 0 Minutes: 2 Seconds: 0
After t1 + t2
Hours: 3 Minutes: 9 Seconds: 20
Hours: 0 Minutes: 2 Seconds: 0