0% found this document useful (0 votes)
33 views

SRICHARAN SRIDHAR 20MIA1014 - PSOOP - Java - IPS9

The document contains 4 code examples that demonstrate inheritance and interfaces in Java. The first example defines a Circle class that implements a GeometricObject interface to calculate area and perimeter. It also defines a ResizableCircle class that extends Circle and implements a Resizable interface to resize the circle radius by a percentage. The second example similarly defines Circle and Rectangle classes that implement a GeometricObject interface. The third example defines a MovablePoint class that implements a Point interface to move the point coordinates. The fourth example is a repeat of the first example.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

SRICHARAN SRIDHAR 20MIA1014 - PSOOP - Java - IPS9

The document contains 4 code examples that demonstrate inheritance and interfaces in Java. The first example defines a Circle class that implements a GeometricObject interface to calculate area and perimeter. It also defines a ResizableCircle class that extends Circle and implements a Resizable interface to resize the circle radius by a percentage. The second example similarly defines Circle and Rectangle classes that implement a GeometricObject interface. The third example defines a MovablePoint class that implements a Point interface to move the point coordinates. The fourth example is a repeat of the first example.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Sricharan Sridhar-20mia1014-IPS9

1)

import java.util.Scanner;
interface GeometricObject_2
{
public double getPerimeter ();
public double getArea ();
}
class Circle3 implements GeometricObject_2
{
protected double radius;
Circle3 (double radius)
{
this.radius = radius;
}
@Override public double getPerimeter ()
{
return 2 * Math.PI * radius;
}
@Override public double getArea ()
{
return Math.PI * radius * radius;
}
public String toString ()
{
return "Radius: " + radius;
}
}

interface Resizable
{
public void resize (int percent);
}
class ResizableCircle extends Circle3 implements Resizable
{
ResizableCircle (double radius)
{
super (radius);
}
@Override public void resize (int percent)
{
radius += radius * percent / 100.0;
System.out.println ("The Resized radius is: " + radius);
}
}

public class Main


{
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
int percent;
double radius;
System.out.println ("Radius: ");
radius = sc.nextDouble ();
System.out.println ("Percent value: ");
percent = sc.nextInt ();
Circle3 c = new Circle3 (radius);
c.toString ();
System.out.println ("\nPerimeter: " + c.getPerimeter () + "\nArea: " + c.getArea ());
ResizableCircle rc = new ResizableCircle (radius);
rc.resize (percent);
sc.close ();
}
}
2)
import java.util.*;
interface Geometricobject
{
double getArea ();
double getPerimeter ();
}
class Circles implements Geometricobject
{
private double r;
public Circles (double r)
{
this.r = r;
}
public String toString ()
{
return " " + r;
}
@Override public double getArea ()
{
double area = r * r * Math.PI;
return area;
}
@Override public double getPerimeter ()
{
double p = 2 * r * Math.PI;
return p;
}
}

class Rectangles implements Geometricobject


{
double length, width;
public Rectangles (double length, double width)
{
this.length = length;
this.width = width;
}
public String toString ()
{
return " " + length + " " + width;
}
@Override public double getArea ()
{
double area = length * width;
return area;
}
@Override public double getPerimeter ()
{
double p = 2 * (length + width);
return p;
}
}

public class Main


{
public static void main (String args[])
{
double r, l, w;
Scanner sc = new Scanner (System.in);
System.out.println ("Enter the radius");
r = sc.nextDouble ();
Circles o1 = new Circles (r);
System.out.println ("The perimeter of the Circlesis = " +
o1.getPerimeter ());
System.out.println ("\r");
System.out.println ("The area of the Circlesis = " + o1.getArea ());
System.out.println ("Enter the length and width");
l = sc.nextDouble ();
w = sc.nextDouble ();
Rectangles o2 = new Rectangles (l, w);
System.out.println ("The perimeter of the Rectangles is = " +
o2.getPerimeter ());

System.out.println ("\r");
System.out.println ("The area of the Rectangles is = " + o2.getArea ());
}
}
3)
import java.util.Scanner;
interface point2{
public void moveUp();
public void moveDown();
public void moveLeft();
public void moveRight();
}

class movPoint2 implements point2{


int x;
int y;
float xSpeed;
float ySpeed;

movPoint2(int x, int y, float xSpeed, float ySpeed) {


this.xSpeed=xSpeed;
this.ySpeed=ySpeed;
}

@Override
public void moveUp() {
y-=ySpeed;
}
@Override
public void moveDown() {
y+=ySpeed;
}

@Override
public void moveLeft() {
x-=xSpeed;
}

@Override
public void moveRight() {
y-=ySpeed;
}

public String toString() {


return "X: " + x + "Y: " + y + "; XSpeed: " + xSpeed + "; YSpeed: " + ySpeed;
}

public class Main {


public static void main(String[] args) {
int x;
int y;
float xSpeed;
float ySpeed;
Scanner scan=new Scanner(System.in);
System.out.println("Enter X, Y Co-ordinates and Respective Speeds");
x=scan.nextInt();
y=scan.nextInt();
xSpeed=scan.nextFloat();
ySpeed=scan.nextFloat();
movPoint2 mp=new movPoint2(x,y,xSpeed,ySpeed);
mp.moveDown();
mp.moveLeft();
System.out.println(mp.toString());
scan.close();
}
}
4)
import java.util.Scanner;
interface GeometricObject_2
{
public double getPerimeter ();
public double getArea ();
}
class Circle3 implements GeometricObject_2
{
protected double radius;
Circle3 (double radius)
{
this.radius = radius;
}

@Override public double getPerimeter ()


{
return 2 * Math.PI * radius;
}

@Override public double getArea ()


{
return Math.PI * radius * radius;
}

public String toString ()


{
return "Radius: " + radius;
}
}

interface Resizable
{
public void resize (int percent);
}
class ResizableCircle extends Circle3 implements Resizable
{
ResizableCircle (double radius)
{
super (radius);
}

@Override public void resize (int percent)


{
radius += radius * percent / 100.0;
System.out.println ("The Resized value is: " + radius + ";");
}
}

public class Main


{

public static void main (String[]args)


{
// TODO Auto-generated constructor stub
Scanner sc = new Scanner (System.in);
int percent;
double radius;
System.out.println ("Enter the radius : ");
radius = sc.nextDouble ();
System.out.println ("Enter the percent value : ");
percent = sc.nextInt ();
Circle3 c = new Circle3 (radius);
c.toString ();
System.out.println ("\nPerimeter: " + c.getPerimeter () + "; \nArea: " +
c.getArea () + ";");
ResizableCircle rc = new ResizableCircle (radius);
rc.resize (percent);
sc.close ();
}
}

You might also like