Wednesday, 28 August 2013

Create an Applet that displays the a House. If the mouse is rolled over roof, the message “Mouse is over roof” should be displayed on the status bar. If the mouse is rolled over door, the message “Mouse is over door” should be displayed on the status bar. Like wise for windows and walls

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

/**
 *
 * @author Administrator
 */
public class House extends Applet {

    Rectangle r[] = new Rectangle[4];
    Polygon p = null;
    int a[] = {500, 700, 600};
    int b[] = {200, 200, 100};
    int x = 0, y = 0;
    String msg = null;

    /**
     * Initialization method that will be called after the applet is loaded
     * into the browser.
     */
    public void init() {

        r[0] = new Rectangle(500, 200, 200, 200);
        r[1] = new Rectangle(550, 300, 100, 100);
        r[2] = new Rectangle(525, 225, 50, 50);
        r[3] = new Rectangle(625, 225, 50, 50);
        p = new Polygon(a, b, 3);
        addMouseMotionListener(new A());
    }

    public class A extends MouseMotionAdapter {

        @Override
        public void mouseMoved(MouseEvent m) {

            x = m.getX();
            y = m.getY();
            int z = getShape(x, y);
            if (z == 1) {
                msg = "Mouse is over door";
            } else if (z == 2 || z == 3) {
                msg = "Mouse is over Window ";
            } else if (z == 4) {
                msg = "Mouse is over roof";
            } else {
                msg = "Mouse is over walls";
            }

            repaint();
        }

        public int getShape(int x, int y) {

            if (r[1].contains(x, y)) {
                return 1;
            } else if (r[2].contains(x, y)) {
                return 2;
            } else if (r[3].contains(x, y)) {
                return 3;
            } else if (p.contains(x, y)) {
                return 4;
            } else {
                return -1;
            }
        }
    }

    public void paint(Graphics g) {
        g.setColor(Color.PINK);
        g.fillRect(500, 200, 200, 200);
        g.setColor(Color.red);
        g.fillRect(550, 300, 100, 100);
        g.setColor(Color.GRAY);
        g.fillRect(525, 225, 50, 50);
        g.fillRect(625, 225, 50, 50);
        g.setColor(Color.RED);
        g.fillPolygon(a, b, 3);
        showStatus(msg);
    }
    // TODO overwrite start(), stop() and destroy() methods
}

Pass the following parameters to Applet: a. Shapes (Rectangle or Oval) b. Style (Hollow or Solid) c. BgColor (R,G,B value) d. FrColor (R,G,B value) e. X f. Y g. Width h. Height

import java.awt.*;
import java.applet.*;

public class PeraComputer extends Applet {

    int xs[] = {100, 220, 160};
    int ys[] = {270, 270, 220};
    String CPUBgColor = new String();
    String CPUFrColor = new String();
    String MouseBgColor = new String();
    String MouseFrColor = new String();
    String KeyBgColor = new String();
    String KeyFrColor = new String();
    String ScreenImage = new String();
    String ScreenFrColor=new String();
    Image img=null;
    int x1, x2, x3, x4, x5, x6, x7, y1, y2, y3, y4, y5, y6, y7;
  
    public void init() {
        CPUBgColor = getParameter("CPUBgColor");
        CPUFrColor = getParameter("CPUFrColor");
        MouseBgColor = getParameter("MouseBgColor");
        MouseFrColor = getParameter("MouseFrColor");
        KeyBgColor = getParameter("KeyBgColor");
        KeyFrColor = getParameter("KeyFrColor");
        ScreenImage = getParameter("ScreenImage");
        ScreenFrColor=getParameter("ScreenFrColor");
        x1 = CPUBgColor.indexOf(",");
        y1 = CPUBgColor.lastIndexOf(",");
        x2 = CPUFrColor.indexOf(",");
        y2 = CPUFrColor.lastIndexOf(",");
        x3 = MouseBgColor.indexOf(",");
        y3 = MouseBgColor.lastIndexOf(",");
        x4 = MouseFrColor.indexOf(",");
        y4 = MouseFrColor.lastIndexOf(",");
        x5 = KeyBgColor.indexOf(",");
        y5 = KeyBgColor.lastIndexOf(",");
        x6 = KeyFrColor.indexOf(",");
        y6 = KeyFrColor.lastIndexOf(",");
        x7 = ScreenFrColor.indexOf(",");
        y7= ScreenFrColor.lastIndexOf(",");

    }

    public void paint(Graphics g) {
       
        img=getImage(getCodeBase(),ScreenImage);
        g.drawImage(img, 100, 100, this);
        //Monitor
        g.setColor(new Color(Integer.parseInt(ScreenFrColor.substring(0, x7)), Integer.parseInt(ScreenFrColor.substring(x7 + 1, y7)), Integer.parseInt(ScreenFrColor.substring(y7 + 1))));
        g.drawRect(100, 100, 120, 120);
        g.fillPolygon(xs, ys, 3);
        //CPU
        g.setColor(new Color(Integer.parseInt(CPUBgColor.substring(0, x1)), Integer.parseInt(CPUBgColor.substring(x1 + 1, y1)), Integer.parseInt(CPUBgColor.substring(y1 + 1))));
        g.fillRect(270, 100, 70, 170);
        g.setColor(new Color(Integer.parseInt(CPUFrColor.substring(0, x2)), Integer.parseInt(CPUFrColor.substring(x2 + 1, y2)), Integer.parseInt(CPUFrColor.substring(y2 + 1))));
        g.drawRect(270, 100, 70, 170);
        g.fillRect(305,110,30,10);
        g.fillOval(325,125,8,8);
        g.fillOval(287,142,35,35);
        g.fillOval(290,187,30,30);
        g.fillOval(293,227,25,25);
        //KeyBoard
        g.setColor(new Color(Integer.parseInt(KeyBgColor.substring(0, x5)), Integer.parseInt(KeyBgColor.substring(x5 + 1, y5)), Integer.parseInt(KeyBgColor.substring(y5 + 1))));
        g.fillRect(100, 320, 120, 50);
         g.setColor(new Color(Integer.parseInt(KeyFrColor.substring(0, x6)), Integer.parseInt(KeyFrColor.substring(x6 + 1, y6)), Integer.parseInt(KeyFrColor.substring(y6 + 1))));
        g.drawRect(100, 320, 120, 50);
        g.fillOval(110,325,10,10);
        g.fillOval(125,325,10,10);
        g.fillOval(140,325,10,10);
        g.fillOval(155,325,10,10);
        g.fillOval(170,325,10,10);
        g.fillOval(185,325,10,10);
        g.fillOval(200,325,10,10);
        g.fillRect(105,340,10,10);
        g.fillRect(120,340,10,10);
        g.fillRect(135,340,10,10);
        g.fillRect(150,340,10,10);
        g.fillRect(165,340,10,10);
        g.fillRect(180,340,10,10);
        g.fillRect(195,340,10,10);
        g.fillRect(210,340,5,10);
        g.fillRect(105,355,10,10);
        g.fillRect(120,355,10,10);
        g.fillRect(135,355,10,10);
        g.fillRect(150,355,10,10);
        g.fillRect(165,355,10,10);
        g.fillRect(180,355,10,10);
        g.fillRect(195,355,10,10);
        g.fillRect(210,355,5,10);
        //Mouse
        g.setColor(new Color(Integer.parseInt(MouseBgColor.substring(0, x3)), Integer.parseInt(MouseBgColor.substring(x3 + 1, y3)), Integer.parseInt(MouseBgColor.substring(y3 + 1))));
        g.fillRect(245, 335, 30, 25);
        g.fillArc(245, 320, 30, 30, 0, 180);
        g.setColor(new Color(Integer.parseInt(MouseFrColor.substring(0, x4)), Integer.parseInt(MouseFrColor.substring(x4 + 1, y4)), Integer.parseInt(MouseFrColor.substring(y4 + 1))));
        g.drawArc(245, 320, 30, 30, 0, 180);
        g.drawRect(245, 335, 30, 25);
        g.drawRect(258,320,4,15);

    }
}

