Ch-8, Part-1: Declare a class Room and create its objects in Java.
class Room
{
float length, width, height;
byte nWindows;
void setAttr (float l, float w, float h, byte n)
{
length=l;
width=w;
height=h;
nWindows=n;
}//end of setAttr() method
double area()
{
return (length * width);
}
void display()
{
System.out.println("=======================");
System.out.println("Length : "+ length);
System.out.println("Width : "+ width);
System.out.println("Height : "+ height);
System.out.println("Number of Windows : "+ nWindows);
System.out.println("Area : "+ area());
System.out.println("=======================\n");
}//end of display()method
}//end of Room class
/*using Room class to create object and run application*/
class RoomDemo
{
public static void main(String args[])
{
Room r1; //(Declaration) reference variable with null value by default
r1 = new Room(); //instantiation
Room r2 = new Room(); //initialization
r1.display();
r2.display();
//Assign value of attributes
r1.setAttr(18,12.5f,10,(byte)2);
r2.setAttr(14,11,10,(byte)1);
r1.display();
r2.display();
System.out.println("Area of Room with Length: " + r1.length + ", Width: " + r1.width + " = " + r1.area());
System.out.println("Area of Room with Length: " + r2.length + ", Width: " + r2.width + " = " + r2.area());
}//end of main
}//end of RoomDemo class
{
float length, width, height;
byte nWindows;
void setAttr (float l, float w, float h, byte n)
{
length=l;
width=w;
height=h;
nWindows=n;
}//end of setAttr() method
double area()
{
return (length * width);
}
void display()
{
System.out.println("=======================");
System.out.println("Length : "+ length);
System.out.println("Width : "+ width);
System.out.println("Height : "+ height);
System.out.println("Number of Windows : "+ nWindows);
System.out.println("Area : "+ area());
System.out.println("=======================\n");
}//end of display()method
}//end of Room class
/*using Room class to create object and run application*/
class RoomDemo
{
public static void main(String args[])
{
Room r1; //(Declaration) reference variable with null value by default
r1 = new Room(); //instantiation
Room r2 = new Room(); //initialization
r1.display();
r2.display();
//Assign value of attributes
r1.setAttr(18,12.5f,10,(byte)2);
r2.setAttr(14,11,10,(byte)1);
r1.display();
r2.display();
System.out.println("Area of Room with Length: " + r1.length + ", Width: " + r1.width + " = " + r1.area());
System.out.println("Area of Room with Length: " + r2.length + ", Width: " + r2.width + " = " + r2.area());
}//end of main
}//end of RoomDemo class
No comments
Post your comments