You are on page 1of 40

[JAVA BEGINEER]

Java
Java is a programming language done by Sunmicro Systems which is heavily use for web applications,standard applicationetc.James Gosling is the founder of the Java.

Java Application
Applet Swing Corba Servelet

jdk Software
To work with Java programmes you need to have jdk software.

Version on jdk
1.2, 1.2.1, 1.3, 1.4, 1.5.0

Where jdk installed


If you have a jdk 1.5.0 it will create folder on C drive called Java.There you finding the Java software.

Installing jdk 1.5.0 software


jdk 1.5.0 helps you to install Java your computer system by this jdk 1.5.0 work with Java language.

Seting with the Java compliments


Go to the command prompt and seting the Java path.

Seting path temporaly


Go to the command prompt and type like this Set path=C:\Program Files\Java\jdk1.5.0\bin

Seting path permanantly


Go to the notepad and type Set path=C:\Program Files\Java\jdk1.5.0\bin and save as autoexec.bat on the C drive and restart the machine. No | 10070 1

[JAVA BEGINEER]

Checking whether path properly set


Type: - C:\path PATH= C:\Program Files\Java\jdk1.5.0\bin 1. What will happen if path will upset? the error message displaying in the command prompt as " Java cannot be recognizing as internal or external command." 2. How do I check whether Java is installed to my computer? go to the command prompt then type Java and enter.

Writing a simple Java programme

class Myfirst { public static void main(String gihan[]) { System.out.print("Hellow"); } }

Note: - When you saving your source code that need to be save by giving class name as it is and the java extention.

Java compilation project


source code object code byte code

javac classname.java

java classname

"classname.java"

classname.class

No | 10070 2

[JAVA BEGINEER]

Things to consider writing a programme


