473,394 Members | 1,701 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

time conversion

This is my first week in Java and i have been given the task of writing a simple program to convert inputs from a test harness and convert them to hrs,min,sec. as well as increment and decrement, and compare two inputs. basically i have no clue. i have looked all over the net but most of it is too advanced to understand. can anyone point me in the right direction?
thank you
Oct 19 '06 #1
7 3041
r035198x
13,262 8TB
This is my first week in Java and i have been given the task of writing a simple program to convert inputs from a test harness and convert them to hrs,min,sec. as well as increment and decrement, and compare two inputs. basically i have no clue. i have looked all over the net but most of it is too advanced to understand. can anyone point me in the right direction?
thank you
Split your task into smaller ones and then ask about a more specific problem. Have you programmed I/O in any language before?
Oct 20 '06 #2
I did well in C++. I understand the math part so i think i can do the actual conversions but i am having trouble with the using the test harness to pass in values. i have some code written, but it doesn't work. thank you
public class TimeSpan {

private int hour;
private int min;
private int sec;

public TimeSpan(){
hour = 0;
min = 0;
sec = 0;
}

public void setfromHours(int newHour){
hour = newHour;

}

public void setFromMinutes(int newMin){
min = newMin;
}

public void setfromSeconds(int newSec){
sec = newSec;
}

public double toHour(){
return hour + min/60 + sec/3600;

}
public double toMinutes(){
return hour * 60 + min + sec/60;

}

public int toSeconds(){
return hour*3600 + min*60 + sec;

}

public void increment(TimeSpan step){
hour = hour + step.hour;
min = min + step.min;
sec = sec + step.sec;
}

public void decrement(TimeSpan step){
hour = hour - step.hour;
min = min - step.min;
sec = sec - step.sec;

}

public int compare(TimeSpan step){
if (hour * 3600 + min * 60 + sec < step.hour * 3600 + step.min * 60+
step.sec){
return -1;
}
else if (hour*3600 + min*60 + sec > step.hour * 3600 + step.min * 60+
step.sec){
return 1;
}
else if (hour*3600 + min*60 + sec == step.hour * 3600 + step.min * 60+
step.sec){
return 0;
}
}
}

//test harness
public class T_TimeSpan {


public static void main(String[] args) {

int step;
TimeSpan ts=new TimeSpan();
ts.setFromMinutes(-372);
System.out.println(ts.toMinutes());

}

}
Oct 20 '06 #3
i forgot to mention that we must use "eclipse" to write this.
Oct 20 '06 #4
Split your task into smaller ones and then ask about a more specific problem. Have you programmed I/O in any language before?
I did well in C++. I understand the math part so i think i can do the actual conversions but i am having trouble with the using the test harness to pass in values. i have some code written, but it doesn't work. thank you
public class TimeSpan {

private int hour;
private int min;
private int sec;

public TimeSpan(){
hour = 0;
min = 0;
sec = 0;
}

public void setfromHours(int newHour){
hour = newHour;

}

public void setFromMinutes(int newMin){
min = newMin;
}

public void setfromSeconds(int newSec){
sec = newSec;
}

public double toHour(){
return hour + min/60 + sec/3600;

}
public double toMinutes(){
return hour * 60 + min + sec/60;

}

public int toSeconds(){
return hour*3600 + min*60 + sec;

}

public void increment(TimeSpan step){
hour = hour + step.hour;
min = min + step.min;
sec = sec + step.sec;
}

public void decrement(TimeSpan step){
hour = hour - step.hour;
min = min - step.min;
sec = sec - step.sec;

}

public int compare(TimeSpan step){
if (hour * 3600 + min * 60 + sec < step.hour * 3600 + step.min * 60+
step.sec){
return -1;
}
else if (hour*3600 + min*60 + sec > step.hour * 3600 + step.min * 60+
step.sec){
return 1;
}
else if (hour*3600 + min*60 + sec == step.hour * 3600 + step.min * 60+
step.sec){
return 0;
}
}
}

//test harness
public class T_TimeSpan {


public static void main(String[] args) {

int step;
TimeSpan ts=new TimeSpan();
ts.setFromMinutes(-372);
System.out.println(ts.toMinutes());

}

}
Oct 23 '06 #5
r035198x
13,262 8TB
I did well in C++. I understand the math part so i think i can do the actual conversions but i am having trouble with the using the test harness to pass in values. i have some code written, but it doesn't work. thank you
public class TimeSpan {

private int hour;
private int min;
private int sec;

public TimeSpan(){
hour = 0;
min = 0;
sec = 0;
}

public void setfromHours(int newHour){
hour = newHour;

}

public void setFromMinutes(int newMin){
min = newMin;
}

public void setfromSeconds(int newSec){
sec = newSec;
}

public double toHour(){
return hour + min/60 + sec/3600;

}
public double toMinutes(){
return hour * 60 + min + sec/60;

}

public int toSeconds(){
return hour*3600 + min*60 + sec;

}

public void increment(TimeSpan step){
hour = hour + step.hour;
min = min + step.min;
sec = sec + step.sec;
}

public void decrement(TimeSpan step){
hour = hour - step.hour;
min = min - step.min;
sec = sec - step.sec;

}

public int compare(TimeSpan step){
if (hour * 3600 + min * 60 + sec < step.hour * 3600 + step.min * 60+
step.sec){
return -1;
}
else if (hour*3600 + min*60 + sec > step.hour * 3600 + step.min * 60+
step.sec){
return 1;
}
else if (hour*3600 + min*60 + sec == step.hour * 3600 + step.min * 60+
step.sec){
return 0;
}
}
}

//test harness
public class T_TimeSpan {


public static void main(String[] args) {

int step;
TimeSpan ts=new TimeSpan();
ts.setFromMinutes(-372);
System.out.println(ts.toMinutes());

}

}

Expand|Select|Wrap|Line Numbers
  1. //patched it up a bit so that it compiles
  2. public class TimeSpan {
  3.  
  4. private int hour;
  5. private int min;
  6. private int sec;
  7.  
  8. public TimeSpan(){
  9. hour = 0;
  10. min = 0;
  11. sec = 0;
  12. }
  13.  
  14. public void setfromHours(int newHour){
  15. hour = newHour;
  16.  
  17. }
  18.  
  19. public void setFromMinutes(int newMin){
  20. min = newMin;
  21. }
  22.  
  23. public void setfromSeconds(int newSec){
  24. sec = newSec;
  25. }
  26.  
  27. public double toHour(){
  28. return hour + min/60 + sec/3600;
  29.  
  30. }
  31. public double toMinutes(){
  32. return hour * 60 + min + sec/60;
  33.  
  34. }
  35.  
  36. public int toSeconds(){
  37. return hour*3600 + min*60 + sec;
  38.  
  39. }
  40. //What will happen if for example hour = 20 and step.hour = 21?
  41. public void increment(TimeSpan step){
  42. hour = hour + step.hour;
  43. min = min + step.min;
  44. sec = sec + step.sec;
  45. }
  46.  
  47. public void decrement(TimeSpan step){
  48. hour = hour - step.hour;
  49. min = min - step.min;
  50. sec = sec - step.sec;
  51.  
  52. }
  53.  
  54. public int compare(TimeSpan step){
  55.     int retVal = 0;
  56.     //why not reuse the toSeconds method that you have already written above?
  57. if (hour * 3600 + min * 60 + sec < step.hour * 3600 + step.min * 60+
  58. step.sec){
  59. retVal = -1;
  60. }
  61. else if (hour*3600 + min*60 + sec > step.hour * 3600 + step.min * 60+
  62. step.sec){
  63. retVal = 1;
  64. }
  65. else if (hour*3600 + min*60 + sec == step.hour * 3600 + step.min * 60+
  66. step.sec){
  67. retVal = 0;
  68. }
  69. return retVal;
  70. }
  71. public static void main(String[] args) {
  72.  
  73. int step;
  74. TimeSpan ts=new TimeSpan();
  75. ts.setFromMinutes(-372);
  76. System.out.println(ts.toMinutes());
  77.  
  78. }
  79. }
Oct 23 '06 #6
Just wanted to say thanx for the help
Nov 1 '06 #7
r035198x
13,262 8TB
Just wanted to say thanx for the help
Good luck in defending the universe, Voltron.
Nov 2 '06 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: dan glenn | last post by:
Say, I want to set a cookie and have it expire an hour after it's set. It's looking like this is only possible for browsers which are in the same time zone as my server?? In other words, if I...
2
by: learning_C++ | last post by:
I programmed this code with a function "get_current_time" in the begining. When I compiled with the command g++ -Wall -g xxx.xpp -o xxx there are so many errors. please help me and thanks, ...
1
by: heirou | last post by:
I'm a novice in this subject....I've made a database that requires a time conversion. For example, if local time is 1200, determine the time in Korea. I use two fields: a date field, and a time...
6
by: DCSudolcan | last post by:
I know that a program can create and properly initialize an array of pointers to functions at build time, but can something like the following be done at build time? void foo(void); unsigned...
5
by: Paulers | last post by:
Hello, I'm working on an app that requires the functionality to convert the time in Austrailia to the time in New York (EST). I am wondering, what is the bestway to approach this in vb.net? Is...
3
by: Jason S | last post by:
is there any way to use templates to bind integer/floating point constants to a template for compile-time use? e.g. template <double conversion> class meters { const factor = conversion;
3
by: moni | last post by:
Hi, I wanted to convert a time value in the form of time_t into a readable form in C# or vice versa, in order to be able to subtract two time values and give the result in msecs. eg. I...
3
by: Evan Klitzke | last post by:
Although it is not present in ANSI C, the GNU version of stftime supports the conversion character %z, which is a time offset from GMT. The four digit time offset is required in RFC 2822...
5
by: fimarn | last post by:
I am trying to get rid of compile time error that I am getting only in RHEL5 (not in RHEL4) apparently due to the changes in the stl_list.h file. The error that I am getting is coming from the...
5
by: Grey Alien | last post by:
I need to convert timestamps that are given as the number of seconds that have elapsed since midnight UTC of January 1, 1970, (not counting leap seconds). It seems all of the std C functions...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.