简体   繁体   中英

Class to count variables design issue

I'm new to OO programing and having a bit of trouble with the design of my program to use the concepts. I have done the tutorials but am still having problem.

I have a recursion that takes a value of items(could be anything in this example, stocks) and figures out what number of them are needed to equal a specific value(in this code 100). This part works but I want to know if a stock's weighting exceeds a threshold. Originally I approached this problem with a method that did a for loop and calculated the entire list of values but this is super inefficient because its doing it on every loop of the recursion. I thought this would be a good time to try to learn classes because I could use a class to maintain state information and just increment the value on each loop and it'll let me know when the threshold is hit.

I think I have the code but I don't fully understand how to design this problem with classes. So far it runs the loop each step of the recursion because I'm initially the class there. Is there a better way to design this? My end goal is to be notified when a weighting is exceeded(which I can somewhat already do) but I want to do in way that uses the least bit of resources(avoiding inefficient/unnecessary for loops)

Code(Here's the entire code I have been using to learn but the problem is with the Counter class and its location within the findVariables method):

import java.util.Arrays;


public class LearningClassCounting {

    public static int[] stock_price = new int[]{ 20,5,20};
    public static int target = 100;

    public static void main(String[] args) {
        // takes items from the first list
        findVariables(stock_price, 100, new int[] {0,0,0}, 0, 0);
    }

    public static void findVariables(int[] constants, int sum, 
            int[] variables, int n, int result) {
        Counter Checker = new Counter(stock_price, variables);
        if (n == constants.length) { 
            if (result == sum) {
                System.out.println(Arrays.toString(variables));
            }
        } else if (result <= sum){ //keep going
            for (int i = 0; i <= 100; i++) {
                variables[n] = i;
                Checker.check_total_percent(n, i);
                findVariables(constants, sum, variables, n+1, result+constants[n]*i);
            }
        }
    }

}

class Counter {
    private int[] stock_price;
    private int[] variables;
    private int value_so_far;
    public Counter(int[] stock_price, int[] variables) {
        this.stock_price = stock_price;
        this.variables = variables; 
        for (int location = 0; location < variables.length; location++) {
            //System.out.println(variables[location]  + " * " + stock_price[location] + " = " + (variables[location] * stock_price[location]) );
            value_so_far = value_so_far + (variables[location] * stock_price[location]);
        }
        //System.out.println("Total value so far is " + value_so_far);
        //System.out.println("************");
    } 

    public  void check_total_percent(int current_location, int percent) {
        // Check to see if weight exceeds threshold
        //System.out.println("we are at " + current_location + " and " + percent + " and " + Arrays.toString(variables));
        //System.out.println("value is " + stock_price[current_location] * percent);
        //formula I think I need to use is:
        if (percent == 0) {
            return;
        }
        int current_value = (stock_price[current_location] * percent);
        int overall_percent = current_value/(value_so_far + current_value);
        if (overall_percent > 50 ) {
            System.out.println("item " + current_location + " is over 50%" );
        }
    }
}

What you're describing sounds like a variant of the famous knapsack problem. There are many approaches to these problems, which are inherently difficult to calculate.

Inherently, one may need to check "all the combinations". The so-called optimization comes from backtracking when a certain selection subset is already too large (eg, if 10 given stocks are over my sum, no need to explore other combinations). In addition, one can cache certain subsets (eg, if I know that XY and Z amount to some value V, I can reuse that value). You'll see a lot of discussion of how to approach these sort of problems and how to design solutions.

That being said, my view is that while algorithmic problems of this sort may be important for learning how to program and structure code and data structures, they're generally a very poor choice for learning object-oriented design and modelling.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM