THINK FIRST·CODE LATER

← All labs

Fix and clean the AI's late-penalty code

Problem

The syllabus rule for late submissions in StudyBuddy's "deadline" feature is:

  • submitted on time (0 days late or earlier) → full score;
  • 1 to 7 days late → lose 10% of the score per day late;
  • more than 7 days late → 0;
  • the result is rounded to the nearest integer (halves up), and never negative.

An AI assistant produced this method:

static int f(int s, int d) {
    int r = s;
    if (d > 0) r = s - s / 10 * d;     // 10% per day
    if (d >= 7) r = 0;
    return r;
}

It has two bugs (look closely at integer division and at the boundary) and several smells. Write a clean, correct program.

Input: lines score daysLate until the end of input (score 0–100 integer, daysLate any integer). For each, print score=S late=D -> R. At the end print Checked n submissions.

Input:

85 0
85 1
85 7
85 8
100 3
67 2

Output:

score=85 late=0 -> 85
score=85 late=1 -> 77
score=85 late=7 -> 26
score=85 late=8 -> 0
score=100 late=3 -> 70
score=67 late=2 -> 54
Checked 6 submissions

(For 85 and 1 day: 85 × 0.9 = 76.5 → 77. The AI version gives 77 only by luck: 85 / 10 * 1 = 8.)

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

s7-2-data-1.txt

85 0
85 1
85 7
85 8
100 3
67 2
What your program must print for s7-2-data-1.txt
score=85 late=0 -> 85
score=85 late=1 -> 77
score=85 late=7 -> 26
score=85 late=8 -> 0
score=100 late=3 -> 70
score=67 late=2 -> 54
Checked 6 submissions

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < s7-2-data-1.txt

Windows PowerShell does not understand <:

Get-Content s7-2-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.