Programming Assignment 2
CS 12300
Programming I: Java
Fall, 2020
This assignment is due Thursday, September 10.
This assignment makes use of the files contained in this zip file.
In the zip file there is a Java class called Hw2_long.java
that prints the following picture. Your assignment is to write a Java program called Hw2.java
that prints the exact same picture, but uses "variable abstraction" to make the program much shorter.
++++++++++++++++++++************++++++++++++++++++++
==================== ====================
==================== ====================
==================== ====================
==================== ====================
==================== ====================
==================== ====================
==================== ====================
==================== ====================
==================== ====================
==================== ====================
==================== ====================
==================== ====================
++++++++++++++++++++____________++++++++++++++++++++
++++++++++++++++++++************++++++++++++++++++++
==================== ====================
==================== ====================
==================== ====================
==================== ====================
==================== ====================
==================== ====================
==================== ====================
==================== ====================
==================== ====================
==================== ====================
==================== ====================
==================== ====================
++++++++++++++++++++____________++++++++++++++++++++
Remember that "variable abstraction" is the idea of giving a name to a value that we want to use, and reuse, many times. For example, the string "++++++++++++++++++++"
appears several times in the above picture. So we might want to give it a name.
String s = "++++++++++++++++++++";
Notice how the single letter s
now refers to a twenty letter string. You can get a short program to produce a long output by defining and using String
variables that have short names but represent long parts of the desired output.
The file Hw2_long.java
in the zip file has 2,581 characters in it. You find that out by right clicking on the file and choosing the "Properties" menu item. For this assignment, write a Java program Hw2.java
that outputs the above picture, but your Java program should have no more than 600 characters in it (you check this by right clicking on your file Hw2.java
and choosing the "Properties" item from the popup menu).
Your Hw2.java
program is supposed to be much shorter than the Hw2_long.java
program, but your Hw2.java
program should still be a properly formatted and indented Java program with your name and email address at the beginning.
Write a second program, called Hw2_short.java
, that also outputs the same picture as Hw2_long.java
, but your Hw2_short.java
does not need to be nicely formatted. The goal of your Hw2_short.java
program is to produce the given output with the shortest program that you can come up with. For example, I have a version of Hw2_short.java
that contains 220 characters. Your Hw2_short.java
program must compile, run, and produce the correct output, but other than that, you can break any Java rule you want in order to get the program shorter.
Here is an example of something you can do to shorten a program. Suppose you need to declare two string variables.
String a = "cat";
String b = "dog";
You can combine these two declarations into one declaration.
String a = "cat", b = "dog";
Notice that you only need to declare String
one time, and you replace one semicolon with a comma.
You can also remove "un-needed" spaces.
String a="cat",b="dog";
In the zip file there is a program called Hw2_short_demo.java
that has only 142 characters in it but it produces 1,024 output characters. Look at how it uses variables to amplify its output (and notice that it is kind of unreadable). Here is a puzzle. Can you modify Hw2_short_demo.java
so that it is a shorter program but it produces a longer output?
Turn in a zip file called CS123Hw2Surname.zip
(where Surname
is your last name) containing your versions of Hw2.java
and Hw2_short.java
.
This assignment is due Thursday, September 10.