Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

need help with this java program use the code below to solve this problem The Shape class is abstract and contains one method: getArea. Your

need help with this java program

use the code below to solve this problem

The Shape class is abstract and contains one method: getArea. Your task is to create

two subclasses of Shape: Circle and Square. Then, you should write a driver program

with a main method that creates an array capable of holding four Shape objects.

Populate your array with two Circle objects and two Square objects. Finally, call the

getArea method on each Shape in your array and display the results

.

You should not make any changes to Shape.java.

Hints: In case you have repressed your memories of geometry class, the area of

a circle is pi times the radius squared, where pi is approximately 3.14159. Java has the value of pi stored more accurately as the constant Math.PI. The area of a square is

the length of its side, squared. You will likely need to declare fields in your Circle and Square classes to hold the radius and side length, respectively.

You will also need constructors for each class, to initialize the fields of the class.

Example Assume your array of Shape object contains the following:

A circle with a radius of 1

A circle with a radius of 5.27

A square with a side of length 14

A square with a side of length 10

Then the output of your driver program should be:

3.141592653589793

87.25113860888395

196.0

100.0

here is the code so far:

******lab.pkg1.practice.starting.code******(driver class)

package lab.pkg1.practice.starting.code;

public class Lab1PracticeStartingCode {

public static void main(String[] args) { // TODO write code here to create an array of four Shape objects, // call the getArea method of each one, and display the results. }

}

*****shape.java*******

package lab.pkg1.practice.starting.code;

public abstract class Shape {

public abstract double getArea(); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Graph Databases In Action

Authors: Dave Bechberger, Josh Perryman

1st Edition

1617296376, 978-1617296376

More Books

Students also viewed these Databases questions

Question

3. How has Starbucks changed since its early days?

Answered: 1 week ago