Java that runs as a real app
Most first programs print to a console and stop. App assignments give students buttons, text fields and labels on a phone screen, and their Java decides what happens when someone taps one.
Press Run, then type a bill amount. The app keeps running while you use it.
TipApp.java
Java app, one screen
public class TipApp {
double bill = 0;
public void onStart() {
setText("total", "Enter a bill amount");
}
public void onBillChanged(String value) {
bill = toDouble(value);
showTotal(0.18);
}
public void onGenerousClicked() {
showTotal(0.25);
}
void showTotal(double rate) {
double tip = bill * rate;
setText("total", "Tip " + round(tip) + " on " + round(bill));
}
}Press Run to compile and start the app.
Run compiles the code and calls onStart(). Typing and clicks call your event methods while the same app keeps running.
Events, not just a main method
Run compiles the code and calls onStart(). After that the program stays alive: clicking a button or typing in a field calls the matching method, and the app keeps its state between events.
That makes state, methods and event handling concrete. A counter that forgets its value is a bug the student can see rather than a mark on a rubric.
You lay out the screen
Place widgets in the designer, give each one a name, and students reach them from Java by that name. Export and import app files to reuse a layout across classes or hand a starting point to students.
Changing code does not change a running app. Restart ends it and clears the fields, which is the same model students meet later in any event-driven framework.