CS 1063  Lab 4:  Hyperbolas and Star Wars Names

The Hyperbola and StarWarsNames Classes

Objectives

Hand-in Requirements

All projects and laboratories will be submitted electronically through Blackboard.  Zip up your entire lab directory to submit as the source.  (Right click on the lab folder and follow Send To > Compressed (zipped) Folder or 7-Zip > Add to "lab4.zip".)  The lab folder should include the following:

Note that output files are not required for this assignment.

Introduction

This assignment has two parts: Hyperbola Drawing and The Star Wars Name Generator.  For this assignment, you will create two .java files: Hyperbola.java and StarWarsName.java.  These two files plus DrawingPanel.java should be in your lab4 folder.

Part I: Hyperbola Drawing

In your lab4 directory, create a program named Hyperbola.java

This program uses the Graphics object in a DrawingPanel. In order to use the DrawingPanel you must download DrawingPanel.java.  Save the DrawingPanel.java file to your lab4 directory.

Make a program that draws the following picture (click to see full size):



To help get you started, the following code creates a DrawingPanel object and then accesses the Graphics object in order to call drawLine.  The below only makes two calls to drawLine, which produces an "X" shape.  You will need to replace these calls with one loop (or two) that can produce the hyperbola pictures.

import java.awt.*;

public class Hyperbola {

  // The width and height of the DrawingPanel.
  public static final int PANEL_SIZE = 444;

  // Right now this draws an X, but should draw two hyperbolas.
  public static void main(String[] args) {
    DrawingPanel panel = new DrawingPanel(PANEL_SIZE, PANEL_SIZE);
    Graphics g = panel.getGraphics();
    g.drawLine(0, 0, PANEL_SIZE, PANEL_SIZE);
    g.drawLine(0, PANEL_SIZE, PANEL_SIZE, 0);
  }
}

Your hyperbola DrawingPanel should be 512 pixels wide and 512 pixels tall. The hyperbola in the lower left-hand corner has grid lines separated by 32 pixels, and the hyperbola in the upper right-hand corner has grid lines separated by 8 pixels. Here is what just the first five lines of the lower left-hand corner hyperbola looks like:



To be clear, here are the coordinates used to generate those five lines:

drawLine from point (0, 0) to (0, 512)
drawLine from point (0, 32) to (32, 512)
drawLine from point (0, 64) to (64, 512)
drawLine from point (0, 96) to (96, 512)
drawLine from point (0, 128) to (128, 512)

But do not use those numbers directly (your code must use a loop).  Use class constants for the size of the drawing panel, and for the 8- and 32-pixel intervals of the hyperbolas.  You must pick clear names for the class constants.

Note that the sample code that draws an "X" uses a class constant for the size of the panel with the wrong value.  See pages 106-108 of your text to find out how class constants can be used.

A properly implemented Hyperbola program shouldn't take more than 18-25 lines of code and one or two for loops.

Part II: The Star Wars name Generator

Also in your lab4 directory, create a program named StarWarsName.java.

The Star Wars universe is a world based on a series of movies from the 70s and then later the late-90s and early-00s.  The characters in the stories tend to have very funny names, like: Yoda, Moff Tarkin, Bail Organa, Darth Maul, Ponda Baba, and Edcel Bar Gane.  How made up!

For this part of the assignment, you will write you own Star Wars Name Generator, using a simple algorithm that manipulates strings input by the user.

Here are three example sessions:

Star Wars Name Generator

Enter your first name: Thom
Enter your last name: Yorke
Enter your mother's maiden name: Selway
Enter the city where you were born: Wellingborough
Enter the first car your drove: Audi
You are THOYO SEWEL of KEAUDI
Star Wars Name Generator

Enter your first name: Portia
Enter your last name: DeRossi
Enter your mother's maiden name: Lee
Enter the city where you were born: Horsham
Enter the first car your drove: Maserati
You are PORDE LEHOR of SIMASERATI
Star Wars Name Generator

Enter your first name: Emily
Enter your last name: Deschanel
Enter your mother's maiden name: Weir
Enter the city where you were born: Los Angeles
Enter the first car your drove: Dodge
You are EMIDE WELOS of ELDODGE

Here are the rules your program must follow to generate the Star Wars name and planet:

In terms of the coding for StarWarsName.java there are additional requirements to earn full credit:

You may assume that all names entered by the user are three or more characters long.  But, if you'd like, you can (optionally) enhance your program by doubling the user's input; for example, treating the last name "Xu" as "XuXu".

Lab 4 Grading Rubric

Your programs should compile without any errors.  A program with more than one or two compile errors will likely get a zero for the whole assignment.

There are 20 points possible for this assignment:

Good luck, and have fun!