Assignment 5: "Classes and Objects"
Handed out: Tuesday, October 4, 2016 Due: Wednesday, October 12, 2016 Purpose: Learn
- how to write your own class,
- how to make objects of your class in the controller class,
- how to create and use state variables,
- how to use public methods in your class as a way to communicate between objects,
- introduction to event handling (e.g., mouse clicks)
Assignment:
- Download this code and uncompress (extract) it. (or download the files individually here) Also, here is a PanelApplication.java that you can run as a Java application that will then run the "Tester.java" applet.
- Look at the source code (.java) file Tester.
- Compile both Tester.java and PanelApplication.java. Run PanelApplication.java.
- What to Do:
- Make your own class. Create a new file, e.g., "JH_target.java". In it put the usual import statement, then "public class JH_target" (change the name to your class name!), and below that goes your code, in curly braces, to include state var. declarations and methods, as usual. Your class must have the following three public methods:
- public void paint(Graphics g)
- public void setX(int newX)
- public void setY(int newY)
- Your target class should keep track of the x,y coordinate of your target. Your paint method paints your target at the current x,y location, and your setX and setY methods take integers as input and update the x or y coordinate of your target. Your set methods should NOT call repaint, or paint. (That will done by the controller! (which means the Tester class in this case...) )
- Add code to the Tester class to create multiple (>4) instances (objects) of your target at different locations, and paint them there. Each target should be assigned to a different variable.
- Style: Indent, and comment (at a minimum, a main comment plus comments for each method in your target class, and comments for each loop in your Tester class),
- Email your source code in a zipped folder.