-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7914a60
commit f31580d
Showing
4 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package class_basic; | ||
|
||
public class Box { | ||
private double width, height, depth; | ||
|
||
public Box(){ | ||
width = 5; | ||
height = 10; | ||
depth = 2; | ||
} | ||
public Box(double len){ | ||
width = height = depth = len; | ||
} | ||
|
||
public Box(double width, double height, double depth){ | ||
this.width = width; | ||
this.height = height; | ||
this.depth = depth; | ||
} | ||
|
||
public Box(Box box){ | ||
width = box.width; | ||
depth = box.depth; | ||
height = box.height; | ||
} | ||
|
||
|
||
public double getWidth() { | ||
return width; | ||
} | ||
|
||
public void setWidth(double width) { | ||
this.width = width; | ||
} | ||
|
||
public double getHeight() { | ||
return height; | ||
} | ||
|
||
public void setHeight(double height) { | ||
this.height = height; | ||
} | ||
|
||
public double getDepth() { | ||
return depth; | ||
} | ||
|
||
public void setDepth(double depth) { | ||
this.depth = depth; | ||
} | ||
|
||
public void setDim(double width, double height, double depth){ | ||
this.width = width; | ||
this.height = height; | ||
this.depth = depth; | ||
} | ||
|
||
public boolean equalTo(Box o){ | ||
return (o.width==width && o.height==height && o.depth==depth); | ||
} | ||
|
||
public double volume(){ | ||
return height*width*depth; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Box[width=" + width + ",height=" + height + ",depth=" + depth + "]"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package class_basic; | ||
|
||
public class Rectangle { | ||
private int width, length; | ||
|
||
// constructor - special method, named as "ClassName" and no return type | ||
public Rectangle(int width, int length){ | ||
this.width = width; | ||
this.length = length; | ||
} | ||
|
||
public double getArea(){ | ||
return width*length; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package class_basic; | ||
|
||
public class box_test { | ||
public static void main(String[] args) { | ||
Box box1 = new Box(); | ||
// Test constructors and toString() | ||
System.out.println(box1.toString()); // Explicitly calling toString() | ||
System.out.println(box1); // Implicit call to | ||
System.out.println(box1.volume()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package class_basic; | ||
|
||
public class test_rectangle { | ||
public static void main(String[] args) { | ||
Rectangle rectangle_1 = new Rectangle(5, 10); | ||
Rectangle rectangle_2 = new Rectangle(6, 12); | ||
|
||
System.out.println("Area of the rectangle = " + rectangle_1.getArea()); | ||
System.out.println("Area of the rectangle 2 = " + rectangle_2.getArea()); | ||
} | ||
|
||
} |