Session 4
TicketMachine.java
/**
* A TicketMachine that sells tickets for a fixed price.
* Demonstrates Javadoc comments with preconditions and postconditions.
*
* @author An AP Java student
* @version 1.0
* @since 2025
*/
public class TicketMachine {
private int price; // price of a single ticket
private int balance; // money inserted by the user
private int total; // total money collected by the machine
/**
* Constructs a TicketMachine with the given ticket price.
*
* @param ticketPrice the price of a single ticket
* Precondition: ticketPrice > 0
* Postcondition: price is set to ticketPrice, balance and total are 0
*/
public TicketMachine(int ticketPrice) {
price = ticketPrice;
balance = 0;
total = 0;
}
/**
* Inserts money into the machine.
*
* @param amount the amount of money to insert
* Precondition: amount > 0
* Postcondition: balance is increased by amount
*/
public void insertMoney(int amount) {
if (amount > 0) {
balance += amount;
}
}
/**
* Prints a ticket if enough money has been inserted.
*
* Precondition: balance >= price
* Postcondition: balance is decreased by price,
* total is increased by price
*/
public void printTicket() {
if (balance >= price) {
// simulate ticket printing
System.out.println("##################");
System.out.println("# Ticket");
System.out.println("# Price: " + price + " cents");
System.out.println("##################");
System.out.println();
balance -= price;
total += price;
} else {
System.out.println("You must insert at least: "
+ (price - balance) + " more cents.");
}
}
/**
* Returns the current balance (unspent money).
*
* @return the current balance
* Precondition: none
* Postcondition: balance remains unchanged
*/
public int getBalance() {
return balance;
}
/**
* Empties the machine of all collected money.
*
* @return the total money collected
* Precondition: none
* Postcondition: total is reset to 0
*/
public int emptyMachine() {
int amount = total;
total = 0;
return amount;
}
}
GamePoints.java
public class GamePoints {
public static void main(String[] args) {
int level1Points = 120;
int level2Points = 45;
int totalPoints = level1Points + level2Points;
int pointDifference = level1Points - level2Points;
int bonusMultiplier = level1Points * 2;
int averagePoints = totalPoints / 2;
int remainderPoints = totalPoints % 2;
System.out.println(totalPoints);
System.out.println(pointDifference);
System.out.println(bonusMultiplier);
System.out.println(averagePoints);
System.out.println(remainderPoints);
}
}
OldMacDonaldv1.java
public class OldMacDonaldv1 {
public static void main(String args[]) {
System.out.println("Old MacDonald had a farm.");
System.out.println("E-I-E-I-O");
System.out.println("And on this farm, they had a cow.");
System.out.println("E-I-E-I-O");
System.out.println("With a moo moo here and a moo moo there");
System.out.println("Here a moo, there a moo, everywhere a moo moo");
System.out.println("Old MacDonald had a farm");
System.out.println("E-I-E-I-O");
}
}
OldMacDonaldv2.java
public class OldMacDonaldv2 {
public static void intro() {
System.out.println("Old MacDonald had a farm");
chorus();
}
public static void chorus() {
System.out.println("E-I-E-I-O");
}
public static void main(String[] args) {
intro();
System.out.println("And on that farm they had a cow.");
chorus();
System.out.println("With a moo moo here and a moo moo there");
System.out.println("Here a moo, there a moo, everywhere a moo moo");
// TODO:
// 1. Call the method intro()
// 2. Print out the line "And on that farm..."
// with a duck or another animal
// 3. Call the method chorus
// 4. Print out the lines with the appropriate animal sounds
// 5. Call the method intro again
}
}
OldMacDonaldv3.java
public class OldMacDonaldv3 {
public static void main(String args[]) {
System.out.println("Old MacDonald had a farm.");
System.out.println("E-I-E-I-O");
System.out.println("And on this farm, they had a cow.");
System.out.println("E-I-E-I-O");
System.out.println("With a moo moo here and a moo moo there");
System.out.println("Here a moo, there a moo, everywhere a moo moo");
System.out.println("Old MacDonald had a farm");
System.out.println("E-I-E-I-O");
System.out.println("And on this farm, they had a duck.");
System.out.println("E-I-E-I-O");
System.out.println("With a quack quack here and a quack quack there");
System.out.println("Here a quack, there a quack, everywhere a quack quack");
System.out.println("Old MacDonald had a farm");
System.out.println("E-I-E-I-O");
}
}
OldMacDonaldv4.java
public class OldMacDonaldv4 {
public static void main(String args[]) {
intro();
verse("cow", "moo");
intro();
verse("duck", "quack");
intro();
}
/**
* The verse method prints out a verse for any given animal and sound.
*
* @param animal the name of the animal
* @param sound the sound the animal makes
*/
public static void verse(String animal, String sound) {
System.out.println("And on this farm, they had a " + animal);
chorus();
System.out.println("With a " + sound + " " + sound
+ " here and a " + sound + " " + sound + " there");
System.out.println("Here a " + sound
+ ", there a " + sound
+ ", everywhere a " + sound + " " + sound);
}
/**
* The intro method prints the introductory line of the song
* and then calls the chorus method.
*/
public static void intro() {
System.out.println("Old MacDonald had a farm");
chorus();
}
/**
* The chorus method prints the repeating "E-I-E-I-O" line.
*/
public static void chorus() {
System.out.println("E-I-E-I-O");
}
}
AntsSong.java
public class AntsSong
{
public static void chorus(String num)
{
System.out.println("The ants go marching " + num
+ " by " + num + " hurrah, hurrah");
System.out.println("The ants go marching " + num
+ " by " + num + " hurrah, hurrah");
}
public static void verse(String num, String action)
{
System.out.println("The ants go marching " + num + " by " + num);
System.out.println("The little one stops to " + action);
System.out.println("And they all go marching down to the ground");
System.out.println("To get out of the rain, BOOM! BOOM! BOOM! BOOM!\n");
}
public static void main(String args[])
{
// Call the chorus and verse methods
// with the correct arguments
// to print out all three verses above.
}
}
PoemLab.java
/**
* The Poem class...
*
* @author
* @since
* @version
*/
import java.util.*;
public class PoemLab {
// prints a simple header
public static void intro() {
System.out.println("Stopping by Woods — A Programmed Poem");
System.out.println("in the style of Robert Frost (1923)");
System.out.println("--------------------------------------");
System.out.println();
}
/**
* Prints a 4-line stanza inspired by the poem's scene/thought pattern.
* @param place a location detail (e.g., "the woods near the village")
* @param time a time/mood detail (e.g., "this quiet winter evening")
* @param image a sensory image (e.g., "the sweep of easy wind and downy flake")
* @param innerThought a reflective line (e.g., "I watch the dark fill up the snow")
*/
public static void stanza(String place, String time, String image, String innerThought) {
System.out.println("Whose " + place + " these are, I think I know;");
System.out.println("It is " + time + " and softly still.");
System.out.println("I notice " + image + ", slow and low,");
System.out.println(innerThought + ".");
System.out.println();
}
// Refrain method: echo the poem’s famous closing repetition.
public static void chorus() {
System.out.println("And miles to go before I sleep,");
System.out.println("And miles to go before I sleep.");
System.out.println();
}
public static void main(String[] args) {
// Initialize the Scanner. Declare a variable named in
// that reads from System.in
Scanner in = new Scanner(System.in);
intro();
// Example stanza calls (you must add/modify your own):
stanza(
"woods by the village road",
"this hush of falling snow tonight",
"the whispering wind and downy flakes",
"I pause and let the quiet fill"
);
stanza(
"quiet grove beyond the lake",
"a late and silver-stitched twilight",
"hoof-bells chiming once, then still",
"I weigh the pull of rest against the path"
);
// TODO #1:
// Write a THIRD stanza with your own vivid imagery.
// Change all four parameters so the scene evolves.
// stanza("<place>", "<time>", "<image>", "<innerThought>");
// TODO #2
// Write a FOURTH stanza that adds a subtle shift in mood or choice.
// stanza("<place>", "<time>", "<image>", "<innerThought>");
// TODO #3:
// After your FINAL stanza, call the chorus to close the poem:
// chorus();
}
}
Last updated
Was this helpful?