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

Java 3 Shami

Uploaded by

717823l121
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Java 3 Shami

Uploaded by

717823l121
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING 23CSR306 JAVA PROGRAMMING LABORATORY

Ex no: 3
DEVELOP PROGRAMS INCORPORATING PACKAGES, ABSTRACT CLASSES, AND INTERFACES
TO STRUCTURE CODE MODULARLY AND ENFORCE RECOVERY
Date:

Question 1:
Write a Java program to implement the following relationship and create a Main class to invoke all the
methods.

AIM:
To develop a java program for the given condition.
SOURCE CODE:
import java.io.*;
abstract class Shape
{
protected String color;
protected boolean filled;
public Shape() {}
public Shape(String color, boolean filled)
{
this.color = color;
this.filled = filled;
}
public String getColor()
{
return color;
}
public void setColor(String color)
{
this.color = color;
}
public boolean isFilled()
{
return filled;
}
public void setFilled(boolean filled)
{
this.filled = filled;

}
public abstract double getArea();
Register Number : 717823L152
DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING 23CSR306 JAVA PROGRAMMING LABORATORY
public abstract double getPerimeter();
@Override
public String toString()
{
return "Shape [color=" + color + ", filled=" + filled + "]";
}

}
class Circle extends Shape
{
protected double radius;
public Circle()
{
super();
}
public Circle(double radius)
{
super();
this.radius = radius;
}
public Circle(double radius, String color, boolean filled)
{
super(color, filled);
this.radius = radius;
}
public double getRadius()
{
return radius;
}
public void setRadius(double radius)
{
this.radius = radius;
}@Override
public double getArea()
{
return Math.PI * radius * radius;
}@Override
public double getPerimeter()
{
return 2 * Math.PI * radius;
}@Override
public String toString()
{
return "Circle [radius=" + radius + "]";
}

}
class Rectangle extends Shape
{
protected double length;
protected double width;
public Rectangle()
{
super();
}
public Rectangle(double length, double width)
{
Register Number : 717823L152
DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING 23CSR306 JAVA PROGRAMMING LABORATORY
super();
this.length = length;
this.width = width;
}
public Rectangle(double length, double width, String color, boolean filled)
{
super(color, filled);
this.length = length;
this.width = width;
}
public double getLength()
{
return length;
}
public void setLength(double length)
{
this.length = length;
}
public double getWidth()
{
return width;
}
public void setWidth(double width)
{
this.width = width;
}
public double getArea()
{
return length * width;

}
public double getPerimeter()
{
return 2 * (length + width);
}@Override
public String toString()
{
return "Rectangle [length=" + length + ", width=" + width + "]";
}

}
class Square extends Rectangle
{
public Square()
{
super();
}
public Square(double side)
{
super(side, side);
}
public Square(double side, String color, boolean filled)
{
super(side, side, color, filled);
}
public double getSide()
{
Register Number : 717823L152
DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING 23CSR306 JAVA PROGRAMMING LABORATORY
return super.getLength();
}
public void setLength(double side)
{
super.setLength(side);
}
public void setSide(double side)
{
super.setLength(side);
super.setWidth(side);
}
public void setWidth(double side)
{
super.setWidth(side);
}@Override
public String toString()
{
return "Square [Side=" + getSide() + "]";
}

}
public class TestShape
{
public static void main(String[] args)
{
Square s = new Square(10.0, "yellow", false);
Rectangle r = new Rectangle(20.5, 15.5, "green", true);
Circle c = new Circle(9.432, "blue", false);
System.out.println(s.getPerimeter());
System.out.println(s.getSide());
System.out.println(s.getColor());
System.out.println(s.getArea());
System.out.println(s);
System.out.println(r.getPerimeter());
System.out.println(r.getWidth());
System.out.println(r.getArea());
System.out.println(r);
System.out.println(c.getPerimeter());
System.out.println(c.isFilled());
System.out.println(c.getArea());
System.out.println(c);
}
}
OUTPUT:

Register Number : 717823L152


DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING 23CSR306 JAVA PROGRAMMING LABORATORY

Question 2
Write a Java program to implement the following relationship and create a Main class to invoke all the
methods.

AIM:
To develop a java program for the given condition.

SOURCE CODE:
import java.io.*;
abstract class CabinRental
{
private int cabinno;
private double rentalrate;
public CabinRental(int cabinno)
{
this.cabinno = cabinno;
}
public int getCabinno()
{
return cabinno;
}
public void setCabinno(int cabinno)
{
this.cabinno = cabinno;
}
public double getRentalrate()
{
return rentalrate;
}
public void setRentalrate(double rentalrate)
{
this.rentalrate = rentalrate;
}
public abstract double calculateRent(int days);
}