Applet that displays the following shapes a. Fido Dido b. House c. Mobile d. Computer

FIDO DIDO

import java.awt.*;
import java.applet.Applet;
public class Fido_Dido extends Applet {
 public void paint(Graphics g)
   {
     g.setColor(Color.GREEN);
     g.fillArc(160,150,150,240,180,180);
     g.setColor(Color.BLACK);
     g.fillOval(190,300,30,30);
     g.fillOval(250,300,30,30);
     g.drawLine(235,310,235,350);
     g.drawArc(160,229,10,40,-90,180);
     g.drawArc(180,229,10,40,-90,180);
     g.drawArc(200,229,10,40,-90,180);
     g.drawArc(220,229,10,40,-90,180);
     g.drawArc(240,229,10,40,-90,180);
     g.drawArc(260,229,10,40,-90,180);
     g.drawArc(280,229,10,40,-90,180);
     g.drawArc(300,229,10,40,-90,180);
     g.drawArc(161,188,10,40,90,180);
     g.drawArc(181,188,10,40,90,180);
     g.drawArc(201,188,10,40,90,180);
     g.drawArc(221,188,10,40,90,180);
     g.drawArc(241,188,10,40,90,180);
     g.drawArc(261,188,10,40,90,180);
     g.drawArc(281,188,10,40,90,180);
     g.drawArc(301,188,10,40,90,180);
     g.setColor(Color.RED);
     g.drawArc(210,340,50,30,0,-180);
   }
}


HOUSE

import java.awt.*;
import java.applet.Applet;

public class House extends Applet{
    int xs[]={550,700,625};
    int ys[]={190,190,90};
    public void paint(Graphics g)
    {

        g.setColor(Color.red);
        g.fillRect(550, 190, 150, 150);
        g.setColor(Color.green);
        g.fillPolygon(xs, ys, 3);
        g.drawLine(625, 100, 550, 190);
        g.drawLine(625, 100, 700, 190);
        g.drawLine(550,190,495,250);
        g.drawLine(700,190,750,250);
        g.setColor(Color.gray);
        g.fillRect(570,210,25,40);
        g.fillRect(600,265,50,70);
        g.setColor(Color.black);
        g.fillRect(623,265,4,70);

    }

}




















MOBILE

import java.awt.*;
import java.applet.Applet;
public class mobile extends Applet {
public void paint(Graphics g)
    {
       g.setColor(Color.BLACK);
       g.drawRoundRect(40,60,100,200,20,20);
       g.fillRoundRect(40,60,100,200,20,20);
       g.setColor(Color.GRAY);
       g.drawRoundRect(55,61,70,99,20,20);
       g.fillRoundRect(55,61,70,99,20,20);
       g.setColor(Color.darkGray);
       g.fillRect(55,162,20,28);
       g.fillRect(80,162,20,28);
       g.fillRect(105,162,20,28);
       g.fillRect(55,192,20,15);
       g.fillRect(80,192,20,15);
       g.fillRect(105,192,20,15);
       g.fillRect(55,210,20,15);
       g.fillRect(80,210,20,15);
       g.fillRect(105,210,20,15);
       g.fillRect(55,227,20,15);
       g.fillRect(80,227,20,15);
       g.fillRect(105,227,20,15);


      
    }
}











COMPUTER

 import java.awt.*;
import java.applet.*;

public class Computer extends Applet {

