Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Using object-oriented programming approach, write a Java program that compute

ID: 3741579 • Letter: 1

Question

1.      Using object-oriented programming approach, write a Java program that computes and outputs the area of the shaded region in the diagram.

-        Define a class Circle (Circle.java)

o   One private data member: radius;

o   Two overloaded constructors;

o   Get and set methods for the private data member;

o   Method computeArea which computes and returns the area;

o   Method computeCircumference which computes and returns the circumference;

o   toString() method.

-        Define a class Trapezoid (Trapezoid.java)

o   Three private data members: base1, base2, and height;

o   Two overloaded constructors;

o   Get and set methods for each private data member;

o   Method computeArea which computes and returns the area;

o   toString() method.

-        (ShadedRegion.java) Write the client class that inputs a radius, a base1, a base2, and a height, then creates a Circle object and a Trapezoid object based on the user input, then uses these two objects to compute and output the shaded area (you can assume that the circle is smaller enough to fit inside the trapezoid).

Explanation / Answer

//ShadedRegion.java
import java.util.Scanner;
public class ShadedRegion {
  
   public static void main(String[] args) {
       double radius=0;
       double base1 = 0;
       double base2=0;
       double height=0;
      
       //prompt for radius
       Scanner scan=new Scanner(System.in);
       System.out.println("Enter radius,r of Circle : ");
       radius=Double.parseDouble(scan.nextLine());
       //prompt for base1
       System.out.println("Enter base1,of Trapezoid : ");
       base1=Double.parseDouble(scan.nextLine());
      
       //prompt for base2
       System.out.println("Enter base2,of Trapezoid : ");
       base2=Double.parseDouble(scan.nextLine());
       //prompt for height
       System.out.println("Enter height , of Trapezoid : ");
       height=Double.parseDouble(scan.nextLine());
      
      
       //create circle object
       Circle circle=new Circle(radius);
       //create Trapezoid object
       Trapezoid trap=new Trapezoid(base1, base2, height);
      
       //find shaded area
       double shadedarea=trap.computeArea()-circle.computeArea();

       System.out.println(circle);
       System.out.println(trap);
       System.out.printf("Shaded Area :%10.2f ",shadedarea);
          
      
   }

}

---------------------------------------

//Circle.java
public class Circle {
   //declare radius variable
   private double radius;
   //Circle constructor
   public Circle() {
       radius=0;
   }
   //Circle parameterized constructor
   public Circle(double r) {
       this.radius=r;
   }
  
   public void setradius(double radius)
   {
       this.radius=radius;
   }
  
   public double getradius()
   {
       return radius;
   }
   /*computeArea method*/
   public double computeArea()
   {
       return Math.PI*radius*radius;
   }
   /*computeCircumference method*/
   public double computeCircumference()
   {
       return 2*Math.PI*radius;
   }
   /*toString method*/     
   public String toString() {
       return String.format("Circle , radius %.2f ", radius);
   }
}

---------------------------------------

//Trapezoid.java
public class Trapezoid {
   //instance variabls of class
   private double base1;
   private double base2;
   private double height;
  
   public Trapezoid() {
       base1=0;
       base2=0;
       height=0;
   }
  
   public Trapezoid(double base1, double base2,
           double height) {
       this.base1=base1;
       this.base2=base2;
       this.height=height;         
   }
   public void setbase1(double base1){
       this.base1=base1;
   }
   public void setbase2(double base2){
       this.base2=base2;
   }
   public void setheight(double height){
       this.height=height;
   }
  
   public double getbase1(){
       return base1;
   }
   public double getbase2(){
       return base2;
   }
   public double getheight(){
       return height;
   }
  
   public double computeArea()
   {
       return 0.5*(base1+base2)*height;
   }
  
   /*Override toString method*/
   public String toString() {
       return String.format("Trapezoid , base1 %.2f, base2 %.2f and height %.2f ",
               base1,base2, height);
   }
}

---------------------------------------

Sample Output:

Enter radius,r of Circle :
4
Enter base1,of Trapezoid :
6
Enter base2,of Trapezoid :
8
Enter height , of Trapezoid :
10
Circle , radius 4.00

Trapezoid , base1 6.00, base2 8.00 and height 10.00

Shaded Area : 19.73