[go: up one dir, main page]

0% found this document useful (0 votes)
2 views4 pages

experiment_no_4java

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 4

Experiment No : 04

Problem Statement : Create interface shape area() method.


Class rectangle and triangle inherits shape class. Calculate area
of rectangle and triangle.

interface shape
{
double area();
}
class rectangle implements shape
{
private double width;
private double height;

public rectangle(double width,double height)


{
this.width=width;
this.height=height;
}
@Override
public double area()
{
return width*height;
}
}
class triangle implements shape
{
private double base;
private double height;

public triangle(double base,double height)


{
this.base=base;
this.height=height;

@Override
public double area()
{
return 0.5*base*height;
}
}
public class calculatearea
{
public static void main(String args[])
{
rectangle r1=new rectangle(10,20);
System.out.println("Area of Rectangle is:"+r1.area());
triangle t1=new triangle(10,20);
System.out.println("Area of Triangle is:"+t1.area());
}

Output:

Problem Statement : Class student with variable rollno, getrollno(),


setrollno() methods.Class test inherits student class and have variables
sub1, sub2 and getmarks(), setmarks() methods.Interface sports with
variable smarks and set() method.Class result inherits test class and an
implements sport interface and displays that marks. Demonstrate these
classes with application.

class Student {
int rollno;

public void setrollno(int rollno) {


this.rollno = rollno;
}

public int getrollno() {


return rollno;
}
}

class Test extends Student {


int sub1, sub2;

public void setMarks(int sub1, int sub2) {


this.sub1 = sub1;
this.sub2 = sub2;
}

public int getSub1() {


return sub1;
}

public int getSub2() {


return sub2;
}
}

interface Sports {
int smarks = 60;

void setSportsMarks(int smarks);


}

class Result extends Test implements Sports {


int sportsMarks;

@Override
public void setSportsMarks(int smarks) {

this.sportsMarks = smarks;
}
public void display() {
int total = sub1 + sub2 + sportsMarks;
System.out.println("Roll Number: " + getrollno());
System.out.println("Marks in Subject 1: " + getSub1());
System.out.println("Marks in Subject 2: " + getSub2());
System.out.println("Sports Marks: " + sportsMarks);
System.out.println("Total Marks: " + total);
}
}

public class studentinfop4 {


public static void main(String[] args) {
Result = new Result();
result.setrollno(101);
result.setMarks(85, 90);
result.setSportsMarks(15);
result.display();
}
}

Output:

Conclusion:
Students are able to implement the interface concept.

You might also like