Computer Science, asked by mathytch, 2 months ago

Create a package myPackage that contains the following classes. The description of
each class is given along with it.
Class Name: Square

Method:
(i)areaSquare() to calculate area of square accepting the required values from calling
module and prints the value.
(ii) perimeterSquare() to calculate perimeter of the square, accepting required values
from calling module and prints the value.

Class Name: Rectangle
Method:
(i)areaRectangle() to calculate area of rectangle accepting the required values from
calling module and prints the value.
(ii) perimeterRectangle() to calculate perimeter of the rectangle accepting required
values from calling module and prints the value.

Define another java class myClass that can access the methods of the myPackage and
call the area and perimeter of Square and Rectangle.
Use different access specifiers for the function declaration and check the scope of
accessing the methods from one class to another class.

Answers

Answered by s751
1

Answer:class Rectangle {

   int length;  

   int breadth;  

   int area;  

   int perimeter;

   void input() {

       Scanner in = new Scanner(System.in);

       System.out.print("Enter length of rectangle: ");

       length = in.nextInt();

       System.out.print("Enter breadth of rectangle: ");

       breadth = in.nextInt();

   }

   void calculate() {

       area = length * breadth;

       perimeter = 2 * (length + breadth);

   }

   void display() {

       System.out.println("Area of Rectangle = " + area);

       System.out.println("Perimeter of Rectangle = " + perimeter);

   }

   public static void main(String args[]) {

       Rectangle obj = new Rectangle();

       obj.input();

       obj.calculate();

       obj.display();

Explanation:

Similar questions