Вы находитесь на странице: 1из 7

CSL-210: Object-Oriented Programming Lab

BS(CS) 2
Semester 02(Fall 2019)
EHTISHAM AKRAM 022

Lab06: Inheritance

Exercises

Exercise 1 (Circle.java, Cylinder.java Application.java)

Write the classes below containing the given instance variables and methods, following the inherited
hierarchy:
Circle
- radius:double
+ Circle()
+ Circle(radius: double)
+ setRadius(radius: double):void
+ getRadius():double
+ calcArea():double
+ calcCircumference():double
+ toString():String
+ equals(Circle c):boolean

Cylinder
- height:double
+ Cylinder()
+ Cylinder(radius: double, height: double)
+ setHeight(height: double):void
+ getHeight():double
+ calcArea():double
+ calcVolume():double
+ toString():String
+ equals(Cylinder c):boolean

Formulas:
The following formulas may be helpful:

Area of a circle: r2

Circumference of a circle: 2r

Surface area of a cylinder: 2rh + 2r2

Volume of a cylinder: r2h

Use the following application class to test your code


CS Department, BUKC 2/7 Semester Fall 2019
CSL-210: Object-Oriented Programming Lab Lab06: Inheritance
public class Application {
public static void main(String []args){
Circle circle1 = new Circle(3);
Circle circle2 = new Circle(5);
System.out.println(circle1);
System.out.println(circle2);
if(circle1.equals(circle2))
System.out.println("Both circles are equal");
else
System.out.println("Both circles are different");

Cylinder cylinder1 = new Cylinder(3,4);


Cylinder cylinder2 = new Cylinder(4,6);
Cylinder cylinder3 = new Cylinder(3,4);
System.out.println(cylinder1);
System.out.println(cylinder2);
if(cylinder1.equals(cylinder3))
System.out.println("Both cylinders are equal");
else
System.out.println("Both cylinders are different");
}
}

Solution:
Code:
public class Cylinder extends Circle{
private double height;
public Cylinder() { }
public Cylinder(double radius,double height) {
setRadius(radius);
this.height=height; }
public void setHeight(double height) {
this.height=height; }
public double getHeight() {
return height; }
@Override
public double calcArea() {
return (2*pi*getRadius()*height)+(2*pi*getRadius()); }
public double calcVolume() {
return (pi*(getRadius()*getRadius())*height); }
@Override
public String toString() {
return "Surface Area of Cylinder:\n: "+calcArea()+"\n: "+calcVolume(); } }
CS Department, BUKC 3/7 Semester Fall 2019
CSL-210: Object-Oriented Programming Lab Lab06: Inheritance
public class Circle {
private double radius;
final double pi=3.14;
public Circle() { }
public Circle(double radius) {
this.radius=radius; }
public void setRadius(double radius) {
this.radius=radius; }
public double getRadius() {
return radius; }
public double calcArea() {
return (pi*(radius*radius)); }
public double calcCircumference() {
return (2*pi*radius); }
public String toString() {
return "Area of Circle:\n: "+calcArea()+"\nCircumference of Circle: "+calcCircumference(); }
public Boolean equals(Cylinder C) {
return true; } }

public class Application {


public static void main(String []args){
Circle circle1 = new Circle(3);
Circle circle2 = new Circle(5);
System.out.println(circle1);
System.out.println(circle2);
if(circle1.equals(circle2))
System.out.println("Both circles are equal");
else
System.out.println("Both circles are different");
Cylinder cylinder1 = new Cylinder(3,4);
Cylinder cylinder2 = new Cylinder(4,6);
Cylinder cylinder3 = new Cylinder(3,4);
System.out.println(cylinder1);
System.out.println(cylinder2);
if(cylinder1.equals(cylinder3))
System.out.println("Both cylinders are equal");
else
System.out.println("Both cylinders are different"); }}

Output:
CS Department, BUKC 4/7 Semester Fall 2019
CSL-210: Object-Oriented Programming Lab Lab06: Inheritance

Exercise 2 (PurchaseItem.java,WeightedItem.java, CountedItem.java, )

Consider a superclass PurchaseItem which models customer’s purchases. This class has:
 Two private instance variables name (String) and unitPrice (double).
 One constructor to initialize the instance variables.
 A default constructor to initialize name to “no item”, and unitPrice to 0. use this()
 A method getPrice that returns the unitPrice.
 Accessor and mutator methods.
 A toString method to return the name of the item followed by @ symbol, then the unitPrice.

Consider two subclasses WeighedItem and CountedItem. WeighedItem has an additional instance
variable weight (double) in Kg while CountedItem has an additional variable quantity (int) both
private.
 Write an appropriate constructor for each of the classes making use of the constructor of
the superclass in defining those of the subclasses.
 Override getPrice method that returns the price of the purchasedItem based on its unit
price and weight (WeighedItem), or quantity (CountedItem). Make use of getPrice of the
superclass
 Override also toString method for each class making use of the toString method of the
superclass in defining those of the subclasses.
toString should return something that can be printed on the receipt.
For example
Banana @ 3.00 1.37 Kg 4.11 PKR (in case of WeighedItem class)
Pens @ 4.5 10 units 45 PKR (in case of CountedItem class)
Class Diagram:
CS Department, BUKC 5/7 Semester Fall 2019
CSL-210: Object-Oriented Programming Lab Lab06: Inheritance

PurchaseItem CountedItem
- name:String - quantity:int
- unitPrice:double
+ CountedItem ()
+ PurchaseItem () + CountedItem (quantity:int)
+PurchaseItem(name:String,unitPrice:double) + getPrice(): double
+ getPrice(): double + getQuantity():double
+ getData():double +setQuantity(quantityt:int):void
+setData(name:String,unitPrice:double):void + toString():String
+ toString():String

WeightedItem
- weight:double
+WeightedItem()
+WeightedItem(weight:double)
+ getPrice(): double
+ getWeight():double
+setWeight(weight:double):void
+ toString():String

Code:
public class PurchaseItem {
private String name;
private double unitPrice;
PurchaseItem (String name,double unitPrice) {
this.name=name;
this.unitPrice=unitPrice; }
PurchaseItem () {
name="no item";
unitPrice=1; }
public double getPrice() {
return unitPrice; }
public String getName() {
return name; }
public void setName(String name) {
this.name=name; }
public void setprice(double unitPrice) {
this.unitPrice=unitPrice; }
@Override
public String toString() {
return "name of product :\n: "+name+ "@\n: "+unitPrice; }
public class WeighedItem extends PurchaseItem {
CS Department, BUKC 6/7 Semester Fall 2019
CSL-210: Object-Oriented Programming Lab Lab06: Inheritance
private double weight;
WeighedItem(double weight,double unitPrice,String name) {
super.getName();
super.getPrice();
this.weight=weight; }
WeighedItem() { }
public void setWeight(double weight) {
this.weight=weight; }
public double getWeight() {
return weight; }
@Override
public double getPrice() {
return (super.getPrice()*weight); }
public String toString() {
return " "+getName()+"@"+getWeight()+ " "+super.getPrice()+"KG "+getPrice()+"PKR"; } }

public class CountItem extends PurchaseItem{


private double quantity;
CountItem(String name,double quantity,double unitPrice) {
super.getPrice();
super.getPrice();
this.quantity=quantity; }
CountItem() { }
public void setQuantity(double quantity) {
this.quantity=quantity; }
public double getQuantity() {
return quantity; }
@Override
public double getPrice() {
return (super.getPrice()*quantity); }
@Override
public String toString() {
return " "+getName()+"@"+getQuantity()+ " "+super.getPrice()+"Unit "+getPrice()+"PKR"; } }

import java.util.Scanner;
public class testClass {
public static void main(String arg[]) {
CountItem s1=new CountItem();
WeighedItem s2=new WeighedItem();
s2.setWeight(3);
s2.setName("Banana");
s2.setprice(1.37);
s1.setName("Pen");
s1.setprice(10);
s1.setQuantity(4.5);
System.out.println(s1);
System.out.println(s2);
}}
CS Department, BUKC 7/7 Semester Fall 2019
CSL-210: Object-Oriented Programming Lab Lab06: Inheritance
Output:

Вам также может понравиться