-
Add up time with a class
Hey,
I have to make a class to add up time.
the methods and the variables i have to use are.
Time class
private int hour
private int minute
private int second
the methods of this class are
time(int h,int m, int s)
void show()
time addUp(time t)
void expand(time t)
void setHour(int h)
This class has to be called on from TestTime class where you make two objects of the time type, add these up and show them on screen
This is something i have to do for school but can't get out off
Thnx
-
You'll have to be more specific than that. What have you done so far, and which bits are you finding hard? No one will do it for you, but we will help you.
-
class Time {
private int hour;
private int minute;
private int second;
Time(int u,int m,int s){
hour = u;
minute = m;
second = s;
}
public int addup(tijd t){
return ??;
}
public static void Show(){
System.out.println("The result is " + ??????);
}
}
In the time class i don't now how to add up time. and use setTime method
code to add up time :
result=((second+second)%60) +(((second+second)/60)+((hour+hour)%60))+
(((hour+hour)+((minute+minute)/60)
meaning you add up the seconds and whats over 60 seconds (1 minute) is seconds , same for minutes. I know this isn't the right code but it's the idea of it that i know, but how do i put this in a method (addup).
Class TestTimeƧ
public static main(String []args){
Time t1 = new Time(10.20,12);
time t2 = new Time( 20.30,14);
}
here i have to call on the methods in Time to add them up and show the result.
can i call on the show method with Time.show()
-
Well first up, your addUp method needs to return a Time object, not an int. You need to change its signature.
For the method, declare three int variables and name them newHour, newMinute and newSecond. Then use the formula you have to calculate the new values for these. eg
Code:
newSeconds = (this.seconds + t.getSeconds) % 60;
You then need to use these variables to create a new Time object, and return it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks