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();} 
     });
    } 
}

Sunday 24 January 2016

Java collections

Java code_except

* AddArguments :-

public class AddArguments {
  public static void main (String args[]) {
    int sum = 0;
    for ( String arg : args ) {
      sum += Integer.parseInt(arg);
    }
    System.out.println("Sum = " + sum);
  }

}

* AddArguments 2 :-

public class AddArguments2 {
  public static void main (String args[]) {
    try {
      int sum = 0;
      for ( String arg : args ) {
sum += Integer.parseInt(arg);
      }
      System.out.println("Sum = " + sum);
    } catch (NumberFormatException nfe) {
      System.err.println("One of the command-line "
+ "arguments is not an integer.");
    }
  }
}

* AddArguments 3 :-

public class AddArguments3 {
  public static void main (String args[]) {
    int sum = 0;
    for ( String arg : args ) {
      try {
        sum += Integer.parseInt(arg);
      } catch (NumberFormatException nfe) {
        System.err.println("[" + arg + "] is not an integer"
                           + " and will not be included in the sum.");
      }
    }
    System.out.println("Sum = " + sum);
  }
}


* HelloWorld Code :-

public class HelloWorld {
  public static void main (String args[]) {
    int i = 0;

    String greetings [] = {
      "Hello world!",
      "No, I mean it!",
      "HELLO WORLD!!"
    };

    while (i < 4) {
      try {
        System.out.println (greetings[i]);
i++;
      } catch (ArrayIndexOutOfBoundsException e){
        System.out.println("Re-setting Index Value");
        i = 0;
      } finally {
        System.out.println("This is always printed");
      }
    }
  }
}


Java Class2

Friday 22 January 2016

Java class1

1. Overriding

* Child

public class Child extends Parent {
  private void doSomething() {}

}

* Parent

public class Parent {
  public void doSomething() {}

}


* UseBoth

public class UseBoth {
  public void doOtherThing() {
    Parent p1 = new Parent();
    Parent p2 = new Child(); // illegal
    p1.doSomething();
    p2.doSomething();
  }

}


2.  Codes ID :-

*  Employee

public class Employee {

  private String name;
  private MyDate birthDate;
  private float  salary;

  // Constructor
  public Employee(String name, MyDate DoB, float salary) {
    this.name = name;
    this.birthDate = DoB;
    this.salary = salary;
  }

  public String getDetails() {
    return "Name: " + name + "\nSalary: " + salary
           + "\nBirth Date: " + birthDate;
  }
}


*  Manager

public class Manager extends Employee {
  private String department;

  public Manager(String name, MyDate DoB,
float salary, String dept) {
    super(name, DoB, salary);
    this.department = dept;
  }

  public String getDetail() {
    return super.getDetails() + "\nDepartment: " + department;
  }
}


*  MyDate

public class MyDate {
  private int day;
  private int month;
  private int year;

  public MyDate(int day, int month, int year) {
    this.day   = day;
    this.month = month;
    this.year  = year;
  }

  public boolean equals(Object o) {
    boolean result = false;
    if ( (o != null) && (o instanceof MyDate) ) {
      MyDate d = (MyDate) o;
      if ( (day == d.day) && (month == d.month)
  && (year == d.year) ) {
        result = true;
      }
    }
    return result;
  }

  public int hashCode() {
    return (day ^ month ^ year);
  }
}


*  TestEquals


public class TestEquals {
  public static void main(String[] args) {
    MyDate  date1 = new MyDate(14, 3, 1976);
    MyDate  date2 = new MyDate(14, 3, 1976);

    if ( date1 == date2 ) {
      System.out.println("date1 is identical to date2");
    } else {
      System.out.println("date1 is not identical to date2");
    }

    if ( date1.equals(date2) ) {
      System.out.println("date1 is equal to date2");
    } else {
      System.out.println("date1 is not equal to date2");
    }

    System.out.println("set date2 = date1;");
    date2 = date1;

    if ( date1 == date2 ) {
      System.out.println("date1 is identical to date2");
    } else {
      System.out.println("date1 is not identical to date2");
    }
  }
}


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

Java Arrays

1. SimpleCharArray

public class SimpleCharArray {
    public static void main(String[] args) {
        char[] characters = createArray();
       
        printArray(characters);
        System.out.println();
    }
   
    public static char[] createArray() {
        char[] s;
       
        s = new char[26];
        for ( int i=0; i<26; i++ ) {
            s[i] = (char) ('A' + i);
        }
       
        return s;
    }
   