Register Number : 717823L152


DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING 23CSR306 JAVA PROGRAMMING LABORATORY
class HolidayCabinRental extends CabinRental
{
private double charge;
public HolidayCabinRental(int cabinno, double charge)
{
super(cabinno);
this.charge = charge;
}
public double calculateRent(int days)
{
return ((days * getRentalrate()) / 7) + charge;
}

}
class RegularCabinRental extends CabinRental
{
private double charge;
public RegularCabinRental(int cabinno, double charge)
{
super(cabinno);
this.charge = charge;
}
public double calculateRent(int days)
{
return ((days * getRentalrate()) / 7) + charge;
}

}
public class TestCabinRental
{
public static void main(String[] args)
{
HolidayCabinRental n1 = new HolidayCabinRental(10, 150);
RegularCabinRental n2 = new RegularCabinRental(1, 100);
if (n1.getCabinno() >= 1 && n1.getCabinno() <= 3) {
n1.setRentalrate(950);
}
else
{
n1.setRentalrate(1100);
}
System.out.println(n1.getCabinno());
System.out.println(n1.getRentalrate());
Register Number : 717823L152
DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING 23CSR306 JAVA PROGRAMMING LABORATORY
System.out.println(n1.calculateRent(30));
System.out.println(n2.getCabinno());
System.out.println(n2.getRentalrate());
System.out.println(n2.calculateRent(15));
}
}

OUTPUT:

Register Number : 717823L152


DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING 23CSR306 JAVA PROGRAMMING LABORATORY

Question 3
Write a Java program to implement the following relationship and create a Main class to invoke all the
methods.

AIM:
To develop a java program for the given condition.
SOURCE CODE:
import java.io.*;
interface Movable
{
public abstract void moveUp();
public abstract void moveDown();
public abstract void moveLeft();
public abstract void moveRight();
}
class MovablePoint implements Movable
{
private int x;
private int y;
private int xSpeed;
private int ySpeed;
public MovablePoint(int x, int y, int xSpeed, int ySpeed)
{
this.x = x;
this.y = y;
this.xSpeed = xSpeed;
this.ySpeed = ySpeed;
}
public void moveUp()
{
x = x + xSpeed;
System.out.println(x+", "+y);
}
public void moveDown()
{
y = y - ySpeed;
System.out.println(x+", "+y);
}
public void moveLeft()
{
x = x - xSpeed;
System.out.println(x+", "+y);
}
public void moveRight()
Register Number : 717823L152
DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING 23CSR306 JAVA PROGRAMMING LABORATORY
{
y = y + ySpeed;
System.out.println(x+", "+y);
}@Override
public String toString()
{
return "MovablePoint [x=" + x + ", y=" + y + ", xSpeed=" + xSpeed + ", ySpeed=" +
ySpeed + "]";
}

}
class MovableCircle implements Movable
{
private int radius;
private MovablePoint center;
public MovableCircle(int radius, MovablePoint center)
{
super();
this.radius = radius;
this.center = center;
}
public void moveUp()
{
center.moveUp();
}
public void moveDown()
{
center.moveDown();
}
public void moveLeft()
{
center.moveLeft();
}
public void moveRight()
{
center.moveRight();
}@Override
public String toString()
{
return "MovableCircle [radius=" + radius + ", center=" + center + "]";
}
}
public class TestMovablePoint1
{
public static void main(String[] args)
{
MovablePoint n1 = new MovablePoint(4, 5, 12, 3);
MovablePoint n2 = new MovablePoint(3, 1, 6, 5);
MovableCircle c1 = new MovableCircle(7, n2);
n1.moveUp(); n1.moveDown();n1.moveLeft();
System.out.println(n1); c1.moveUp(); c1.moveDown();c1.moveRight();
System.out.println(c1);
}
}

Register Number : 717823L152


DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING 23CSR306 JAVA PROGRAMMING LABORATORY

OUTPUT:

PREPARATION 30

LAB PERFORMANCE 30

REPORT 40

TOTAL 100

INITIAL OF FACULTY

RESULT:

Thus, the Java programs using packages, abstract classes, and interfaces to structure code modularly
and enforce abstraction has been successfully developed and the output is verified

Register Number : 717823L152


DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING 23CSR306 JAVA PROGRAMMING LABORATORY

Register Number : 717823L152

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy