Tutorial 9: Introduction to Constructor for Rectangle class
Code:
MainRectangle.java
package tutorial;
import tutorial.Rectangle;
/**
*
* @author Ankit
*/
public class MainRectangle {
public static void main(String[] args) {
//nameofclass objectname = new nameofclass();
//instantiating the class
Rectangle rectFirstRectangle = new Rectangle(10,20);
Rectangle rectSecondRectangle = new Rectangle(50);
Rectangle rectThirdRectangle = new Rectangle();
//initialization of the data members of the Rectangle class
//objname.datamembername;
//rectFirstRectangle.setiLength(10);
//rectFirstRectangle.setiWidth(20);
//calculate area
//access the methods of the class
//int iAreaOfRectFirst = rectFirstRectangle.calcArea();
//print out
//System.out.println("area of my first rectangle is:"+ iAreaOfRectFirst);
System.out.print("Area of my first rectangle of dimention:"+rectFirstRectangle.getiLength()+"*"+rectFirstRectangle.getiWidth());
System.out.println(" is:"+rectFirstRectangle.calcArea());
System.out.print("Area of my second rectangle of dimention:"+rectSecondRectangle.getiLength()+"*"+rectSecondRectangle.getiWidth());
System.out.println(" is:"+rectSecondRectangle.calcArea());
System.out.print("Area of my third rectangle of dimention:"+rectThirdRectangle.getiLength()+"*"+rectThirdRectangle.getiWidth());
System.out.println(" is:"+rectThirdRectangle.calcArea());
}
}
import tutorial.Rectangle;
/**
*
* @author Ankit
*/
public class MainRectangle {
public static void main(String[] args) {
//nameofclass objectname = new nameofclass();
//instantiating the class
Rectangle rectFirstRectangle = new Rectangle(10,20);
Rectangle rectSecondRectangle = new Rectangle(50);
Rectangle rectThirdRectangle = new Rectangle();
//initialization of the data members of the Rectangle class
//objname.datamembername;
//rectFirstRectangle.setiLength(10);
//rectFirstRectangle.setiWidth(20);
//calculate area
//access the methods of the class
//int iAreaOfRectFirst = rectFirstRectangle.calcArea();
//print out
//System.out.println("area of my first rectangle is:"+ iAreaOfRectFirst);
System.out.print("Area of my first rectangle of dimention:"+rectFirstRectangle.getiLength()+"*"+rectFirstRectangle.getiWidth());
System.out.println(" is:"+rectFirstRectangle.calcArea());
System.out.print("Area of my second rectangle of dimention:"+rectSecondRectangle.getiLength()+"*"+rectSecondRectangle.getiWidth());
System.out.println(" is:"+rectSecondRectangle.calcArea());
System.out.print("Area of my third rectangle of dimention:"+rectThirdRectangle.getiLength()+"*"+rectThirdRectangle.getiWidth());
System.out.println(" is:"+rectThirdRectangle.calcArea());
}
}
Rectangle.java
package tutorial;
public class Rectangle {
//data memebers
//variables of the class
private int iLength;
private int iWidth;
//member functions
//functions of the class
int calcArea(){
return (iLength*iWidth);
}
//constructors
//two argument constructor with (int, int)
public Rectangle(int iLength, int iWidth) {
this.iLength = iLength;
this.iWidth = iWidth;
}
//one argument constructor (int)
public Rectangle(int iLength) {
this.iLength = this.iWidth = iLength ;
}
//no argument constructor
public Rectangle() {
this.iLength = 0;
this.iWidth = 0;
}
//do nothing constructor
// public Rectangle() {
// }
public class Rectangle {
//data memebers
//variables of the class
private int iLength;
private int iWidth;
//member functions
//functions of the class
int calcArea(){
return (iLength*iWidth);
}
//constructors
//two argument constructor with (int, int)
public Rectangle(int iLength, int iWidth) {
this.iLength = iLength;
this.iWidth = iWidth;
}
//one argument constructor (int)
public Rectangle(int iLength) {
this.iLength = this.iWidth = iLength ;
}
//no argument constructor
public Rectangle() {
this.iLength = 0;
this.iWidth = 0;
}
//do nothing constructor
// public Rectangle() {
// }
No comments:
Post a Comment