Assignment 2 (PS2): Java Static Methods and Variables

This assignment focuses on static methods, the println statement, and simple variables.

Due: 11:55 PM, Oct. 21, 2025, Tuesday.


Part 0. Programming Style

In this part, please read the Elements of Programming Style carefully, and follow the guidelines in the remaining assignments of this class.

Part 1. ATM Password

In the first part, we practice reading code with methods. Below is a program that prints out an ATM password.
//*******************************************************************
//
//   File: ATMPassword.java          Assignment No.: 2
//
//   Author:       Email: 
//
//   Class: ATMPassword
// 
//   Time spent on this problem: 
//   --------------------
//   
//   The original program prints out: 
//
//*******************************************************************
public class ATMPassword
{
  public static void main (String[] args)
  {
    System.out.println("Dear thieves, please don't steal my ATM card.");
    System.out.println("But if you do, here's my password:");
    firstpart();
    secondpart();
    System.out.println("");
  }
  
  public static void firstpart() {
    System.out.print("1");
  }
 
  public static void secondpart() {
    System.out.print("8");
    firstpart();
    lucky();
    lastbit();
  }
  
  public static void lucky() {
    System.out.print("7");
  }
  
  public static void lastbit() {
    System.out.print("3");
    lucky();
    System.out.print("4");
  }
}

Step 1: Try to figure out what the program does simply by looking at it. After you have decided what it prints out, create ATMPassword.java by copying and pasting this program. Add a comment at the beginning of ATMPassword.java indicating what the program prints out.

Step 2: It is possible to change the password (the program output) to: 1374813747374 by adding a single line of code (i.e., some text followed by a single semicolon) somewhere in the program. Add the line to ATMPassword.java to the appropriate location.

Part 2. Generate A Cumulative Song

In the second part, you will write methods to denote structure and remove redundancy.

A cumulative song is one where each verse builds upon previous verses. Examples of cumulative songs are "The House That Jack Built" and "There Was An Old Lady Who Swallowed A Fly." For this part, you will write a program that outputs a cumulative song below that is a variation of a classic holiday song. Name the class Song in a file named Song.java. (Use exactly this file name, including identical capitalization.)
On the 1st day of "Xmas", my true love gave to me
a partridge in a pear tree.

On the 2nd day of "Xmas", my true love gave to me
two turtle doves, and
a partridge in a pear tree.

On the 3rd day of "Xmas", my true love gave to me
three French hens,
two turtle doves, and
a partridge in a pear tree.

On the 4th day of "Xmas", my true love gave to me
four calling birds,
three French hens,
two turtle doves, and
a partridge in a pear tree.

On the 5th day of "Xmas", my true love gave to me
five golden rings,
four calling birds,
three French hens,
two turtle doves, and
a partridge in a pear tree.

On the 6th day of "Xmas", my true love gave to me
six geese a-laying,
five golden rings,
four calling birds,
three French hens,
two turtle doves, and
a partridge in a pear tree. 

<< your custom 7th verse goes here >>

The first six verses printed by your program must exactly reproduce the output. This includes identical wording, spelling,  spacing, punctuation, and capitalization.

Please note: Centering of the text is NOT required for this assignment.

However, to encourage creativity, the last verse of your song (the final bold part in << >> ) may print any text you like. Creative verses submitted may be shown in class anonymously at a later date. The only restrictions on your custom verse are the following:

One way to write this program would be to simply write a series of println statements that output each line of the song in order. But such a solution would not receive full credit. Part of the challenge of this assignment lies in recognizing the structure and redundancy of the song and improving the code using static methods.

Style Guideline

You should not place any println statements in the main method of the Song class . (It is okay for main to have empty println statements to print blank lines. Instead of printing in main, use static methods for two reasons:

  1. Structure

    You should write static methods to capture the structure of the song.

  2. Eliminating Redundancy

    You should use only one println statement for each distinct line of the song (other than blank lines). For example, the following line appears several times in the output, but you should have only one println statement in your program that prints that line of the song:

    a partridge in a pear tree.

    A design with many methods where each method prints just one line is not good style. Instead, identify groups of lines that appear in multiple places in the song and create methods to represent those groups. There is a general cumulative structural redundancy to the song that you should eliminate with your methods. Recall that methods can call other methods if necessary (which can themselves call other methods, and so on). The key question to ask is whether you have repeated lines or groups of lines of code that could be eliminated if you structured your methods differently. This includes sequences of println statements and also repeated sequences of method calls.

    In this assignment, you do not have to eliminate redundancy in lines that are similar but not identical, such as these:

    On the 1st day of "Xmas", my true love gave to me
    On the 2nd day of "Xmas", my true love gave to me

    We will learn how to do this later in the course.

    For this assignment, you should limit yourself to the Java features covered until Friday, Jan. 29th.

    As a point of reference, a solution we have for this part has 14 methods other than main and is around 90 lines long including comments and blank lines. This is just a rough guideline; you do not have to match this exactly.

  3. As always, please include comments in Song.java
    //*******************************************************************
    //
    //   File: Song.java          Assignment No.: 2
    //
    //   Author: <your name>      Email: <your email>
    //
    //   Class: Song
    // 
    //   Time spent on this problem: 
    //   --------------------
    //      This program xxxx.
    //   
    //
    //*******************************************************************
    
Please check this file for expected output.

Part 3. Five Hundred Miles

In the third part, we see how variables can help to eliminate redundancy.

The following program prints out the chorus to a popular song:

//*******************************************************************
//
//   File: WalkMiles.java     Assignment No.: 2
//
//   Author: <your name>      Email: <your email>
//
//   Class: WalkMiles
// 
//   Time spent on this problem: 
//   --------------------
//      This program generates variations of the 500 miles song.
//
//*******************************************************************
public class WalkMiles
{
  public static void main (String[] args)
  {
    //500 miles (by The Proclaimers) chorus:
    System.out.println("But I would walk 500 miles");
    System.out.println("And I would walk 500 more");
    System.out.println("Just to be the man who walks 1000 miles");
    System.out.println("To fall down at your door");
  }
}

Walking 500 miles seems pretty unrealistic. Let's say you wanted to play around with alternative distances. If you edit the above program directly, you'd have to edit three different lines of code (the first three System.out.println statements), and the song may not be even consistent: for example, use 200, 200, and 1000 as the three numbers do not add up. Create a class WalkMiles in a file named WalkMiles.java. At the beginning of the main method, create two variables representing the first two distance values. Then the first println statement uses the first distance value, the second uses the second distance value, and the third uses the sum of the two distance values. Make sure that your program has the identical output (line for line) if the two distance values are both 500.


Part 4. Submit Your Assignment

All you need to submit to Gitee for assignment #2 are (1) ATMPassword.java; (2) Song.java; and (3)WalkMiles.java. Remember to fill in the line "The original program prints out: " in ATMPassword.java.

The submission repository for ps2 is https://gitee.com/simmonsong/ct-xmuf25-ps2.

Please follow the instructions in Assignments Submission to submit your assignments.

Git introduction is a help document for git utilization..

Good luck and enjoy!

Enjoy!


Some problems derived from Building Java Programs.
Copyright (c) 2025, Qiao Xiang, Qingyu Song, School of Informatics, Xiamen University.