SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Interfaces And Inner Classes
Exercise:
Programming Projects
Question:10 | ISBN:9780132830317 | Edition: 5

Question

Define an interface named Shape with a single method named area that calculates the area of the geometric shape:

public double area();

Next, define a class named Circle that implements Shape. The Circle class should have an instance variable for the radius, a constructor that sets the radius, accessor/mutator methods for the radius, and an implementation of the area method. Also define a class named Rectangle that implements Shape. The Rectangle class should have instance variables for the height and width, a constructor that sets the height and width, accessor and mutator methods for the height and width, and an implementation of the area method.

The following test code should then output the area of the Circle and Rectangle objects:

public static void main(String[] args)

{

Circle c = new Circle(4); // Radius of 4

Rectangle r = new Rectangle(4,3); // Height = 4, Width = 3

ShowArea(c);

ShowArea(r);

}

public static void ShowArea(Shape s)

{

double area = s.area();

System.out.println("The area of the shape is " + area);

}

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

//Shape interface
public interface Shape 
{
	//implements shape interface in class
	public double area();
}

//Circle class definition
public class Circle implements Shape
{

	//private member of class
	private double radius;
	
	//parameterized constructor 
	public Circle(double r)
	{
		radius = r;
	}
	//Returns the radius 
	public double getRadius()
	{
		return radius;
	}
	//Assign the new radius 
	public void setRadius(double newR)
	{
		radius = newR;
	}

	//Returns the area of the circle
	public double area()
	{
		return radius * radius * Math.PI;
	}
}

//The rectangle class implements the Shape interface 
public class Rectangle implements Shape
{

	//declare the private members 
	private double length;
	private double width;
	
	//constructor to set length and width 
	public Rectangle(double l, double w)
	{
		length = l;
		width = w;
	}

	//Returns the length 
	public double getLength()
	{
		return length;
	}

	//Returns the width 
	public double getWidth()
	{
		return width;
	}

	//Assign the new value for length
	public void setLength(double newL)
	{
		length = newL;
	}
	//Assign the new value for width
	public void setWidth(double newW)
	{
		width = newW;
	}

	//returns the area of rectangle
	public double area()
	{
		return length * width;
	}
}

import java.io.*;
import java.util.*;

public class Drivertest
{
	public static void main(String args[])
	{
		
		//create an object for circle class 
		Circle c = new Circle(4);
//create an object for rectangle class 
		Rectangle r = new Rectangle(4, 3);
		//call the method ShowArea using Circle object
		ShowArea(c);
		//call the method ShowArea using rectangle object
		ShowArea(r);
	}
	public static void ShowArea(Shape s)
	{
		double area = s.area();
		System.out.println("The area of the shape is " + area);
	}
}

 

0 0

Discussions

Post the discussion to improve the above solution.