How to Create Myoval & Myrectangle Classes
Object-oriented programming is a software development paradigm that deals with the interaction between objects. An object is an instance of a class in a running program. A class is a blueprint for an object and exists as source code. If you are interested in learning object-oriented programming, then you should practice programming in Java. Java is a very popular object-oriented language and fluency in this language is a very marketable skill. One way to practice Java is to write two simple classes that create ovals and rectangles.
Things You'll Need
- Java Software Development Kit with NetBeans Integrated Development Environment (IDE) Bundle (see Resource for link)
Instructions
-
-
1
Load the NetBeans IDE by clicking on its program icon. When the program loads, navigate to “New/New Project” and select “Java Application” from the list on the right-hand side of the screen. A new source code file appears in the NetBeans text editor. The source code file contains an empty main method.
-
2
Create a class called “myRectangle” by writing the following statement above the main method:
public class myRectangle
{}The curly brackets mark the code block for this class. All the code for this class must go inside these brackets.
-
-
3
Create two private data members for the rectangle class. These data members cannot be accessed directly by any other object. They will store the length and width values of the rectangle. Write the following inside the code block for “myRectangle.”
private float length = 0.0, width = 0.0;
-
4
Create a constructor for the class. A constructor is invoked every time a class is instantiated into an object. You can use a constructor to set critical parameters for your class, like the length and width variables of “myRectangle.” A constructor that sets these variables looks like this:
myRectangle(float length, float width)
{
this.length = length;
this.width = width;
} -
5
Create a “setter” method for the two variables. This allows other objects (client objects) to request the “myRectangle” class to change the value of one of its private variables. These methods are very simplistic and look like this:
public void setLength(float length)
{ this.length = length; }public void setWidth(float width)
{ this.length = width; } -
6
Create a “getter” method for the two variables. A “getter” method is the opposite of a “setter” method: it requests that the “myRectangle” class tell a client object what the value of its variables are. These methods are also very simplistic:
public float getLength()
{ return length;}
public float getWidth()
{ return width;} -
7
Create a “myOval” class using the same method as you used in “myRectangle.” The two classes are nearly identical, only “myOval” doesn’t have length or width variables, but instead it has two foci variables: fociA and fociB.
-
8
Create instances of “myOval” and “myRectangle” by placing the following code inside the “main” method:
myOval oval = new myOval(1.0, 2.0);
myRectangle rect = new myRectangle(5.0, 3.0); -
9
Print the length and width of the rectangle object to the output window by using the “getter” methods and the “println” function, like this:
System.out.println("Rectangle Width: " + rect.getWidth() );
System.out.println("Rectangle Length: " + rect.getLength() ); -
10
Execute the program by pressing the “F6” button. The program creates two objects out of the classes “myOval” and “myRectangle.” The output window also displays the width and length of the rectangle, like this:
Rectangle Width: 3.0
Rectangle Length: 5.0
-
1