Based on the particle example redo the areashapes assignment to implement the polymorphism concept (via method...

50.1K

Verified Solution

Question

Programming

Based on the particle example redo the areashapes assignment toimplement the polymorphism concept (via method overloading andoverriding) whereby the user selects a shape, and the programcalculates, and displays the shape's area using \"displayArea()\".

/*
* To change this license header, choose License Headers in ProjectProperties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package areashapes;

/**
*
* @author Maore
*/


public class AreaShapes{
public static void main(String[] args) {
Circle c=new Circle();
Triangle t= new Triangle();
Trapezium tz= new Trapezium();
Oval o= new Oval();


System.out.println(\"Area of the Circle:\"+c.area());//singleinheritance
System.out.println(\"Area of the Triangle is:\" + t.area());
System.out.println(\"Area of the Trapezium is:\" + tz.area());
System.out.println(\"Area of the Oval is:\" + o.area());
MethodsRectangle mr=new MethodsRectangle();//methodoverloading
  

//the data type is integer therefore displayArea method will beinvoked.
System.out.println(\"Area of the Rectangle:\"+mr.displayArea(3,4));
//double data type therefore double type method will beinvoked
System.out.println(\"Area of the Rectangle:\"+mr.displayArea(3.4,4.3));
  
  
//method overriding
Dimensions d=new MethodsRectangle();
d.displayArea();//this will get the method displayArea from theMethodsRectangle class not from the Dimensions class because it isoverridden by the
//child class
  
  
//final keyword
final double unchangeble=1;
  
System.out.print(unchangeble);
  
}
}
class Dimensions{ //parent or base class
double radius=7;
double pi= 8;//circle
double length,width;//rectangle
double base,height;//triangle
double a=14,b=16,h=19;//trapezium;
double a1,b1;//oval;
  
//method override
public void displayArea() {
System.out.print(\"Display Area\");
}
  
}

//single inheritance
class Circle extends Dimensions{
  
public double area() {
return (super.pi*super.radius*super.radius);
// super keyword to get the variables of the previous class
}
}

//hierarchical inheritance i.e.,all every class in the programinheriting the parent class
// Circle class included in this hierarchical inheritance)

class Triangle extends Dimensions{
double base=7;
double height= 8;
  
  
public double area() {
return(this.base*this.height);//here we've used this keyword foraccess the variable of current class


}
}

class Trapezium extends Dimensions{
  
public double area() {
return((super.a+super.height)/2)*super.h;//super keyword to accessthe variable of current class
}
}
  
  
  
class Oval extends Dimensions{
double a1= 16;
double b1=20;
double pi= 3.14;
public double area() {
return((this.a1*this.b1*this.pi)/2);//this keyword to access thevariable of current class
}
}
//and finally we will look into the method overloading and methodoverriding in this class

class MethodsRectangle extends Dimensions{
public int displayArea(int x,int y) {// this method gives area forinteger dimensions
return(x*y);
}
public double displayArea(double x,double y) {
return(x*y);//this method will give area for double typedimensions
}
public void displayArea() {
System.out.print(\"\nparent class method overriden by this classmethod !!! \");//this method will override the method in Dimensionsclass
}
  
}


  

  

Answer & Explanation Solved by verified expert
3.9 Ratings (743 Votes)
Note All comments starting with are theones made by me Let me know in the comments if you think something else needs to be done import javautilScanner To change this license header choose License Headers in Project Properties To change this template file choose Tools Templates and open the template in the editor package areashapes didnt know its intent so have commented you can uncomment it if you want author Maore public class Main public static void mainString args Scanner myObj new ScannerSystemin I am using the switch menu to enable user selection SystemoutprintEnter the option number to select the shape 1circle 2triangle 3trapezium 4oval 5rectangle int imyObjnextInt switchi case 1 Circle cnew Circle    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students