CS 148 Lab 7 – Conditional Statements
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.
Checkpoint 1
Person A types.
In this exercise, you will write methods to produce procedurally-generated images using conditional statements.
Create a labs.lab07
package in your IntelliJ project. Create class Picks
to contain your methods. Add the following five methods:
halfColor
stripesColor
circleColor
checkColor
boxColor
Have each return an int
representation of a Color
. Also have each accept four int
parameters: a column index, a row index, an image width, and an image height. Eventually you will use these parameters to arbitrate between colors. As a first step, however, just get your code to compile by returning 0.
The class PicksUtilities
below creates an image, calls upon your methods to fill in its colors, and displays the image. With the package selected in the Project pane, paste in its code. Do not edit this file; close it so that you are not tempted.
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class PicksUtilities {
private interface Colorizer {
int get(int c, int r, int width, int height);
}
public static void showHalf() {
showImage(Picks::halfColor);
}
public static void showBox() {
showImage(Picks::boxColor);
}
public static void showCircle() {
showImage(Picks::circleColor);
}
public static void showCheck() {
showImage(Picks::checkColor);
}
public static void showStripes() {
showImage(Picks::stripesColor);
}
private static void showImage(Colorizer colorizer) {
BufferedImage image = new BufferedImage(512, 512, BufferedImage.TYPE_INT_RGB);
for (int r = 0; r < image.getHeight(); ++r) {
for (int c = 0; c < image.getWidth(); ++c) {
image.setRGB(c, r, colorizer.get(c, r, image.getWidth(), image.getHeight()));
}
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JLabel label = new JLabel(new ImageIcon(image));
frame.add(label);
frame.pack();
frame.setVisible(true);
}
}
Back in your Picks
class, add a main
method that shows an image generated by calling halfColor
by calling this method from PicksUtilities
:
PicksUtilities.showHalf();
Run it. You should see an image that is all black.
You now have all the pieces in place to run and test your code. Now go back and complete the five methods to produce the images shown below. Use the parameters you are given to choose between colors. Feel free to pick different colors than you see in the images; just match the overall structure. To create a color from RGB components, use the Color
class: Color color = new Color(r, g, b)
. Alternatively, use the builtin Color
constants: Color.RED
, Color.PINK
, and so on. Turn a Color
into an int
with its getRGB
method.
- In
halfColor
, write a conditional statement that chooses between two colors to produce this image: - In
stripesColor
, write a conditional statement that chooses between three colors to produce this image: - In
circleColor
, write a conditional statement that chooses between two colors to produce this image: - In
checkColor
, write a conditional statement that chooses between two colors to produce this image: - In
boxColor
, write a conditional statement that chooses between three colors to produce this image:
When you are ready to show your work, call all five show*
methods from your main
.
Checkpoint 2
Person B types.
Write a class Ordinal
with a main
method (to test) 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
Be sure to test your code on a large variety of numbers. It’s very easy to think you’ve solved this correctly when you actually haven’t.
In main
, support a calculator-like interaction to generate ordinal numbers. 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 7th > 1113 1113th > 73 73rd
Provide a means of gracefully stopping your program. The red stop button is not graceful.