Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Java Question

Java Question
Thread Tools
Nodnarb
Mac Elite
Join Date: Jun 2004
Status: Offline
Reply With Quote
Jul 11, 2004, 09:37 PM
 
Hi,
I recently started learning java, and I have written a few programs. I was looking to inhace a program called work that calculates how much I earn in a given day or week. I was wondering if theres a code for java that allows you to subtract time values. For example, if I want to make it ask if I know how many hours I worked (lets say 6:00 PM-11:00PM), could I get it to figure out how many hours that is? (And also work with A.M. values). The program is written underneath (sorry if it's not indented correctly, I'm not quite sure how to do that either yet.) Please let me know if anyone knows a solution to the time thing!
Thanks,
-Brandon


__________________________________________________ ____________
/*The DummiesIO.get_______ is something the author of the java
* dummies book I'm reading wrote for input and stuff. Also this is
* one of my first programs so go easy on me!.
*/



import java.text.NumberFormat;
class work
{
public static void main(String args[])
{
double rate, hours, gross, net, tax, tips, total;
char reply;
NumberFormat currency =
NumberFormat.getCurrencyInstance();
String niceTotal1;
String niceTotal2;
String niceTotal3;
String niceTotal4;
String niceTips;

System.out.println();
System.out.print("How many hours did you work? ");
tips=0;
rate=5.50;
hours=DummiesIO.getDouble();
gross=rate*hours;
tax=gross*.07636363636363636;
net=gross-tax;

System.out.println();

System.out.println("Do you know how much you earned in tips?");
System.out.print("(Y/N) ");
reply = DummiesIO.getChar();

if (reply=='y' || reply=='Y')
{
System.out.println("How much?");
tips=DummiesIO.getDouble();

total=net+tips;
niceTotal1 = currency.format(gross);
niceTotal2 = currency.format(tax);
niceTotal3 = currency.format(net);
niceTotal4 = currency.format(total);
niceTips = currency.format(tips);

System.out.print("--------------------");
System.out.println("--------------------------");

System.out.print("Hours\tTips\tGross");
System.out.println("\tTax\tNet Pay\tTotal");

System.out.print(hours);
System.out.print("\t");
System.out.print(niceTips);
System.out.print("\t");
System.out.print(niceTotal1);
System.out.print("\t");
System.out.print(niceTotal2);
System.out.print("\t");
System.out.print(niceTotal3);
System.out.print("\t");
System.out.println(niceTotal4);
System.out.print("-------------------");
System.out.println("---------------------------");
}
else
{
niceTotal1 = currency.format(gross);
niceTotal2 = currency.format(tax);
niceTotal3 = currency.format(net);

System.out.println("-------------------------------");

System.out.print("Hours\tGross");
System.out.println("\tTax\tNet Pay");

System.out.print(hours);
System.out.print("\t");
System.out.print(niceTotal1);
System.out.print("\t");
System.out.print(niceTotal2);
System.out.print("\t");
System.out.println(niceTotal3);
System.out.println("-------------------------------");

}

}
}
--------------------------------------------------------------

Thanks again"
     
Nodnarb  (op)
Mac Elite
Join Date: Jun 2004
Status: Offline
Reply With Quote
Jul 11, 2004, 09:40 PM
 
I forgot to mention that if you woul like to run the program, you can go to the author's website and click either "Download all the code" or "Download DummiesIO.java" and put it in a folder along with the code above and the run it from the terminal
thanks!
     
Arkham_c
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Jul 12, 2004, 11:18 AM
 
The best thing to do is store the values as dates (GregorianCalendar). Create a calendar with the start time. Create another calendar with the end time. You can subtract the differences by calling getTimeInMillis() on each calendar and doing the subtraction to get the number of milliseconds between them.
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
Nodnarb  (op)
Mac Elite
Join Date: Jun 2004
Status: Offline
Reply With Quote
Jul 12, 2004, 02:51 PM
 
Can I have an example of what you mean?
     
Arkham_c
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Jul 12, 2004, 03:34 PM
 
Originally posted by brandon420506:
Can I have an example of what you mean?
Code:
import java.util.GregorianCalendar; public class SampleClass { public SampleClass() { //int year, int month, int date, int hour, int minute GregorianCalendar start_time = new GregorianCalendar(2004,07,12,8,00); GregorianCalendar end_time = new GregorianCalendar(2004,07,12,18,00); long start_time_ms = start_time.getTimeInMillis(); long end_time_ms = end_time.getTimeInMillis(); double time_in_minutes = (end_time_ms - start_time_ms)/(1000*60); double time_in_hours = time_in_minutes/60; System.out.println(time_in_minutes + " minutes"); System.out.println(time_in_hours + " hours"); } public static void main(String[] args) { SampleClass sc = new SampleClass(); } }
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 02:33 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,