WT 8
WT 8
1. Write a program in java to class Employee having private data members ID, company_name and
salary.Create a nested class called programmer which have
get_Emp_details() which access private data members of Employee class and get employee
details and display_Emp_details() function to display employee details.Take appropriate
member functions if required and work on the following. A. Inner class
B. Static Nested class
C. Method local inner class
D. Annonymous inner class
code:
import java.util.*;
public class employee
{
static int id;
static String comp_name;
static double salary;
void display_Emp_details()
{
System.out.println("Employee ID : "+id);
System.out.println("Company Name : "+comp_name);
System.out.println("Salary : "+salary);
}
}
output:
2. A plastic manufacturer sells plastic in different shapes like Two_D sheet and Three_D box. The
cost of sheet is Rs 40/ per square ft. and the cost of box is Rs 60/ per cubic ft. Implement
it in Java to calculate the cost of plastic as per the dimensions given by the user where
Three_D inherits from Two_D.
Code:
import java.util.Scanner;
class two_d
{
Scanner s1=new Scanner(System.in);
float l,b,s;
final int cost1=40;
final int cost2=60;
void input()
{
System.out.print("Enter length and breadth for sheet (feet sq): ");
l=s1.nextFloat();
b=s1.nextFloat();
System.out.print("Enter height of box in cubic feet: ");
s=s1.nextFloat();
}
}
class three_d extends two_d
{
void show()
{
System.out.print("Cost for sheet: "+l*b*cost1+"\n");
System.out.print("Cost for cube: "+s*s*cost2+"\n");
}
public static void main(String ss[])
{
three_d t1=new three_d();
t1.input();
t1.show();
}
}
output:
3. Illustrate the execution of constructors in multi-level inheritance with three Java classes –
plate(length, width), box(length, width, height), wood box(length, width, height, thick)
code:
import java.util.*;
class plate
{
int length,width,height,thick;
plate(int length,int width)
{
System.out.print("Length is: "+length+"\n");
System.out.print("Width is: "+width+"\n\n\n");
}
}
class box extends plate
{
box(int length,int width,int height)
{
super(1,1);
System.out.print("Length is: "+length+"\n");
System.out.print("Width is: "+width+"\n");
System.out.print("Height is: "+height+"\n\n\n");
}
}
class wood_box extends box
{
wood_box(int length,int width,int height,int thick)
{
super(1,1,1);
System.out.print("Length is: "+length+"\n");
System.out.print("Width is: "+width+"\n");
System.out.print("Height is: "+height+"\n");
System.out.print("Thick is: "+thick+"\n\n\n");
}
public static void main(String ss[])
{
wood_box w1=new wood_box(1,2,3,4);
}
}
Output:
4. Write a program in java to define a class Shape which has data member “area”and a
member function showArea(). Derive two classes Circle and Rectangle from Shape class. Add
appropriate data members and member functions to calculate and display the area of
Circle and Rectangle
code:
class q4_shape
{
public static void main(String arg[])
{
Rectangle r = new Rectangle(10, 4);
Circle c = new Circle(3.5);
System.out.println("Rectangle Area : " + r.getArea());
System.out.println("Circle Area : " + c.getArea());
System.out.println();
}
}
class Rectangle
{
double length;
double breadth;
Rectangle(double length, double breadth)
{
this.length = length;this.breadth = breadth;
}
double getArea()
{
return length * breadth;
}
}
class Circle
{
double radius;
Circle(double radius)
{
this.radius = radius;
}
double getArea()
{
return (22.0/7.0) * radius * radius;
}
}
output: