java program need help!
I need to do my assingmnet for class. If any one know how to help me i would appreciate it a lot. This is my assingment:
Write a class to represent a time interval named ElapsedTime. This class represents elapsed time in days hours and minutes. It might be used by a shipping company to estimate and keep track od how long their vessels are at sea. If trip takes 7 1/2 days, elapsed time would be 7 days 12 hours and 0 minutes. Represent the current time interval as a number of minutes, stored in an int Java variable.
Constructor : ElapsedTime (int days, int hours int minutes)
This creates an object of the ElapsedTime class whose instance data is initialized according to the parameters of the constructor. For example, if the invocation of the constructor is ElapsedTime (0, 0, 0) an object is created representing a time interval of zero minutes. You may assume that all three arguments are never negative.
Public methods:
void setTime (int days int hours int minutes)
setTime() sets the current time interval according to its arguments. For example if the method invocation is setTime (7, 3,22) the time interval is 10,282 minutes. All three arguments are never negative.
void addTime (int days, hours minutes)
addTime adds to the objects current time interval according to its arguments.For example if the method invocation si addTime (1,0,0) the time interval is incresed by 1440 minutes. One or all of the arguments may be negative or zero.
Use the class Math.abs to make sure your instance data is always non-negative.
int getDays()
getDays returns the number of days not exceeded by the current time interval. This is never negative. For example if the current time is 1439 minutes getDays will return 0.
int getHours()
getHours returns number of hours in the current time interval after all whole days are taken out. This is never negative and never exceeds 23.
int getMinutes()
getMinutes returns the number of minutes in the current time interval after all whole days and all whole hours are taken out. This is never negative and never exceeds 59.
String toString()
to String returns a String object representing the current time interval in days hours and minutes. THe form of the String shuld be days, hours, minutes where days is at least one digit, hours is always two digit between 00 and 23 and minutes two digits between 00 and 59. You can use Decimal Format class with a pattern of "00" to force a number to be represented by a string with a leading zero.
Part 2.
Write a test program to test the ElapsedTime class. The program shuld be written as a seperate class that instantiates an ElapsedTime object and calls its methods. In the test program, test each method with arguments supplied by your program to see if it works. For each test, use JOptionPane.showMessageDialog() to output what the test is what the results are and what was expected.
PLEASE IF ANYONE KNOW HOW TO DO THIS HELP ME!
THANK YOU!
[3066 byte] By [
knindza] at [2007-11-11 8:17:05]

# 4 Re: java program need help!
public class ElapsedTime{
private int timeInMinutes=0;
public ElapsedTime(){
}
public void setTime (int days, int hours, int minutes, int timeInterval){
timeInterval=(days*1440)+(hours*60)+minutes;
}
public void addTime (int days, int hours, int minutes, int time, int timeInterval){
Math.abs(timeInMinutes=0);
time=timeInterval+(days*1440)+(hours*60)+minutes;
}
public int getDays (int days, int timeInterval){
days=timeInterval/1440;
return days;
}
public int getHours (int timeInterval, int hours){
hours=timeInterval/60;
return hours;
}
public int getMinutes (int timeInterval, int minutes){
minutes=timeInterval/1440;
return minutes;
}
public String toString (){
}
}
I`am not sure about the one string toString.
# 5 Re: java program need help!
Do you know how Java classes track or use "time"? Have you studied the "time" classes included with the Java API? Including the Date and Calendar classes?
I think if I were approaching this project, I would save the "start" time - the current "time" when I create an object of the class. Then, whenever I query the object for its elapsed time, it would report back to me the difference between what I have stored as the start time and now. I could then make the various unit conversions to report the days, hours, minutes, seconds, etc. which have elapsed.
The "toString" method just creates some representation of the current state of the class as a String object. What do you think is relevant for a user to know about the current state of the class?
nspils at 2007-11-11 22:39:56 >

# 6 Re: java program need help!
well, there are several problems in your code: for example you have
public int getDays (int days, int timeInterval){
days=timeInterval/1440;
return days;
}
but the local varaible days is not defined. correct would be:
public int getDays (int days, int timeInterval){
int days=(int)(timeInterval/1440);
return days;
}
or shorter:
public int getDays (int days, int timeInterval){
return (int)(timeInterval/1440);
}
in none of your methods you have properly initialized/declared your local variables.
then your toString method. it youst should generate a string representation of your
elapsed time. eg.:
public String toString (){
return getDays()+" days "+getHours()+" hours "+getMinutes();
}
but you will recognize, that this methods will return the whole time in their measurement, eg. getMinutes will return the whole elapsed time in minutes.
so you will have to splitt your time in minutes to the different measurements:
on calculating the splitting to days, hours and minutes you have to consider the rest properly:
int days = (int) (timerIntervall/1440);
int rest = timeInterval-days*1440;
int hours = (int) (rest/60);
rest = rest-hours*60;
int mins = rest;