Write a Java Program with class Rectangle having instance variable width, length, area and colour. Create methods like setData() and area().Create two object of Rectangle and compare their area and colour. If area and colour both are the same for the objects then display “Matching Rectangles”, otherwise display “Non matching Rectangle”.

 

AIM:  Write a Java Program with class Rectangle having instance variable width, length, area and colour. Create methods like setData() and area().Create two objects of Rectangle and compare their area and colour. If area and colour both are the same for the objects then display “Matching Rectangles”, otherwise display “Non matching Rectangle”.

 

CODE:

import java.util.Scanner;

class Rectangle {

int width,length,area;

String colour;

            void setData(int w,int l,String c) {

             width =w;          

             length=l;

             colour=c;

            }

            void area() {

            area = width * length;

            }

}

public class p11{

 public static void main(String args[]){

            int width,length;

            String colour;

            Scanner in = new Scanner(System.in);

            System.out.print("\nEnter Width of rectangle: ");

            width = in.nextInt();

            System.out.print("\nEnter Length of rectangle: ");

            length = in.nextInt();

            System.out.print("\nEnter Colour of rectangle: ");

            Scanner read = new Scanner(System.in);

            colour = read.nextLine();

 

            System.out.println("\n-----Enter values for Second Rectangle----- ");

 

            Rectangle r1 = new Rectangle();

            r1.setData(width,length,colour);

            r1.area();

            System.out.print("\nEnter Width of rectangles: ");

            width = in.nextInt();

            System.out.print("\nEnter Length of rectangles: ");

            length = in.nextInt();

            System.out.print("\nEnter Colour of rectangles: ");

            colour = read.nextLine();

            Rectangle r2 = new Rectangle();

            r2.setData(width,length,colour);

            r2.area();

 

            if(r1.area==r2.area && r1.colour.equals(r2.colour)){

                        System.out.println("\nMatching Rectangles");

                        }

            else{

            System.out.println("\nNon Matching Rectangles");

            }

            }

 }

OUTPUT:

Matching Rectangles:


Non-Matching Rectangles:



Comments

Popular posts from this blog

Write an application that illustrates method overriding in the same package and different packages. Also demonstrate accessibility rules in inside and outside packages

Describe abstract class called Shape which has three subclasses say Triangle, Rectangle, and Circle. Define one method area()in the abstract class and override this area() in these three subclasses to calculate for specific object.

Write a program that illustrates interface inheritance. Interface P12 inherits from both P1 and P2. Each interface declares one constant and one method. The class Q implements P12. Instantiate Q and invoke each of its methods. Each method displays one of the constants.