Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6588225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:01:17+00:00 2026-05-25T17:01:17+00:00

I just started to learn Java. There is a course in MIT and there

  • 0

I just started to learn Java. There is a course in MIT and there is an assignment to write a class for pay calculation. Part of the task is:

  • The base pay must not be less than the minimum wage ($8.00 an hour). If it is, print an error.
  • If the number of hours is greater than 60, print an error message.

The code I have written is as follows:

public class fooCorporation {
    private float totalPay;
    private float calc;

    public float totalPay(double basePay, int hours){

        if(hours <= 40){
            totalPay = (float) (basePay * hours);
        }
        else if(hours > 40){
            int extraHours =  hours - 40;
            float payForFortyHours = (float) (basePay * hours);
            float payForextraHours = (float) (basePay * 1.5 * extraHours);

            totalPay = payForFortyHours + payForextraHours;
        }
        return totalPay;
    }

    public static void main(String[] args){
        fooCorporation foo = new fooCorporation();
        System.out.println("employee 1: " + foo.totalPay(7.50,35));
        System.out.println("employee 2: " + foo.totalPay(8.20,47));
        System.out.println("employee 3: " + foo.totalPay(10.00,73));
    }
}

The problem I am facing is how to print an error message from the function totalPay() when some conditions are not met. Can the above logic be written in a different way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:01:17+00:00Added an answer on May 25, 2026 at 5:01 pm

    You generally don’t print things in the middle of general purpose functions since (1) it’s annoying to the people using those functions; and (2) it gives no indication of a problem to the calling code.

    Usually, you would either throw an exception or have some specific return code indicating an error condition.

    Then the caller can decide what to print (or do otherwise if printing is not appropriate).

    For example, if you can determine that the total pay should always be positive, just return -1.0 for an error condition. For example:

    public class fooCorporation {
        public float totalPay (double basePay, int hours) {
            if (hours < 0) return -1.0;
            if (hours <= 40) return (float) (basePay * hours);
    
            int extraHours =  hours - 40;
            float payForFortyHours = (float) (basePay * hours);
            float payForextraHours = (float) (basePay * 1.5 * extraHours);
    
            return payForFortyHours + payForextraHours;
        }
    
        public static void main(String[] args){
            fooCorporation foo = new fooCorporation();
            float pay = foo.totalPay (7.50, 35);
            if (pay < 0) System.out.println ("employee 1: invalid hours");
            else         System.out.println ("employee 1: " + pay);
    
            pay = foo.totalPay (8.20, 47);
            if (pay < 0) System.out.println ("employee 2: invalid hours");
            else         System.out.println ("employee 2: " + pay);
    
            pay = foo.totalPay (10.00, -4);
            if (pay < 0) System.out.println ("employee 3: invalid hours");
            else         System.out.println ("employee 3: " + pay);
        }
    }
    

    That method of yours, by the way, gives you double time and a half (x2.5) for overtime since you’re not adjusting the base hours down to 40. Make sure that’s what you want rather than time and a half (x1.5).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just started to learn about tie . I have a class named Link
I have just started to learn the very basics of Java programming. Using a
I just started to learn Android, I'm trying to write a widget which is
I have just started to learn Java and is curious is it any good
I just started to learn Ruby and as a .Net developer, I'm wondering if
I just started to learn how to use action listeners. To my understanding it
I've just started to learn C (using Thinking In C ) and I'm wondering
I have just started to learn Ruby and got a good take on the
I just have started to learn Haskell and combine reading books and tutorials with
We just started a new project in Java EE in our office and i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.