英文:
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 < 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) {
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));
} else {
System.out.println("to small");
}
答案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<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));
System.out.println(theBigList);
int sum = 0;
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);
avg = Math.round(sum) / (double) theBigList.size();
System.out.println("Average: " + String.format("%.2f", avg));
return avg;
} else {
System.out.println("to small");
return -1;
}
}
Alternatively, if you wanted to put the ArrayList as a parameter, you can change the method signature:
public double getAverageNotes(List<Integer> list){
List<Integer> theBigList = list;
...
and define the array list to be passed in:
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));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论