Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I tried to change the array to arraylist. Reply if this doesn't compile correctly.</p> <p>import java.util.ArrayList;</p> <p>public class Student {</p> <pre><code>private String name; // private int[] tests; private ArrayList&lt;Integer&gt; tests; public Student() { this(""); } public Student(String nm) { // this(nm, 3); name = nm; } /* * public Student(String nm, int n) { name = nm; tests = new int[n]; for * (int i = 0; i &lt; tests.length; i++) { tests[i] = 0; } } */ /* * public Student(String nm, int[] t) { tests = new int[t.length]; } */ public Student(Student s) { this(s.name, s.tests); } public Student(String name2, ArrayList&lt;Integer&gt; tests2) { name = name2; tests = tests2; } public int getNumberOfTests() { return tests.size(); } public void setName(String nm) { name = nm; } public String getName() { return name; } // public void setScore(int i, int score) { // tests[i - 1] = score; public void setScore(int score) { tests.add(score); } public int getScore(int i) { // return tests[i - 1]; return tests.get(i - 1); } public int getAverage() { int sum = 0; for (int score : tests) { sum += score; } // return sum / tests.length; return sum / tests.size(); } public int getHighScore() { int highScore = 0; for (int score : tests) { highScore = Math.max(highScore, score); } return highScore; } public String toString() { String str = "Name: " + name + "\n"; for (int i = 0; i &lt; tests.size(); i++) { str += "test " + (i + 1) + ": " + tests.get(i) + "\n"; } str += "Average: " + getAverage(); return str; } public String validateData() { if (name.equals("")) { return "SORRY: name required"; } for (int score : tests) { if (score &lt; 0 || score &gt; 100) { String str = "SORRY: must have " + 0 + " &lt;= test score &lt;= " + 100; return str; } } return null; } public ArrayList&lt;Integer&gt; getTests() { return tests; } public void setTests(ArrayList&lt;Integer&gt; tests) { this.tests = tests; } </code></pre> <p>}</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