*Program of Area With Multiple Classes*
class Room { float length,breadth; +void getdata(float a, float b) { length =a; breadth=b; } } class RoomArea { public static void main(String args[]) { float area; Room room1=new Room(); [Link](14,10); area=[Link]*[Link]; [Link]("Area : -\n"+area); } }
Output: Area: -140.0
* Program of Java using Command line Arguments*
class ComLineTest { public static void main (String args[]) { int count, i=0; String string; count=[Link]; [Link]("Number of arguments:- " + count); while(i< count) { string=args[i]; i=i+1; [Link] (i+ " : " + "Java is" + string+ "!"); } } }
Output: Number of arguments: - 0
* Program of Reading Data from Keyboard*
import [Link].*; class JAVA_005 { public static void main(String[] args) { DataInputStream Input=new DataInputStream([Link]); String N; String Str; try { [Link]("Enter an Integer value: -\n "); N=[Link]( ); [Link]("Enter a String value: - \n"); Str=[Link]( ); int n=[Link](N); [Link]("\nThe Integer value is : " + n); [Link]("\nThe String value is : " + Str); } catch(IOException e) { [Link](e); } } }
Output: Enter an Integer value: 10 Enter a String value: 50 The Integer value is 10 The String value is 50
* Program of Display numbers in the form of Triangle*
class Displaying { public static void main(String args[]) { int i,j; for(i=1;i<=9;i++) { for(j=1;j<=i;j++) { [Link](+i); } [Link]("\n"); } } }
Output: 1 22 333 4444 55555 666666 7777777 88888888 999999999
* Program of Odd_Even using If Statement*
import [Link].*; class Even_Odd { public static void main(String[] args) { DataInputStream Input=new DataInputStream([Link]); String N; try { [Link]("Enter the Number: - "); N=[Link]( ); int n=[Link](N); if((n%2)==0) [Link]("\nThis Number is Even \n"); if((n%2)==1) [Link]("\nThis Number is ODD \n" ); } catch(IOException e) { [Link](e); } } }
Output: Enter the Number: 10 This Number is Even Enter the Number: 3 This Number is Odd
* Program of find Odd_Even using If-Else Statement*
class ifelse { public static void main(String args[]) { int number[]={50,65,56,71,81}; int even=0,odd=0; for(int i=0;i<[Link]; i++) { if((number[i]%2)==0) { even+=1; } else { odd+=1; } } [Link]("Even Numbers: -\n"+even+"\nOdd Numbers: -\n"+odd); } }
Output: Even Numbers: 2 Odd Numbers: 3
Program of Displaying largest Number using Nesting IfElse Statement*
class Nested_ifelse { public static void main(String[] args) { int a=325,b=712,c=478; [Link]("Largest Value is: -\n"); if(a>b) { if(a>c) { [Link](a); } else { [Link](c); } } else if(c>b) { [Link](c); } else { [Link](b); } } }
Output: Largest Value is: 712
* Program to find Divisons using Else-If Ladder Statement*
class elseifladder { public static void main(String args[]) { int roll_number[]={111,222,333,444}; int mar4s[]={81,75,43,58}; for(int i=0;i<roll_number.length;i++) { if(marks[i]>79) [Link](roll_number[i]+""+"Honours"); else if(marks[i]>59) [Link](roll_number[i]+"Ist Divison"); else if(marks[i]>49) [Link](roll_number[i]+"IInd Division"); else [Link](roll_number[i]+"Fail"); } } }
Output: 111 Honours 222 Ist Divison 333 Fail 444 IInd Divison
Program using Switch Statement*
class cityguide { public static void main(String args[]) { char choice; [Link]("Select your choice"); [Link]("M-> Madras"); [Link]("B-> Bombay"); [Link]("C-> Cacutta"); [Link]("Choice --->"); [Link](); try { switch(choice=(char)[Link]()) { case 'M': case 'm': [Link]("Madras: Booklet 5"); break; case 'B': case 'b': [Link]("Bombay: Booklet 9"); break; case 'C': case 'c': [Link]("Calcutta: Booklet 15"); break; default: [Link]("Invalid Choice (IC)"); } } catch(Exception e) { [Link]("I/O Error"); } } }
Output: M-> Madras B-> Bombay C-> Cacutta Choice ---> M Madras: Booklet 5
* Program to show the sum of first 20 integers using For-statement*
public class JAVA_027 { public static void main(String[] args) { int limit=20; int sum=0; for(int i=1;i<=limit;i++) sum+=i; [Link]("Sum of first 20 integers: -\n " + sum); } }
Output: Sum of first 20 integers: 210
*Program to show the entered text using While Statement*
class whiletest { public static void main(String args[]) { StringBuffer string = new StringBuffer(); char c; [Link]("Enter a String: -\n"); try { while((c=(char)[Link]())!='\n') { [Link](c); } } catch (Exception e) { [Link]("Error in Input"); } [Link]("You have entered......\n\n"); [Link](string); } }
Output: Enter a String: MANPREET KAUR You have entered.. MANPREET KAUR
*A Program to show the sum of 20 integers using DoWhile Statement*
public class dowhile { public static void main(String[] args) { int limit=20; int sum=0; int i=1; do { sum+=i; i++; } while(i<=limit); [Link]("Sum of first 20 integers : -\n " + sum); } }
Output: Sum of first 20 integers: 210
*A Program to show the Factorial using Nested loops*
public class JAVA_030 { public static void main(String[] args) { long limit=5; long factorial=1; for(int i=1;i<=limit;i++) { factorial=1; for(int j=2;j<=i;j++) factorial*=j; [Link]("Factorial of " + i + " = " + i + "! = " + factorial); } } }
Output: Factorial of 1 = 1! = 1 Factorial of 2 = 2! = 2 Factorial of 3 = 3! = 6 Factorial of 4 = 4! = 24 Factorial of 5 = 5! = 120
*A
Program of Rectangle using classes & objects*
class Rectangle { int length,width; void getdata(int x,int y) { length=x; width=y; } int rectarea() { int area=length*width; return(area); } } class rectarea1 { public static void main(String args[]) { int area1,area2; Rectangle rect1=new Rectangle(); Rectangle rect2=new Rectangle(); [Link]=15; [Link]=10; area1=[Link]*[Link]; [Link](20,12); area2=[Link](); [Link]("Area1: -\n"+area1); [Link]("Area2: -\n"+area2); } }
Output: Area1: 150 Area2: 240
*A Program of using Constructors in Java*
class Rectangle { int length,width; Rectangle(int x,int y) { length=x; width=y; } int rectarea() { return(length*width); } } class Rectanglearea { public static void main(String args[]) { Rectangle rect1=new Rectangle(15,10); int area1=[Link](); [Link]("Area1: -\n"+area1); } }
Output: Area1: 150
*A Program of Method Overloading in Java*
class Room { int length,breadth; Room(int x,int y) { length=x; breadth=y; } Room(int x) { length=breadth=x; } int area() { return(length*breadth); } } class Roomarea { public static void main(String args[]) { Room room1=new Room(25,15); Room room2=new Room(20); int area1=[Link](); int area2=[Link](); [Link]("Without Overloaded value is\n"+area1); [Link]("With Overloaded value is\n"+area2); } }
Output: Without Overloaded value is 375 With Overloaded Value is 400
* A Program of defining & using Static Members*
class Mathoperation { static int mul(int x,int y) { return (x*y); } static int divide(int x,int y) { return x/y; } } class mathapplication { public static void main(String args[]) { int a=[Link](4,5); int b=[Link](a,2); [Link]("B: -\n"+b); } }
Output: B: 10
*A Program of Single Inheritance*
class Room { int length; int breadth; Room(int x,int y) { length=x; breadth=y; } int area() { return(length*breadth); } } class bedroom extends Room { int height; bedroom(int x,int y,int z) { super(x,y); height=z; } int volume() { return (length*breadth*height); } } class inhertest { public static void main(String args[]) { bedroom room1=new bedroom(14,12,10); int area1=[Link](); int volume1=[Link](); [Link]("Area1: -\n"+area1); [Link]("Volume: -\n"+volume1); } }
Output: Area1: 168 Volume: 1680
*A Program of Method Overriding*
class Super { int x; Super(int x) { this.x=x; } void display() { [Link]("Super x="+x); } } class Sub extends Super { int y; Sub (int x,int y) { super(x); this.y=y; } void display() { [Link]("Super x: -\n"+x); [Link]("Sub y: -\n"+y); } } class OverRideTest { public static void main(String args[]) { Sub s1=new Sub(100,200); [Link](); } }
Output: Super x: 100 Sub y: 200
*A Program of Sorting a list of numbers*
class sorting { public static void main(String args[]) { int no[]={55,40,80,65,71}; int n=[Link]; for(int i=0;i<n;i++) { [Link](""+no[i]); } for(int i=0;i<n;i++) { for(int j=i+1;j<n;j++) { if(no[i]<no[j]) { int temp=no[i]; no[i]=no[j]; no[j]=temp; } } } [Link]("Sorted List\n"); for(int i=0;i<n;i++) { [Link](""+no[i]); } [Link](""); }
Output: 55 40 80 65 71 Sorted List 80 71 65 55 40
*A Program of Sorting a list of numbers*
public class two_d { public static void main(String[] args) { int[][] array={ {10,-1,28,13,44} , {5,36,97,-18,11} }; [Link]("The contents of the 2D Array are : \n"); for(int i=0;i<[Link];i++) { [Link]("Dimension # " + (i+1)); for(int j=0;j<array[i].length;j++) [Link]("\t Array[" + i + "][" + j + "] = " + array[i][j]); } } }
Output: Dimension # 1 Array[0][0]=10 Array[0][1]=-1 Array[0][1]=28 Array[0][1]=13 Array[0][1]=44 Dimension # 2 Array[1][0]=5 Array[1][1]=36 Array[1][2]=97 Array[1][3]=-18 Array[1][4]=11
*A Program of Alphabetically ordering of Strings*
class Stringordering { static String name[]={"Madras","Delhi","Ahmedabad","Calcutta","Bombay"}; public static void main(String args[]) { int size=[Link]; String temp=null; for(int i=0;i<size;i++) { for(int j=i+1;j<size;j++) { if(name[j].compareTo(name[i])<0) { temp=name[i]; name[i]=name[j]; name[j]=temp; } } } for(int i=0;i<size;i++) { [Link](name[i]); } } }
Output: Ahmedabad Bombay Calcutta Delhi Madras
*A Program of implementing Interfaces*
Interface Area { final static float pi=3.14F; 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); } } class InterfaceTest { public static void main(String args[]) { Rectangle rect=new Rectangle(); Circle cir=new Circle(); Area area; area=rect; [Link]("Area of Rectangle: -\n"+[Link](10,20)); area = cir; [Link]("Area of Circle: \n"+[Link](10,0)); } }
Output: Area of Rectangle: 200.0 Area of Circle: 314.0
*A Program of Implementing Multiple Inheritance*
class student { int rollnumber; void getnumber(int n) { rollnumber=n; } void putnumber() { [Link]("Roll No"+rollnumber); } } class test extends student { float part1,part2; void getmarks(float m1, float m2) { part1=m1; part2=m2; } void putmarks() { [Link]("Marks Obtained: -\n"); [Link]("part 1: -\n"+part1); [Link]("part 2: -\n"+part2); } } interface sports { float sportwt=6.0F; void putwt(); } class Results extends test implements sports { float total; public void putwt() { [Link]("Sports WT: -\n"+sportwt); } void display() { total =part1+part2+sportwt; putnumber();
putmarks(); putwt(); [Link]("Total Score: -\n"+total); } } class hybrid { public static void main(String args[]) { Results student1=new Results(); [Link](1234); [Link](27.5F,33.0F); [Link](); } }
Output: part 1: 27.5 part 2: 33.0 Sports WT: 6.0 Total score: 66.5
*A Program of Abstract class*
abstract class A { abstract void callme(); void callmetoo() { [Link]("This is a concrete method\n"); } } class B extends A { void callme() { [Link]("B is implementation of callme"); } } class abstract_demo { public static void main(String args[]) { B b=new B(); [Link](); [Link](); } }
Output: B is implementation of Callme This is a concrete method
*PROGRAM TO FIND THE AREA OF RECTANGLE USING ABSTRACT CLASSES*
abstract class Figure { double dim1,din2; Figure(double a,double b) { dim1=a; dim2=b; } abstract double area(); { class RectangleArea() extends Figure { Rectangle (double a,double b) { super(a,b); } double area() { [Link]("Inside area for rectangle"); return dim1*dim2; } } } class Triangle extends Figure { Triangle(double a,double b) { super (a,b); } double area() { [Link]("Inside area for Triangle"); return (dim1*dim2)/2; } } class AbstractArea { public static void main(String args[]) Rectangle r= new Rectangle(9,5); Triangle t= new Triangle(10,8); Figure figref; Figref=r
[Link]("area is"+[Link][]); Figref=t; [Link]("area is"+[Link]()); } }
OUTPUT:-
*A Program of Casting Operation*
class casting { public static void main(String args[]) { float sum; int i; sum=0.0F; for(i=1;i<=10;i++) { sum=sum+1/(float)i; [Link]("I: - "+i+"\n"); [Link]("Sum: - "+sum+"\n"); } } }
Output: I: - 1 I: - 2 I: - 3 I: - 4 I: - 5 I: - 6 I: - 7 I: - 8 I: - 9 I: - 10 Sum: - 1 Sum: - 1.5 Sum: - 1.83333 Sum: - 2.08333 Sum: - 2.2833 Sum: - 2.45 Sum: - 2.59286 Sum: - 2.71786 Sum: - 2.82897 Sum: - 2.92897
*A Program of Nesting of Methods*
class nesting { int m,n; nesting(int x,int y) { m=x; n=y; } int largest() { if(m>=n) return(m); else return(n); } void display() { int large=largest(); [Link]("Largest value: -\n"+large); } } class nestingtest { public static void main(String args[]) { nesting nest=new nesting(50,40); [Link](); } }
Output: Largest value: 50
*A Program of using Increment Operator*
class incrementoperator { public static void main(String args[]) { int m=10,n=20; [Link]("M: - "+m); [Link]("n: - "+n); [Link]("++m: - "+ ++m); [Link]("n++: - "+n++); [Link]("m: - "+m); [Link]("n: - "+n); } }
Output: M: - 10 n: - 20 ++m: - 11 n++: - 20 m: -11 n: - 21
*A Program of creating an Applet*
import [Link].*; import [Link].*; public class hellojava extends Applet { String str; public void init() { str=getParameter("String"); if(str==null) str = "Java"; str="Hello"+str; } public void paint (Graphics g) { [Link](str,10,100); } }
Output: Appletviewer [Link] Applet
HelloJava
Applet started
*A Program of Starting A Thread Using Runnable*
class FooRunnable implements Runnable { public void run() { for(int x =1; x < 6; x++) { [Link]("Runnable running"); } } } public class TestThreads { public static void main (String [] args) { FooRunnable r = new FooRunnable(); Thread t = new Thread(r); [Link](); } }
Output: % java TestThreads Runnable running Runnable running Runnable running Runnable running Runnable running
*A Program of Implementing Inner Classes*
class MyInner { public void seeOuter() { [Link]("Outer x is " + x); [Link]("Inner class ref is " + this); [Link]("Outer class ref is " + [Link]); } } class MyOuter { private int x = 7; public void makeInner() { MyInner in = new MyInner(); [Link](); } class MyInner public void seeOuter() { [Link]("Outer x is " + x); [Link]("Inner class ref is " + this); [Link]("Outer class ref is " + [Link]); } } public static void main (String[] args) { [Link] inner = new MyOuter().new MyInner(); [Link](); } }
Output: Outer x is 7 Inner class ref is MyOuter$MyInner@113708 Outer class ref is MyOuter@33f1d7
*A Program of Handling Exceptions*
import [Link].*; public class ReadData { public static void main(String args[]) { try { RandomAccessFile raf = new RandomAccessFile("[Link]", "r"); byte b[] = new byte[1000]; [Link](b, 0, 1000); } catch(FileNotFoundException e) { [Link]("File not found"); [Link]([Link]()); [Link](); } catch(IOException e) { [Link]("IO Error"); System. err. println(e. toString()); [Link](); } } }
Output: This will handle different exceptions
*A Program of implementing a package*
package package1; public class classA { public void displayA() { [Link](Class A); } } //Now save it to destination with folder name package1 import [Link]; class PackageTest { public static void main(String args[]) { classA objectA=new classA(); [Link](); } }
Output: Class A
*A Program of Creating file through file object*
import [Link].*; class Writer1 { public static void main(String [] args) { try { boolean newFile = false; File file = new File; [Link]([Link]()); newFile = [Link](); [Link](newFile); [Link]([Link]()); } catch(IOException e) { } } }
Output: false true true And also produces an empty file in your current directory. If you run the code a second time you get the output true false true