Last modified: January 12, 2024
Lab A3.1 Pen
Pen Class
- Create a class to represent a pen and name it in the format
PX_LastName_FirstName_Pen
- Make a class variable named defaultColor and initialize it to "Black". This variable should be accessibe from any class.
- Declare an instance variable to represent the width of the pen as an integer. The width cannot change once it has been set. Do not initialize it yet. This variable should be freely accessible from any other class. Make sure you declare the variable with the correct key words and appropriate name to support these requirements using correct coding conventions.
- Declare a class variable to represent the default width of pens. This variable should be a constant that is initialized to 1 and can never change. This variable should be freely accessible from any other class. Make sure you declare the variable with the correct key words and appropriate name to support these requirements using correct coding conventions.
- Declare an instance variable named position that stores an instance of the Point class. This variable should be freely accessible from any other class.
- Declare an instance variable that stores the color of the pen as a String. This variable should only be directly accessible from inside this class.
- Define a default constructor that initializes the pen color to defaultColor, the width of the pen to the default width and the position to (0, 0)
- Define a second constructor that takes a width and initializes the width of the pen to that width. The color and position should be initialized the same as in the default constructor.
- Define a third constructor that takes both a width and a color and initializes the width and color of the pen accordingly. The position should still be initialized to (0, 0).
- Define a method named printDefaults() that prints the default color and default width. It should output something like this:
Default Color: Black, Default Width: 1
. This method should be declared in such a way that you can call it on the your pen class from any other class without having to make an instance of your class.
- Define a getter and setter for the color of the pen.
- Define an instance method named moveTo that takes parameters x and y and sets the position of the pen to (x, y)
- Define an instance method named toString() that returns a String describing the pen in the following format:
A Black Pen of width 4 located at (25, 16)
Driver Class
- Create a driver class with a main method.
- Print the default color for pens. Example:
The default color for pens is Black
- Change the default color for pens to "Blue".
- call the printDefaults() method
- Create an instance of a pen using the default constructor
- Print the width of the pen. Example:
pen width is 1
- Store the default width for pens in a local variable.
- Print the default width. Example:
The default width of pens is 1
- Store the x and y values of the pen you created in local variables.
- print the x and y values of the pen you created. Example:
The pen is at (0, 0)
- Change the position of the x coordinate of the pen to 23
- Print the position of the pen using the toString method of the Point class. Example:
Position is java.awt.Point[x=10,y=0]
- Store the color of the pen you created in a local variable
- Print the color of the pen you created. Example:
The pen color is Blue
- Change the color of the pen to "Green"
- Print out the new color of the pen. Example:
The pen color is Green
- Create a Pink pen with width 3 using the constructor that takes a width and color
- Use the move method to move the pink pen to (9, 67)
- Print the result of calling toString() on the pink pen.
Expected Output
The default color for pens is Black
defaultColor is Blue and DEFAULT_WIDTH is 1
pen width is 1
The default width of pens is 1
The pen is at (0, 0)
Position is java.awt.Point[x=23,y=0]
The pen color is Blue
The pen color is Green
A Pink Pen of width 3 located at (9, 67)
Submission
- Make sure you have named your classes correctly using the format
PX_LastName_FirstName_Pen
and PX_LastName_FirstName_Driver
where X is your period.
For example, if your name is Michael Wang and you are in 2nd period, the classes should be named P2_Wang_Michael_Pen
and P2_Wang_Michael_Driver
.
- Make sure you changed all references in your code accordingly so the code will still compile.
- If you changed the class names in BlueJ, the file names should have been automatically changed to match.
- Look in the file explorer/finder inside your project folder and find the corresponding java files named in the format
PX_LastName_FirstName_Pen.java
and PX_LastName_FirstName_Driver.java
. You should submit those two files below.
- You can drag and drop or click on the Browse button and select both files so they are both submitted in a single submission.
You must Sign In to submit to this assignment