On this page:
Intro
1 Lab Skeleton
2 Clickable Shapes
3 Convertable Units of Volume
4 Directories
6.12

Lab 10: Exam Practice

Intro

You’ll work in this lab with your assigned partner.

The two of you will work as a team to solve problems. At any time, one of you will be the Head and the other will be the Hands. The Head does the thinking and the Hands does the typing. Hands type only what the Head tells them to, but you’re free to discuss any issues that pop up. You should switch off during the lab to make sure each of you get practice problem solving, dealing with syntax, and getting finger exercises on the keyboard.

1 Lab Skeleton

You must start this lab with this project skeleton. Unzip the file into your IdeaProjects directory and open it with IntelliJ to get started.

We’ll be using the JavaLib image and world library again in this lab; it’s all set up in the project skeleton. See the documentation for details about available classes/methods.

2 Clickable Shapes

A Shape is a data structure representing a clickable shape. All Shapes have some position, can be drawn as a WorldImage, and can be clicked (if the position of the click is inside the boundry of the image).

Ex 1: Create an interface Shape with three public methods:

Ex 2: Design a class Circle that implements the Shape interface. Its constructor should consume three values: Integer coordinates x & y and an Integer radius r.

Ex 3: Design a class Rectangle that implements the Shape interface. Its constructor should consume four values: Integer coordinates x & y and Integer dimensions width & height.

Ex 4: Design a class Square that implements the Shape interface. Its constructor should consume three values: Integer coordinates x & y and an Integer side-length side. Hint: you can use your Rectangle class in this implementation.

3 Convertable Units of Volume

Swap Head and Hands.

A unit of Volume allows us to easily measure some amount of liquid. But there are too many units to keep track! Let’s design a program that manipulates units to make the conversion easier. A liter will be our canonical unit of volume.

Ex 5: Implement an interface Volume with a single public method: Double asLiter().

Ex 6: Design the trivial class Liter that implements Volume. Its constructor should take a single Double argument: the amount of liters.

Ex 7: Design the class Cup that implements Volume. Its constructor should take a single Double argument: the amount of cups. Note: a single US cup is equal to about 0.24 liters.

Ex 8: Design the class Gallon that implements Volume. Its constructor should take a single Double argument: the amount of gallons. Note: a single US gallon is equal to about 3.9 liters.

Ex 9: Design another class of your choice that implements Volume. Its constructor should take a single Double argument: the amount of whatever unit you’re representing.

Ex 10: Implement a static method in each of your Volume classes Volume fromLiter(Double liters) that given some amount of liters returns a Volume of that class. For example: Cup.fromLiter(1.0).asLiter() should return a double close to 1.0.

4 Directories

Swap Head and Hands.

A directory looks a lot like a tree.

A Directory is one of:

We want to test to see if directories with certain kinds of names exist.

Ex 11: Implement the Directory method Boolean exists(Predicate<String> p) that tests whether this directory’s name or some sub-directory’s name satisfies the predicate p.