This assignment focuses on static methods, the println statement, and simple variables.
Due: 11:55 PM, Oct. 21, 2025, Tuesday.
//******************************************************************* // // 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.
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.
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:
//******************************************************************* // // File: Song.java Assignment No.: 2 // // Author: <your name> Email: <your email> // // Class: Song // // Time spent on this problem: // -------------------- // This program xxxx. // // //*******************************************************************
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.
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!