teaching machines

CS 148: Lab 7 – Conditionals and Loops

Welcome to lab 7!

If you have checkpoints from the last lab to show your instructor or TA, do so immediately. No credit will be given if you have not already completed the work, nor will credit be given after the first 10 minutes of this lab.

In this lab, we’ll explore conditional statements, which let us execute different actions under different circumstances. Conditional statements make our code sensitive, which is the best way to be. We’ll also explore loops which let the computer do the same thing over and over again without us doing the same thing over and over again.

Checkpoint 1

Person A types.

Write a class Ordinal with a main method and a method named getOrdinal. Have getOrdinal accept an int and return a String representation of that number with its correct ordinal suffix. For example:

System.out.println(getOrdinal(1)); // prints 1st
System.out.println(getOrdinal(2)); // prints 2nd
System.out.println(getOrdinal(3)); // prints 3rd
System.out.println(getOrdinal(4)); // prints 4th
System.out.println(getOrdinal(5)); // prints 5th
System.out.println(getOrdinal(6)); // prints 6th
System.out.println(getOrdinal(7)); // prints 7th
System.out.println(getOrdinal(8)); // prints 8th
System.out.println(getOrdinal(9)); // prints 9th
System.out.println(getOrdinal(10)); // prints 10th
System.out.println(getOrdinal(11)); // prints 11th
System.out.println(getOrdinal(12)); // prints 12th
System.out.println(getOrdinal(13)); // prints 13th
System.out.println(getOrdinal(14)); // prints 14th
System.out.println(getOrdinal(15)); // prints 15th
System.out.println(getOrdinal(16)); // prints 16th
System.out.println(getOrdinal(17)); // prints 17th
System.out.println(getOrdinal(18)); // prints 18th
System.out.println(getOrdinal(19)); // prints 19th
System.out.println(getOrdinal(20)); // prints 20th
System.out.println(getOrdinal(21)); // prints 21st
System.out.println(getOrdinal(22)); // prints 22nd
System.out.println(getOrdinal(100)); // prints 100th
System.out.println(getOrdinal(101)); // prints 101st
System.out.println(getOrdinal(102)); // prints 102nd
System.out.println(getOrdinal(111)); // prints 111th

In main, support a calculator-like interaction. Repeatedly prompt the user for an int and then print its ordinal number. When you run your program, you should see something like the following:

> 7
> 1113
> 73

Provide a means of gracefully stopping your program. The red stop button is not graceful.

Checkpoint 2

Person B types.

Write a program that turns a color image into an ASCII art representation. Find an image of your choice. (You might want to scale it down in an image editor—at least initially.) Read in the image with ImageIO.read. Use BufferedImage.getRGB to extract each pixel’s color as an int, but then construct a new Color object using the int representation. Convert the Color to a grayscale intensity in [0, 1] using this popular formula:

0.2126 * red + 0.7152 * green + 0.0722 * blue

Use this grayscale intensity to map to an appropriate character in an ASCII “colormap.” Either make your own or you can use this one:


A grayscale intensity close to 0 maps to a character that covers a lot of pixels ($). A grayscale intensity close to 1 maps to a character that covers few pixels ( ).

Print the symbols so that the ASCII representation looks like your original image. Pixels have a square aspect ratio, but our fonts generally do not. Characters are taller than they are wide. Because of this, you will see some distortion. Paste your output in this tool, which squashes the lines together.

If your image is big, Eclipse may not show the entire console output. Open the Eclipse preferences and find Run/Debug / Console. Uncheck Limit console output.


Leave a Reply

Your email address will not be published. Required fields are marked *