![]() |
| |||||||
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 (permalink) |
| Go State! Join Date: Dec 2007
Posts: 950
Reputation: 52 ![]() cBay Rating: | GUIDLINES AND OVERVIEW OF PROJECT IN THIRD POST!! That is the assignment.... Code: //HOMEWORK 3- The Duration class that is used by the Exercise Calculator
public class Duration {
private int hours;
private int minutes;
private int seconds;
//Default constructor that sets the variables to zero
public Duration(){
hours = 0;
minutes = 0;
seconds = 0;
}
//This constructor takes the hours, minutes, and seconds as parameters and sets them
//into the proper format with no more than 59 in the seconds and minutes
public Duration(int hr, int min, int sec){
if (min > 59){
hr = hr + min / 60;
min = min % 60;
}
if (sec > 59){
min = min + sec / 60;
sec = sec % 60;
}
hours = hr;
minutes = min;
seconds = sec;
}
//This constructor takes just seconds as the parameter. It then takes the seconds and
//converts it to the hours, minutes, and seconds format.
public Duration(int totalSeconds){
hours = totalSeconds / 3600;
totalSeconds = totalSeconds % 3600;
minutes = totalSeconds / 60;
seconds = totalSeconds % 60;
}
//This takes a Duration object as a parameter and then copies the info inside it to a
//different Duration object.
public Duration(Duration other){
hours = other.getHours();
minutes = other.getMinutes();
seconds = other.getSeconds();
}
//This returns the hours for the object called on.
public int getHours(){
return hours;
}
//This returns the minutes for the object called on.
public int getMinutes(){
return minutes;
}
//This returns the seconds for the object called on.
public int getSeconds(){
return seconds;
}
//This method sets the hours
public void setHours(int hr){
hr = hours;
}
//This method takes any int and separates into minutes and hours
public void setMinutes(int min){
if(min > 59){
hours = hours + min / 60;
minutes = min % 60;
}
else min = minutes;
}
//This method takes any int as seconds and separates it into minutes and seconds
public void setSeconds(int sec){
if(sec > 59){
minutes = minutes + sec / 60;
seconds = sec % 60;
}
else sec = seconds;
}
//This method takes a duration object as a parameter and adds the hours, minutes, and seconds
//of that object to the object that is already running.
public Duration add(Duration plus){
Duration n = new Duration(hours+plus.getHours(), minutes+plus.getMinutes(), seconds+plus.getSeconds());
return n;
}
//This method takes an int as an argument and multiplies the hours, minutes, and seconds each
//by the the int passed in.
public Duration multiply(int n){
Duration s = new Duration(hours * n, minutes* n, seconds * n);
return s;
}
//This returns the hours, minutes, and seconds of an object in a readable string
public String toString(){
return (hours+" hours "+minutes+" minutes "+seconds+" seconds");
}
//This takes a Duration object as an argument and tests if the hours, minutes, and seconds are the
//same as the object already running. It returns true if they are equal and false if they are not
//equal or if the object equals null.
public boolean equals(Duration equal){
if (equal == null){
return false;
}
if(hours == equal.getHours() && minutes == equal.getMinutes() && seconds == equal.getSeconds()){
return true;
}
return false;
}
//This takes a Duration object and compares it to the object already running and returns 1 if the object
//already running is bigger, -1 if it is smaller, and 0 if they are the same.
public int compareTo(Duration compare){
int sec = compare.getHours()*3600 + compare.getMinutes()*60 + compare.getSeconds();
int originalSec = hours*3600 + minutes*60 + seconds;
if(sec > originalSec) return -1;
if(sec < originalSec) return 1;
return 0;
}
}
Here is what I have so far in the Time Class..... Code: public class Time {
private int hours;
private int minutes;
private int seconds;
public Time(int hours1, int min1, int sec1) throws Exception{
if(sec1<0 || sec1>59 || min1<0 || min1>59 || hours1<0 || hours1>23){
throw new Exception("Invalid time entry");
}
hours = hours1;
minutes = min1;
seconds = sec1;
}
//This returns the hours for the object called on.
public int getHours(){
if(hours>12){
hours -= 12;
}
return hours;
}
//This returns the minutes for the object called on.
public int getMinutes(){
return minutes;
}
//This returns the seconds for the object called on.
public int getSeconds(){
return seconds;
}
////////////////////////////////////////////////////HAVE TO RETURN AM/PM
//This returns the hours, minutes, and seconds of an object in a readable string
public String toString(){
return (hours+" hours "+minutes+" minutes "+seconds+" seconds");
}
public static Time subtract(Time sub){
if()
}
}
Last edited by icefisherman; 04-01-2009 at 06:25 PM.. |
| | |
| Sponsored Links | |
| | #3 (permalink) |
| Go State! Join Date: Dec 2007
Posts: 950
Reputation: 52 ![]() cBay Rating: | Shit I thought that would happen Code: ComS 227 |
| | |
| | #7 (permalink) |
| Senior Member Join Date: Dec 2007
Posts: 837
Reputation: 64 ![]() cBay Rating: | Ok, I'll start with the toString method since that's where it appears you left off. Code: public String toString()
{
String output;
if (hours > 12)
output = hours%12 + ":" + minutes + ":" + seconds +" PM";
else output = hours%12 + ":" + minutes + ":" + seconds + " AM";
return output;
}
Code: public static Duration Subtract(Time t1, Time t2)
{
int hr_dif, min_dif, sec_dif;
if t1.getHours() < t2.getHours)
//throw exception I'll leave this up to you.
else
{
hr_dif = t1.getHours() - t2.getHours();
min_dif = t1.getMinutes - t2.getMinutes();
sec_dif = t1.getSeconds() - t2.getSeconds();
}
//create new Duration object with the difference in time
Duration time_dif = new Duration(hr_dif, min_dif, sec_dif);
return time_dif;
}
EDIT: The subtract method is incorrect for a number of reasons (negative seconds) and (what if seconds/minutes > 60). I'll leave this part up to you. ;-) 2nd EDIT: I'm retarded... the Duration class already handles minutes/seconds > 60... so really you have two options. Either use the absolute value function or double negate them. Ex. (if min < 0) min = -min. Last edited by userrc651; 03-25-2009 at 07:21 PM.. |
| | |
| | #12 (permalink) |
| Member Join Date: Feb 2009
Posts: 65
Reputation: 0 ![]() cBay Rating: | LMAO icefisherman doesn't this fall under the academic dishonesty rule? I don't blame you though I was about to do the same shit like 2 weeks ago.
__________________ Habu Gaming Mouse (Ordered 2/15/09 | Received 3/19/09) Windows Vista Ultimate (Ordered 4/19/09 | Received 5/22/09) Xbox 360 (Ordered 4/28/09 | Received N/A ) |
| | |
| | #16 (permalink) |
| Senior Member Join Date: Dec 2008
Posts: 304
Reputation: 29 ![]() cBay Rating: | Well just one thing I noticed. Code: //This method sets the hours
public void setHours(int hr){
hr = hours;
}
Code: //This method sets the hours
public void setHours(int hr){
hours = hr;
}
|
| | |
![]() |
| Tags |
| bored, coms, homework, java |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Java question | noxify | General | 1 | 11-06-2008 07:21 PM |
| Java or c++? | zero | Chit Chat | 10 | 10-26-2008 07:02 PM |
| Java Programming Teacher....at 16 | Ryutso | Chit Chat | 19 | 09-18-2008 04:20 PM |
| Java Programming | Slashmolder | General | 15 | 09-04-2008 08:29 AM |