    int xs[] = {100, 220, 160};
    int ys[] = {270, 270, 220};
     public void paint(Graphics g) {
        //Monitor
        g.setColor(Color.red);
        g.drawRect(100, 100, 120, 120);
        g.fillPolygon(xs, ys, 3);
        //CPU
        g.setColor(Color.black);
        g.fillRect(270, 100, 70, 170);
        g.setColor(Color.red);
        g.drawRect(270, 100, 70, 170);
        g.fillRect(305,110,30,10);
        g.fillOval(325,125,8,8);
        g.fillOval(287,142,35,35);
        g.fillOval(290,187,30,30);
        g.fillOval(293,227,25,25);
        //KeyBoard
        g.setColor(Color.black);
        g.fillRect(100, 320, 120, 50);
        g.setColor(Color.red);
        g.drawRect(100, 320, 120, 50);
        g.fillOval(110,325,10,10);
        g.fillOval(125,325,10,10);
        g.fillOval(140,325,10,10);
        g.fillOval(155,325,10,10);
        g.fillOval(170,325,10,10);
        g.fillOval(185,325,10,10);
        g.fillOval(200,325,10,10);
        g.fillRect(105,340,10,10);
        g.fillRect(120,340,10,10);
        g.fillRect(135,340,10,10);
        g.fillRect(150,340,10,10);
        g.fillRect(165,340,10,10);
        g.fillRect(180,340,10,10);
        g.fillRect(195,340,10,10);
        g.fillRect(210,340,5,10);
        g.fillRect(105,355,10,10);
        g.fillRect(120,355,10,10);
        g.fillRect(135,355,10,10);
        g.fillRect(150,355,10,10);
        g.fillRect(165,355,10,10);
        g.fillRect(180,355,10,10);
        g.fillRect(195,355,10,10);
        g.fillRect(210,355,5,10);
        //Mouse
        g.setColor(Color.black);
        g.fillRect(245, 335, 30, 25);
        g.fillArc(245, 320, 30, 30, 0, 180);
        g.setColor(Color.red);
        g.drawArc(245, 320, 30, 30, 0, 180);
        g.drawRect(245, 335, 30, 25);
        g.drawRect(258,320,4,15);
        }
 }
 /*
 <applet code="Computer.java" width=500 height=500>
 </applet>
 */

Applet that displays your name 10 times using different colors and Font.

import java.awt.*;
import java.applet.Applet;
public class Name extends Applet {
    public void paint(Graphics g)
    {
        setBackground(Color.magenta);
        setForeground(Color.BLACK);
        g.setFont(new Font("Bodoni MT Black",Font.ITALIC,30));
        g.drawString("Hemant", 100, 100);

        g.setColor(Color.YELLOW);
        Font f1=new Font("Arial",Font.BOLD+Font.ITALIC,30);
        g.setFont(f1);
        g.drawString("Hemant", 100,150);

        g.setColor(Color.BLUE);
        Font f2=new Font("Times new roman",Font.BOLD,30);
        g.setFont(f2);
        g.drawString("Hemant", 100,200);

        g.setColor(Color.GREEN);
        Font f3=new Font("Helvetica",Font.BOLD+Font.ITALIC,30);
        g.setFont(f3);
        g.drawString("Hemant", 100,250);

        g.setColor(Color.RED);
        Font f4=new Font("Ms scan",Font.BOLD+Font.ITALIC,30);
        g.setFont(f4);
        g.drawString("Hemant", 100,300);

        g.setColor(Color.WHITE);
        Font f5=new Font("Algerian",Font.BOLD+Font.ITALIC,40);
        g.setFont(f5);
        g.drawString("Hemant", 100,350);

        g.setColor(Color.cyan);
        Font f6=new Font("comic",Font.BOLD+Font.ITALIC,30);
        g.setFont(f6);
        g.drawString("Hemant", 100,400);

        g.setColor(Color.gray);
        Font f7=new Font("caliographic",Font.BOLD+Font.ITALIC,30);
        g.setFont(f7);
        g.drawString("Hemant", 100,450);

        g.setColor(Color.ORANGE);
        Font f8=new Font("Viner Hand ITC",Font.BOLD+Font.ITALIC,30);
        g.setFont(f8);
        g.drawString("Hemant", 100,500);

        g.setColor(Color.PINK);
        Font f9=new Font("Papyrus",Font.BOLD+Font.ITALIC,30);
        g.setFont(f9);
        g.setColor(Color.yellow);
        g.drawString("Hemant", 100,50);
       
    }
}

Tuesday, 27 August 2013

Create a matrix that contains sales of 4 products for 4 regions and perform the following operations a. Total sales of products for region-wise b. Total sales of products product-wise c. Highest and lowest selling products d. Total sales made by the company

import java.util.Scanner;

class Product {

