Classes: Creating classes


Text of video
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class Rectangle {
  private double width=1;
  private double length=1;
  public Rectangle() {
	  
  }//constructor
  public Rectangle(double width, double length) {
	if(width>0) this.width=width;
	if(length>0) this.length=length;
  }//constructor
public double getWidth() {
	return width;
}//getWidth
public void setWidth(double width) {
	if(width>0) this.width = width;
}//setWidth
public double getLength() {
	return length;
}//getLength
public void setLength(double length) {
	if(length>0) this.length = length;
}//setLength
@Override
public String toString() {
	return "" + width + "x" + length;
}//toString
  
}//Rectangle
CODE
1
2
3
4
5
6
7
8
9
10
public class Shapes {

	public static void main(String[] args) {
		Rectangle myRect=new Rectangle();
		Rectangle myRect2=new Rectangle(3,0);
		System.out.println(myRect);
		System.out.println(myRect2);
	}

}
CODE

NEXT: Files