1. Java is case sensitive; you need to write capital, simple, space as it is. 2. Each an every statement needs to finish with semicolon. Ex:-System.out.print("Hellow"); 3. If you open code block ({) is need to be closed. 4. Main method is place of the programme. { public static void main(String gihan[])

Ex1:- Display your name and address. class ex1 { public static void main(String args[]) { System.out.println("Gihan Shavithra Rannulu"); System.out.println("45,Udahamulla Station Road,Delkanda,Nugegoda."); } }

print & println


print use to print in same line,println use to move to next line.

Variable
Variable is a tempory memory location created on RAM, which holds data tempory.In Java there are several types of variable.

Sytax of this type of variable


Datatype < variable name >; Ex: - int tot;

What is Datatype
Datatype is describing what sort of a data will be holding on the variable whether it is a number, character, currency values. No | 10070 3

[JAVA BEGINEER]

RAM
25 tot Sing name

Ex: - int tot; String name;

Assigning value to the variable


Sytax:-

1.datatype variable = value 2.tot = 95;


Ex2: class ex2 { public static void main(String args[]) { int num1; num1=85; System.out.print(num1); } } Ex3: class ex3 { public static void main(String args[]) { int num1,num2,tot; num1=85; num2=85; tot=num1+num2; System.out.print(tot); } No | 10070 4 Ex: - int tot=85;

[JAVA BEGINEER] }

Adding comments
Comments help you to increase the readability of the programme.

single comments
// this is single comments

multiple comments
/* this is multiple comments */ Ex4: class ex4 { //this is the main method public static void main(String args[]) { int num1; num1=95; System.out.print(num1); } } Ex5: // working with charactor values class ex5 { //this is the main method public static void main(String args[]) { char cha; //when you adding charactor value need to put single value cha='G'; System.out.print(cha); } }

Arithmatic Operators
+ - addition / - divide
No | 10070 5

[JAVA BEGINEER]

* % ++ -Ex6: -

multiply substraction modulus increment one by one decrement one by one

//multiplication class ex6 { public static void main(String args[]) { int num1=20; int num2=20; int tot; tot=num1*num2; System.out.print("This is the total "+tot); } }

Using Moduler
Ex7: class ex7 { public static void main(String args[]) { int x=100; double y=20; System.out.print("Moduler is "+y%x); } }

Datatype
int(integer) String float double

Description
use to hold use to hold & symbols use to hold use to hold whole numbers numbers,characters decimal numbers decimal numbers No | 10070 6

[JAVA BEGINEER]

Datatypes Integer
long int short byte double float char boolean 64 bites 32 bites 16 bites 8 bites 64 bites 32 bites

Floating Characters Boolean

Working with if statement


If statement use to check conditions in your programme.If the conditions satisfied you will execute the statement.

if(marks>75) { System.out.print("Pass"); }
Ex9: class ex9 { public static void main(String args[]) { int marks; marks=85; if (marks>70) { System.out.print("Pass"); } } }

No | 10070 7

[JAVA BEGINEER]

Multiple if
if(condition1) { Statement1; } else if(condition2) { Statement2; }
Ex10: - write a java application to following grading system. marks >75-D , marks>55-C , marks>45-S , else-fail class ex10 { public static void main(String gihan[]) { int marks; marks=85; if(marks>75) { System.out.print("D"); } else if(marks>55) { System.out.print("C"); } else if(marks>45) { System.out.print("S"); } else System.out.print("Fail"); } } Ex11: - If the value == to the value display "Yes". class ex11 { public static void main(String args[]) { int value; value=50; No | 10070 8

[JAVA BEGINEER] if (value==50) { System.out.print("Yes"); } } }

Not equal
Sy: Ex12: class ex12 { public static void main(String args[]) { int value; value=30; if (value!=50) { System.out.print("Not equal"); } else System.out.print("Equal"); } }

x!=20

Java Keywords
1.abstract 2.assert 3.boolean 4.break 5.byte 6.case 7.finally 8.class 9.const 10. continue 11. default 12. do 13. double 14. else 15. extends 16. final 17. catch 18. float 19. for 20. go to 21. if 22. implements
No | 10070 9

[JAVA BEGINEER]

23. imports 24. instance of 25. int 26. interface 27. long 28. native 29. new 30. private

31. protected 32. public 33. return 34. short 35. switch 36. this 37. try 38. while

Ex13: - Write an application program to calcute the total and average put the if avg is greater than 70 , display grade as Pass. class ex13 { public static void main(String args[]) { int num1=100; int num2=50; int tot; int avg; tot=num1+num2; avg=tot/2; if(avg>70) { System.out.print("Pass"); } } }

Control Statement
Control statement use to repeat the statement till the condition is satisfied.

Loops
Loops helps you to repeat the statement till the condition is satisfied.

while loop

while(condition) { Statement; }

Ex14: No | 10070 10

[JAVA BEGINEER] class ex14 { public static void main(String args[]) { int x=0; while(x<16) { System.out.print("x"); //increment the x value by one. x=x+1; } } } Ex15: - To print x in line by line type println. class ex15 { public static void main(String args[]) { int x=0; while(x<16) { System.out.println("x"); //increment the x value by one. x=x+1; } } } Ex16: - Write a program to display series of number from 1 to 10 by using while loop. class ex16 { public static void main(String args[]) { int x=0; while (x<10) { System.out.println(x); x=x+1; } } } Ex17: - Write an application program to print numbers up to 50,5 by 5. No | 10070 11

[JAVA BEGINEER] class ex17 { public static void main(String args[]) { int x=0; while(x<50) { System.out.println(x); x=x+5; } } } Ex18: - Write a application program to display the months in the year, when the month value is assign as given below. month 1,2,12-Winter , month 6,7,8-Spring ,month 9,10,11-Autom , else nothing class ex18 { public static void main(String args[]) { int month; month=10; if(month==12||month==1||month==2) { System.out.print("Winter"); } else if(month==6||month==7||month==8) { System.out.print("Spring"); } else if(month==9||month==10||month==11) { System.out.print("Autom"); } } } Ex19: - Write an application program to Log in Success if password and username both are correct. class ex19 { public static void main(String args[]) { String passwd,ud; No | 10070 12

[JAVA BEGINEER] passwd="123456"; ud="Gihan"; if(passwd=="123456"&&ud=="Gihan") { System.out.print("Log in Success"); } else System.out.print("Log in Fail"); } }

for loop

for(x=0;x<10;x++) { statement; }

Ex20: - using for loops. class ex20 { public static void main(String args[]) { for(int x=0;x<10;x++) { System.out.println("x"); } } } Ex21: - Print numbers 5 by 5 using for loops. class ex21 { public static void main(String args[]) { for (int x=0;x<50;x=x+5) { System.out.println(x); } } }

Case Statement

No | 10070 13

[JAVA BEGINEER] Case statement use to help with multiple if statement where case statements are much more easier than if conditions. Sy: -

switch(x) { case value 1; statement1; break; case value 2; statement 2; break; }

Ex22: class ex22 { public static void main(String args[]) { int days; days=3; switch(days) { case 1: System.out.print("Monday"); break; case 2: System.out.print("Tuesday"); break; case 3: System.out.print("Wednesday"); break; case 4: System.out.print("Thursday"); break; case 5: System.out.print("Friday"); break; case 6: System.out.print("Saturday"); break; case 7: System.out.print("Sunday"); } } } No | 10070 14

[JAVA BEGINEER]

Ex23: class ex23 { public static void main(String args[]) { int season; season=11; switch(season) { case 1: case 2: case 12: System.out.print("Winter"); break; case 6: case 7: case 8: System.out.print("Spring"); break; case 9: case 10: case 11: System.out.print("Autom"); } } } Ex24: - Write a for loop to display numbers from 0 to 50. class ex24 { public static void main(String args[]) { for(int x=0;x<50;x=x+5) { System.out.println(x); } } }

Arrays
Which is use to hold value temporary where it is created on RAM.where it has name,where array has a name and may dimentional.It has a name followed by a datatype. elements No | 10070 15

[JAVA BEGINEER]

index

0 1

2 3 4

Array has elements which is accessing where index.

Creating a Array
Sy: Ex: -

<array name>=new datatype [elements];


Days =new int[6];

Accessing Array Element


You can access array element by using index. sy: Ex: Ex25: class ex25 { public static void main(String args[]) { int month[]; month=new int[6]; } } Ex26: //Assigning values to the array class ex26 { public static void main(String args[]) { int month[]; month=new int[6]; month[4]=20; //how to print the array value System.out.print(month[4]); } }

array name[element]=value;
Days [5] =31;

No | 10070 16

[JAVA BEGINEER] Ex27: class ex27 { public static void main(String args[]) { //Assigning values with out indicating the elements int month[]={2,3,4,56,7,8,9,4,45}; //how to print the array value System.out.print(month[3]); } } Ex28: - Write a program to create ten elements of a integer array and store numbers in the elements. class ex28 { public static void main(String gihan[]) { int month[]; month=new int[10]; month[0]=2; month[1]=55; month[2]=10; month[3]=11; month[4]=35; month[5]=30; month[6]=15; month[7]=45; month[8]=5; month[9]=20; System.out.print(month[5]); } }

Ex29: - Create a string array and store names. class ex29 { public static void main(String args[]) { String name[]; name=new String[5]; No | 10070 17

[JAVA BEGINEER] name[0]="Ranil"; name[1]="Kamal"; name[2]="Nuwan"; name[3]="Sunil"; name[4]="Nihal"; System.out.print(name[3]); } }

Ex30: - Create a array to hold mark1, mark2, total and average and display total and average. class ex30 { public static void main(String args[]) { int value[]; value=new int[4]; value[0]=44; value[1]=54; value[2]=value[0]+value[1]; value[3]=value[2]/2; System.out.println("Total is .."+value[2]); System.out.print("Average is .."+value[3]); } }

do while
sy: -

do { statement } while(condition);

Ex31: class ex31 { No | 10070 18

[JAVA BEGINEER] public static void main(String gihan[]) { int x=0; do { //first you will execute the statement System.out.println(x); //increment x value by 1 x=x+1; } while(x<10); } }

Ex32: - Write do while loop to print numbers decending order. class ex32 { public static void main(String gihan[]) { int x=10; do { System.out.println(x); x=x-1; } while(x>0); } }

Ex33: - Write for loop to display numbers from 0 to 10 if numbers==5 exits from the loop. class ex33 { public static void main(String gihan[]) { for(int x=0;x<=10;x++) { if(x==5) break; System.out.println("Value is "+x); } } No | 10070 19

[JAVA BEGINEER] }

Ex34: - Write a program to calcute the student grading system. class ex34 { public static void main(String gihan[]) { int mk1,mk2,tot,avg; mk1=85; mk2=95; tot=mk1+mk2; avg=tot/2; if(avg>75) { System.out.print("D"); } else if(avg>55) { System.out.print("C"); } else if(avg>45) { System.out.print("S"); } else System.out.print("Fail"); } }

Ex35: class ex35 { public static void main(String gihan[]) { int x,y; for(x=0;x<10;x++) { for(y=x;y<10;y++) System.out.print("x"); No | 10070 20

[JAVA BEGINEER] System.out.println(""); } } }

Ex36: - Write a program to calcute GW of a employee.Use a array (GW=hw x hr) class ex36 { public static void main(String gihan[]) { int emp[]; int gw,hw,hr; hw=45; hr=30; emp=new int[3]; emp[0]=hw; emp[1]=hr; emp[2]=emp[0]*emp[1]; gw=emp[2]; System.out.print("GW is "+gw); } } Ex37: - Create a 10 element to store the values where for loops. class ex37 { public static void main(String gihan[]) { int num[]; num=new int[10]; for(int x=0;x<10;x++) { num[x]=x; System.out.println("Value "+num[x]); } } }

Ex38: - There is a integer array which consist of value print these value by using for loops. No | 10070 21

[JAVA BEGINEER] class ex38 { public static void main(String gihan[]) { //Assigning values to the variable int num[]={2,3,4,5,6,7,8,9}; for(int x=0;x<5;x++) { System.out.println("Value "+num[x]); } } }

Ex39: - When we haven't the length of elements. class ex39 { public static void main(String gihan[]) { int num[]={2,3,4,5,6,7,8,9}; for(int x=0;x<num.length;x++) { System.out.println("Value "+num[x]); } } }

Ex40: - Search integer array and if you found value as 0 display message as Exit. class ex40 { public static void main(String gihan[]) { int num[]={1,2,3,4,5,0,6,7,8,9}; for(int x=0;x<num.length;x++) { System.out.println("Value "+num[x]); if(num[x]==0) { System.out.print("Exit"); break; } } No | 10070 22

[JAVA BEGINEER] } }

Ex41: - Write a application to store student names and find the student called 'Sunil' and display it. class ex41 { public static void main(String gihan[]) { String name[]={"Ranil","Kamal","Gihan","Sunil","Amal"}; for(int x=0;x<name.length;x++) { System.out.println("Name is "+name[x]); if(name[x]=="Sunil") { System.out.print("He is Sunil"); break; } } } }
Ex42:-

import java.applet.Applet; import java.awt.*; public class ex42 extends Applet {


//defining variable button

Button b1; public void init() { b1=new Button("Java Programming"); add(b1); } }


Ex43:-

import java.applet.Applet; import java.awt.*; public class ex43 extends Applet { Button b1; Label l1; TextField t1; No | 10070 23

[JAVA BEGINEER] public void init() { b1=new Button("Java Programming"); add(b1); l1=new Label("Hello"); add(l1); t1=new TextField(10); add(t1); } }
Ex44:-

import java.applet.Applet; import java.awt.*; public class ex44 extends Applet { Label l1,l2; TextField t1,t2; public void init() { l1=new Label("Your Name"); add(l1); t1=new TextField(); add(t1); l2=new Label("E-mail"); add(l2); t2=new TextField(); add(t2); } }
Ex45:-

import java.applet.Applet; import java.awt.*; public class ex45 extends Applet { Label l1,l2; TextField t1,t2; public void init() { GridLayout x=new GridLayout(2,2); setLayout(x); //row1 l1=new Label("Your Name"); add(l1); t1=new TextField(); add(t1); No | 10070 24

[JAVA BEGINEER] //row2 l2=new Label("E-mail"); add(l2); t2=new TextField(); add(t2); } }


Ex46:-

import java.applet.Applet; import java.awt.*; public class ex46 extends Applet { Button b1,b2; Label l1,l2; TextField t1,t2; public void init() { GridLayout x=new GridLayout(3,2); setLayout(x); //row1 l1=new Label("Your Name"); add(l1); t1=new TextField(); add(t1); //row2 l2=new Label("E-mail"); add(l2); t2=new TextField(); add(t2); //row3 b1=new Button("Ok"); add(b1); b2=new Button("Cancel"); add(b2); } }
Ex47:-

import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class ex47 extends Applet implements ActionListener { Button b1; TextField t1; No | 10070 25

[JAVA BEGINEER] public void init() { t1=new TextField(20); add(t1); b1=new Button("Ok"); add(b1); b1.addActionListener(this); } public void actionPerformed(ActionEvent e) { if(e.getSource()==b1) { t1.setText("Hellow Java"); } } }
Ex48:-

import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class ex48 extends Applet implements ActionListener { Button b1,b2; Label l1,l2; TextField t1,t2; public void init() { GridLayout x=new GridLayout(3,2,2,2); setLayout(x); //row1 l1=new Label("Your Name"); add(l1); t1=new TextField(20); add(t1); //row2 l2=new Label("E-mail"); add(l2); t2=new TextField(20); add(t2); //row3 b1=new Button("Ok"); add(b1); b2=new Button("Clear"); add(b2); b1.addActionListener(this); b2.addActionListener(this); No | 10070 26

[JAVA BEGINEER] } public void actionPerformed(ActionEvent e) { if(e.getSource()==b1) { t1.setText("Gihan Shavithra Rannulu"); t2.setText("gsr@yahoo.com"); } if(e.getSource()==b2) { t1.setText(""); t2.setText(""); } } }
Ex49:-

import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class ex49 extends Applet implements ActionListener { Button b1,b2; Label l1,l2,l3; TextField t1,t2,t3; public void init() { GridLayout x=new GridLayout(4,2,3,3); setLayout(x); //row1 l1=new Label("Value 1"); add(l1); t1=new TextField(20); add(t1); //row2 l2=new Label("Value 2"); add(l2); t2=new TextField(20); add(t2); //row3 l3=new Label("Total"); add(l3); t3=new TextField(20); add(t3); //row4 b1=new Button("Ok"); No | 10070 27

[JAVA BEGINEER] add(b1); b2=new Button("Cancel"); add(b2); b1.addActionListener(this); b2.addActionListener(this); } public void actionPerformed(ActionEvent e) { int val1,val2,tot; if(e.getSource()==b1) { val1=Integer.parseInt(t1.getText()); val2=Integer.parseInt(t2.getText()); tot=val1+val2; t3.setText(String.valueOf(tot)); } if(e.getSource()==b2) { t1.setText(""); t2.setText(""); t3.setText(""); } } }
Ex50:-

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ex50 extends JApplet implements ActionListener { JButton b1,b2; JLabel l1,l2,l3,l4,l5,l6; JTextField t1,t2,t3,t4,t5,t6; public void init() { GridLayout x=new GridLayout(7,2,5,5); setLayout(x); //row1 l1=new Label("Student Name"); add(l1); t1=new TextField(20); add(t1); //row2 l2=new Label("Mark 1"); add(l2); t2=new TextField(20); No | 10070 28

[JAVA BEGINEER] add(t2); //row3 l3=new Label("Mark 2"); add(l3); t3=new TextField(20); add(t3); //row4 l4=new Label("Total"); add(l4); t4=new TextField(20); add(t4); //row5 l5=new Label("Average"); add(l5); t5=new TextField(20); add(t5); //row6 l6=new Label("Grade"); add(l6); t6=new TextField(20); add(t6); //row7 b1=new Button("Ok"); add(b1); b2=new Button("Cancel"); add(b2); b1.addActionListener(this); b2.addActionListener(this); } public void actionPerformed(ActionEvent e) { int mk1,mk2,tot,avg,grad; if(e.getSource()==b1) { mk1=Integer.parseInt(t2.getText()); mk2=Integer.parseInt(t3.getText()); tot=mk1+mk2; avg=tot/2; if(avg>75) { t6.setText("D"); } else if(avg>55) { t6.setText("C"); } No | 10070 29

[JAVA BEGINEER] else if(avg>45) { t6.setText("S"); } else { t6.setText("F"); } t4.setText(String.valueOf(tot)); t5.setText(String.valueOf(avg)); } if(e.getSource()==b2) { t1.setText(""); t2.setText(""); t3.setText(""); t4.setText(""); t5.setText(""); t6.setText(""); } } }
Ex51:-

import java.awt.*; import java.awt.event.*; import java.applet.Applet; public class ex51 extends Applet implements ActionListener { Button b1,b2; Label l1,l2,l3,l4; TextField t1,t2,t3,t4; public void init() { GridLayout x=new GridLayout(5,2,5,5); setLayout(x); //row 1 l1=new Label("Employee No"); add(l1); t1=new TextField(20); add(t1); //row 2 l2=new Label("Salary"); add(l2); t2=new TextField(20); add(t2); //row 3 No | 10070 30

[JAVA BEGINEER] l3=new Label("Commission"); add(l3); t3=new TextField(20); add(t3); //row 4 l4=new Label("Salary After Commission"); add(l4); t4=new TextField(20); add(t4); //row 5 b1=new Button("Click Me"); add(b1); b2=new Button("Cancel"); add(b2); b1.addActionListener(this); b2.addActionListener(this); } public void actionPerformed(ActionEvent e) { float sal,com,tosal; if(e.getSource()==b1) { sal=Float.parseFloat(t2.getText()); com=sal*10/100; tosal=sal+com; if(sal<10000) { t3.setText(String.valueOf(com)); t4.setText(String.valueOf(tosal)); } else { t3.setText("No,Commission"); t4.setText(String.valueOf(sal)); } } if(e.getSource()==b2) { t1.setText(""); t2.setText(""); t3.setText(""); t4.setText(""); } } }
Ex52:-

No | 10070 31

[JAVA BEGINEER] import java.awt.*; import javax.swing.*; public class ex52 extends JApplet { public void init() { Container c=getContentPane(); JButton b1=new JButton("OK"); c.add(b1); } }
Ex53:-

import import import public {

java.awt.*; java.awt.event.*; javax.swing.*; class ex53 extends JApplet

JButton b1,b2; JLabel l1,l2; JTextField t1,t2; public void init() { GridLayout x=new GridLayout(3,2,5,5); setLayout(x); Container c=getContentPane(); //row 1 l1=new JLabel(" Name "); c.add(l1); t1=new JTextField(20); c.add(t1); //row 2 l2=new JLabel(" E-mail c.add(l2); t2=new JTextField(20); c.add(t2); //row 3 b1=new JButton("Enter"); c.add(b1); b2=new JButton("Cancel"); No | 10070 32

");

[JAVA BEGINEER] c.add(b2); } } }


Ex54:-

import import import public {

java.awt.*; java.awt.event.*; javax.swing.*; class ex54 extends JApplet implements ActionListener

JButton b1,b2; JLabel l1,l2; JTextField t1,t2; public void init() { GridLayout x=new GridLayout(3,2,5,5); setLayout(x); Container c=getContentPane(); //row 1 l1=new JLabel(" Name "); c.add(l1); t1=new JTextField(20); c.add(t1); //row 2 l2=new JLabel(" E-mail c.add(l2); t2=new JTextField(20); c.add(t2);

");

//row 3 b1=new JButton("Enter"); c.add(b1); b1.addActionListener(this); b2=new JButton("Cancel"); c.add(b2); b2.addActionListener(this); } No | 10070 33

[JAVA BEGINEER] public void actionPerformed (ActionEvent e) { if(e.getSource()==b1) { t1.setText("Gihan Rannulu"); t2.setText("gsr@yahoo.com"); } if(e.getSource()==b2) { t1.setText(""); t2.setText(""); } } }
Ex55:-

import java.io.*; class ex55 { public static void main(String args[]) throws Exception { String str; str="Hellow Java"; byte arr[]=str.getBytes(); OutputStream x=new FileOutputStream("Java.txt"); x.write(arr); x.close(); } }
Ex56:-

import java.io.*; class ex56 { public static void main(String args[]) throws Exception { No | 10070 34

[JAVA BEGINEER] String str; int mk1,mk2,tot; mk1=85; mk2=95; tot=mk1+mk2; str=("Total is "+String.valueOf(tot)); byte arr[]=str.getBytes(); OutputStream x=new FileOutputStream("Value.txt"); x.write(arr); x.close(); } }
Ex57:-

import java.io.*; class ex57 { public static void main(String args[]) throws Exception { String str; str1="Hellow Gihan Rannulu"; byte arr[]=str.getBytes(); OutputStream x=new FileOutputStream("Total +.txt"); x.write(arr); x.close(); } }
Ex58:-

import java.io.*; class ex58 { public static void main(String args[]) throws Exception { String str; str="Hellow Gihan Rannulu"; byte arr[]=str.getBytes(); OutputStream x=new FileOutputStream("Gihan 1.txt"); OutputStream y=new FileOutputStream("Gihan 2.txt"); x.write(arr); y.write(arr); x.close(); } }

No | 10070 35

[JAVA BEGINEER]
Ex59:-

import java.io.*; class ex59 { public static void main(String args[]) { int a=0; int b=50/a; } }
Ex60:-

import java.io.*; class ex60 { public static void main(String args[]) { try { int a=0; int b=50/a; System.out.println("Can't help"); } catch(ArithmeticException e) { System.out.println("I have catch the exception"); } finally { System.out.println("Divide by 0 "); } } }
Ex61:-

class ex61 { public static void main(String args[]) { try { int a=new int[3]; int[0]="Gihan"; } catch(ArrayStoreException e) { System.out.println("I have catch the exception"); No | 10070 36

[JAVA BEGINEER] } } }
Ex62:-

class ex62 { public static void main(String args[]) { try { int a=args.length; System.out.println("a is :"+a); //arithmetic exception int b=42/a; int c[]={1}; //array exception c[49]=99; } catch(ArithmeticException e) { System.out.println("Divided by 0"); } catch(ArrayIndexOutOfBoundException e) { System.out.println("Array is out of bound"); } } }
Ex63:-

class ex63 { public static void main(String args[]) { try { int c,d; c=0; d=100/c; System.out.println("Divided by 0") } try { int a[]; a=new int[4]; a[10]=200; No | 10070 37

[JAVA BEGINEER] System.out.println("Value is :"+a[10]); } catch(ArithmeticException e) { System.out.println("I have catch the exception"); } catch(ArrayIndexOutOfBoundException e) { System.out.println("Array is out of bound"); } } }
Ex64:-

import java.awt.*; import java.applet.*; import java.net.*; public class ex64 extends Applet { public void start() { AppletContext x=getAppletContext(); URL url=getCodeBase(); try { x.showDocument(new URL(url+"test.html")); } catch(MalformedURLException e) { x.showStatus("URL is not found"); } } }
Ex65:-

//sending a message import java.net.*; import java.io.*; class client extends Thread { Socket socket; DataInputStream in; PrintStream out; String IP="127.0.0.1"; int port=8000; String message; public static void main(String args[]) No | 10070 38

[JAVA BEGINEER] { client.getConnection(); client.recieveMessage(); } public void getConnection() { try { socket=new Socket(IP,port); in=new DataInputStream(socket.getInputStream()); out=new PrintStream(socket.getOutputStream()); } catch(UnknownHostException e) { System.out.println("Client get connection"); } catch(IOException e) { System.out.println("IO Exception"); } } private void sendMessage() { message="Hello Server"; System.out.println("Client sent"+message); } public void recieveMessage() { try { while(true) { message=in.readLine(); System.out.println("Client recieved message"); } } } }

No | 10070 39

[JAVA BEGINEER]

No | 10070 40

You might also like