DESC 391 Assignments
This page lists the assignments for DESC 391, Fall 2008. Assignments will be graded according to the following guidelines (thanks to Bilal Abdul Kader for delineating these criteria):
- Correct execution of the program (75%)
- Compilation without errors
- No logical errors at run time
- Implementation of all required instructions
- User-friendly prompts (if any)
- Coherent display (if any)
- Elegance of the code (25%)
- Correct usage of Java structure and logical flow
- Neat implementation of class code
- Usage of correct naming conventions for classes, variables, and functions
- Usage of meaningful class, variable and function names
- Consistent indentation and alignment of code blocks
- Usage of comments within the code
- Adding a description section for the class at the beginning of the file (a few lines to describe the class, its methods, attributes, and general idea about its functionality)
- Description of dependencies and inheritance (if any)
- Innovation above and beyond the requirements (bonus points)
- Adding extra graphics or features
- Smart usage of advanced Java structures
- Reusing previous code (if possible)
- Impressive logic in implementing complicated functions
Partial sources for some assignments:
Assignment 1: Design playing cards
Due: 2:45 pm on Wednesday October 1
The purpose of this assignment is for you to begin working inside Java code; familiarize yourself with the structure of a Java program; and begin using Java programming structures. This assignment requires you to use the following resources:
- The BlueJ IDE
- The Card BlueJ project, prepared specifically for this assignment. Download this file, and unzip it to your computer. Then open the unzipped folder, named "card" in BlueJ, with Project -> "Open project ...".
Note: If you downloaded the Card project before 3:15 pm September 26, there was a bug in the code. I have posted instructions for fixing the bug without having to redownload the code.
Presentation
After you compile the given card project, create a new Card object: Right-click the Card class -> new Card() -> keep the default name (card1) and click OK. The Card object (card1) appears in red at the bottom.
The Card object has two public methods, which you can access by right-clicking card1: drawCards() and drawCards(String rank, String suit). First call drawCards(). A Java GUI window will open up, which looks like this:

The left card pane (with the red 10) is for you to draw the face of the cards. The right pane (with the house and sun) is for you to draw a design of the back of the card.
Under the hood
Before continuing, take a look at the code that generates the card. In the BlueJ project window, double-click the Card class, and scroll to the following code:
/**
* Draw default playing cards. Defaults are convenient for testing.
*/
public void drawCards()
{
drawCards( "10", "H" ); // arbitrary values
}
/**
* Draw playing cards. Edit this code to specify the drawing.
* @param rank Rank of the card (A, 2 to 10, J, Q, K)
* @param suit Suit of the card (S for spades, C for clubs,
* H for hearts, D for diamonds)
*/
public void drawCards( String rank, String suit )
{
// Set the image for the face ranks
if ( rank == "J" )
canvas.setFaceCardImage("images/jack.png");
else if ( rank == "Q" )
canvas.setFaceCardImage("images/queen.png");
else if ( rank == "K" )
canvas.setFaceCardImage("images/king.png");
canvas.setCardRank( rank );
canvas.setCardSuit( suit );
canvas.setVisible(true);
/* Drawing code begins here */
PictureSquare newWall = new PictureSquare();
newWall.moveVertical(80);
newWall.moveHorizontal(200);
newWall.changeSize(100);
newWall.makeVisible();
public void drawCards( String rank, String suit ) is the only method you are required to edit for this assignment. Each time you call drawCards(String rank, String suit) from BlueJ, it asks you to enter the rank and the suit (don't forget to include the double quote marks, because these values are strings—e.g "10" and "D", not 10 and D). Your final code must work for any valid value for rank and suit. You will have to completely rewrite everything from the line
/* Drawing code begins here */
up to the end of that method. You are not required (and do not need) to edit any other code for this assignment, though you are free to do so if you wish. However, for testing purposes while you are coding, it is inconvenient to repeatedly type in the values of the rank and suit you are testing. Thus, there is the drawCards() method that doesn't take any parameters, but simply creates a card based on whatever default is in the code (10 of hearts, in the original code). For convenience of testing, change the defaults to whatever you want to test, and then you can repeatedly run those values.
The project gives you five shapes you can work with: PictureEllipse, PictureCircle, PictureRectangle, PictureSquare, and PictureTriangle. (I used these funny names so as not to conflict with some existing Java classes with the same names.) The example code that draws the house shows examples of how to create and manipulate the shapes. To see the full range of possibilities of the given shapes, in the BlueJ Tools menu, select Project Documentation (Ctrl+J). Click each class on the left to see the methods available to each shape class. Here are some helpful ntoes:
- All the methods available to PictureShape are available to all the other shapes.
- To make an upside-down triangle, just use negative values for the y2offset and y3offset.
- Available colors are red, black, blue, yellow, green, magenta, white, cyan, darkGray, gray, lightGray, orange, and pink (exact spelling only). For all other colours, use hexadecimal RGB colour codes, with no spaces, prefixed with "#". Codes are available at http://en.wikipedia.org/wiki/Web_colors. For example, for Brown, use the String "#A52A2A".
Requirements

Face of the card
Write code to draw the pictures of the suit for the face card within the black borders of the card on the left. You should use the shapes you've been given to construct the appropriate suits (spades, clubs, hearts, and diamonds). However, rather than drawing the number of shapes that matches the rank (number) of the card, you can draw one shape in the middle regardless of its rank, like the ace. However, you should repeat a smaller version of the shape at the top left corner, below the number, and at the bottom right corner, above the number. You do not need to invert the shape at the bottom, as on real cards. For example, the completed 10 of hearts would looks something like the picture here.
- Your card has to print the correct shape based on the values entered. For example, if someone enters drawShapes("5","S"), it should show the five of spades.
- The current code will automatically print the correct rank (number) in the correct colour. You don't have to worry about this part. You only have to show the correct shape based on what the user enters.
- For the jack, queen and king, the current code will show the correct picture. However, you still have to show the correct small version of the suit on the top left or bottom right.
Back of the card
For the back of the card, you can make any simple design that is significantly different from the example given. Your design must meet the following minimum requirements:
- Unlike the example, the design must fully fit within the black borders you are given.
- You must use at least five different shape objects. Among these, you must use a circle/ellipse, triangle, and square/rectangle.
- You must use at least four different colours.
Submission
To submit the assignment, do the following:
- Package your completed project as a .jar file. In BlueJ, Project -> Create Jar File... -> Continue (Main class: none; select Include source and BlueJ project files).
- E-mail the jar file to
This e-mail address is being protected from spambots. You need JavaScript enabled to view it
no later than 2:45 pm on Wednesday October 1.
- This assignment counts as 10% of your class grade.
Assignment 2: Miscellaneous programs
Due: 2:45 pm on Wednesday October 22
The purpose of this assignment is for you to learn how to write classes, use arrays and ArrayLists, and use looping structures in Java. This is a series of three exercises. I recommend that you use the main() method in each class to write your own tests, but this is not required and will not be graded.
1. Palindromes
A palindrome is a word that is exactly the same when spelt forwards or backwards. Examples are madam, radar, kayak, redder, racecar, and deified. Write a program that implements a method testWord(String inputWord) that determines if inputWord is a palindrome; it should print its output to the terminal. Treat uppercase letters as lowercase letters. Write your solution in one class: Palindrome.
2. Counter
Define a class called Counter. An object of this class is used to count things, so it records a count that is a non-negative whole number (that is, greater than or equal to zero). (Hint: You need only one instance variable.) Include the following methods:
- Increase the count by 1: increment()
- Decrease the count by 1: decrement()
- Set the counter to a value passed to the method: setCount( int )
- Return the current count value: getCount()
- Print out the current value of the counter to the terminal, with an appropriate text label (e.g. "Counter value is 5."): printCount()
Be sure that no method allows the value of the counter to become negative. Be sure that no instance variable is accessible outside of the class. Write your solution in one class: Counter.
3. Flower Counter
Write a program in a class FlowerCounter that computes the cost of flowers sold at a flower stand. There are five kinds of flowers—petunia, pansy, rose, violet, and carnation. They cost, respectively, 50¢, 75 ¢, $1.50, 50 ¢, and 80 ¢ per flower.
- Create an array of strings that holds the names of these flowers. Create another array that holds the cost of each corresponding flower. Your program should receive the name of a flower and the quantity desired by a customer. Locate the flower in the name array and use that index to find the cost per stem in the cost array. Compute and print the total cost of the sale. Write your solution in one class: FlowerCounterArrays.
- Repeat the same assignment using ArrayLists instead of arrays. Write your solution in one class: FlowerCounterArrayLists.
- Both solutions should implement the following two methods:
- addOrder(String flowerName, int quantity) to add an item to the order.
- printTotal() to print out the total cost to the terminal.
Submission
To submit the assignment, do the following:
- Package your completed assignment (four classes) as a .jar file. In BlueJ, Project -> Create Jar File... -> Continue (Main class: none; select Include source and BlueJ project files). Name the file:
"FirstName LastName Assignment 2.jar".
- E-mail the jar file to
This e-mail address is being protected from spambots. You need JavaScript enabled to view it
no later than 2:45 pm on Wednesday October 22.
- This assignment counts as 12% of your class grade.
Assignment 3: Student Worker Hierarchy
Due: 11:59 pm on Wednesday November 12
The purpose of this assignment is for you to learn how to write class inheritance structures using regular classes, abstract classes, and interfaces. Implement the four classes/interfaces described below in Java. Use the class type specified, and implement the inheritance, if specified. Implement the methods specified using the exact names and parameters specified. You have to figure out the correct return type, if applicable. The method names imply the existence of certain attributes. You have to figure out what attributes are needed in the right classes, and add them in. The attributes should be properly encapsulated. (Note: if another class will inherit attributes from the current class, the attributes should be marked as protected instead of private to allow subclasses to access them.)
|
Name
|
Data type and inheritance
|
Attributes
|
Methods (use exact names provided)
|
|
Person
|
Abstract class (inherits from nothing else)
|
- Any attributes required by the methods
|
- getName(), setName(String)
- getAge(), setAge(byte). Do not permit values that are too high or too low.
- Abstract method void printInfo(): Print all attributes (whether inherited or from current class level) to the terminal with appropriate labels, each on a separate line
|
|
Student
|
Class (inherits Person)
|
- Any attributes required by the methods
|
- getSchool(), setSchool(String), getMajor(), setMajor(String)
- getGPA(), setGPA(float). Do not permit values that are too high or too low.
|
|
Worker
|
Interface (inherits from nothing else)
|
There are no instance variables in an interface (only constants are permitted) |
- getEmployer(), setEmployer(String)
- getWage(), setWage(float). Do not permit values that are too high (max $100/hr) or too low.
- void printInfo(): Print all attributes (whether inherited or from current class level) to the terminal with appropriate labels, each on a separate line
|
|
StudentWorker
|
Class (inherits from both Student and Worker)
|
- Any attributes required by inheritance
|
- Any methods required by inheritance
|
Notes
- It would help for you to first draw out on paper what the class hierarchy should look like based on the instructions given above for inheritance.
- The best approach to the assignment is to first implement Person, then Student, and then test Person and Student. Next, implement Worker, then StudentWorker, and then test everything together.
- I recommend that you use the main() method in the Student and StudentWorker classes to write your own tests, but this is not required and will not be graded.
- Pay careful attention to whether something is a class, an abstract class, an interface, a method, or an abstract method. If it is not specified as abstract, then it is not abstract, unless required by Java.
- StudentWorker is the hardest class to implement, since I do not specify what attributes or methods you need to implement or how to implement the inheritance—you have to figure out what must be added. However, the instructions require one correct solution, and you have to figure it out based on understanding Java’s rules of inheritance. Refer to the textbook chapters on inheritance to understand these rules.
Submission
To submit the assignment, do the following:
- Package your completed assignment as a .jar file. In BlueJ, Project -> Create Jar File... -> Continue (Main class: none; select Include source and BlueJ project files). Name the file:
"FirstName LastName Assignment 3.jar".
- E-mail the jar file to
This e-mail address is being protected from spambots. You need JavaScript enabled to view it
no later than 11:59 pm on Wednesday November 12.
- This assignment counts as 10% of your class grade.
Assignment 4: Constructors and GUI
Due: 11:59 pm on Monday December 8
The purpose of this assignment is for you to learn how to use constructors within the context of working classes, and how to do basic GUI programming in Java. There are two exercises. I recommend that you use the main() method in each class to write your own tests, but this is not required and will not be graded.
1. Temperature (5%)
Write a class Temperature that represents termperatures in degrees in both Centigrade and Fahrenheit. Use a float for the temperature, and a char for the scale: either 'C' for Centigrade or 'F' for Fahrenheit. The class should have the following methods (use exactly the method names and parameter types specified):
- Four constructors. For each of these constructors, assume zero degrees (0°) if no value is specified, and Centigrade if no scale is given:
- one that receives only the number of degrees;
- one that receives only the scale (C or F);
- one for both degrees and scale, in that order;
- and a default constructor.
- Three getters that return the temperature in the requested format, without changing the internally stored format. The getters should always return values rounded to one decimal place (e.g. 15.3 or 25.2):
- getCentigrade(): returns the temperature in degrees Centigrade.
- getFahrenheit(): returns the temperature in degrees Fahrenheit.
- getTemperature(): returns the temperature in the default format (Centigrade). However, it should not repeat code from other methods.
- Three setters:
- setDegrees(float);
- setScale(char);
- setTemperature(float,char).
- Three comparison methods:
- Overide the equals() method from Object to compare the current object with another Temperature object.
- boolean isLessThan(Temperature): tests if current object is less than another.
- boolean isGreaterThan(Temperature): tests if current object is greater than another.
2. Shape Generator (8%)
Create a GUI application called Shapes that generates new shapes at the user's request. Your finished application will look something like this:

The application must meet the following requirements:
- The position of the labels, combo boxes, text fields, buttons, and drawing panel should be approximately as shown in the picture above.
- You must use a drop-down combo box to list various shape and colour options:
- Give the user the choice of creating two shapes: a rectangle and an ellipse. For creating shapes, the following pages from the Java tutorial could be useful:
- Lesson: Working with Geometry
- Drawing Geometric Primitives. Don't forget to convert the Graphics object to Graphics2D with
Graphics2D g2 = (Graphics2D) g;
- For 1% bonus credit, you could also create a triangle. (Make sure everything is working before you go for the bonus!) For the triangle, create an isoceles triangle where the width is the length of the bottom line, and the height is the vertical height from the top point to the bottom line. You would need to create it as a polygon.
- Give the user the choice of any five colours. At least two colours that you offer the user must not be built-in default colours. That is, use the Color API to create your own colours that you offer the user.
- The "New Shape" button creates a shape on the drawing panel according to the current values in the combo boxes and text fields.
- The "Reset All" button clears the values of the combo boxes and text fields, and erases every shape from the panel.
- Your GUI application should end when the user closes the window.
Submission
To submit the assignment, do the following:
- Package your completed assignment as one single .jar file. In BlueJ, Project -> Create Jar File... -> Continue (Main class: none; select Include source and BlueJ project files). Name the file:
"FirstName LastName Assignment 4.jar".
- E-mail the jar file to
This e-mail address is being protected from spambots. You need JavaScript enabled to view it
no later than 11:59 pm on Monday December 8.
- This assignment counts as 13% of your class grade.
|