Wednesday, 3 February 2016

RDBMS Lab @ Home 1

1. Shopping Spree is a leading departmental store in Shanghai. The store has a number of regular customers who purchase bulk items. The store also conducts regular feedback sessions to analyze customer satisfaction levels. Chen, the Customer Analyst of Shopping Spree, has to make the ER diagram to represent the preceding situation, and then to map the ER diagram to the corresponding tables. Help Chen to do the same.

Ans









2. Design an ER diagram for the following situation:
An organization has two types of employees, salaried and wage earning. Both the types of employees have some common properties, such as employee code, employee name, and employee address. However, the salaried employees have other additional properties: basic, allowance, and House Rent Allowance (HRA). The wage earning employees have distinct properties that are daily wage and overtime.

Ans


3.  Consider the following Doctor-details table.

 In the preceding table, you have to identify primary key, candidate key, and alternate key and give the reason.


Ans 
4.  You have been hired by a regional hospital to create a database that will be used to store the information of the patients. Implementing the database will make the data easily accessible. The following ER diagram represents the relationship between the patient and the doctor. 

In the preceding diagram, you have to identify the entities, their attributes, and the type of relationship between the entities.


Ans  

5. Tom is working in an organization as a database administrator. Based on the hierarchy, the organization has multiple departments and each department has multiple employees. The following ER diagram represents the relationship between the departments and the employees.

 In the preceding diagram, you have to identify entities, their attributes, and type of relationship between the entities.

Ans 

6. Lee Wong is the newly appointed database administrator at Standard bank. The management of this bank wants to computerize the process of banking. According to the bank's policy, one customer can have many accounts but one account cannot be shared by many customers. The following ER diagram represents the relationship between a customer and the accounts owned by a particular customer. 

In the preceding diagram, you have to identify entities, their attributes, and type of relationship between the entities.


Ans. 
7.  Consider the following scenario: 
An author can write multiple books and a book can be written by more than one author. Identify the relationship between the author entity and the book entity and represent it with an ER Diagram.

Ans  


8.  Consider the following scenario: 
In a college, there are multiple departments. Each department has various faculty members. Only one of these faculty members holds the position of the Head of the Department. Identify the relationship between the faculty and the department and represent it with an ER Diagram.


Ans  

Saturday, 30 January 2016

Java _Network Files

Programmers Files  :-

1. SimpleClient

Ans

public class TestThread {
  public static void main(String args[]) {
    Xyz r = new Xyz();
    Thread t = new Thread(r);
    t.start();
  }
}

class Xyz implements Runnable {
  int i;

  public void run() {
    i = 0;

    while (true) {
      System.out.println("Hello " + i++);
      if ( i == 50 ) {
break;
      }
    }
  }
}

  
2.  SimpleServer

Ans

import java.net.*;
import java.io.*;

public class SimpleServer {
  public static void main(String args[]) {
    ServerSocket s = null;

    // Register your service on port 5432
    try {
      s = new ServerSocket(5432);
    } catch (IOException e) {
      e.printStackTrace();
    }

   // Run the listen/accept loop forever
    while (true) {
      try {
        // Wait here and listen for a connection
        Socket s1 = s.accept();
System.out.println("Connection accepted: port=" + s1.getPort());

        // Get output stream associated with the socket
        OutputStream s1out = s1.getOutputStream();
        BufferedWriter bw = new BufferedWriter(
          new OutputStreamWriter(s1out));

        // Send your string!
        bw.write("Hello Net World!\n");

        // Close the connection, but not the server socket
        bw.close();
        s1.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}


Java _ threads Files

Programmers Files :-


1. Sync File :-https://drive.google.com/open?id=0B08RKBSts8D8aFB2cVVlVFIzTlk




*1 .ControlledThread

Ans

public class ControlledThread extends Thread {
  static final int SUSP = 1;
  static final int STOP = 2;
  static final int RUN = 0;
  private int state = RUN;

  public synchronized void setState(int s) {
    state = s;
    if ( s == RUN ) {
      notify();
    }
  }

  public synchronized boolean checkState() {
    while ( state == SUSP ) {
      try {
        wait();
      } catch (InterruptedException e) {
        // ignore
      }
    }
    if ( state == STOP ) {
      return false;
    }
    return true;
  }

  public void run() {
    while ( true ) {
      //doSomething();

      // Be sure shared data is in consistent state in
      // case the thread is waited or marked for exiting
      // from run()
      if ( !checkState() ) {
        break;
      }
    }
  }
}


*2.  MyStack

Ans

public class MyStack {
  int idx = 0;
  char [] data = new char[6];

  public void push(char c) {
    data[idx] = c;
    idx++;
  }

  public char pop() {
    idx--;
    return data[idx];
  }
}


*3. TestMyStack

Ans

public class TestMyStack {
   public static void main(String args[]) {
     MyStack stack = new MyStack();
     stack.push('H');
     stack.push('i');
     System.out.println("Stack is: " + String.copyValueOf(stack.data));

     stack.pop();
     stack.push('o');
     System.out.println("After one pop and another push, Stack is: " + 
String.copyValueOf(stack.data));
   }
}
     

*4. TestThread

Ans

public class TestThread {
  public static void main(String args[]) {
    Xyz r = new Xyz();
    Thread t = new Thread(r);
    t.start();
  }
}

class Xyz implements Runnable {
  int i;

  public void run() {
    i = 0;

    while (true) {
      System.out.println("Hello " + i++);
      if ( i == 50 ) {
break;
      }
    }
  }
}


Code Files :- https://drive.google.com/open?id=0B08RKBSts8D8LVZyY3hsU25XZUE

Java _guiapps Files

Programmers Files :-

1. SampleMenu

Ans

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SampleMenu
    implements ActionListener, ItemListener {
  private JFrame f;
  private JMenuBar mb;
  private JMenu m1;
  private JMenu m2;
  private JMenu m3;
  private JMenuItem mi1;
  private JMenuItem mi2;
  private JMenuItem mi3;
  private JMenuItem mi4;
  private JCheckBoxMenuItem mi5;

  public void go() {
    f = new JFrame("Menu");
    mb = new JMenuBar();
    m1 = new JMenu("File");
    m2 = new JMenu("Edit");
    m3 = new JMenu("Help");
    mb.add(m1);
    mb.add(m2);
    mb.add(m3);
    f.setJMenuBar(mb);

    mi1 = new JMenuItem("New");
    mi2 = new JMenuItem("Save");
    mi3 = new JMenuItem("Load");
    mi4 = new JMenuItem("Quit");
    mi1.addActionListener(this);
    mi2.addActionListener(this);
    mi3.addActionListener(this);
    mi4.addActionListener(this);
    m1.add(mi1);
    m1.add(mi2);
    m1.add(mi3);
    m1.addSeparator();
    m1.add(mi4);

    mi5 = new JCheckBoxMenuItem("Persistent");
    mi5.addItemListener(this);
    m1.add(mi5);

    f.setSize(200,200);
    f.setVisible(true);
  }

  public void actionPerformed( ActionEvent ae) {
    System.out.println("Button \"" +
        ae.getActionCommand() + "\" pressed.");

    if (ae.getActionCommand().equals("Quit")) {
      System.exit(0);
    }
  }

  public void itemStateChanged(ItemEvent ie) {
    String state = "deselected";

    if (ie.getStateChange() == ItemEvent.SELECTED) {
      state = "selected";
    }
    System.out.println(ie.getItem() + " " + state);
  }

  public static void main (String args[]) {
    SampleMenu sampleMenu = new SampleMenu();
    sampleMenu.go();
  }
}

2. SampleMenu1

Ans

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SampleMenu1 {
  private JFrame f;
  private JMenuBar mb;

  public void go() {
    f = new JFrame("MenuBar");
    mb = new JMenuBar();
    f.setJMenuBar(mb);

    f.setSize(200,200);
    f.setVisible(true);
  }

  public static void main (String args[]) {
    SampleMenu1 sampleMenu = new SampleMenu1();
    sampleMenu.go();
  }
}


3.  SampleMenu2

Ans

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SampleMenu2 {
  private JFrame f;
  private JMenuBar mb;
  private JMenu m1;
  private JMenu m2;
  private JMenu m3;

  public void go() {
    f = new JFrame("Menu");
    mb = new JMenuBar();
    m1 = new JMenu("File");
    m2 = new JMenu("Edit");
    m3 = new JMenu("Help");
    mb.add(m1);
    mb.add(m2);
    mb.add(m3);
    f.setJMenuBar(mb);

    f.setSize(200,200);
    f.setVisible(true);
  }

  public static void main (String args[]) {
    SampleMenu2 sampleMenu = new SampleMenu2();
    sampleMenu.go();
  }
}


4. SampleMenu3

Ans

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SampleMenu3
    implements ActionListener, ItemListener {
  private JFrame f;
  private JMenuBar mb;
  private JMenu m1;
  private JMenu m2;
  private JMenu m3;
  private JMenuItem mi1;
  private JMenuItem mi2;
  private JMenuItem mi3;
  private JMenuItem mi4;

  public void go() {
    f = new JFrame("MenuItem");
    mb = new JMenuBar();
    m1 = new JMenu("File");
    m2 = new JMenu("Edit");
    m3 = new JMenu("Help");
    mb.add(m1);
    mb.add(m2);
    mb.add(m3);
    f.setJMenuBar(mb);

    mi1 = new JMenuItem("New");
    mi2 = new JMenuItem("Save");
    mi3 = new JMenuItem("Load");
    mi4 = new JMenuItem("Quit");
    mi1.addActionListener(this);
    mi2.addActionListener(this);
    mi3.addActionListener(this);
    mi4.addActionListener(this);
    m1.add(mi1);
    m1.add(mi2);
    m1.add(mi3);
    m1.addSeparator();
    m1.add(mi4);

    f.setSize(200,200);
    f.setVisible(true);
  }

  public void actionPerformed( ActionEvent ae) {
    System.out.println("Button \"" + 
        ae.getActionCommand() + "\" pressed.");

    if (ae.getActionCommand().equals("Quit")) {
      System.exit(0);
    }
  }

  public void itemStateChanged(ItemEvent ie) {
    String state = "deselected";

    if (ie.getStateChange() == ItemEvent.SELECTED) {
      state = "selected";
    }
    System.out.println(ie.getItem() + " " + state);
  }

  public static void main (String args[]) {
    SampleMenu3 sampleMenu = new SampleMenu3();
    sampleMenu.go();
  }
}


5. SampleMenu4

Ans

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SampleMenu4
    implements ActionListener, ItemListener {
  private JFrame f;
  private JMenuBar mb;
  private JMenu m1;
  private JMenu m2;
  private JMenu m3;
  private JMenuItem mi1;
  private JMenuItem mi2;
  private JMenuItem mi3;
  private JMenuItem mi4;
  private JCheckBoxMenuItem mi5;

  public void go() {
    f = new JFrame("CheckboxMenuItem");
    mb = new JMenuBar();
    m1 = new JMenu("File");
    m2 = new JMenu("Edit");
    m3 = new JMenu("Help");
    mb.add(m1);
    mb.add(m2);
    mb.add(m3);
    f.setJMenuBar(mb);

    mi1 = new JMenuItem("New");
    mi2 = new JMenuItem("Save");
    mi3 = new JMenuItem("Load");
    mi4 = new JMenuItem("Quit");
    mi1.addActionListener(this);
    mi2.addActionListener(this);
    mi3.addActionListener(this);
    mi4.addActionListener(this);
    m1.add(mi1);
    m1.add(mi2);
    m1.add(mi3);
    m1.addSeparator();
    m1.add(mi4);

    mi5 = new JCheckBoxMenuItem("Persistent");
    mi5.addItemListener(this);
    m1.add(mi5);

    f.setSize(200,200);
    f.setVisible(true);
  }

  public void actionPerformed( ActionEvent ae) {
    System.out.println("Button \"" + 
        ae.getActionCommand() + "\" pressed.");

    if (ae.getActionCommand().equals("Quit")) {
      System.exit(0);
    }
  }

  public void itemStateChanged(ItemEvent ie) {
    String state = "deselected";

    if (ie.getStateChange() == ItemEvent.SELECTED) {
      state = "selected";
    }
    System.out.println(ie.getItem() + " " + state);
  }

  public static void main (String args[]) {
    SampleMenu4 sampleMenu = new SampleMenu4();
    sampleMenu.go();
  }
}


6.  TestColor

Ans

import java.awt.*;
public class TestColor {
  public static void main(String[] args) {
    TestColor tester = new TestColor();
    tester.launchFrame();
  }
  TestColor() {}
  private void launchFrame() {
    Frame f = new Frame();
    Button b = new Button("Purple");
    Color purple = new Color(255, 0, 255);
    b.setBackground(purple);
    f.add(b);
    f.pack();
    f.setVisible(true);
  }
}


7. TestPrinting

Ans


import java.awt.*;
import java.awt.event.*;

public class TestPrinting {

  private Frame frame;
  private TextArea output;
  private TextField input;

  public TestPrinting() {
    frame = new Frame("Chat Room");
    output = new TextArea(10,50);
    input = new TextField(50);
  }

  public void launchFrame() {
    Button button;

    frame.setLayout(new BorderLayout());

    frame.add(output,BorderLayout.WEST);
    frame.add(input,BorderLayout.SOUTH);

    Panel p1 = new Panel(); 

    // Add Send button
    button = new Button("Send");
    button.addActionListener(new SendHandler());
    p1.add(button);

    // Add Print button
    button = new Button("Print");
    button.addActionListener(new PrintHandler());
    p1.add(button);

    // Add Quit button
    button = new Button("Quit");
    // Use an anonymous inner class for variety.
    button.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
System.exit(0);
      }
    });
    p1.add(button);

    frame.add(p1,BorderLayout.CENTER);

    // Attach listener to the appropriate components
    frame.addWindowListener(new CloseHandler());
    input.addActionListener(new InputHandler());

    frame.setSize(440,210);
    frame.setVisible(true);
  }

  private void copyText() {
    String text = input.getText();
    output.setText(output.getText()+text+"\n");
    input.setText("");
  }

  private class SendHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      copyText();
    }
  }

  private class PrintHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      Toolkit toolkit = frame.getToolkit();
      PrintJob job = toolkit.getPrintJob(frame, "Print the Chat Room", null);
      Graphics g = job.getGraphics();

      frame.print(g);
      g.dispose();
      job.end();
    }
  }

  private class CloseHandler extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
      System.exit(0);
    }
  }
    
  private class InputHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      copyText(); 
    }
  }

  public static void main(String[] args) {
    TestPrinting c = new TestPrinting();
    c.launchFrame();
  }
}


Java _events Files

Programmers file 

1. ButtonHandler

Ans

import java.awt.event.*;

public class ButtonHandler implements ActionListener {
  public void actionPerformed(ActionEvent e) {
    System.out.println("Action occurred");
    System.out.println("Button's command is: "
                       + e.getActionCommand());
  }

}

2.  MouseClickHandler

Ans

import java.awt.*;
import java.awt.event.*;

public class MouseClickHandler extends MouseAdapter {

// We just need the mouseClick handler, so we use
// the Adapter to avoid having to write all the
// event handler methods

public void mouseClicked (MouseEvent e) {
// Do stuff with the mouse click...
}

}

3. TestAnonymous

Ans

import java.awt.*;
import java.awt.event.*;

public class TestAnonymous {
  private Frame f;
  private TextField tf;

  public TestAnonymous() {
    f = new Frame("Anonymous classes example");
    tf = new TextField(30);
  }

  public void launchFrame() {
    Label label = new Label("Click and drag the mouse");
    // Add components to the frame
    f.add(label, BorderLayout.NORTH);
    f.add(tf, BorderLayout.SOUTH);
    // Add a listener that uses an anonymous class
    f.addMouseMotionListener( new MouseMotionAdapter() {
      public void mouseDragged(MouseEvent e) {
        String s = "Mouse dragging:  X = "+ e.getX()
                    + " Y = " + e.getY();
        tf.setText(s);
      }
    }); // <- note the closing parenthesis
    f.addMouseListener(new MouseClickHandler());
    // Size the frame and make it visible
    f.setSize(300, 200);
    f.setVisible(true);
  }

  public static void main(String args[]) {
    TestAnonymous obj = new TestAnonymous();
    obj.launchFrame();
  }
}

4. TestButton

Ans

import java.awt.*;

public class TestButton {
  private Frame f;
  private Button b;

  public TestButton() {
    f = new Frame("Test");
    b = new Button("Press Me!");
    b.setActionCommand("ButtonPressed");
  }

  public void launchFrame() {
    b.addActionListener(new ButtonHandler());
    f.add(b,BorderLayout.CENTER);
    f.pack();
    f.setVisible(true);
  }

  public static void main(String args[]) {
    TestButton guiApp = new TestButton();
    guiApp.launchFrame();
  }
}


5.  TestInner

Ans

import java.awt.*;
import java.awt.event.*;

public class TestInner {
  private Frame f;
  private TextField tf;

  public TestInner() {
    f = new Frame("Inner classes example");
    tf = new TextField(30);
  }

  public void launchFrame() {
    Label label = new Label("Click and drag the mouse");
    // Add components to the frame
    f.add(label, BorderLayout.NORTH);
    f.add(tf, BorderLayout.SOUTH);
    // Add a listener that uses an Inner class
    f.addMouseMotionListener(this.new MyMouseMotionListener());
    f.addMouseListener(new MouseClickHandler());
    // Size the frame and make it visible
    f.setSize(300, 200);
    f.setVisible(true);
  }

  class MyMouseMotionListener extends MouseMotionAdapter {
      public void mouseDragged(MouseEvent e) {
        String s = "Mouse dragging:  X = "+ e.getX()
                    + " Y = " + e.getY();
        tf.setText(s);
      }
    }

  public static void main(String args[]) {
    TestInner obj = new TestInner();
    obj.launchFrame();
  }
}


6. TwoListener

Ans

import java.awt.*;
import java.awt.event.*;

public class TwoListener
     implements MouseMotionListener, 
                MouseListener {
  private Frame f;
  private TextField tf;

  public TwoListener() {
    f = new Frame("Two listeners example");
    tf = new TextField(30);
  }

  public void launchFrame() {
    Label label = new Label("Click and drag the mouse");
    // Add components to the frame
    f.add(label, BorderLayout.NORTH);
    f.add(tf, BorderLayout.SOUTH);
    // Add this object as a listener
    f.addMouseMotionListener(this);
    f.addMouseListener(this);
    // Size the frame and make it visible
    f.setSize(300, 200);
    f.setVisible(true);
  }

  // These are MouseMotionListener events
  public void mouseDragged(MouseEvent e) {
    String s =  "Mouse dragging:  X = " + e.getX()
                + " Y = " + e.getY();
    tf.setText(s);
  }

  public void mouseEntered(MouseEvent e) {
    String s = "The mouse entered";
    tf.setText(s);
  }

  public void mouseExited(MouseEvent e) {
    String s = "The mouse has left the building";
    tf.setText(s);
  }

  // Unused MouseMotionListener method.
  // All methods of a listener must be present in the
  // class even if they are not used.
  public void mouseMoved(MouseEvent e) { }

  // Unused MouseListener methods.
  public void mousePressed(MouseEvent e) { }
  public void mouseClicked(MouseEvent e) { }
  public void mouseReleased(MouseEvent e) { }

  public static void main(String args[]) {
    TwoListener two = new TwoListener();
    two.launchFrame();
  }
}


