Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I convert existing procedural code to use classes?
    text
    copied!<p>I'm trying to learn Java and basically my approach has been to take the procedural style I learned with python, and apply it to Java. So I never use classes and just put everything in a single class with many methods(which I just use as python functions). I think I've hit a problem, and need to bite the bullet and use classes, but I'm having trouble wrapping my head around how to do it.</p> <p>To simplify my problem(ignore the poor design- it's just to illustrate the point), I have a program that takes a list and within a for loop does some math on each item(in this case adds 1 to the value of the list). I only want it to do work on 2 items on the list and then stop(in this example it's the first 2 items but in my real program it could be anywhere in the list). Here's the working code that is similar to how I'm already doing it:</p> <p>No Classes:</p> <pre><code>public class LearningClasses { 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&lt;list.length;current_location++) { for (int i =0; i&lt;100; i++){ if (check_size(data_list) == false ) { break; } data_list[current_location] = (list[current_location]+1); } } //its done now lets print the results for (Integer item : data_list) { System.out.println(item); } } private static boolean check_size(int[] data_list) { // TODO Auto-generated method stub int count = 0; for (int item : data_list) { if (item != 0) { count++; if (count&gt;=2) { break; } } } if (count&gt;=2) { return false; } else { return true; } } } </code></pre> <p>The problem with this code is although it works it's inefficient because it calculates the count on every iteration of the second for loop. In my program I cannot put anything above the first <code>for loop</code> but I can put anything below it, so I thought instead of doing the count every time maybe I could use a class to somehow maintain state and just increment the number as oppose to recalculating every time.</p> <p>With classes:</p> <pre><code>public class LearningClassesCounter { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub 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&lt;list.length;current_location++) { //can only put commands in here. Nothing above. Counter checker = new Counter(data_list); System.out.println(checker.check_data()); for (int i =0; i&lt;100; i++){ data_list[current_location] = (list[current_location]+1); } } //its done now lets print the results for (Integer item : data_list) { System.out.println(item); } } } class Counter { private int count; // current value private boolean continue_or_not; private int[] data_list; // create a new counter with the given parameters public Counter(int[] data_list) { data_list = this.data_list; count = 0; continue_or_not = true; } public boolean check_data() { // TODO Auto-generated method stub int count = 0; for (int item : data_list) { if (item != 0) { count++; if (count&gt;=3) { break; } } } if (count&gt;=3) { return false; } else { return true; } } // increment the counter by 1 public void increment() { count++; } // return the current count public int value() { return count; } } </code></pre> <p>This doesn't work because it thinks the data_list is a null pointer(I know I'm declaring it null, but if I make it private int[] data_list = data_list it doesn't compile either). My ultimate goal is to have some kind of controls, in this case its limiting it to 2 items but I want to also add other limits like total value of al items cannot exceed X or cannot be lower than X and want to save CPU power by not having to do full calculations every time. So I think I need to be able to increment the values and then need to check that those increments haven't exceeded thresholds. </p> <p>Can anyone help me understand what I'm doing wrong? Am I only wrong with syntax; or am I designing this wrong?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload