Final challenge: three changes
After this: Add a column, a rule and a report line to the finished program, each in the right place.
This challenge has five steps, and each step is scored on its own. The teacher asks for three changes to the finished grade book: a third mark on every line, a pass mark of 70 with a count of the passes, and a line with the lowest average. For each change you say where in the program it belongs, and then predict or complete the line that makes it. Every step shows the changed program in Python and in Java with the change marked, and the answers are the same in both. You can try as many times as you like, and hints work as they do in the lessons.
The finished program, as stage 4 left it, for reference:
class Student: def __init__(self, name, marks): self.name = name self.marks = marks
def average(self): return sum(self.marks) / len(self.marks)
def __str__(self): return f"{self.name}: {self.average():.2f}"
def load(filename): students = [] with open(filename) as f: lines = f.readlines() for line in lines: try: parts = line.strip().split(",") marks = [int(parts[1]), int(parts[2])] students.append(Student(parts[0], marks)) except ValueError: print("Skipping bad line: " + line.strip()) return students
def top_student(students): best = students[0] for s in students: if s.average() > best.average(): best = s return best
def class_average(students): total = 0 for s in students: total = total + s.average() return total / len(students)
def report_lines(students): lines = [] for s in students: lines.append(str(s)) lines.append("Top student: " + top_student(students).name) lines.append(f"Class average: {class_average(students):.2f}") return lines
students = load("marks.txt")with open("report.txt", "w") as f: for line in report_lines(students): f.write(line + "\n")with open("report.txt") as f: saved = f.readlines()for line in saved: print(line.strip())Output
Ana: 78.50 Ben: 75.00 Cy: 90.00 Top student: Cy Class average: 81.17
import java.io.IOException;import java.io.PrintWriter;import java.nio.file.Files;import java.nio.file.Path;import java.util.ArrayList;import java.util.List;
class Student { String name; List<Integer> marks;
Student(String name, List<Integer> marks) { this.name = name; this.marks = marks; }
double average() { int total = 0; for (int m : marks) { total = total + m; } return (double) total / marks.size(); }
public String toString() { return name + ": " + String.format("%.2f", average()); }}
public class Main { static List<Student> load(String filename) throws IOException { List<Student> students = new ArrayList<>(); List<String> lines = Files.readAllLines(Path.of(filename)); for (String line : lines) { try { String[] parts = line.split(","); List<Integer> marks = List.of(Integer.parseInt(parts[1]), Integer.parseInt(parts[2])); students.add(new Student(parts[0], marks)); } catch (NumberFormatException e) { System.out.println("Skipping bad line: " + line); } } return students; }
static Student topStudent(List<Student> students) { Student best = students.get(0); for (Student s : students) { if (s.average() > best.average()) { best = s; } } return best; }
static double classAverage(List<Student> students) { double total = 0; for (Student s : students) { total = total + s.average(); } return total / students.size(); }
static List<String> reportLines(List<Student> students) { List<String> lines = new ArrayList<>(); for (Student s : students) { lines.add(s.toString()); } lines.add("Top student: " + topStudent(students).name); lines.add("Class average: " + String.format("%.2f", classAverage(students))); return lines; }
public static void main(String[] args) throws IOException { List<Student> students = load("marks.txt"); try (PrintWriter out = new PrintWriter("report.txt")) { for (String line : reportLines(students)) { out.println(line); } } List<String> saved = Files.readAllLines(Path.of("report.txt")); for (String line : saved) { System.out.println(line); } }}Output
Ana: 78.50 Ben: 75.00 Cy: 90.00 Top student: Cy Class average: 81.17
Step 1 of 5A new column
A new column
The file gains a third mark on every line: Ana,72,85,90. Where does the finished program change so that all three marks count?
Predict the report
The change from step 1 is made, marked below. The file holds Ana,72,85,90, Ben,90,60,75 and Cy,88,92,80. What is Ana's line in the report?
class Student: def __init__(self, name, marks): self.name = name self.marks = marks
def average(self): return sum(self.marks) / len(self.marks)
def __str__(self): return f"{self.name}: {self.average():.2f}"
def load(filename): students = [] with open(filename) as f: lines = f.readlines() for line in lines: try: parts = line.strip().split(",") marks = [int(parts[1]), int(parts[2]), int(parts[3])] students.append(Student(parts[0], marks)) except ValueError: print("Skipping bad line: " + line.strip()) return students
def top_student(students): best = students[0] for s in students: if s.average() > best.average(): best = s return best
def class_average(students): total = 0 for s in students: total = total + s.average() return total / len(students)
def report_lines(students): lines = [] for s in students: lines.append(str(s)) lines.append("Top student: " + top_student(students).name) lines.append(f"Class average: {class_average(students):.2f}") return lines
students = load("marks.txt")with open("report.txt", "w") as f: for line in report_lines(students): f.write(line + "\n")with open("report.txt") as f: saved = f.readlines()for line in saved: print(line.strip())import java.io.IOException;import java.io.PrintWriter;import java.nio.file.Files;import java.nio.file.Path;import java.util.ArrayList;import java.util.List;
class Student { String name; List<Integer> marks;
Student(String name, List<Integer> marks) { this.name = name; this.marks = marks; }
double average() { int total = 0; for (int m : marks) { total = total + m; } return (double) total / marks.size(); }
public String toString() { return name + ": " + String.format("%.2f", average()); }}
public class Main { static List<Student> load(String filename) throws IOException { List<Student> students = new ArrayList<>(); List<String> lines = Files.readAllLines(Path.of(filename)); for (String line : lines) { try { String[] parts = line.split(","); List<Integer> marks = List.of(Integer.parseInt(parts[1]), Integer.parseInt(parts[2]), Integer.parseInt(parts[3])); students.add(new Student(parts[0], marks)); } catch (NumberFormatException e) { System.out.println("Skipping bad line: " + line); } } return students; }
static Student topStudent(List<Student> students) { Student best = students.get(0); for (Student s : students) { if (s.average() > best.average()) { best = s; } } return best; }
static double classAverage(List<Student> students) { double total = 0; for (Student s : students) { total = total + s.average(); } return total / students.size(); }
static List<String> reportLines(List<Student> students) { List<String> lines = new ArrayList<>(); for (Student s : students) { lines.add(s.toString()); } lines.add("Top student: " + topStudent(students).name); lines.add("Class average: " + String.format("%.2f", classAverage(students))); return lines; }
public static void main(String[] args) throws IOException { List<Student> students = load("marks.txt"); try (PrintWriter out = new PrintWriter("report.txt")) { for (String line : reportLines(students)) { out.println(line); } } List<String> saved = Files.readAllLines(Path.of("report.txt")); for (String line : saved) { System.out.println(line); } }}A new rule
A new rule: a student passes when their average is 70 or more, and the report should say how many passed. Where does the counting belong?
Count the passes
The new function is written, with one line missing. Complete it so that a student is counted when the average is 70 or more. The report then ends with Passes: 3.
class Student: def __init__(self, name, marks): self.name = name self.marks = marks
def average(self): return sum(self.marks) / len(self.marks)
def __str__(self): return f"{self.name}: {self.average():.2f}"
def load(filename): students = [] with open(filename) as f: lines = f.readlines() for line in lines: try: parts = line.strip().split(",") marks = [int(parts[1]), int(parts[2])] students.append(Student(parts[0], marks)) except ValueError: print("Skipping bad line: " + line.strip()) return students
def top_student(students): best = students[0] for s in students: if s.average() > best.average(): best = s return best
def class_average(students): total = 0 for s in students: total = total + s.average() return total / len(students)
def passes(students): count = 0 for s in students: ____________________ # ← this student passes when the average is 70 or more count = count + 1 return count
def report_lines(students): lines = [] for s in students: lines.append(str(s)) lines.append("Top student: " + top_student(students).name) lines.append(f"Class average: {class_average(students):.2f}") lines.append("Passes: " + str(passes(students))) return lines
students = load("marks.txt")with open("report.txt", "w") as f: for line in report_lines(students): f.write(line + "\n")with open("report.txt") as f: saved = f.readlines()for line in saved: print(line.strip())import java.io.IOException;import java.io.PrintWriter;import java.nio.file.Files;import java.nio.file.Path;import java.util.ArrayList;import java.util.List;
class Student { String name; List<Integer> marks;
Student(String name, List<Integer> marks) { this.name = name; this.marks = marks; }
double average() { int total = 0; for (int m : marks) { total = total + m; } return (double) total / marks.size(); }
public String toString() { return name + ": " + String.format("%.2f", average()); }}
public class Main { static List<Student> load(String filename) throws IOException { List<Student> students = new ArrayList<>(); List<String> lines = Files.readAllLines(Path.of(filename)); for (String line : lines) { try { String[] parts = line.split(","); List<Integer> marks = List.of(Integer.parseInt(parts[1]), Integer.parseInt(parts[2])); students.add(new Student(parts[0], marks)); } catch (NumberFormatException e) { System.out.println("Skipping bad line: " + line); } } return students; }
static Student topStudent(List<Student> students) { Student best = students.get(0); for (Student s : students) { if (s.average() > best.average()) { best = s; } } return best; }
static double classAverage(List<Student> students) { double total = 0; for (Student s : students) { total = total + s.average(); } return total / students.size(); }
static int passes(List<Student> students) { int count = 0; for (Student s : students) { ____________________ // ← this student passes when the average is 70 or more count = count + 1; } } return count; }
static List<String> reportLines(List<Student> students) { List<String> lines = new ArrayList<>(); for (Student s : students) { lines.add(s.toString()); } lines.add("Top student: " + topStudent(students).name); lines.add("Class average: " + String.format("%.2f", classAverage(students))); lines.add("Passes: " + passes(students)); return lines; }
public static void main(String[] args) throws IOException { List<Student> students = load("marks.txt"); try (PrintWriter out = new PrintWriter("report.txt")) { for (String line : reportLines(students)) { out.println(line); } } List<String> saved = Files.readAllLines(Path.of("report.txt")); for (String line : saved) { System.out.println(line); } }}A new report line
A function that finds the lowest average is added, a mirror of the top-student one, and the report gets one more line. Both are marked. What is the last line of the report?
class Student: def __init__(self, name, marks): self.name = name self.marks = marks
def average(self): return sum(self.marks) / len(self.marks)
def __str__(self): return f"{self.name}: {self.average():.2f}"
def load(filename): students = [] with open(filename) as f: lines = f.readlines() for line in lines: try: parts = line.strip().split(",") marks = [int(parts[1]), int(parts[2])] students.append(Student(parts[0], marks)) except ValueError: print("Skipping bad line: " + line.strip()) return students
def top_student(students): best = students[0] for s in students: if s.average() > best.average(): best = s return best
def class_average(students): total = 0 for s in students: total = total + s.average() return total / len(students)
def lowest_student(students): lowest = students[0] for s in students: if s.average() < lowest.average(): lowest = s return lowest
def report_lines(students): lines = [] for s in students: lines.append(str(s)) lines.append("Top student: " + top_student(students).name) lines.append(f"Class average: {class_average(students):.2f}") lines.append(f"Lowest average: {lowest_student(students).average():.2f}") return lines
students = load("marks.txt")with open("report.txt", "w") as f: for line in report_lines(students): f.write(line + "\n")with open("report.txt") as f: saved = f.readlines()for line in saved: print(line.strip())import java.io.IOException;import java.io.PrintWriter;import java.nio.file.Files;import java.nio.file.Path;import java.util.ArrayList;import java.util.List;
class Student { String name; List<Integer> marks;
Student(String name, List<Integer> marks) { this.name = name; this.marks = marks; }
double average() { int total = 0; for (int m : marks) { total = total + m; } return (double) total / marks.size(); }
public String toString() { return name + ": " + String.format("%.2f", average()); }}
public class Main { static List<Student> load(String filename) throws IOException { List<Student> students = new ArrayList<>(); List<String> lines = Files.readAllLines(Path.of(filename)); for (String line : lines) { try { String[] parts = line.split(","); List<Integer> marks = List.of(Integer.parseInt(parts[1]), Integer.parseInt(parts[2])); students.add(new Student(parts[0], marks)); } catch (NumberFormatException e) { System.out.println("Skipping bad line: " + line); } } return students; }
static Student topStudent(List<Student> students) { Student best = students.get(0); for (Student s : students) { if (s.average() > best.average()) { best = s; } } return best; }
static double classAverage(List<Student> students) { double total = 0; for (Student s : students) { total = total + s.average(); } return total / students.size(); }
static Student lowestStudent(List<Student> students) { Student lowest = students.get(0); for (Student s : students) { if (s.average() < lowest.average()) { lowest = s; } } return lowest; }
static List<String> reportLines(List<Student> students) { List<String> lines = new ArrayList<>(); for (Student s : students) { lines.add(s.toString()); } lines.add("Top student: " + topStudent(students).name); lines.add("Class average: " + String.format("%.2f", classAverage(students))); lines.add("Lowest average: " + String.format("%.2f", lowestStudent(students).average())); return lines; }
public static void main(String[] args) throws IOException { List<Student> students = load("marks.txt"); try (PrintWriter out = new PrintWriter("report.txt")) { for (String line : reportLines(students)) { out.println(line); } } List<String> saved = Files.readAllLines(Path.of("report.txt")); for (String line : saved) { System.out.println(line); } }}- Add a third mark, a pass rule and a lowest-average line to the finished program, each in the one place where it belongs, and predict the report that comes out.