Anda di halaman 1dari 14

SIMPLE JAVA PROGRAM

import java.lang.Math; import java.io.*; class SquareRoot { public static void main(String args[]) { double x=16; double y; y=Math.sqrt(x); System.out.println("Y="+y); } }

CLASS WITH OBJECTS import java.io.*; class Room { float l; float b; void getdata(float x,float y) { l=x; b=y; }
1

} class RoomArea { public static void main(String args[]) { float area; Room room1=new Room(); room1.getdata(14,10); area=room1.l*room1.b; System.out.println("area="+area); } }

PACKAGES In file Balance of mypack folder package mypack; public class Balance { String name; double bal; public Balance(String n, double b) { name =n; bal=b;
2

} public void show() { System.out.println("name="+name); System.out.println("bal="+bal); } } In TestBalance file outside the mypack folder import mypack.*; import java.io.*; class TestBalance { public static void main(String args[]) { Balance t; t = new Balance("a",123.23); t.show(); } } EXCEPTIONAL HANDLING One try and one catch class Exception {

public static void main(String args[]) { int a=10; int b=5; int c=5; int x,y; try { x=a/(b-c); } catch(ArithmeticException e) { System. out.println("Division by zero"); } y=a/(b+c); System.out.println("y="+y); } } Nested try block class NestedTry { public static void main(String args[]) { try
4

{ int a=10; int b=5; int c=5; int x,y; try { x=a/(b-c); } catch(ArithmeticException e) { System.out.println("division by zero"); } y=a/(c+b); System.out.println("y="+y); } catch(ArrayStoreException e) { System.out.println("wrong"); } } } Throwing own exceptions import java.lang.Exception;
5

class MyException extends Exception { MyException(String message) { super(message); } } class TestMyException { public static void main(String args[]) { int x=5,y=1000; try { float z=(float)x/(float)y; if(z<0.01) { throw new MyException("number is too small"); } } catch(MyException e) { System.out.println("caught my exception"); System.out.println("e.getMessage()");
6

} finally { System.out.println("i am always here"); } } } JAVA IO Reading/writing characters Create a file input.txt before compiling the below program. After compiling and execution the program the file output.txt will be created and contains content of input.txt.

import java.io.*; class CopyCharacters { public static void main(String args[]) { File inFile=new File("input.txt"); File outFile=new File("output.txt"); FileReader ins=null; FileWriter outs=null; try { ins=new FileReader (inFile); outs=new FileWriter(outFile);
7

int ch; while((ch=ins.read())!=-1) { outs.write(ch); } } catch( IOException e) { System.out.println(e); System.exit(-1); } finally { try { ins.close(); outs.close(); } catch(IOException e) { } } } }
8

Writing bytes to a file Create a file called city.txtbefore executing the program. After execution the bytes will be written to file city.txt.

import java.io.*; class WriteBytes { public static void main(String args[]) { byte cities[]={'d','e','l','h','i'}; FileOutputStream outfile=null; try { outfile=new FileOutputStream("city.txt"); outfile.write(cities); outfile.close(); } catch(IOException ioe) { System.out.println(ioe); System.exit(-1); } } }

Reading bytes from a file Type the following after compiling the ReadBytes.java file java ReadBytes city.txt the output will be the content of a file city.txt

import java.io.*; class ReadBytes { public static void main(String args[]) { FileInputStream infile=null; int b; try { infile=new FileInputStream(args[0]); while((b=infile.read())!= -1) { System.out.println((char)b); } infile.close(); } catch(IOException ioe) {
10

System.out.println(ioe); } } } INTERFACES interface Area { final static float pi =22/7; float compute(float x,float y); } class Rectangle implements Area { public float compute(float x,float y) { return(x*y); } } class Circle implements Area { public float compute(float x,float y) { return(pi*x*x); } }
11

class InterfaceTest { public static void main(String args[]) { Rectangle rect=new Rectangle(); Circle cir=new Circle(); Area area ; area=rect; System.out.println("Area of rectangle="+area.compute(10,20)); area=cir; System.out.println("Area of rectangle="+area.compute(10,0)); } } INHERITANCE class student { int rollnumber; void getNumber(int n) { rollnumber=n; } void putnumber( ) { System.out.println("Roll no : " + rollnumber);
12

} } class test extends student { float part1, part2; void getmarks(float m1,float m2) { part1=m1; part2=m2; } void putmarks( ) { System.out.println("marks obtained"); System.out.println("part 1="+part1); System.out.println("part 2="+part2); } } interface sports { float sportwt=6.0f; void putwt(); } class Results extends test implements sports {
13

float total; public void putwt() { System.out.println(" sport wt="+sportwt); } void display ( ) { total=part1+part2; putnumber(); putmarks(); putwt(); System.out.println("Total score="+total); } } class Hybrid { public static void main(String args[]) { Results student1=new Results(); student1.getNumber(1234); student1.getmarks(27.5f,33.0f); student1.display(); } }
14

Anda mungkin juga menyukai