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

  • SEARCH
  • Home
  • 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 7820427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:17:03+00:00 2026-06-02T07:17:03+00:00

I’m new to using OOP, I typically just put all my code in a

  • 0

I’m new to using OOP, I typically just put all my code in a single class and use methods. But I want to maintain state information and think classes are the best fit but I’m having trouble wrapping my head around it.

Say I have a list of items and I want to stop when the total sum of all previous items in the list equals X(in this case 10 so it takes item 1 + 2, then 2+3.etc..until it hits the threshold 10), I can use a method to calculate it but it involves me doing the entire process all over again when all I really need to do is increment by the last item and then see if my data exceeds the threshold. Here’s my code so far but I know its not good because although it works its really just using the class as an independent method and recalculating on every loop. My goal is to,using this structure, reduce loops if not necessary to check thresholds.

Any suggestions?

Code:

public class LearningClassesCounter {
    public static void main(String[] args) {
        int[] list = new int[]{1,2,3,4,5,6,7,8,9,10};
        int[] data_list = new int[list.length];
        for (int current_location = 0; current_location<list.length;current_location++) {
            //can only put commands in here. Nothing above.
            Counter checker = new Counter(data_list);
            System.out.println(checker.check_data(current_location));
            for (int i =0; i<100; i++){
                if (checker.check_data(current_location) == false) {
                    break;
                }
                data_list[current_location] = (list[current_location]+1); //this is just a random function, it could be any math function I just put it in here to show that some work is being done.
            }
        }
        //its done now lets print the results
        for (Integer item : data_list) {
            System.out.println(item);
        }
    }
}


class Counter {
    private int[] data_list;
    private int total_so_far;   
    // create a new counter with the given parameters
    public Counter(int[] data_list) {
        this.data_list = data_list;
        this.total_so_far = 0;
    } 

    public boolean check_data(int current_location) {   
        // TODO Auto-generated method stub
        int total_so_far = 0;
        //System.out.println(total_so_far);
        for (int item : data_list) {
            total_so_far = item + total_so_far;
            if (total_so_far >= 10) {
                break;
            }
        }

        if (total_so_far>=10) {
            return false;
        } else {
            return true;
        }
    }

}

I don’t need anyone to fix my code or anything(I want to do it myself, the code is just to give an idea of what I’m doing). I’m more interested in the flaw in my logic and maybe a way for me to better think about designing classes so I can apply them to my own situations better.

  • 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-06-02T07:17:04+00:00Added an answer on June 2, 2026 at 7:17 am

    So the solution is that you do not update the data_list directly. Instead have a setter method in the Counter class that takes the index and value to update. It updates the value in the array and also updates a count value.

    Something like this:

    class Counter{
        private final int[] list;
        private count = 0;
        private final maxCount = 10;
    
    
        public Counter(int[] list){
           this.list = list;
        }
    
        public boolean updateValueAndCheckPastMax(int index, int value){
             list[index] = value;
             count += value;
             return count >= maxCount;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I want to construct a data frame in an Rcpp function, but when I
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.