wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Using constructors to add and manage time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using constructors to add and manage time" Watch "Using constructors to add and manage time" New topic
Author

Using constructors to add and manage time

Brandon Peeples
Ranch Hand

Joined: Jan 06, 2010
Posts: 38
Hello, I would like some advice / guidance for my java problem. I am trying to stores a time in hours, minutes, and seconds. Additional time may be added to the current time and then it reports the real-valued time in hours in decimal form. Here is an example input and output.

Input:

Time t = new Time(3, 45, 15);
System.out.println(t + " equals " + t.getTime());
t.add(2, 30, 55);
System.out.println(t + " equals " + t.getTime());
t.add(0, 55, 45);
System.out.println(t + " equals " + t.getTime());

Output:

3:45:15 equals 3.754166666666667
6:16:10 equals 6.269444444444445
7:11:55 equals 7.198611111111111

Here is my actual code:



I have the first part of it working out correctly. My problem is adding the additional time and converting that additional time to the decimal hour form. The "add" part of my program is basically blank because I don't know what to do in order to add additional time and convert it. Any help would be greatly appreciated. Thanks guys.
Brandon Peeples
Ranch Hand

Joined: Jan 06, 2010
Posts: 38
After researching more about the "this" keyword. I figured out how to make it work.

Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Are you writing this as an exercise or for an actual program because then I would recommend to use the java classes for this: java.util.Date and java.util.Calendar or the joda-time library.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Brandon Peeples
Ranch Hand

Joined: Jan 06, 2010
Posts: 38
It was for an exercise. I appreciate the recommendation. I've never heard of the things you mentioned. Thank you.
Muhammad usman shad
Ranch Hand

Joined: Jan 06, 2010
Posts: 30
Hi peeples,
I am happy to sat that i have developed a program in which you can do what you are desiring,

But i am sorry to say that i could not find you here because i forgot your name and topic.

I have programmed it a week ago but due to forgetting your name, I am sending it to you now:

Here is the code,
Copy it,
Then,
Execute it and enjoy:



/* Hey, This is the program which will give you an Exact Dynamic Output */
/* You will enter the inputs of HH MM SS and then enter the time you want */
/* to add in HH MM SS and then you will get your required output */

import java.util.*;

class TimeAddition
{
public static void main (String[] args)
{
TimeAddition timeAddObj = new TimeAddition();
timeAddObj.insertTime();
}
public void insertTime()
{
double hoursInserted,minutesInserted,secondsInserted;

System.out.println("Please enter the time:\n");
Scanner s = new Scanner(System.in);
hoursInserted = s.nextDouble();
minutesInserted = s.nextDouble();
secondsInserted = s.nextDouble();

TimeAddition timeAddObj = new TimeAddition();
timeAddObj.addTime(hoursInserted,minutesInserted,secondsInserted);
}
public void addTime(double hoursInserted, double minutesInserted, double secondsInserted)
{
double hoursAdded,minutesAdded,secondsAdded;

System.out.println("Please enter the Time you want to add:\n");

Scanner s = new Scanner(System.in);
hoursAdded = s.nextDouble();
minutesAdded = s.nextDouble();
secondsAdded = s.nextDouble();

TimeAddition timeAddObj = new TimeAddition();
timeAddObj.resultantTime(hoursInserted,minutesInserted,secondsInserted,hoursAdded,minutesAdded,secondsAdded);
}
public void resultantTime(double hoursInserted,double minutesInserted,double secondsInserted,double hoursAdded,double minutesAdded,double secondsAdded)
{
double resultedHours;
double resultedMinutes;
double resultedSeconds;

resultedHours = hoursInserted+hoursAdded;
resultedMinutes = minutesInserted+minutesAdded;
resultedSeconds = secondsInserted+secondsAdded;

TimeAddition timeAddObj = new TimeAddition();
timeAddObj.decimalTime(resultedHours,resultedMinutes,resultedSeconds);
}
public void decimalTime(double resultedHours, double resultedMinutes, double resultedSeconds)
{
double decTime;
decTime = resultedHours+(resultedMinutes/60)+(resultedSeconds/60/60);

System.out.println("Your Required Added Time in Decimal is:\n"+decTime+" Hours...");
}
}
Brandon Peeples
Ranch Hand

Joined: Jan 06, 2010
Posts: 38
I did figure out a solution but I do appreciate the response. It's always nice to see things done another way. Thanks!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

Muhammad, please Use Code Tags in the future.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using constructors to add and manage time