import java.awt.*;
import java.awt.event.*;
class xg2{
private Frame f;
private Button b1;
private Button b2;
private TextField t,t1;
public xg2()
{
f=new Frame("GUI Initialised");
b1=new Button(" B1");
b2=new Button(" B2");
t=new TextField(30);
t1=new TextField(30);
b2.setActionCommand(" B2");
}
public void launchframe()
{
f.setLayout(new FlowLayout(FlowLayout.CENTER,20,0)); //installs d layout mgr intu the frame
f.add(t,BorderLayout.NORTH);
f.add(t1,BorderLayout.SOUTH);
b1.addActionListener(new bh());
b2.addActionListener(new bh());
f.add(b1);
f.add(b2);
f.pack(); //neats up the window
f.setVisible(true);
}
class bh implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println("Action occurred");
String s=t.getText();
String s2=t.getText();
t1.setText(s2);
}
}
public static void main(String args[])
{
xg2 guiwindow=new xg2();
guiwindow.launchframe();
}
}
Sunday, August 26, 2007
GUI based example-2
import java.awt.*;
class xg1
{
private Frame f;
private Panel pan;
public xg1()
{
f=new Frame("GUI Initialised");
pan=new Panel();
}
public void launchframe()
{
f.setSize(200,200);
f.setBackground(Color.blue);
f.setLayout(null);
pan.setSize(100,100);
pan.setBackground(Color.yellow);
f.add(pan);
f.setVisible(true);
}
public static void main(String args[])
{
xg1 guiwindow=new xg1();
guiwindow.launchframe();
}
}
class xg1
{
private Frame f;
private Panel pan;
public xg1()
{
f=new Frame("GUI Initialised");
pan=new Panel();
}
public void launchframe()
{
f.setSize(200,200);
f.setBackground(Color.blue);
f.setLayout(null);
pan.setSize(100,100);
pan.setBackground(Color.yellow);
f.add(pan);
f.setVisible(true);
}
public static void main(String args[])
{
xg1 guiwindow=new xg1();
guiwindow.launchframe();
}
}
GUI based example-1
import java.awt.*;
public class xg
{
private Frame f;
public xg()
{
f=new Frame("GUI Support Initialised");
}
public void launchframe()
{
f.setSize(170,170);
f.setBackground(Color.black);
f.setVisible(true);
}
public static void main(String args[])
{
xg guiwindow=new xg();
guiwindow.launchframe();
}
}
public class xg
{
private Frame f;
public xg()
{
f=new Frame("GUI Support Initialised");
}
public void launchframe()
{
f.setSize(170,170);
f.setBackground(Color.black);
f.setVisible(true);
}
public static void main(String args[])
{
xg guiwindow=new xg();
guiwindow.launchframe();
}
}
Monday, August 20, 2007
Creating an Array object, for I/P of records in a Student class, using methods for the same as well as O/P
import java.io.DataInputStream;
class student1
{
String name;
String s1,s2,s3;
int roll;
void getdata(String a, int b, String c, String d, String e)
{
name=a;
roll=b;
s1=c;
s2=d;
s3=e;
}
void display()
{
System.out.println(name+"\t"+roll+"\t"+s1+"\t"+s2+"\t"+s3);
}
}
class x16
{
public static void main(String s[])
{
DataInputStream in = new DataInputStream(System.in);
student1[] ob=new student1[3]; //....creatin an array of n references
for(int y=0;y<3;y++)
{
ob[y]=new student1(); //....generating array in the Heap memory.
}
try
{
for(int i=0;i<3;i++)
{
System.out.println("Enter Name: ");
String n = in.readLine();
System.out.println("Enter Roll: ");
int r = Integer.parseInt(in.readLine());
System.out.println("Enter Subject1: ");
String sub1 = in.readLine();
System.out.println("Enter Subject2: ");
String sub2 = in.readLine();
System.out.println("Enter Subject3: ");
String sub3 = in.readLine();
ob[i].getdata(n,r,sub1,sub2,sub3);
}
}
catch(Exception e)
{}
System.out.println("Name\tRoll\tS1\tS2\tS3");
for(int x=0;x<3;x++)
{
System.out.println(ob[x].name+"\t"+ob[x].roll+"\t"+ob[x].s1+"\t"+ob[x].s2+"\t"+ob[x].s3);
}
}
}
class student1
{
String name;
String s1,s2,s3;
int roll;
void getdata(String a, int b, String c, String d, String e)
{
name=a;
roll=b;
s1=c;
s2=d;
s3=e;
}
void display()
{
System.out.println(name+"\t"+roll+"\t"+s1+"\t"+s2+"\t"+s3);
}
}
class x16
{
public static void main(String s[])
{
DataInputStream in = new DataInputStream(System.in);
student1[] ob=new student1[3]; //....creatin an array of n references
for(int y=0;y<3;y++)
{
ob[y]=new student1(); //....generating array in the Heap memory.
}
try
{
for(int i=0;i<3;i++)
{
System.out.println("Enter Name: ");
String n = in.readLine();
System.out.println("Enter Roll: ");
int r = Integer.parseInt(in.readLine());
System.out.println("Enter Subject1: ");
String sub1 = in.readLine();
System.out.println("Enter Subject2: ");
String sub2 = in.readLine();
System.out.println("Enter Subject3: ");
String sub3 = in.readLine();
ob[i].getdata(n,r,sub1,sub2,sub3);
}
}
catch(Exception e)
{}
System.out.println("Name\tRoll\tS1\tS2\tS3");
for(int x=0;x<3;x++)
{
System.out.println(ob[x].name+"\t"+ob[x].roll+"\t"+ob[x].s1+"\t"+ob[x].s2+"\t"+ob[x].s3);
}
}
}
Interpreting the Grade, (If-else Ladder)
import java.io.DataInputStream;
class x17
{
public static void main(String s[])
{
DataInputStream in = new DataInputStream(System.in);
int p;
try
{
System.out.println("Enter your percentage: ");
p=Integer.parseInt(in.readLine());
if(p>=75)
{
System.out.println("Distinction");
}
else
{
if(p>=60)
{
System.out.println("First Division");
}
else
{
if(p>=50)
{
System.out.println("Second Division");
}
else
{
System.out.println("FAIL");
}
}
}
}
catch(Exception e)
{}
}
}
class x17
{
public static void main(String s[])
{
DataInputStream in = new DataInputStream(System.in);
int p;
try
{
System.out.println("Enter your percentage: ");
p=Integer.parseInt(in.readLine());
if(p>=75)
{
System.out.println("Distinction");
}
else
{
if(p>=60)
{
System.out.println("First Division");
}
else
{
if(p>=50)
{
System.out.println("Second Division");
}
else
{
System.out.println("FAIL");
}
}
}
}
catch(Exception e)
{}
}
}
Saturday, August 18, 2007
Overriding a Function
Overriding a Function
class calculate
{
float r,l,b,a;
float p=3.14f;
void area(float d,float c)
{
l=d;
b=c;
a = (l * b )/2;
System.out.println("Area of a Triangle: " + a );
}
void area(float x)
{
r=x;
a=((p)*(r*r));
System.out.println("Area of a Circle: "+a);
}
void area(int z,float q)
{
l=z;
b=q;
a=(l*b);
System.out.println("Area of a Rectangle: "+a);
}
}
class x10
{
public static void main(String s[])
{
calculate ob=new calculate();
ob.area(30.2f,22.2f);
ob.area(4.00f);
ob.area(3,4.03f);
}
}
class calculate
{
float r,l,b,a;
float p=3.14f;
void area(float d,float c)
{
l=d;
b=c;
a = (l * b )/2;
System.out.println("Area of a Triangle: " + a );
}
void area(float x)
{
r=x;
a=((p)*(r*r));
System.out.println("Area of a Circle: "+a);
}
void area(int z,float q)
{
l=z;
b=q;
a=(l*b);
System.out.println("Area of a Rectangle: "+a);
}
}
class x10
{
public static void main(String s[])
{
calculate ob=new calculate();
ob.area(30.2f,22.2f);
ob.area(4.00f);
ob.area(3,4.03f);
}
}
Thursday, August 16, 2007
convertin d price of an item from decimal to whole number
import java.io.DataInputStream;
class Price
{
public static void main(String arg[])
{
float price=0.0f;
try
{
DataInputStream in = new DataInputStream(System.in);
System.out.println("enter the price in decimal : ");
price=Float.valueOf(in.readLine()).floatValue();
}
catch(Exception e) {}
price = price*100;
System.out.println("price in whole num is =" + (int)price);
}
}
class Price
{
public static void main(String arg[])
{
float price=0.0f;
try
{
DataInputStream in = new DataInputStream(System.in);
System.out.println("enter the price in decimal : ");
price=Float.valueOf(in.readLine()).floatValue();
}
catch(Exception e) {}
price = price*100;
System.out.println("price in whole num is =" + (int)price);
}
}
calculation of Distance using d=u*t+(a*t*t)/2 and providin user flexibility to hav itz own value for u,a,t
import java.io.DataInputStream;
class Distance
{
public static void main(String arg[])
{
float u=0.0f,a=0.0f;
int t=0;
float distance=0.0f;
int ch=0;
do
{
try
{
DataInputStream in = new DataInputStream(System.in);
System.out.println("enter the value of u,a,t ?");
u=Float.valueOf(in.readLine()).floatValue();
a=Float.valueOf(in.readLine()).floatValue();
t=Integer.parseInt(in.readLine());
}
catch(Exception e) {}
distance=u*t+(a*t*t)/2;
System.out.println("distance = " + distance);
System.out.println("1-execute");
System.out.println("2-terminate");
try
{
DataInputStream in = new DataInputStream(System.in);
ch=Integer.parseInt(in.readLine());
}
catch(Exception e) {}
}
while(ch==1);
}
}
class Distance
{
public static void main(String arg[])
{
float u=0.0f,a=0.0f;
int t=0;
float distance=0.0f;
int ch=0;
do
{
try
{
DataInputStream in = new DataInputStream(System.in);
System.out.println("enter the value of u,a,t ?");
u=Float.valueOf(in.readLine()).floatValue();
a=Float.valueOf(in.readLine()).floatValue();
t=Integer.parseInt(in.readLine());
}
catch(Exception e) {}
distance=u*t+(a*t*t)/2;
System.out.println("distance = " + distance);
System.out.println("1-execute");
System.out.println("2-terminate");
try
{
DataInputStream in = new DataInputStream(System.in);
ch=Integer.parseInt(in.readLine());
}
catch(Exception e) {}
}
while(ch==1);
}
}
Taking input frm user-----DataInputStream is a deprecated class. Use BufferedReader.
......came up with a better solution, including, user I/P, for float, integer and String as well, please......post...for other data types asap....
import java.io.*;
class inp
{
public static void main(String args[])
{
try{BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String name;
int age;
float marks;
System.out.println("Enter your name : ");
name=br.readLine();
System.out.println("Enter our age: ");
age=Integer.parseInt(br.readLine());
System.out.println("Enter your marks: ");
marks=Float.valueOf(br.readLine()).floatValue();
System.out.println("\nName : "+name+"\nAge: "+age+"\nMarks: "+marks);
}
catch(Exception e)
{}
}
}
......the older one.....
import java.io.*;
class inp
{
public static void main(String args[])
{
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String name;
System.out.println("Enter your name : ");
name=br.readLine();
System.out.println("Entered name : "+name);
}
catch(Exception e)
{}
}
}
import java.io.*;
class inp
{
public static void main(String args[])
{
try{BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String name;
int age;
float marks;
System.out.println("Enter your name : ");
name=br.readLine();
System.out.println("Enter our age: ");
age=Integer.parseInt(br.readLine());
System.out.println("Enter your marks: ");
marks=Float.valueOf(br.readLine()).floatValue();
System.out.println("\nName : "+name+"\nAge: "+age+"\nMarks: "+marks);
}
catch(Exception e)
{}
}
}
......the older one.....
import java.io.*;
class inp
{
public static void main(String args[])
{
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String name;
System.out.println("Enter your name : ");
name=br.readLine();
System.out.println("Entered name : "+name);
}
catch(Exception e)
{}
}
}
Classes
Code, showing the use of Inheritance, paramaterised constructors etc.
class staff
{
int code;
String name;
staff(int a, String b)
{
code=a;
name=b;
}
}
class t extends staff
{
String sub;
t(int a,String b,String c)
{
super(a,b);
sub=c;
}
}
class nt extends staff
{
String skill;
nt(int a,String b,String d)
{
super(a,b);
skill=d;
}
}
class x9
{
public static void main(String[] s)
{
t ob1=new t(111,"XYZ","MATH");
nt ob2=new nt(222,"ABC","XXX");
System.out.println("Teacher:-\nName: "+ob1.name+"\nCode: "+ob1.code+"\nSubject: "+ob1.sub);
System.out.println("\nTeacher:-\nName: "+ob2.name+"\nCode: "+ob2.code+"\nSubject: "+ob2.skill);
}
}
class staff
{
int code;
String name;
staff(int a, String b)
{
code=a;
name=b;
}
}
class t extends staff
{
String sub;
t(int a,String b,String c)
{
super(a,b);
sub=c;
}
}
class nt extends staff
{
String skill;
nt(int a,String b,String d)
{
super(a,b);
skill=d;
}
}
class x9
{
public static void main(String[] s)
{
t ob1=new t(111,"XYZ","MATH");
nt ob2=new nt(222,"ABC","XXX");
System.out.println("Teacher:-\nName: "+ob1.name+"\nCode: "+ob1.code+"\nSubject: "+ob1.sub);
System.out.println("\nTeacher:-\nName: "+ob2.name+"\nCode: "+ob2.code+"\nSubject: "+ob2.skill);
}
}
Java Programs @ Lab session (2007)
Program, an example on how to accept user I/P......using DataInputStream
import java.io.DataInputStream
class x2
{
public static void main(String s[])
{
float price=0.0f;
float nprice=0.0f;
try
{
DataInputStream in =DataInputStream(System.in);
System.out.println("Number: ");
price=Float.valueOf(in.readline()).floatValue();
}
catch(exception e)
{
newprice=(price*100);
System.out.println(nprice);
}
}
}
Code related to the concept of Branching and Array
CODE-1
class x5
{
public static void main(String[] s)
{
int m[]={30,67,56,39,89,54,90,95,76,98};
int c1=0,c2=0,c3=0,c4=0;
for(int x=0;x<=9;x++)
{
if(m[x]>=0 && m[x]<=40)
{ c1++; }
else
{
if(m[x]>=41 && m[x]<=60)
{ c2++; }
else
{
if(m[x]>=61 && m[x]<=80)
{ c3++; }
else { c4++; }
}
}
}
System.out.println("No. of students scoring marks b/w 0-40: "+c1);
System.out.println("No. of students scoring marks b/w 41-60: "+c2);
System.out.println("No. of students scoring marks b/w 61-80: "+c3);
System.out.println("No. of students scoring marks b/w 81-100: "+c4);
}
}
CODE-2
class x6
{
public static void main(String s[])
{
int c=50,p=67,m=100;
int tl=c+p+m;
if(tl>=200)
{
if(m>=60 &&amp; p>=50 && c>=40)
{
System.out.println("Admission granted");
System.out.println("Total="+tl);
System.out.println("Maths="+m);
System.out.println("Physics="+p);
System.out.println("Chemistry="+c);
}
else
{
System.out.println("Marks obtained insufficient");
}
}
else
{
System.out.println("Admission denied");
}
}
}
Exception Handling, as well as Command Line Argument passing
class x7
{
public static void main(String[] s)
{
try
{
int x=0,y=0;
int a=0,b=0,c=0,d=0,m=0,n=0;
a=Integer.parseInt(s[0]);
b=Integer.parseInt(s[1]);
c=Integer.parseInt(s[2]);
d=Integer.parseInt(s[3]);
m=Integer.parseInt(s[4]);
n=Integer.parseInt(s[5]);
System.out.println("val: "+a+b+c+d+m);
x=((m*d)-(b*n)/(a*d)-(c*b));
y=((n*a)-(m*c)/(a*d)-(c*b));
System.out.println("O/P: x: "+x);
}
catch(Exception e)
{
if(((a*d)-(c*b))==0)
{
System.out.println("Divisor cannot be ZERO/0");
}
}
}
}
import java.io.DataInputStream
class x2
{
public static void main(String s[])
{
float price=0.0f;
float nprice=0.0f;
try
{
DataInputStream in =DataInputStream(System.in);
System.out.println("Number: ");
price=Float.valueOf(in.readline()).floatValue();
}
catch(exception e)
{
newprice=(price*100);
System.out.println(nprice);
}
}
}
Code related to the concept of Branching and Array
CODE-1
class x5
{
public static void main(String[] s)
{
int m[]={30,67,56,39,89,54,90,95,76,98};
int c1=0,c2=0,c3=0,c4=0;
for(int x=0;x<=9;x++)
{
if(m[x]>=0 && m[x]<=40)
{ c1++; }
else
{
if(m[x]>=41 && m[x]<=60)
{ c2++; }
else
{
if(m[x]>=61 && m[x]<=80)
{ c3++; }
else { c4++; }
}
}
}
System.out.println("No. of students scoring marks b/w 0-40: "+c1);
System.out.println("No. of students scoring marks b/w 41-60: "+c2);
System.out.println("No. of students scoring marks b/w 61-80: "+c3);
System.out.println("No. of students scoring marks b/w 81-100: "+c4);
}
}
CODE-2
class x6
{
public static void main(String s[])
{
int c=50,p=67,m=100;
int tl=c+p+m;
if(tl>=200)
{
if(m>=60 &&amp; p>=50 && c>=40)
{
System.out.println("Admission granted");
System.out.println("Total="+tl);
System.out.println("Maths="+m);
System.out.println("Physics="+p);
System.out.println("Chemistry="+c);
}
else
{
System.out.println("Marks obtained insufficient");
}
}
else
{
System.out.println("Admission denied");
}
}
}
Exception Handling, as well as Command Line Argument passing
class x7
{
public static void main(String[] s)
{
try
{
int x=0,y=0;
int a=0,b=0,c=0,d=0,m=0,n=0;
a=Integer.parseInt(s[0]);
b=Integer.parseInt(s[1]);
c=Integer.parseInt(s[2]);
d=Integer.parseInt(s[3]);
m=Integer.parseInt(s[4]);
n=Integer.parseInt(s[5]);
System.out.println("val: "+a+b+c+d+m);
x=((m*d)-(b*n)/(a*d)-(c*b));
y=((n*a)-(m*c)/(a*d)-(c*b));
System.out.println("O/P: x: "+x);
}
catch(Exception e)
{
if(((a*d)-(c*b))==0)
{
System.out.println("Divisor cannot be ZERO/0");
}
}
}
}
Subscribe to:
Posts (Atom)