Question

In: Computer Science

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

Based on the particle example redo the areashapes assignment to implement the polymorphism concept (via method overloading and overriding) whereby the user selects a shape, and the program calculates, and displays the shape's area using "displayArea()" .

/*
* 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;

/**
*
* @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());//single inheritance
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();//method overloading
  

//the data type is integer therefore displayArea method will be invoked.
System.out.println("Area of the Rectangle:"+mr.displayArea(3, 4));
//double data type therefore double type method will be invoked
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 the MethodsRectangle class not from the Dimensions class because it is overridden 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 program inheriting 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 for access the variable of current class


}
}

class Trapezium extends Dimensions{
  
public double area() {
return((super.a+super.height)/2)*super.h;//super keyword to access the 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 the variable of current class
}
}
//and finally we will look into the method overloading and method overriding in this class

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


  

  

Solutions

Expert Solution

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 java.util.Scanner;
/*
* 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 main(String[] args) {
        Scanner myObj = new Scanner(System.in);
        
        // ### I am using the switch menu to enable user selection
        System.out.print("Enter the option number to select the shape (1-circle 2-triangle 3-trapezium 4-oval 5-rectangle): ");
        int i=myObj.nextInt();
        switch(i){
            case 1:
                Circle c=new Circle();
                System.out.println("Area of the Circle:"+c.displayArea());//single inheritance
                break;
            case 2:
                Triangle t= new Triangle();
                System.out.println("Area of the Triangle is:" + t.displayArea());
                break;
            case 3:
                Trapezium tz= new Trapezium();
                System.out.println("Area of the Trapezium is:" + tz.displayArea());
                break;
            case 4: 
                Oval o= new Oval();
                System.out.println("Area of the Oval is:" + o.displayArea());
                break;
            case 5:
                MethodsRectangle mr=new MethodsRectangle();//method overloading
                Dimensions d=new MethodsRectangle();
                //the data type is integer therefore displayArea method will be invoked.
                System.out.println("Area of the Rectangle:"+mr.displayArea(3, 4));
                //double data type therefore double type method will be invoked
                System.out.println("Area of the Rectangle:"+mr.displayArea(3.4, 4.3));
                d.displayArea();//this will get the method displayArea from the MethodsRectangle class not from the Dimensions class because it is overridden by the
                //child class
                break;
            default: //### entering anything else wil result in this output
                //method overriding
                Dimensions dim=new Dimensions();
                dim.displayArea();
        }
        
          
        // ### dont think the following are of any use so have commented
        //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 double displayArea() {
        System.out.print("No shape seleted to show the display area.");//### Text modified to suite the program
        return 0;
    }
}

//single inheritance
class Circle extends Dimensions{
  
    public double displayArea() {
        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 program inheriting the parent class
// Circle class included in this hierarchical inheritance)
class Triangle extends Dimensions{
    double base=7;
    double height= 8;
      
    public double displayArea() { //### Changed the name of the funciton to displayArea
        return(this.base*this.height);//here we've used this keyword for access the variable of current class
    }
}

class Trapezium extends Dimensions{
    public double displayArea() { //### Changed the name of the funciton to displayArea
        return((super.a+super.height)/2)*super.h;//super keyword to access the variable of current class
    }
}
  
class Oval extends Dimensions{
    double a1= 16;
    double b1=20;
    double pi= 3.14;
    public double displayArea() { //### Changed the name of the funciton to displayArea
        return((this.a1*this.b1*this.pi)/2);//this keyword to access the variable of current class
    }
}

//and finally we will look into the method overloading and method overriding in this class
class MethodsRectangle extends Dimensions{
    public int displayArea(int x,int y) {// this method gives area for integer dimensions
        return(x*y);
    }
    public double displayArea(double x,double y) {
        return(x*y);//this method will give area for double type dimensions
    }
    // ### Not sure if changes need to be made here too, since no value of x and y is given in the class
    public double displayArea() {
        System.out.print("\nparent class method overriden by this class method !!! ");//this method will override the method in Dimensions class
        return 0;
    }
}



Related Solutions

explain the concept of competency based education. what are the advantage of this method of learning...
explain the concept of competency based education. what are the advantage of this method of learning for student who have to learn a large number of skill?
What is the Administration procedure of Metered Dose inhalers via spacer, describing the correct evidence-based method
What is the Administration procedure of Metered Dose inhalers via spacer, describing the correct evidence-based method
Calculating process sigma for multiple steps: Opportunity-based method and Throughtput method .Explain them on the example.
Calculating process sigma for multiple steps: Opportunity-based method and Throughtput method .Explain them on the example.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT