0% found this document useful (0 votes)
3 views23 pages

JAVA Lab 7

The document outlines a Java lab assignment involving the creation of a class hierarchy with a base class 'Shape', derived classes 'Circle' and 'Cylinder', and includes methods for calculating area and volume. It also demonstrates the use of protected members for accessing properties in subclasses, and provides additional classes for geometric shapes like 'Point', 'Line', 'Rectangle', and 'Square'. The code includes constructors, method overrides, and examples of object instantiation and output.

Uploaded by

ytlerin1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views23 pages

JAVA Lab 7

The document outlines a Java lab assignment involving the creation of a class hierarchy with a base class 'Shape', derived classes 'Circle' and 'Cylinder', and includes methods for calculating area and volume. It also demonstrates the use of protected members for accessing properties in subclasses, and provides additional classes for geometric shapes like 'Point', 'Line', 'Rectangle', and 'Square'. The code includes constructors, method overrides, and examples of object instantiation and output.

Uploaded by

ytlerin1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

JAVA Lab7

Am.sc.u4aie24029

1 Create a class hierarchy containing base class


shape, derived class circle and another class
cylinder that inherits circle. Shape has method
getArea(). Add appropriate constructors to
initialise each object. Make all data members as
private. Override toString() to display the state of
objects.

Code :

class Shape {
private String color;

public Shape() {
this.color = "undefined";
}

public Shape(String color) {


this.color = color;
}

public void printColor() {


System.out.println("Color: " + color);
}
public void draw() {
System.out.println("Drawing shape");
}

public void saveArea() {


System.out.println("Saving area...");
}

public double getArea() {


return 0;
}

public String toString() {


return "Color: " + color;
}
}

class Circle extends Shape {


private double radius = 1.0;

public Circle() {
super();
}
public Circle(double radius) {
super();
this.radius = radius;
}

public Circle(double radius, String color) {


super(color);
this.radius = radius;
}

public double getRadius() {


return radius;
}

public void setRadius(double radius) {


this.radius = radius;
}

public double getArea() {


return Math.PI * radius * radius;
}

public String toString() {


return super.toString() + ", Radius: " + radius
+ ", Area: " + getArea();
}
}

class Cylinder extends Circle {


private double height = 1.0;
public Cylinder() {
super();
}

public Cylinder(double height) {


super();
this.height = height;
}

public Cylinder(double radius, double height) {


super(radius);
this.height = height;
}

public Cylinder(double radius, double height,


String color) {
super(radius, color);
this.height = height;
}

public double getHeight() {


return height;
}

public void setHeight(double height) {


this.height = height;
}

public double getVolume() {


return getArea() * height;
}

public String toString() {


return super.toString() + ", Height: " + height
+ ", Volume: " + getVolume();
}
}

public class main {


public static void main(String[] args) {
Shape s = new Shape("Red");
System.out.println(s);

Circle c = new Circle(5.5, "Blue");


System.out.println(c);

Cylinder cy = new Cylinder(3.3, 10, "Green");


System.out.println(cy);
}
}

Output :
2 Make data members protected in shape and
circle classes and try to access it in sub
class.

Code :

class Shape {
protected String color;

public Shape() {
this.color = "undefined";
}

public Shape(String color) {


this.color = color;
}

public void printColor() {


System.out.println("Color: " + color);
}
public void draw() {
System.out.println("Drawing shape");
}

public void saveArea() {


System.out.println("Saving area...");
}

public double getArea() {


return 0;
}

public String toString() {


return "Color: " + color;
}
}

class Circle extends Shape {


protected double radius = 1.0;

public Circle() {
super();
}

public Circle(double radius) {


super();
this.radius = radius;
}

public Circle(double radius, String color) {


super(color);
this.radius = radius;
}

public double getRadius() {


return radius;
}

public void setRadius(double radius) {


this.radius = radius;
}

public double getArea() {


return Math.PI * radius * radius;
}

public String toString() {


return super.toString() + ", Radius: " + radius + ", Area: "
+ getArea();
}
}

class Cylinder extends Circle {


private double height = 1.0;
public Cylinder() {
super();
}

public Cylinder(double height) {


super();
this.height = height;
}

public Cylinder(double radius, double height) {


super(radius);
this.height = height;
}

public Cylinder(double radius, double height, String color) {


super(radius, color);
this.height = height;
}

public double getHeight() {


return height;
}

public double getVolume() {


return getArea() * height;
}
public String toString() {
return "Accessing Protected -> Color: " + color + ", Radius:
" + radius +
", Area: " + getArea() + ", Height: " + height + ",
Volume: " + getVolume();
}
}

public class Main {


public static void main(String[] args) {
Cylinder cyl = new Cylinder(2.0, 5.0, "Yellow");
System.out.println(cyl);
}
}

Output :
3

Code :

public class Point {


private double x;
private double y;

public Point() {
this.x = 0;
this.y = 0;
}

public Point(double x, double y) {


this.x = x;
this.y = y;
}

public double distance(Point other) {


double dx = this.x - other.x;
double dy = this.y - other.y;
return Math.sqrt(dx * dx + dy * dy);
}

public String toString() {


return "(" + x + "," + y + ")";
}
}

public class Line {


private Point p1;
private Point p2;

public Line(double x1, double y1, double x2, double


y2) {
this.p1 = new Point(x1, y1);
this.p2 = new Point(x2, y2);
}
public Line(Point p1, Point p2) {
this.p1 = p1;
this.p2 = p2;
}

public double getLength() {


return p1.distance(p2);
}

public String toString() {


return "Line[p1=" + p1.toString() + ", p2=" +
p2.toString() + "]";
}
}

public class Line {


private Point p1;
private Point p2;

public Line(double x1, double y1, double x2, double


y2) {
this.p1 = new Point(x1, y1);
this.p2 = new Point(x2, y2);
}
public Line(Point p1, Point p2) {
this.p1 = p1;
this.p2 = p2;
}

public double getLength() {


return p1.distance(p2);
}

public String toString() {


return "Line[p1=" + p1.toString() + ", p2=" +
p2.toString() + "]";
}
}

public class TestGeometry {


public static void main(String[] args) {
Point a = new Point(0, 0);
Point b = new Point(3, 4);

Line line1 = new Line(a, b);


Line line2 = new Line(1, 2, 4, 6);

System.out.println(line
System.out.println("Length: " + line1.getLength());
System.out.println(line2);
System.out.println("Length: " + line2.getLength());
}
}
4

package gshapes;

public abstract class Shape {


protected String color = "red";
protected boolean filled = true;
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();


public abstract double getPerimeter();
public String toString() {
return "Shape[color=" + color + ", filled=" + filled
+ "]";
}
}

package gshapes;

public class Circle extends Shape {


protected double radius = 1.0;

public Circle() {}

public Circle(double radius) {


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;
}

public double getArea() {


return Math.PI * radius * radius;
}

public double getPerimeter() {


return 2 * Math.PI * radius;
}

public String toString() {


return "Circle[" + super.toString() + ", radius=" +
radius + "]";
}
}

package gshapes;
public class Rectangle extends Shape {
protected double width = 1.0;
protected double length = 1.0;

public Rectangle() {}

public Rectangle(double width, double length) {


this.width = width;
this.length = length;
}

public Rectangle(double width, double length, String


color, boolean filled) {
super(color, filled);
this.width = width;
this.length = length;
}

public double getWidth() {


return width;
}

public void setWidth(double width) {


this.width = width;
}
public double getLength() {
return length;
}

public void setLength(double length) {


this.length = length;
}

public double getArea() {


return width * length;
}

public double getPerimeter() {


return 2 * (width + length);
}

public String toString() {


return "Rectangle[" + super.toString() + ", width="
+ width + ", length=" + length + "]";
}
}

package gshapes;
public class Square extends Rectangle {

public Square() {}

public Square(double side) {


super(side, side);
}

public Square(double side, String color, boolean


filled) {
super(side, side, color, filled);
}

public double getSide() {


return width;
}

public void setSide(double side) {


this.width = side;
this.length = side;
}

public void setWidth(double side) {


setSide(side);
}

public void setLength(double side) {


setSide(side);
}

public String toString() {


return "Square[" + super.toString() + "]";
}
}

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