Results 1 to 7 of 7
Thread: Can't seem to grasp this
- 11-10-2009, 06:07 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 20
- Rep Power
- 0
Can't seem to grasp this
Im new to Java, only having three sessions in my first class but I can't seem to wrap my head around these exercises.
Write a calss that declares a variable named minutes, which holds minutes worked on a job, and assign a value. Display the minutes in hours and minutes, for example. 3 hours and 27 minutes. Be sure to use a named constant where appropriate.
Part B.
Write an interactive version that accepts minutes worked from a user.
Problem 2:
Write a class that accepts a user's hourly rate of pay and number of hours worked. Display the user's gross pay, the withholding tax (15% of gross pay) and the net pay, (gross pay - withholding)
Can anyone gimme a hint or point me in the right direction please?
- 11-10-2009, 06:48 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Start by reading the instructions sentence by sentence and acting on the command in each of the sentences.
- 11-10-2009, 06:51 AM #3
Member
- Join Date
- Oct 2009
- Posts
- 20
- Rep Power
- 0
How would you give a variable minutes the value of 3 hours 15 minutes?
- 11-10-2009, 07:19 AM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
"variable named minutes, which holds minutes worked on a job"
You could just use an int (or long) for the minutes.
You then do the conversion to that String representation in a separate method using basic arithmetic.
- 11-10-2009, 07:22 AM #5
Member
- Join Date
- Oct 2009
- Posts
- 20
- Rep Power
- 0
I think I see what you're saying...for example 120 minutes I could use the modulus or remainder operator to convert it to hours and minutes....I got ya
- 11-10-2009, 07:53 AM #6
Member
- Join Date
- Oct 2009
- Posts
- 20
- Rep Power
- 0
As soon as you said arithmetic, it all clicked! Thank you very much.
//Import necessary packages
import java.util.Scanner;
public class Time2
{
public static void main(String [] args)
{
//Declare variables
int minutes;
//Create Scanner for input
Scanner inputDevice = new Scanner(System.in);
//Get user input
System.out.print("How many minutes were worked on the job?");
minutes = inputDevice.nextInt();
//Give output
System.out.println("Time spent on job is " + minutes/60 + " hours" + minutes%60 + " minutes");//Output given in Hour : Minute format.
}//End Main
}//End Class
- 11-10-2009, 08:47 AM #7
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
There was a hint that you missed. "Be sure to use a named constant where appropriate".
A constant would be a value that does not change in the program. It's value will always be the same and it can be used on multiple locations. To declare a constant you use the modifiers
public static final
Do you spot which one should be a constant?


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks