Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Hey, I hava problem with my code I cannot figure out where is the problem... public class Student { private String name; private ArrayList tests
Hey,
I hava problem with my code I cannot figure out where is the problem...
public class Student { private String name; private ArrayListtests = new ArrayList (); private ArrayList assignments = new ArrayList (); public Student(String n) { name = n; } public void addTest(double score) { tests.add(score); } public void addAssignment(double score) { assignments.add(score); } public double getAverage(ArrayList a) { if (a.size() = 0) return 0; else { double total = 0; for (int i=0; i< a.size(); i++) { total += a.get(i) } return total/a.size(); } } public double getTestAverage() { return getAverage(tests); } public double getAssignmentAverage() { return getAverage(assignments); } public double calcOverallAverage(double testPct, double assignPct) { return testPct*getTestAverage() + assignPct*getAssignmentAverage(); } public String toString() { return name + " has test average: " + getTestAverage() + " and assignment average: " + getAssignmentAverage(); } }
public class Driver { public static void main(String[] args) { Student s1 = new Student("Sandy"); s1.addAssignment(78.0); s1.addAssignment(92.5); s1.addAssignment(87.5); s1.addAssignment(77.5); s1.addTest(69.5); s1.addTest(83.0); printStudentInfo(s1); Student s2 = new Student("Tommy"); double[] scores = {77.0, 82, 95, 98, 83.5, 100, 76, 88.5, 91.5, 85}; for (int i=0; i<=scores.length; i+=2) { s2.addAssignment(scores[i]); s2.addTest(scores[i+1]); } printStudentInfo(s1); } public void printStudentInfo(Student s) { System.out.println(s); double avg1 = s.calcOverallAverage(0.50, 0.50); double avg2 = s.calcOverallAverage(0.60, 0.40); System.out.printf("Overall average by 50/50 and 60/40: %5.2f and %5.2f ", avg1, avg2); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started