CSL 210 Lab06 Inheritance
CSL 210 Lab06 Inheritance
CSL 210 Lab06 Inheritance
BS(CS) 2
Semester 02(Fall 2019)
EHTISHAM AKRAM 022
Lab06: Inheritance
Exercises
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: 2r
Surface area of a cylinder: 2rh + 2r2
Volume of a cylinder: r2h
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; } }
Output:
CS Department, BUKC 4/7 Semester Fall 2019
CSL-210: Object-Oriented Programming Lab Lab06: Inheritance
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"; } }
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: