Slip22

 Q1) Write a program to create an abstract class named Shape that contains two integers and an empty method named printArea(). Provide three classes named Rectangle, Triangle and Circle such that each one of the classes extends the class Shape. Each one of the classes contain only the method printArea() that prints the area of the given shape. (use method overriding). [10 marks] 

import java.util.*;

abstract class shape

{

   int x,y;

   abstract void area(double x,double y);

}

class Rectangle extends shape

{

void area(double x,double y)

{

    System.out.println("Area of rectangle is :"+(x*y));

}

}

class Circle extends shape

{

void area(double x,double y)

{

   System.out.println("Area of circle is :"+(3.14*x*x));

}

}

class Triangle extends shape

{

void area(double x,double y)

{

   System.out.println("Area of triangle is :"+(0.5*x*y));

}

}

public class Slip22_1

{

  public static void main(String[] args)

  {

Rectangle r=new Rectangle();

        r.area(2,5);

Circle c=new Circle();

c.area(5,5);

Triangle t=new Triangle();

t.area(2,5);

  }

}

Q2) Write a program that handles all mouse events and shows the event name at the center of the Window, red in color when a mouse event is fired. (Use adapter classes).

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;


public class Slip22_2 extends JFrame {

    String msg= "";

    

    public Slip22_2() {

        setTitle("Mouse Event Demo");

        setSize(400, 400);

setVisible(true);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        addMouseListener(new MyMouseAdapter());

        addMouseMotionListener(new MyMouseAdapter());

    }

    public void paint(Graphics g) {

        super.paint(g);

        g.setColor(Color.RED);

        g.setFont(new Font("Arial", Font.BOLD, 20));

        g.drawString(msg, getWidth() / 2 - 50, getHeight() / 2);

    }


    class MyMouseAdapter extends MouseAdapter {

        public void mouseClicked(MouseEvent e) {

            msg= "Mouse Clicked";

            repaint();

        }

        public void mouseEntered(MouseEvent e) {

            msg= "Mouse Entered";

            repaint();

        }

        public void mouseExited(MouseEvent e) {

            msg= "Mouse Exited";

            repaint();

        }

        public void mousePressed(MouseEvent e) {

            msg= "Mouse Pressed";

            repaint();

        }

        public void mouseReleased(MouseEvent e) {

            msg= "Mouse Released";

            repaint();

        }

        public void mouseDragged(MouseEvent e) {

            msg= "Mouse Dragged";

            repaint();

        }

        public void mouseMoved(MouseEvent e) {

            msg= "Mouse Moved";

            repaint();

        }

    }

 public static void main(String[] args) {

            new Slip22_2();   

    }

}


Comments

Popular posts from this blog

Slip1

Slip3

Slip2