Uploaded by Ahmed Alymani

howeW

advertisement
Ans1
public class Dog {
String name;
String color;
//creating a default constructor
public Dog() {
System.out.print("I am the costructor");
}
void barking() {
}
void hungry() {
}
void sleeping() {
}
public static void main(String[] args) {
// Declaring an object of Dog class using the default constructor
Dog dog = new Dog();
}
}
Ans2
Ans3
import java.util.Scanner;
class Rectangle {
double width,height;
// Parameterized constructor
Rectangle(double width, double height){
this.width=width;
this.height=height;
// Printing the area of the rectangle
System.out.print("\nThe area of the rectangle is : "+(this.width*this.height));
}
}
Test driver class //
public class TestRectangle {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Getting input from the user
System.out.print("Enter the height of the rectangle: ");
double height = input.nextDouble();
System.out.print("Enter the width of the rectangle: ");
double width = input.nextDouble();
// Declaring an object of Rectangle class using parameterized constructor
Rectangle rect = new Rectangle(height,width);
}
}
Download