Pages

Monday, January 30, 2017

Java Program based on layout manager

Hello Friends,Today we learn advance java program.Today I am taking an example of how to create container and create components like label,textbox and button and set their layout.

Java program based on layout manager example
import java.awt.*;
import javax.swing.*;
public class example extends JFrame
{
JTextField t1;
    JButton b1;
JLabel l1;
  
public example() throws Exception
    {
//creating container
      Container c=this.getContentPane();
c.setLayout(null);

                //creating textfield and set their layout
t1=new JTextField();
t1.setBounds(200,100,130,20);

                 //creating label and set their layout
l1= new JLabel("what is your name");
l1.setBounds(50,100,250,30);

                 //creating button and set their layout
    b1=new JButton("Save");
b1.setBounds(70,200,120,42);


c.add(t1);
c.add(l1);
c.add(b1);
    }
      
public  static void main(String args[]) throws Exception
{
example obj=new example();
obj.setTitle("Layout example");
obj.setSize(500,300);
obj.setLocationRelativeTo(null);
obj.setVisible(true);
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}//End of main
}

OUTPUT


Wednesday, January 25, 2017

JAVA PROGRAM TO FIND SUMMATION OF TWO COMPLEX NUMBER USING STATIC FUNCTION AND MEMBER DATA

Hello Friends,Welcome back to Technology learning Raj Roushan after such a long time.
Today I am telling about java program by requesting by my friends.Today i am going to tell you how to write java program to find summation of two complex number Using static method and member data.And why static function can not access the non-static member variable of class.

Java Program

import java.io.*;
class complex
{
int r,i;
int relno,imgno; //this is not accessible by accessible by static class this will generate an error
static int relno,imgno;//this is accessible because static function can access static data
complex()
{
r =0;
i=0;
}
public static void sum(complex c,complex d)
{
relno=c.r+d.r;
imgno=c.i+d.i;
System.out.println("The sum is "+relno+"+"+imgno+"i");
}
public void read()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("\nEnter the real part");
r=Integer.parseInt(br.readLine());
System.out.println("\nEnter the imaginary part");
i=Integer.parseInt(br.readLine());
System.out.println("\nYou have entered : "+r+"+"+i+"i");
}
public static void main(String s[])throws IOException
{
complex c=new complex();
complex d=new complex();
System.out.println("First number");
c.read();
System.out.println("\nSecond number");
d.read();
/*complex add=new complex();
add.sum(c,d);    this is wrong because object is not required for calling static function i will explain in below*/
sum(c,d);
}
}
The output


non-static member variables of a class always belong to an object – meaning that every object has it’s own personal copy of non-static member variables (also known as instance variables). And, static functions have no object to work with since they belong to the class as a whole. Remember that you can call a static function without an object – and for that exact reason a static function can not call instance variables.