Objects First with Java Lab 4: Circle Outline Replace this with your course title and your name/contact details. © David J. Barnes and Michael Kölling
Objects First with Java Basic class structure public class ClassName { Fields Constructors Methods } Three major components of a class: Fields – store data for the object to use Constructors – allow the object to be set up properly when first created Methods – implement the behavior of the object © David J. Barnes and Michael Kölling
Objects First with Java Fields Fields store values for an object. They are also known as instance variables. Fields define the state of an object. public class Square { private int x; private int y; private int size; private Color fillColor; // Further details omitted. } type visibility modifier variable name private int size; © David J. Barnes and Michael Kölling
Objects First with Java Constructors public Square(int x, int y) { this.x = x; this.y = y; size = 50; fillColor = Color.blue; } Constructors initialize an object. They have the same name as their class. They store initial values into the fields. They often receive external parameter values for this. Only here as a brief introduction and to reinforce what they saw last class © David J. Barnes and Michael Kölling
Objects First with Java Methods /** * Gets the size of the square. */ method header/signature return type visibility modifier method name parameter list (empty) public int getSize() { return size; } return statement Only here as a brief introduction start and end of method body (block) © David J. Barnes and Michael Kölling
Method & Field Practice Objects First with Java Method & Field Practice Modify the Circle class to display a colored outline: Add a field outlineColor, plus get & set methods Add another constructor that takes the outline color as a parameter What should the outline color be set to in the original constructors? Update paintComponent to draw the outline AND the filled oval (hint: use drawOval) Use your Picture applet to test and make sure the outline is showing up correctly Constructor + method practice THEN talk about what they’re going to do next time… with making the applet interactive © David J. Barnes and Michael Kölling
Extra: Change to Square & Triangle Make similar changes to Square and Triangle that you did to Circle Add a constructor that accepts as parameters the initial x and y positions, radius, and color Add a field outlineColor, plus get & set methods Add another constructor that takes the outline color as a parameter What should the outline color be set to in the original constructors? Update paintComponent to draw the outline AND the filled oval Use your Picture applet to test and make sure the outline is showing up correctly
What we did today … Concepts practiced Constructors Methods Classes Modified the Circle, Square, and Triangle classes: Added the ability to have a different colored outline
Homework Finish lab exercise Read Chapters 2 and 3