You now know the building blocks every program is made of: values and variables, maths and text, decisions, loops, arrays and methods. This lesson puts them together in one program, built the way programmers really work: a little at a time, running it after each step.
Plan first
The job: a teacher has names and test scores and wants a report with each student's letter grade, the class average and the top student. Before writing code, break it into small jobs, each one simple enough to write and check on its own:
Keep the names and scores in two arrays, in the same order.
Turn a score into a letter (a method with an if chain).
Print a line for each student (a loop).
Work out the average (a running total).
Find the top student (keep the best so far).
Step 1: the grade method
Write and test the smallest piece first. A method that turns a score into a letter can be checked with a few calls, including the edges between grades:
Java
public class Main {
static String letter(int score) {
if (score >= 90) return "A";
if (score >= 80) return "B";
if (score >= 70) return "C";
return "D";
}
public static void main(String[] args) {
System.out.println(letter(95) + letter(90) + letter(89) + letter(70) + letter(12));
}
}
Prints
AABCD
Step 2: the whole report
With the method working, the rest is loops over the arrays. The index i lines up names[i] with scores[i]. Notice how main reads almost like the plan, because each job has a name.
Java
public class Main {
static String letter(int score) {
if (score >= 90) return "A";
if (score >= 80) return "B";
if (score >= 70) return "C";
return "D";
}
static double average(int[] scores) {
int total = 0;
for (int s : scores) total += s;
return (double) total / scores.length;
}
static int bestIndex(int[] scores) {
int best = 0;
for (int i = 1; i < scores.length; i++) {
if (scores[i] > scores[best]) best = i;
}
return best;
}
public static void main(String[] args) {
String[] names = {"Ana", "Ben", "Cy", "Dee"};
int[] scores = {91, 78, 85, 64};
for (int i = 0; i < names.length; i++) {
System.out.printf("%-4s %3d %s%n", names[i], scores[i], letter(scores[i]));
}
System.out.printf("Average: %.1f%n", average(scores));
System.out.println("Top: " + names[bestIndex(scores)]);
}
}
Prints
Ana 91 A
Ben 78 C
Cy 85 B
Dee 64 D
Average: 79.5
Top: Ana
%-4s pads a name to 4 characters on the left and %3d puts a number in a column 3 wide, so the report lines up. bestIndex returns a position rather than a score, so main can look up the name that goes with it.
Where next
Try changing it: add a student, count how many got each letter, or read the scores with a Scanner. Small changes to a working program are the fastest way to learn. When you're ready, the Intermediate Java course shows how to keep a name and a score together in one object instead of two arrays, and how lists, maps and exceptions make programs like this sturdier.
Practice
Put these steps in the order you'd build the report.
Tap the items below in order.
More info
Quick reference
plan
Break the job into small pieces before coding.
build in steps
Write a piece, run it, then add the next.
test the edges
Try values right at the limits: 89, 90, 91.
parallel arrays
Two arrays that line up by index: names[i] goes with scores[i].
%-4s / %3d
Pad text left in 4 spaces / a number in a column 3 wide.