|
Written by Chitu Okoli and Bilal Abdul Kader
|
|
Friday, 19 September 2008 11:35 |
|
Page 5 of 5
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.
|
|
Last Updated on Tuesday, 08 September 2009 16:40 |