Results 1 to 7 of 7
  1. #1
    Boomer1 is offline Member
    Join Date
    Oct 2009
    Posts
    20
    Rep Power
    0

    Default 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?

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

    Default

    Start by reading the instructions sentence by sentence and acting on the command in each of the sentences.

  3. #3
    Boomer1 is offline Member
    Join Date
    Oct 2009
    Posts
    20
    Rep Power
    0

    Default

    How would you give a variable minutes the value of 3 hours 15 minutes?

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

    Default

    "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.

  5. #5
    Boomer1 is offline Member
    Join Date
    Oct 2009
    Posts
    20
    Rep Power
    0

    Default

    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

  6. #6
    Boomer1 is offline Member
    Join Date
    Oct 2009
    Posts
    20
    Rep Power
    0

    Default

    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

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

    Default

    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?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •