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 6913373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:12:39+00:00 2026-05-27T09:12:39+00:00

Well, I was told that I have to create a money class that accepts

  • 0

Well, I was told that I have to create a money class that accepts currency and value. The value should be stored as 2 integers, one representing the dollar value, then the other one in cents.

*It should accept the decimal value precise to two (2) decimal places. *

So I suppose I have to limit the cents value so that it should accept only 1 to 2 digit integers. Now, my problem is, my mentor told me that it is bad practice to do other stuff inside the constructor. How am I supposed to limit the input then if I’m not allowed to do anything with the constructor other than:

public class Money {
    Currency currency;
    int dollar;
    int cents;

    public Money(Currency currency, int dollar, int cents) {
        super();
        this.currency = currency;
        this.dollar = dollar;
        this.cents = cents;
    }
    ..... other code.....
}

Any other ideas on how I am supposed to implement what was instructed of me? Why is it bad practice and is there a way to define constraints without being guilty of this bad practice???

  • 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-27T09:12:40+00:00Added an answer on May 27, 2026 at 9:12 am

    What you are doing is validating the inputs to the constructor. Although in general "other stuff" in a constructor is not optimal, doing input verification code there is certainly warranted. Something like the following is a good pattern IMO:

    public Money(Currency currency, int dollar, int cents) {
        this.currency = currency;
        this.dollar = dollar;
        // validate that cents is 0 to 99
        if (cents < 0 || cents > 99) {
            throw new IllegalArgumentException("Invalid cents value: " + cents);
        }
        this.cents = cents;
    }
    

    Btw, there is no point in calling super() at the front of your constructor unless. The Java language calls the base class constructor automagically under the covers.

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

Sidebar

Related Questions

I have a little table that works pretty well. But I was told to
Today i was told to create tree data structure with the below class, public
I was told by a colleague of mine that Visual Studio allows one to
Well, my website can not redirect to https://www.facebook.com/QuaFootSpa from http://quafootspa.com/ I have tried redirection
well i have a configuration like this in the components part of my config
Well, I have a application which somehow requires some system resources, but how do
I can't see anything on here but I do remember being told that If
How can I make sure that a certain instance of a class will never
For the purposes of interoperability with Java, I need a class that has a
I have a Create New Employee ASP.Net MVC form. My complex object is an

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.