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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:52:21+00:00 2026-05-27T16:52:21+00:00

This method is in a Scores class, that is set the class variable scores,

  • 0

This method is in a Scores class, that is set the class variable scores, but the scores is combination of its sub class, for example, scores = sub scores1 + sub scores2, sub1/sub2 is calculated by the subclasses of Scores.

public void setScores (double scores) {
    this.scores = scores;
}

public void calculateScore(double sub1, double sub2) {
    this.scores = (sub1 + sub2) /2;
}

is there method/design skills/practice to constraint the class can’t alter the scores value (i.e run the setScores), after the calculateScore() just ran.

  • 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-27T16:52:22+00:00Added an answer on May 27, 2026 at 4:52 pm

    You could use a flag calculated and if it is true, throw an exception in setScores(...). Note that this approach only will enforce that constraint at runtime and you might miss a situation that throws the exception in your test.

    Generally it would be favorable to enforce the constraint at compile time, but we’d need some more information. Here’s an idea based on guesses on your other constraints:

    You could also pass the scores to the constructor if you want setScores(...) to be called only once. In this case you either completely get rid of setScores(...) or make in protected to allow only this class, subclasses and classes in the same package to call it.

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

Sidebar

Related Questions

I have this method in my db class public function query($queryString) { if (!$this->_connected)
I'm trying to set up a class so that it's possible to initialize it
I have a rather unique class that allows its child classes to declare virtual
I have a class method that returns a pointer to an inner data structure
This method works as expected - it creates a JTree with a root node
With this method declaration (no overloads): void Method(double d) { // do something with
Take this method /** * @return List of group IDs the person belongs to
Can this method work? public String sayHello(){ return Hello.jsp?name= + laala; } I am
I have this method on a webpart: private IFilterData _filterData = null; [ConnectionConsumer(Filter Data
Every time I call this method my NSMutableData is leaking and I cannot figure

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.