    public static void printArray(char[] array) {
        System.out.print('<');
        for ( int i = 0; i < array.length; i++ ) {
            // print an element
            System.out.print(array[i]);
            // print a comma delimiter if not the last element
            if ( (i + 1) < array.length ) {
                System.out.print(", ");
            }
        }
        System.out.print('>');
    }
}

Java Code stmts

1. Loops

public class Loops {
  public static void main(String [] args) throws Throwable {

loop:
    while (true) {
      for (int i = 0; i < 100; i++) {
        int c = System.in.read();
        if (c == 'x') {
          continue loop;
        } else if (c == 'y') {
          break loop;
        } else {
          System.out.print((char)c);
          //System.out.flush();
        }
      }
      System.out.println(">");
    }
    System.out.println();
  }
}

2.  ScopeExample

public class ScopeExample {
  private int i=1;

  public void firstMethod() {
    int i=4, j=5;

    this.i = i + j;
    secondMethod(7);
  }
  public void secondMethod(int i) {
    this.i = i;
  }
}

3. StmtLabels

/*
 * This file tests a few variations on labelled break/continue statements.
 */

public class StmtLabels {
  public static void main(String[] args) {
    int count = 0;
    
    // test break label on compound statement
  lab1:
    {
      System.out.println("before lab1");
      if ( count == 0 ) 
{ break lab1; }
      System.out.println("after lab1");
    }

    // test break label on an if statement
    System.out.println("before lab1a");
  lab1a:
    if ( count == 0 ) {
break lab1a;
    } else {
System.out.println("else clause; not break to lab1a");
    }
    System.out.println("after lab1a");

    // test continue label on compound statement
  lab2:
    {
      System.out.println("before lab2");
      //if ( count == 0 ) 
      //{ continue lab2; }  CAN NOT continue A COMPOUND STMT
      System.out.println("after lab2");
    }
  }
}


4. TestForLoop

public class TestForLoop {
  public static void main(String[] args) {
    for ( int i = 0; i < 10; i++ )
      System.out.println(i + " squared is " + (i*i));
  }
}


5. TestInitBeforeUse

public class TestInitBeforeUse {

  public void doComputation() {
    int x = (int)(Math.random() * 100);
    int y;
    int z;
    if (x > 50) {
      y = 9;
    }
    z = y + x;  // Possible use before initialization
  }

}

6.  TestScoping

public class TestScoping {
  public static void main(String[] args) {
    ScopeExample  scope = new ScopeExample();

    scope.firstMethod();
  }
}

7.  TestShift

public class TestShift {

  public static String bitPattern(int value) {
    int BIT1_MASK = 1;
    final char[] bits = new char[32];

    for ( int i = 31; i >= 0; i-- ) {
      if ( (value & BIT1_MASK) == 1 ) {
bits[i] = '1';
      } else {
bits[i] = '0';
      }
      value >>>= 1;
    }

    return new String(bits);
  }

  public static void main(String[] args) {
    int num1 = 1357;
    int num2 = -1357;

    System.out.println("num1 (" + num1 + ") as a bit pattern: " + bitPattern(num1));
    System.out.println("num2 (" + num2 + ") as a bit pattern: " + bitPattern(num2));
    System.out.println("" + num1 + " >> 5 = (" + (num1 >> 5) + ") " + bitPattern(num1 >> 5));
    System.out.println("" + num2 + " >> 5 = (" + (num2 >> 5) + ") " + bitPattern(num2 >> 5));
    System.out.println("" + num1 + " >>> 5 = (" + (num1 >>> 5) + ") " + bitPattern(num1 >>> 5));
    System.out.println("" + num2 + " >>> 5 = (" + (num2 >>> 5) + ") " + bitPattern(num2 >>> 5));
    System.out.println("" + num1 + " << 5 = (" + (num1 << 5) + ") " + bitPattern(num1 << 5));
    System.out.println("" + num2 + " << 5 = (" + (num2 << 5) + ") " + bitPattern(num2 << 5));
  }
}

/*
num1 (1357) as a bit pattern:  00000000000000000000010101001101
num2 (-1357) as a bit pattern: 11111111111111111111101010110011
1357   >> 1 = 00000000000000000000001010100110
-1357  >> 1 = 11111111111111111111110101011001
1357   >> 5 = 00000000000000000000000000101010
-1357  >> 5 = 11111111111111111111111111010101
1357  >>> 5 = 00000000000000000000000000101010
-1357 >>> 5 = 00000111111111111111111111010101
1357   << 1 = 00000000000000000000101010011010
-1357  << 1 = 11111111111111111111010101100110
1357   << 5 = 00000000000000001010100110100000
-1357  << 5 = 11111111111111110101011001100000
*/


8.  TestWhileLoops

