Latihan untuk menguji kefahaman pelajar tentang object dan constructor
Tuliskan output untuk program berikut :-
public class TestRec {
public static void main(String[] args) {
Rectangle s4 = new Rectangle();
System.out.println(" Perimeter s4=" + s4.per());
System.out.println("Area s4 =" +s4.area());
Rectangle segi4 = new Rectangle(3,5);
System.out.println(" Perimeter segi4=" + segi4.per());
System.out.println("Area segi4 =" +segi4.area());
}
}
class Rectangle {
double length, width;
double per()
{
return 2*(length+width); }
double area()
{
return length*width; }
Rectangle()
{
width =1;
length =2; }
Rectangle(double newwidth, double newlength)
{
width =newwidth;
length =newlength; }
}
No comments:
Post a Comment