如何将这段代码改进为面向对象编程?

huangapple go评论65阅读模式
英文:

How can I improve this code to object oriented programming?

问题

class ArrayTester {
    private double sum;

    public double getAverageNotes(ArrayList<Integer> theBigList) {
        Random theGenerator = new Random();
        for (int n = 0; n < 4; n++) {
            theBigList.add(theGenerator.nextInt(6) + 1);
        }

        if (theBigList.size() > 2) {
            int max = Collections.max(theBigList);
            int min = Collections.min(theBigList);

            theBigList.remove(Integer.valueOf(max));
            theBigList.remove(Integer.valueOf(min));

            System.out.println(theBigList);
            for (int n = 0; n < theBigList.size(); n++) {
                System.out.println("New note " + n + " of the Informatics is: " + theBigList.get(n));
                sum = sum + theBigList.get(n);
            }
            System.out.println("Collection size is: " + theBigList.size() + "\nExtreme values are: " + min + " and " + max);
            double avg = Math.round(sum) / (double) theBigList.size();
            System.out.println("Average: " + String.format("%.2f", avg));
            return avg;
        } else {
            System.out.println("Too small");
            return -1; // Indicate an error condition or handle it appropriately
        }
    }
}
英文:

I would like improve this code to OOP in Java. How can I for example return value from variable avg or Can I put ArrayList in method parameters?

Thank you in advance

 class ArrayTester {
private double sum;
public void getAverageNotes() {
List &lt; Integer &gt; theBigList = new ArrayList &lt; Integer &gt; ();
Random theGenerator = new Random();
for (int n = 0; n &lt; 4; n++) {
theBigList.add(theGenerator.nextInt(6) + 1);
}
if (theBigList.size() &gt; 2) {
int max = Collections.max(theBigList);
int min = Collections.min(theBigList);
theBigList.remove(Integer.valueOf(max));
theBigList.remove(Integer.valueOf(min));
System.out.println(theBigList);
for (int n = 0; n &lt; theBigList.size(); n++) {
System.out.println(&quot;New note &quot; + n + &quot; of the Informatics is: &quot; + theBigList.get(n));
sum = sum + theBigList.get(n);
}
System.out.println(&quot;Collection size is: &quot; + theBigList.size() + &quot;\nExtreme values are: &quot; + min + &quot; and &quot; + max);
double avg = Math.round(sum) / (double) theBigList.size();
System.out.println(&quot;Average: &quot; + String.format(&quot;%.2f&quot;, avg));
} else {
System.out.println(&quot;to small&quot;);
}

答案1

得分: 1

public double getAverageNotes() {
    List<Integer> theBigList = new ArrayList<Integer>();
    Random theGenerator = new Random();
    
    for (int n = 0; n < 4; n++) {
        theBigList.add(theGenerator.nextInt(6) + 1);
    }
    
    if (theBigList.size() > 2) {
        double avg = 0;
        int max = Collections.max(theBigList);
        int min = Collections.min(theBigList);
        
        theBigList.remove(Integer.valueOf(max));
        theBigList.remove(Integer.valueOf(min));
        
        int sum = 0;
        for (int n = 0; n < theBigList.size(); n++) {
            sum = sum + theBigList.get(n);
        }
        
        avg = Math.round(sum) / (double) theBigList.size();
        return avg;
    } else {
        return -1;
    }
}
public double getAverageNotes(List<Integer> list) {
    List<Integer> theBigList = list;

    // Rest of the code remains the same
}
public static void main(String[] args) {
    List<Integer> list = new ArrayList<Integer>();
    Random theGenerator = new Random();
    
    for (int n = 0; n < 4; n++) {
         list.add(theGenerator.nextInt(6) + 1);
     }
     
    System.out.println(getAverageNotes(list));
}
英文:

You can return the average by returning the avg once it has been calculated. If there are less than 2 items, it will return -1 as a result.

 public double getAverageNotes() {
List&lt;Integer&gt; theBigList = new ArrayList&lt;Integer&gt;();
Random theGenerator = new Random();
for (int n = 0; n &lt; 4; n++) {
theBigList.add(theGenerator.nextInt(6) + 1);
}
if (theBigList.size() &gt; 2) {
double avg = 0;
int max = Collections.max(theBigList);
int min = Collections.min(theBigList);
theBigList.remove(Integer.valueOf(max));
theBigList.remove(Integer.valueOf(min));
System.out.println(theBigList);
int sum = 0;
for (int n = 0; n &lt; theBigList.size(); n++) {
System.out.println(&quot;New note &quot; + n + &quot; of the Informatics is: &quot; + theBigList.get(n));
sum = sum + theBigList.get(n);
}
System.out.println(&quot;Collection size is: &quot; + theBigList.size() + &quot;\nExtreme values are: &quot; + min + &quot; and &quot; + max);
avg = Math.round(sum) / (double) theBigList.size();
System.out.println(&quot;Average: &quot; + String.format(&quot;%.2f&quot;, avg));
return avg;
} else {
System.out.println(&quot;to small&quot;);
return -1;
}
}

Alternatively, if you wanted to put the ArrayList as a parameter, you can change the method signature:

public double getAverageNotes(List&lt;Integer&gt; list){
List&lt;Integer&gt; theBigList = list;
...

and define the array list to be passed in:

 public static void main(String[] args) {
List&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;();
Random theGenerator = new Random();
for (int n = 0; n &lt; 4; n++) {
list.add(theGenerator.nextInt(6) + 1);
}
System.out.println(getAverageNotes(list));
}

huangapple
  • 本文由 发表于 2020年8月19日 12:45:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63480131.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定