This example shows how to define and use a class in Java. Let us consider a rectangle class which has two properties and four methods.
Rectangle
Properties:
private double length
private double width
Constructors:
public Rectangle ()
public Rectangle (double length, double width)
Methods:
public double area()
public double perimeter()
public void scaleUp(double percentage) public void scaleDown(double percentage)
Rectangle
Properties:
private double length
private double width
Constructors:
public Rectangle ()
public Rectangle (double length, double width)
Methods:
public double area()
public double perimeter()
public void scaleUp(double percentage) public void scaleDown(double percentage)
Property get and set functions are used read and write values from and to the properties of the class. The definition includes implementation of default constructor and one overloaded constructor.
The example contains two files. One file defines the Rectangle (Rectangle.java) class and the other file (RectangleDemo.java) contains a demo program to test the Rectangle class.
Source Code
Rectangle.java
/* Definition of class Rectangle */
public class Rectangle {
/* Declaring variables for properties */
private double length;
private double width;
/* Default constructor */
public Rectangle () {
this.length = 0;
this.width = 0;
}
/* Overloaded constructor */
public Rectangle (double length, double width) {
this.length = length;
this.width = width;
}
/* Functions to set/write values to properties */
public void setLength(double length) {
this.length = length;
}
public void setWidth(double width) {
this.width = width;
}
/* Functions to get/read values from properties */
public double getLength() {
return this.length;
}
public double getWidth() {
return this.width;
}
/* Functions to calculate and return area */
public double area() {
return this.length * this.width;
}
/* Functions to calculate and return perimeter */
public double perimeter() {
return 2 * (this.length + this.width);
}
/* Functions to scale up the rectangle by n% */
public void scaleUp(double percentage) {
this.length += this.length * percentage / 100;
this.width += this.width * percentage / 100;
}
/* Functions to scale down the rectangle by n% */
public void scaleDown(double percentage) {
this.length -= this.length * percentage / 100;
this.width -= this.width * percentage / 100;
}
}
public class Rectangle {
/* Declaring variables for properties */
private double length;
private double width;
/* Default constructor */
public Rectangle () {
this.length = 0;
this.width = 0;
}
/* Overloaded constructor */
public Rectangle (double length, double width) {
this.length = length;
this.width = width;
}
/* Functions to set/write values to properties */
public void setLength(double length) {
this.length = length;
}
public void setWidth(double width) {
this.width = width;
}
/* Functions to get/read values from properties */
public double getLength() {
return this.length;
}
public double getWidth() {
return this.width;
}
/* Functions to calculate and return area */
public double area() {
return this.length * this.width;
}
/* Functions to calculate and return perimeter */
public double perimeter() {
return 2 * (this.length + this.width);
}
/* Functions to scale up the rectangle by n% */
public void scaleUp(double percentage) {
this.length += this.length * percentage / 100;
this.width += this.width * percentage / 100;
}
/* Functions to scale down the rectangle by n% */
public void scaleDown(double percentage) {
this.length -= this.length * percentage / 100;
this.width -= this.width * percentage / 100;
}
}
RectangleDemo.java
public class RectangleDemo {
public static void main (String a[]) {
Rectangle r = new Rectangle();
//Rectangle r = new Rectangle(10.5d, 5.5d);
r.setLength(10.5D);
r.setWidth(5.5D);
System.out.println("Length:" + r.getLength());
System.out.println("Width:" + r.getWidth());
System.out.println("Area:" + r.area());
System.out.println("Perimeter:" + r.perimeter());
System.out.println("\n\nScale up by 10%");
r.scaleUp(10D);
System.out.println("Length:" + r.getLength());
System.out.println("Width:" + r.getWidth());
System.out.println("Area:" + r.area());
System.out.println("Perimeter:" + r.perimeter());
System.out.println("\n\nScale down by 10%");
r.scaleDown(10D);
System.out.println("Length:" + r.getLength());
System.out.println("Width:" + r.getWidth());
System.out.println("Area:" + r.area());
System.out.println("Perimeter:" + r.perimeter());
}
}
public static void main (String a[]) {
Rectangle r = new Rectangle();
//Rectangle r = new Rectangle(10.5d, 5.5d);
r.setLength(10.5D);
r.setWidth(5.5D);
System.out.println("Length:" + r.getLength());
System.out.println("Width:" + r.getWidth());
System.out.println("Area:" + r.area());
System.out.println("Perimeter:" + r.perimeter());
System.out.println("\n\nScale up by 10%");
r.scaleUp(10D);
System.out.println("Length:" + r.getLength());
System.out.println("Width:" + r.getWidth());
System.out.println("Area:" + r.area());
System.out.println("Perimeter:" + r.perimeter());
System.out.println("\n\nScale down by 10%");
r.scaleDown(10D);
System.out.println("Length:" + r.getLength());
System.out.println("Width:" + r.getWidth());
System.out.println("Area:" + r.area());
System.out.println("Perimeter:" + r.perimeter());
}
}
Output
Length:10.5
Width:5.5
Area:57.75
Perimeter:32.0
Scale up by 10%
Length:11.55
Width:6.05
Area:69.8775
Perimeter:35.2
Scale down by 10%
Length:10.395000000000001
Width:5.445
Area:56.60077500000001
Perimeter:31.680000000000003
Width:5.5
Area:57.75
Perimeter:32.0
Scale up by 10%
Length:11.55
Width:6.05
Area:69.8775
Perimeter:35.2
Scale down by 10%
Length:10.395000000000001
Width:5.445
Area:56.60077500000001
Perimeter:31.680000000000003
How to test this program yourself?
Now let us test this program in your computer. Create a file named Rectangle.java in a folder (For example D:\MYJAVA) and copy the above listed source code (section 1). Create another file RectangleDemo.java in the same folder and now copy source code from section 2.
Go to the command prompt and move to the folder where you have created the files by using CD command.
Now use javac command to compile the files as follows:
D:\MYJAVA> javac Rectangle.java
D:\MYJAVA> javac RectangleDemo.java
Once the compilation is over without any error, you can see two .class files in the folder with names Rectangle.class and RectangleDemo.class.
Now run the program by using the following command:
D:\MYJAVA> java RectangleDemo
You can download Java Development Kit (JDK) compatible to your platform from the website of Oracle.
No comments:
Post a Comment