CS46A Spring 2020

Homework 01

Requirements

  1. Use Bluej to create your code. If you use an IDE like Eclipse or Netbeans, do not include a package statement. Codecheck will not be able to process your submission and you will get no credit.
  2. You must name your classes exactly as specified. Otherwise Codecheck will not be able to process your submission and you will get no credit.
  3. When you are finished with your code, submit it in Codecheck one final time. Notice at the bottom left there is a "Download" button. Click that and a .signed.zip file will be downloaded.
  4. When you are working on the draft, add the word "draft" to the end of the name, before .signed.zip after downloading. For example FirstProgram_draft.signed.zip. (Leave .signed.zip alone and do not add any extra dots.)
  5. When you are working on the final, add the word "final" to the end of the name, before .signed.zip. For example FirstProgram_final.signed.zip. This will help avoid submitting the wrong version.
  6. You must upload the .signed.zip file produced by Codecheck (not the .java file you wrote) to Canvas. Do not open and alter the downloaded file in anyway. The files are digitally signed and the graders will check that they have not been opened or altered.
  7. For both the draft and the final, you will upload 3 signed.zip files. Upload all three files at one time. Double check in Canvas that the files were uploaded.
  8. Warning: do not submit the final version as the draft. In order to be graded correctly, you must submit a program that does exactly what is specified for the draft - no more, no less.

1A

Draft

If you have not done so, create the directory structure specified in Lab1.

Start a new Bluej project called hw1a_first_draft in the cs46a/homework/hw01/draft folder. You can actually name the project anything, but this is a good naming convention that will help you keep things straight.

In the Bluej project, create a class called FirstProgram. You must use this exact name to pass Codecheck. Double click on the FirstProgram rectangle, remove all content inside, then copy and paste the start code from the 1A draft link below.

Finish the application so that it prints the first line of the poem "If" by Rudyard Kipling exactly like the following:

If you can keep your head when all about you

You will add the code inside the main method. To compile your code, click the Compile button on the top left of the Bluej editor. To run your program, go back to the Bluej workbench, right click on the FirstProgram rectangle, choose void main(String[] args), and click OK.

When you think your code is correct, click on the draft link below, then copy the whole class from Bluej and paste it into the text area in Codecheck. (This will overwrite the start code in Codecheck.) Click submit. If your code passes the test, Congratulations. If not, go back to Bluej, make any necessary changes, and repeat the process.

After you submit, an HTML report about the correctness of your solution is generated. Click the Download Report button and save it until you have all three programs (1A, 1B, and 1C)

Codecheck link for 1A draft

Final

When you are ready to work on the final, copy the Bluej project (hw1a_first_draft) into the cs46a/homework/hw01/final folder. Change the word draft to final. This will help you tell the draft and the final when both are open in Bluej. Open the copied project (hw1a_first_final) in Bluej by double clicking on package.bluej file. Finish the application so that it prints the first two and last two lines of the poem "If" by Rudyard Kipling exactly like the following:

If you can keep your head when all about you
 Are losing theirs and blaming it on you,
Yours is the Earth and everything that's in it,
 And-which is more-you'll be a Man, my son!
Note the second and fourth line are indented 1 space.

Codecheck for 1A final

1B

Draft

Start a new Bluej project called hw1b_unicode_draft in the cs46a/homework/hw01/draft folder. In the project, create a new class called UnicodeForAll . Go to the Codecheck draft link, copy the UnicodeForAll starter code you are given there, and paste it into your Bluej class.

In this application, you will print one line like that below the bar in the following image. You can use the English characters. You will add the code inside the main method.

screenshot draft

The "|" is called a pipe and is located above the Return (Enter) key. The "*" is an asterisk and is located above the 8. It is also the symbol used in Java for multiplication.

When you think your draft code is correct, copy the whole class from Bluej and paste it into the text area in Codecheck. Click submit. This will generate an HTML report about the correctness of your solution. Click the Download Report button and save it until you have all three.

Codecheck link for 1B draft

Final

In this application, you will print 3 lines and use some of the non-English characters. The output for the final version will look exactly as that below the bar in the following image.

screenshot

Copy the Bluej project (hw1b_unicode_draft) into the cs46a/homework/hw01/final folder. Change the word draft to final. Open the final project (hw1b_unicode_final) in Bluej and complete the assignment.

The last two lines in the output needs more explanation. These contain non-English characters. The second line is Spanish. It means girl. Notice the ~ over the "n". The last line contains symbols.

So how do you print non-English characters? Well, every printable character in English and most other languages, along with many symbols, is represented by a hexadecimal number (it is called Unicode). When you type a character on your keyboard, the computer software takes care of translating the character to its Unicode representation. But when we want to display a character that is not on the keyboard like the Spanish ñ, we have to supply the Unicode ourselves. We could also specify the English characters directly with Unicode, but it is much easier to let the computer take care of the translation. Here is a table of a few characters and their Unicode values:

Character Unicode
C \u0043
A \u0041
T \u0054
ñ \u00F1
snowman(snowman) \u2603
male sign(umbrella) \u2602
smiling face symbl (Apple) \uF8FF

In Java, the "\u" is an escape sequence which tells the compiler that the following characters have a special meaning, the Unicode for some character. The following two statements will display the same results: the word "CAT" in all uppercase. The first uses the Unicode values for the letters.

System.out.println("\u0043\u0041\u0054");
System.out.println("CAT");

Note: For this assignment, you must use Unicode for the characters for the non-English characters and not just paste the symbol in the println statement.

Codecheck link for 1B final

1C

Draft

Here it is a picture I like (Joe, Mike, me, and Tom). The draft program will just load the picture and display it.

Happy Retirement
HappyRetirement.jpg

For this project, you will use the Horstmann graphics. It is a good idea to view the videos for this lesson to help you get started. But note that Dr. Horstmann is using the workbench. You will be putting the code to make the picture and call the methods in the main method of a class. That way, you can run the program over and over.

Download hw1c_draft.zip which contains the graphics files you will need in this project. Unzip it into your cs46a/homework/hw01/draft folder. If you use Windows, be sure to unzip correctly. If you do not know how, do a Google search or ask in Piazza.

Open the folder. Double click package.bluej to open the project. Your workbench will look similar to this.

image of workbench

If it does not look like this image, STOP. We need to get you straightened out. You should not see a red folder that says "go up". That means you have not unzipped correctly.

You will type code into the main method of the Retirement class to do the following:

  1. Create a Picture object with the image (HappyRetirement.jpg)
  2. Draw the picture

Codecheck link for 1C draft

Final

The final version will modify the picture in some way before displaying it.

Here is the documentation for the grow method of the Picture class. You can use it to determine the arguments to specify for the amount to grow.

public void grow(double dw,
                 double dh)
     Resizes this picture both horizontally and vertically.


     Parameters:
         dw - the amount by which to resize the width on each side
         dw - the amount by which to resize the height on each side

That means you will use the method like this:

   myPic.grow(-10, -50); 

to reduce myPic by 10 pixels on each side and by 50 pixels on the top and bottom.

Codecheck link for 1C final

Last Modified: Feb 22, 2023