Session 5
Source Code
import java.util.*;
public class Session5 {
public static void main(String[] main) {
System.out.println("Session 5");
// example1 method call
// example2 method call
// example3 method call
// example4 method call
}
// example1 method definition
// example2 method definition
// example3 method definition
// example4 method definition
}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?