Core Java Programme1

package firstprogramjava;
class FirstProgram {
public static void main(String args[]){
System.out.println("Hellooooooooooo.......");
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------package myfirstgui;
//Usually you will require both swing and awt packages
//even if you are working with just swings.
import javax.swing.*;
import java.awt.*;
class FirstGui{
public static void main(String args[]){
//Creating the Frame
JFrame frame = new JFrame("Chat Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
//Creating the MenuBar and adding components
JMenuBar mb = new JMenuBar();
JMenu m1 = new JMenu("FILE");
JMenu m2 = new JMenu("Help");
mb.add(m1);
mb.add(m2);
JMenuItem m11 = new JMenuItem("Open");
JMenuItem m22 =new JMenuItem("Save as");
m1.add(m11);
m1.add(m22);
//Creating the panel at bottom and adding components
JPanel panel = new JPanel(); // the panel is not visible in output
JLabel label = new JLabel("Enter Text");
JTextField tf = new JTextField(10);// accepts upto 10 characters
JButton send = new JButton("Send");
JButton reset = new JButton("Reset");
panel.add(label);// Components Added using Flow Layout
panel.add(tf);
panel.add(send);
panel.add(reset);
//Text Area at the Center
JTextArea ta = new JTextArea();
//Adding Components to the frame.
frame.getContentPane().add(BorderLayout.SOUTH,panel);
frame.getContentPane().add(BorderLayout.NORTH,mb);
frame.getContentPane().add(BorderLayout.CENTER,ta);
frame.setVisible(true);
}
}

Output:-



---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Inheritance class Programme

package inheritclss;
public class InheritClassLesson{
    public static void main(String args[]){
       X x= new X();
       Y y = new  Y();
       y.m2();
       //x.m1();
       //y.m1();
       //x = y;  parent pointing to object of child
       //x.m1() ;
       //y.a=10;
  }

}
class X{
  private int a;
  int b;
     public void m1(){
      System.out.println("This is method m1 of class X");
    }
}

class Y extends X{
     int c; 
     //new instance variable of class Y
        public void m1(){
           //overriden method
           System.out.println("This is method m1 of class Y");
       }
      public void m2(){
          super.m1();
          System.out.println("This is method m2 of class Y");
     }
}

Output:-

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

String

package firststring;
class StringClass{
 public static void main(String []args){
   String strTest = "100";
   /*This statement results in a compilation error as you
   cannot do arithmetic operation on Strings
   System.out.println("Using String:" + (strTest/4));
   Convert the String to Integer*/
   int iTest = Integer.parseInt(strTest);
   System.out.println("Actual String:"+ strTest);
   System.out.println("Converted to Int:" + iTest);
   
   System.out.println("Arithmetic Operation on Int:" + (iTest/5));
 }
}
Output:-
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Array:-

package firstjavaarray;
import java.util.ArrayList;
class FirstArray {
    public static void main(String[] args) {
        //Creating a generic ArrayList
        ArrayList arlTest = new ArrayList();
        //Size of arrayList
        System.out.println("Size of ArrayList at creation: " +  arlTest.size());
        //Lets add some elements to it
        arlTest.add("D");
        arlTest.add("U");
        arlTest.add("K");
        arlTest.add("E");
  
        //Recheck the size after adding elements
        System.out.println("Size of ArrayList after adding elements: " + arlTest.size());
  
        //Display all contents of ArrayList
        System.out.println("List of all elements: " + arlTest);
  
        //Remove some elements from the list
        arlTest.remove("D");
        System.out.println("See contents after removing one element: " + arlTest);
  
        //Remove element by index
        arlTest.remove(2);
        System.out.println("See contents after removing element by index: " + arlTest);
  
        //Check size after removing elements
        System.out.println("Size of arrayList after removing elements: " + arlTest.size());
        System.out.println("List of all elements after removing elements: " + arlTest);
  
        //Check if the list contains "K"
        System.out.println(arlTest.contains("K"));
  
    }
}
Output:-
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


Loops:-

package loopspkg;
class LoopsClass {
 public static void main(String[] args) {
   String[] arrData = {"Alpha", "Beta", "Gamma", "Delta", "Sigma"};
   
   System.out.println("Using conventional For Loop:");
   for(int i=0; i< arrData.length; i++){
     System.out.println(arrData[i]);
   }
   System.out.println("\nUsing Foreach loop:");
   
   for (String strTemp : arrData){
     System.out.println(strTemp);
   }
 }
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Date format

package datepkg;
import java.text.SimpleDateFormat;
import java.util.Date;
 class DateClass {
   public static void main(String args[]) {
     Date objDate = new Date( ); // Current System Date and time is assigned to objDate
     System.out.println(objDate);
     String strDateFormat = "hh:mm:ss a dd-MMM-yyyy";//Date format is Specified
     SimpleDateFormat objSDF = new SimpleDateFormat (strDateFormat);//Date format string is passed as an argument to the Date format object
     System.out.println(objSDF.format(objDate));//Date formatting is applied to the current date
   }
 }
Output:-

Single Thread


package singlethreadd;
public class SingleThreads implements Runnable
{

    /**
     * @param args
     */
    public static void main(String[] args) {
        Thread guruThread1 = new Thread("Guru1");
        Thread guruThread2 = new Thread("Guru2");
        guruThread1.start();
        guruThread2.start();
        System.out.println("Thread names are following:");
        System.out.println(guruThread1.getName());
        System.out.println(guruThread2.getName());
    }
    @Override
    public void run() {
    }

}

Output:-


Java multithreading example

In this example, we will learn about overriding methods run() and start() method of a runnable interface and create two threads of that class and run them accordingly.
Also, we are taking two classes,
  • One which will implement the runnable interface and
  • Other one which will have the main method and execute accordingly.

package multithreadingpkg;


public class MultiThreadingClass {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        GuruThread3 threadguru1 = new GuruThread3("guru1");
        threadguru1.start();
        GuruThread3 threadguru2 = new GuruThread3("guru2");
        threadguru2.start();}
}
class GuruThread3 implements Runnable
{    Thread guruthread;
    private String guruname;
   GuruThread3(String name)
   {
       guruname = name;   
    }
    @Override
    public void run() {
   System.out.println("Thread running" +guruname);
    for(int i=0;i<4;i++)
    {
        System.out.println(i);
        System.out.println(guruname);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            System.out.println("Thread has been interrupted");
        }
    }
    }
    public void start()
    {
        System.out.println("Thread started");
        if(guruthread==null)
        {
            guruthread = new Thread(this,guruname);
            guruthread.start();
        }
         
    }
}

Output:-