• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using constructors to add and manage time

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After researching more about the "this" keyword. I figured out how to make it work.

 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Brandon Peeples
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It was for an exercise. I appreciate the recommendation. I've never heard of the things you mentioned. Thank you.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did figure out a solution but I do appreciate the response. It's always nice to see things done another way. Thanks!
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Muhammad, please Use Code Tags in the future.
 
reply
    Bookmark Topic Watch Topic
  • New Topic