You are on page 1of 23

1-Installation of J2SDK

1. In your browser enter the URL: Java SE 2. In the middle column, choose: Java SE 6 Update 29 and click the Download JDK button. 3. Accept the License Agreement and choose the correct platform (Windows), and click the Download button.. You will be downloading the file jdk-6u29-windows-i586.exe. 4. Save the file (somewhere where you know where you saved it since you will need to run it). 5. Double-click the installer and follow the instructions. 6. You now need to tell your computer where it can find the import Java files. The best instructions to complete your installation can be found at the Sun site: You need to add C:\Program Files\Java\jdk1.6.0_29\bin; to the PATH variable. How you do this will depend on what operating system you are using. i) In Windows 95/98 add or change the PATH statement in the autoexec.bat file ii) PATH current items; C:\Program Files\Java\jdk1.6.0_29\bin iii) In Windows 2000 - Start, Settings, Control Panel, System, Advanced: New or Edit : iv) Add: C:\Program Files\Java\jdk1.6.0_29\bin Click OK v) In Windows XP - Start, Control Panel, double click System icon, open System Control Panel, Advanced, click "Environment Variables", highlight Path, edit, add to path: ;C:\Program Files\Java\jdk1.6.0_29\bin Click OK vi) In Windows Vista - Start, Control Panel, System and Maintenance, System, Advanced system settings, click "Environment Variables", highlight Path, edit, add to path: ;C:\Program Files\Java\jdk1.6.0_29\bin Click OK or Click Start, right-click Computer and choose Properties, Advanced system settings, click "Environment Variables", highlight Path, edit, add to path: ;C:\Program Files\Java\jdk1.6.0_29\bin Click OK vii) Windows 7 - Start, Control Panel, System and Security, System, Advanced system settings (left column), click "Environment Variables", highlight Path, edit, add to path: ;C:\Program Files\Java\jdk1.6.0_29\bin Click OK

or Click Start, right-click Computer and choose Properties, Advanced system settings, click "Environment Variables", highlight Path, edit, add to path: ;C:\Program Files\Java\jdk1.6.0_29\bin Click OK

8. You may have to reboot your computer when the installation is complete.

Test to see if your installation is working


After you've rebooted, in Notepad enter the following program:
public class hello { public static void main(String[] args) { System.out.println("Hello World!"); } }

Save it as hello.java in the C:\ directory 9. Start, run, command

10. Enter: cd .. as many time as it take to get to C:\> (this should be the folder where you stored hello.java) 11. Enter: javac hello.java There should be a slight pause and the another C:\> should appear Enter: java hello Your program should print out: Hello world! Enter: exit to close the DOS window

12. 13. 14.

2. Write a program to show Concept of class

and java.
import java.io.*; public class MainClass { public static void main( String args[] ) { Account account1 = new Account( 50.00 ); // create Account object Account account2 = new Account( -7.53 ); // create Account object System.out.printf( "account1 balance: $%.2f\n", account1.getBalance() ); System.out.printf( "account2 balance: $%.2f\n\n", account2.getBalance() ); double depositAmount; // deposit amount read from user depositAmount = 10.10; account1.credit( depositAmount ); // add to account1 balance System.out.printf( "account1 balance: $%.2f\n", account1.getBalance() ); System.out.printf( "account2 balance: $%.2f\n\n", account2.getBalance() ); depositAmount = 12.12; account2.credit( depositAmount ); // add to account2 balance System.out.printf( "account1 balance: $%.2f\n", account1.getBalance() ); System.out.printf( "account2 balance: $%.2f\n", account2.getBalance() ); } }

class Account { private double balance; // instance variable that stores the balance // constructor public Account( double initialBalance ) { if ( initialBalance > 0.0 ) balance = initialBalance; } public void credit( double amount ) { balance = balance + amount; } public double getBalance() { return balance; }}

OUTPUT

3. Write a program to show Type casting in Java.


class StringConversions { public static void main(String args[]) { int num = 19648; System.out.println(num + " in binary: " + Integer.toBinaryString(num)); System.out.println(num + " in octal: " + Integer.toOctalString(num)); System.out.println(num + " in hexadecimal: " + Integer.toHexString(num)); } }

OUTPUT:
19648 in binary: 100110011000000 19648 in octal: 46300 19648 in hexadecimal: 4cc0

4-Write a program to show the exception handling in Java.


class Exc2 {

public static void main(String args[]){ int d,a; try { d=0; a=42/d; System.out.println("This will not be printed."); } catch (ArithmeticException e) { System.out.println("Division by zero."); } System.out.println("After catch statement"); } }

OUTPUT

5-Write a program to show inheritance.


class A { int i,j; void showij() { System.out.println("i and j: " + i +" " + j); } } class B extends A { int k; void showk() { System.out.println("k:"+k); } void sum() { System.out.println("i+j+k: " + (i+j+k)); } } class SimpleInheritance { public static void main(String args[]) {

A superOb = new A(); B subOb = new B(); superOb.i = 10; superOb.j = 20; System.out.println("contents of superOb:"); superOb.showij(); System.out.println(); subOb.i=7; subOb.j=8; subOb.k=9; System.out.println("contents of subOb:"); subOb.showij(); subOb.showk(); System.out.println(); System.out.println("sum of i,j and k in subOb:"); subOb.sum(); } }

OUTPUT

6. Write a program To show polymorphism.


class Employee { public void work() { System.out.println("I am an employee."); } } class Manager extends Employee { public void work() { System.out.println("I am a manager."); } public void manage() { System.out.println("Managing ..."); } } public class PolymorphismTest1 { public static void main(String[] args) { Employee employee; employee = new Manager(); System.out.println(employee.getClass().getName()); employee.work(); Manager manager = (Manager) employee; manager.manage(); } }

OUTPUT: Manager I am a manager. Managing

7-Write a program to show Access specifier (public, private, protected) in JAVA.

class AcessTest { public static void main (String args[]) { Test ob = new Test (); ob.a = 10; ob.b = 20; ob.setc(100); System.out.println("a,b,and c:" + ob.a + ob.a +" " + ob.b + " " + ob.getc() ); } } class Test { int a; public int b; private int c; void setc (int i) {

c=i; } int getc() { return c; } }

OUTPUT

8. Write a program To show use and advantages of constructors.


class Meal { Meal() { System.out.println("Meal()"); } } class Bread { Bread() { System.out.println("Bread()"); } } class Cheese { Cheese() { System.out.println("Cheese()"); } } class Lettuce { Lettuce() { System.out.println("Lettuce()"); } } class Lunch extends Meal { Lunch() { System.out.println("Lunch()"); } } class PortableLunch extends Lunch { PortableLunch() { System.out.println("PortableLunch()"); } } class Sandwich extends PortableLunch { private Bread b = new Bread(); private Cheese c = new Cheese(); private Lettuce l = new Lettuce();

public Sandwich() { System.out.println("Sandwich()"); } } public class MainClass { public static void main(String[] args) { new Sandwich(); } }

OUTPUT: Meal() Lunch() PortableLunch() Bread() Cheese() Lettuce() Sandwich()

10. Write a program to demonstrate multi-threading using java.


class india extends Thread { public void run() { for(int i=1;i<=5;i++) { System.out.println("India"); } } } class china extends Thread { public void run() { for(int i=1;i<=5;i++) { System.out.println("China"); } }

} class russia extends Thread { public void run() { for(int i=1;i<=5;i++) { System.out.println("Russia"); } } } class thr1 { public static void main(String a[]) { india i=new india(); china c=new china(); russia r=new russia(); i.start(); c.start(); r.start(); }

You might also like