    public static void main(String[] args) {
        int sum = 0, tsum = 0, i, j;
        Scanner in = new Scanner(System.in);
        int arr[][] = new int[4][4];
        System.out.println("Enter product");
        for (i = 0; i <= 3; i++) {
            for (j = 0; j <= 3; j++) {
                arr[i][j] = in.nextInt();
            }
        }
        int h = arr[0][0], l = arr[0][0];
        System.out.println("Matrix form of entered product is:");
        for (i = 0; i <= 3; i++) {
            for (j = 0; j <= 3; j++) {
                System.out.print("\t" + arr[i][j]);
            }
            System.out.println();
        }
        for (i = 0; i <= 3; i++) {
            System.out.println("Total sales of product for region " + i + "is:");
            for (j = 0; j <= 3; j++) {
                sum = sum + arr[j][i];
                tsum = tsum + arr[i][j];
            }
            System.out.println(sum);
            sum = 0;
        }
        for (i = 0; i <= 3; i++) {
            System.out.println("Total sales of product for product" + i + "is");
            for (j = 0; j <= 3; j++) {
                sum = sum + arr[i][j];
            }
            System.out.println(sum);
            sum = 0;
        }
        for (i = 0; i <= 3; i++) {
            for (j = 0; j <= 3; j++) {
                if (h < arr[i][j]) {
                    h = arr[i][j];
                }
            }
        }
        for (i = 0; i <= 3; i++) {
            for (j = 0; j <= 3; j++) {
                if (l > arr[i][j]) {
                    l = arr[i][j];
                }
            }
        }
        System.out.println("Highhest selling product is: " + h);
        System.out.println("Lowest selling product is: " + l);
        System.out.println("Total sales made by company is: " + tsum);
    }
}

Program that accepts a number as command line argument and display its square. If argument contains more than 1 number proper error message should be displayed.

public class Square {

    public static void main(String[] args) {
        if (args.length != 1)
        {
            System.out.println("Invalid argument");
            System.exit(0);

        }
        int num=Integer.parseInt(args[0]);
        System.out.println("Square of number is: "+num*num);



    }
}

Program that accepts a 2D array of 4 by 5 by numbers and display the sum of all the rows, columns and the entire matrix ?

import java.util.Scanner;

public class TwoD {

    public static void main(String[] args) {
        float sum = 0, isum = 0;
        int a[][] = new int[4][5];
        Scanner in = new Scanner(System.in);
        for (int i = 0; i <= 3; i++) {
            for (int j = 0; j <= 4; j++) {
                System.out.println("Enter element no. " + i + j);
                a[i][j] = in.nextInt();
            }
        }
        System.out.println("Entered matrix is:");
        for (int i = 0; i <= 3; i++) {
            for (int j = 0; j <= 4; j++) {
                System.out.print(a[i][j] + "    ");
            }
            System.out.println();
        }
        for (int i = 0; i <= 3; i++) {
            for (int j = 0; j <= 4; j++) {
                sum = sum + a[i][j];
            }
            isum = isum + sum;
            System.out.println("Sum of " + (i + 1) + " row is:" + sum);
            sum = 0;
        }

        for (int i = 0; i <= 4; i++) {
            for (int j = 0; j <= 3; j++) {
                sum = sum + a[j][i];
            }
            System.out.println("Sum of " + (i + 1) + " coloum is:" + sum);
            sum=0;

        }
        System.out.println("Sum of intire matrix is: " + isum);

    }
}

Program that accepts an array by 10 numbers and display them in ascending order

import java.util.Scanner;

public class Ascending {

    public static void main(String[] args) {
        int temp;
        Scanner in = new Scanner(System.in);
        int a[] = new int[10];
        for (int i = 0; i <= 9; i++) {
            System.out.println("Enter element no. " + (i + 1));
            a[i] = in.nextInt();
        }
        for (int i = 0; i <= 9; i++) {
            for (int j = i + 1; j <= 9; j++) {
                if (a[i] > a[j]) {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
        for (int i = 0; i <= 9; i++) {
            System.out.print(a[i] + " ");
        }
    }
}