public class TestWhileLoops {

  public static void main(String[] args) {
    test1();
    test2();
    test3();
    test4();
  }

  private static void test1() {
    System.out.println("Test #1 - while loop with block");
    int i = 0;
    while ( i < 10 ) {
      System.out.println(i + " squared is " + (i*i));
      i++;
    }
  }

  private static void test2() {
    System.out.println("Test #2 - while loop with single statement");
    int i = 0;
    while ( i < 10 )
      System.out.println(++i + " squared is " + (i*i));
  }

  private static void test3() {
    System.out.println("Test #3 - do-while loop with block");
    int i = 0;
    do {
      System.out.println(i + " squared is " + (i*i));
      i++;
    } while ( i < 10 );
  }

  private static void test4() {
    System.out.println("Test #4 - do-while loop with single statement");
    int i = 0;
    do
      System.out.println(++i + " squared is " + (i*i));
    while ( i < 10 );
  }
}


Java Code types

1.Assign

public class Assign {
  public static void main (String args []) {
    // declare integer variables
    int x, y;
    // declare and assign floating point
    float z = 3.414f;
    // declare and assign double
    double w = 3.1415;
    // declare and assign boolean
    boolean truth = true;
    // declare character variable
    char c;
    // declare String variable
    String str;
    // declare and assign String variable
    String str1 = "bye";
    // assign value to char variable
    c = 'A';
    // assign value to String variable
    str = "Hi out there!";
    // assign values to int variables
    x = 6;
    y = 1000;  
  }
}


2. MyDate


public class MyDate {
  private int day;
  private int month;
  private int year;

  public MyDate(int day, int month, int year) {
    this.day   = day;
    this.month = month;
    this.year  = year;
  }
  public MyDate(MyDate date) {
    this.day   = date.day;
    this.month = date.month;
    this.year  = date.year;
  }

  public int getDay() {
    return day;
  }

  public void setDay(int day) {
    this.day = day;
  }

  public MyDate addDays(int more_days) {
    MyDate new_date = new MyDate(this);

    new_date.day = new_date.day + more_days;
    // Not Yet Implemented: wrap around code...

    return new_date;
  }
  public String toString() {
    return "" + day + "-" + month + "-" + year;
  }
}


3. PassTest

public class PassTest {

  // Methods to change the current values
  public static void changeInt(int value) {
    value = 55;
  }
  public static void changeObjectRef(MyDate ref) {
    ref = new MyDate(1, 1, 2000);
  }
  public static void changeObjectAttr(MyDate ref) {
    ref.setDay(4);
  }

  public static void main(String args[]) {
    MyDate date;
    int val;

    // Assign the int 
    val = 11;
    // Try to change it
    changeInt(val);
    // What is the current value?
    System.out.println("Int value is: " + val);

    // Assign the date
    date = new MyDate(22, 7, 1964);
    // Try to change it
    changeObjectRef(date);
    // What is the current value?
    System.out.println("MyDate: " + date);

    // Now change the day attribute
    // through the object reference
    changeObjectAttr(date);
    // What is the current value?
    System.out.println("MyDate: " + date);
  }
}



4. TestMyDate

public class TestMyDate {
  public static void main(String[] args) {
    MyDate my_birth = new MyDate(22, 7, 1964);
    MyDate the_next_week = my_birth.addDays(7);

    System.out.println(the_next_week);
  }
}


Java OOP Programmes

1. Exercise

public class Cat {
  private int weight;

  public Cat() {
     weight = 42;
  }

  public int getWeight() {
    return weight;
  }
  public void setWeight(int newWeight) {
    weight = newWeight;
  }
}

Java Programmes Introduction

Introduction:-


1. Greeting Code :-

//
// The Greeting class declaration.
//
 public class Greeting {
    public void greet() {
       System.out.println("hi");
    }
  }


2. TestGreeting :-

//
// Sample "Hello World" application
//
public class TestGreeting {
   public static void main (String[] args) {
      Greeting hello = new Greeting();
      hello.greet();
    }

 }

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

JDBC Code .02

JDBC Code .01

JDBC Programming Codes

Wednesday 20 January 2016

WCD Exercise. 15

WCD Exercise. 14

WCD Exercise. 13

WCD Exercise. 12

WCD Exercise. 11

WCD Exercise. 10

WCD Exercise. 09

WCD Exercise. 08

WCD Exercise. 07

WCD Exercise. 06

WCD Exercise. 05

WCD Exercise. 04

WCD Exercise. 03

WCD Exercise.02

WCD Exercise 01

Sunday 3 January 2016

Personal Productivity Tools Exercise

NIIT Notes MS Word Student