Java _gui File

1. BorderExample

Ans. 

import java.awt.*;
import javax.swing.*;

public class BorderExample {
  private JFrame f;
  private JButton bn, bs, bw, be, bc;

  public BorderExample() {
    f = new JFrame("Border Layout");
    bn = new JButton("Button 1");
    bc = new JButton("Button 2");
    bw = new JButton("Button 3");
    bs = new JButton("Button 4");
    be = new JButton("Button 5");
  }

  public void launchFrame() {
    f.add(bn, BorderLayout.NORTH);
    f.add(bs, BorderLayout.SOUTH);
    f.add(bw, BorderLayout.WEST);
    f.add(be, BorderLayout.EAST);
    f.add(bc, BorderLayout.CENTER);
    f.setSize(400,200);
    f.setVisible(true);
  }

  public static void main(String args[]) {
    BorderExample guiWindow2 = new BorderExample();
    guiWindow2.launchFrame();
  }
}


2. FlowExample

Ans

import java.awt.*;
import javax.swing.*;

public class FlowExample {
    private JFrame f;
    private JButton b1;
    private JButton b2;
    private JButton b3;
    private JButton b4;
    private JButton b5;

    public FlowExample() {
        f = new JFrame("GUI example");
        b1 = new JButton("Button 1");
        b2 = new JButton("Button 2");
        b3 = new JButton("Button 3");
        b4 = new JButton("Button 4");
        b5 = new JButton("Button 5");
    }

    public void launchFrame() {
        f.setLayout(new FlowLayout());
        f.add(b1);
        f.add(b2);
        f.add(b3);
        f.add(b4);
        f.add(b5);
        f.pack();
        f.setVisible(true);
    }

    public static void main(String args[]) {
        FlowExample guiWindow = new FlowExample();
        guiWindow.launchFrame();
    }

} 

3. FrameExample

Ans

import java.awt.*;

public class FrameExample {
  private Frame f;

  public FrameExample() {
    f = new Frame("Hello Out There!");
  }
  
  public void launchFrame() {
    f.setSize(170,170);
    f.setBackground(Color.blue);
    f.setVisible(true);
  }

  public static void main(String args[]) {
    FrameExample guiWindow = new FrameExample();
    guiWindow.launchFrame();
  }
}


4.  GridExample

Ans

import java.awt.*;
import javax.swing.*;

public class GridExample {
  private JFrame f;
  private JButton b1, b2, b3, b4, b5;

  public GridExample() {
    f = new JFrame("Grid Example");
    b1 = new JButton("Button 1");
    b2 = new JButton("Button 2");
    b3 = new JButton("Button 3");
    b4 = new JButton("Button 4");
    b5 = new JButton("Button 5");
  }

  public void launchFrame() {
    f.setLayout (new GridLayout(3,2));

    f.add(b1);
    f.add(b2);
    f.add(b3);
    f.add(b4);
    f.add(b5);

    f.pack();
    f.setVisible(true);
  }

  public static void main(String args[]) {
    GridExample grid = new GridExample();
    grid.launchFrame();
  }
}


5.  HelloWorldSwing

Ans

import javax.swing.*;        
public class HelloWorldSwing {
    private static void createAndShowGUI() {
        JFrame frame = new JFrame("HelloWorldSwing"); 
//Set up the window.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JLabel label = new JLabel("Hello World");
// Add Label
        frame.add(label);
     frame.setSize(300,200);
// Display Window
        frame.setVisible(true);}

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            //Schedule for the event-dispatching thread:
            //creating,showing this app's GUI.
      public void run() {createAndShowGUI();} 
     });
    } 
}