Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Didn't test any of the code.

Before you think about efficiency, think of writing readable, maintainable, etc. code. A typical start of main is:

public static void main(String[] args) {
 final Application app = new Application();
 app.run();
}

That shows everyone, that your application does not use any application arguments. Then create your Application and try to write code which is readable.

Try to divide your code into meaningful pieces:

  • read grades
  • calculate and display average
  • ask for quit

run()

public void run() {
 boolean shouldQuit = false;
 while(!shouldQuit) {
 final List<Integer> grades = readGrades();
 displayAverage(grades);
 shouldQuit = askForQuit();
 }
}

readGrades()

HOW MANY GRADES DO YOU WISH TO ENTER?:

What if I type 4 but then realize, that I actually wanted to write 5 grades? What about a comma/whitespace separated list of grades?

100, 50, 70, 20

Just read the line, split it and create a List of it.

displayAverage()

private void displayAverage(List<Integer> grades) {
 if(!grades.isEmpty()) {
 float average = avg(grades);
 // println ...
 } else {
 // println: no grades entered
 }
}
private float avg(List<Integer> grades) {
 float sum = 0;
 for(int grade : grades) {
 sum += (float)grade;
 }
 return sum / (float)grades.size();
}

Of course there are other approaches, too, like using Java 8 to calculate the average (http://stackoverflow.com/a/10791579/4469738 https://stackoverflow.com/a/10791579/4469738) or creating more classes to do the job.

I think static main-class and one Application class should be enough for that easy program.

Didn't test any of the code.

Before you think about efficiency, think of writing readable, maintainable, etc. code. A typical start of main is:

public static void main(String[] args) {
 final Application app = new Application();
 app.run();
}

That shows everyone, that your application does not use any application arguments. Then create your Application and try to write code which is readable.

Try to divide your code into meaningful pieces:

  • read grades
  • calculate and display average
  • ask for quit

run()

public void run() {
 boolean shouldQuit = false;
 while(!shouldQuit) {
 final List<Integer> grades = readGrades();
 displayAverage(grades);
 shouldQuit = askForQuit();
 }
}

readGrades()

HOW MANY GRADES DO YOU WISH TO ENTER?:

What if I type 4 but then realize, that I actually wanted to write 5 grades? What about a comma/whitespace separated list of grades?

100, 50, 70, 20

Just read the line, split it and create a List of it.

displayAverage()

private void displayAverage(List<Integer> grades) {
 if(!grades.isEmpty()) {
 float average = avg(grades);
 // println ...
 } else {
 // println: no grades entered
 }
}
private float avg(List<Integer> grades) {
 float sum = 0;
 for(int grade : grades) {
 sum += (float)grade;
 }
 return sum / (float)grades.size();
}

Of course there are other approaches, too, like using Java 8 to calculate the average (http://stackoverflow.com/a/10791579/4469738) or creating more classes to do the job.

I think static main-class and one Application class should be enough for that easy program.

Didn't test any of the code.

Before you think about efficiency, think of writing readable, maintainable, etc. code. A typical start of main is:

public static void main(String[] args) {
 final Application app = new Application();
 app.run();
}

That shows everyone, that your application does not use any application arguments. Then create your Application and try to write code which is readable.

Try to divide your code into meaningful pieces:

  • read grades
  • calculate and display average
  • ask for quit

run()

public void run() {
 boolean shouldQuit = false;
 while(!shouldQuit) {
 final List<Integer> grades = readGrades();
 displayAverage(grades);
 shouldQuit = askForQuit();
 }
}

readGrades()

HOW MANY GRADES DO YOU WISH TO ENTER?:

What if I type 4 but then realize, that I actually wanted to write 5 grades? What about a comma/whitespace separated list of grades?

100, 50, 70, 20

Just read the line, split it and create a List of it.

displayAverage()

private void displayAverage(List<Integer> grades) {
 if(!grades.isEmpty()) {
 float average = avg(grades);
 // println ...
 } else {
 // println: no grades entered
 }
}
private float avg(List<Integer> grades) {
 float sum = 0;
 for(int grade : grades) {
 sum += (float)grade;
 }
 return sum / (float)grades.size();
}

Of course there are other approaches, too, like using Java 8 to calculate the average (https://stackoverflow.com/a/10791579/4469738) or creating more classes to do the job.

I think static main-class and one Application class should be enough for that easy program.

Source Link
Aitch
  • 151
  • 3

Didn't test any of the code.

Before you think about efficiency, think of writing readable, maintainable, etc. code. A typical start of main is:

public static void main(String[] args) {
 final Application app = new Application();
 app.run();
}

That shows everyone, that your application does not use any application arguments. Then create your Application and try to write code which is readable.

Try to divide your code into meaningful pieces:

  • read grades
  • calculate and display average
  • ask for quit

run()

public void run() {
 boolean shouldQuit = false;
 while(!shouldQuit) {
 final List<Integer> grades = readGrades();
 displayAverage(grades);
 shouldQuit = askForQuit();
 }
}

readGrades()

HOW MANY GRADES DO YOU WISH TO ENTER?:

What if I type 4 but then realize, that I actually wanted to write 5 grades? What about a comma/whitespace separated list of grades?

100, 50, 70, 20

Just read the line, split it and create a List of it.

displayAverage()

private void displayAverage(List<Integer> grades) {
 if(!grades.isEmpty()) {
 float average = avg(grades);
 // println ...
 } else {
 // println: no grades entered
 }
}
private float avg(List<Integer> grades) {
 float sum = 0;
 for(int grade : grades) {
 sum += (float)grade;
 }
 return sum / (float)grades.size();
}

Of course there are other approaches, too, like using Java 8 to calculate the average (http://stackoverflow.com/a/10791579/4469738) or creating more classes to do the job.

I think static main-class and one Application class should be enough for that easy program.

lang-java

AltStyle によって変換されたページ (->オリジナル) /