Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
456 views

Java Programs

This document contains summaries of multiple Java programs that demonstrate various programming concepts like classes and objects, methods, inheritance, arrays, sorting, and more. The programs cover concepts like calculating area and volume, reading input from the user, displaying output in different formats, and performing operations like sorting and ordering. Overall the document shows examples of how to apply different Java programming concepts to solve problems.

Uploaded by

Sunny Chauhan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
456 views

Java Programs

This document contains summaries of multiple Java programs that demonstrate various programming concepts like classes and objects, methods, inheritance, arrays, sorting, and more. The programs cover concepts like calculating area and volume, reading input from the user, displaying output in different formats, and performing operations like sorting and ordering. Overall the document shows examples of how to apply different Java programming concepts to solve problems.

Uploaded by

Sunny Chauhan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 38

*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(); room1.getdata(14,10); area=room1.length*room1.breadth; System.out.println("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=args.length; System.out.println("Number of arguments:- " + count); while(i< count) { string=args[i]; i=i+1; System.out.println (i+ " : " + "Java is" + string+ "!"); } } }

Output: Number of arguments: - 0

* Program of Reading Data from Keyboard*


import java.io.*; class JAVA_005 { public static void main(String[] args) { DataInputStream Input=new DataInputStream(System.in); String N; String Str; try { System.out.println("Enter an Integer value: -\n "); N=Input.readLine( ); System.out.println("Enter a String value: - \n"); Str=Input.readLine( ); int n=Integer.parseInt(N); System.out.println("\nThe Integer value is : " + n); System.out.println("\nThe String value is : " + Str); } catch(IOException e) { System.err.println(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++) { System.out.print(+i); } System.out.print("\n"); } } }

Output: 1 22 333 4444 55555 666666 7777777 88888888 999999999

* Program of Odd_Even using If Statement*


import java.io.*; class Even_Odd { public static void main(String[] args) { DataInputStream Input=new DataInputStream(System.in); String N; try { System.out.println("Enter the Number: - "); N=Input.readLine( ); int n=Integer.parseInt(N); if((n%2)==0) System.out.println("\nThis Number is Even \n"); if((n%2)==1) System.out.println("\nThis Number is ODD \n" ); } catch(IOException e) { System.err.println(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<number.length; i++) { if((number[i]%2)==0) { even+=1; } else { odd+=1; } } System.out.println("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; System.out.println("Largest Value is: -\n"); if(a>b) { if(a>c) { System.out.println(a); } else { System.out.println(c); } } else if(c>b) { System.out.println(c); } else { System.out.println(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) System.out.println(roll_number[i]+""+"Honours"); else if(marks[i]>59) System.out.println(roll_number[i]+"Ist Divison"); else if(marks[i]>49) System.out.println(roll_number[i]+"IInd Division"); else System.out.println(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; System.out.println("Select your choice"); System.out.println("M-> Madras"); System.out.println("B-> Bombay"); System.out.println("C-> Cacutta"); System.out.println("Choice --->"); System.out.flush(); try { switch(choice=(char)System.in.read()) { case 'M': case 'm': System.out.println("Madras: Booklet 5"); break; case 'B': case 'b': System.out.println("Bombay: Booklet 9"); break; case 'C': case 'c': System.out.println("Calcutta: Booklet 15"); break; default: System.out.println("Invalid Choice (IC)"); } } catch(Exception e) { System.err.println("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; System.out.println("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; System.out.println("Enter a String: -\n"); try { while((c=(char)System.in.read())!='\n') { string.append(c); } } catch (Exception e) { System.out.println("Error in Input"); } System.out.println("You have entered......\n\n"); System.out.println(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); System.out.println("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; System.out.println("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(); rect1.length=15; rect1.width=10; area1=rect1.length*rect1.width; rect2.getdata(20,12); area2=rect2.rectarea(); System.out.println("Area1: -\n"+area1); System.out.println("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=rect1.rectarea(); System.out.println("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=room1.area(); int area2=room2.area(); System.out.println("Without Overloaded value is\n"+area1); System.out.println("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=Mathoperation.mul(4,5); int b=Mathoperation.divide(a,2); System.out.println("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=room1.area(); int volume1=room1.volume(); System.out.println("Area1: -\n"+area1); System.out.println("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() { System.out.println("Super x="+x); } } class Sub extends Super { int y; Sub (int x,int y) { super(x); this.y=y; } void display() { System.out.println("Super x: -\n"+x); System.out.println("Sub y: -\n"+y); } } class OverRideTest { public static void main(String args[]) { Sub s1=new Sub(100,200); s1.display(); } }

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=no.length; for(int i=0;i<n;i++) { System.out.println(""+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; } } } System.out.println("Sorted List\n"); for(int i=0;i<n;i++) { System.out.println(""+no[i]); } System.out.println(""); }

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} }; System.out.println("The contents of the 2D Array are : \n"); for(int i=0;i<array.length;i++) { System.out.println("Dimension # " + (i+1)); for(int j=0;j<array[i].length;j++) System.out.println("\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=name.length; 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++) { System.out.println(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; System.out.println("Area of Rectangle: -\n"+area.compute(10,20)); area = cir; System.out.println("Area of Circle: \n"+area.compute(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() { System.out.println("Roll No"+rollnumber); } } class test extends student { float part1,part2; void getmarks(float m1, float m2) { part1=m1; part2=m2; } void putmarks() { System.out.println("Marks Obtained: -\n"); System.out.println("part 1: -\n"+part1); System.out.println("part 2: -\n"+part2); } } interface sports { float sportwt=6.0F; void putwt(); } class Results extends test implements sports { float total; public void putwt() { System.out.println("Sports WT: -\n"+sportwt); } void display() { total =part1+part2+sportwt; putnumber();

putmarks(); putwt(); System.out.println("Total Score: -\n"+total); } } class hybrid { public static void main(String args[]) { Results student1=new Results(); student1.getnumber(1234); student1.getmarks(27.5F,33.0F); student1.display(); } }

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() { System.out.println("This is a concrete method\n"); } } class B extends A { void callme() { System.out.println("B is implementation of callme"); } } class abstract_demo { public static void main(String args[]) { B b=new B(); b.callme(); b.callmetoo(); } }

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() { System.out.println("Inside area for rectangle"); return dim1*dim2; } } } class Triangle extends Figure { Triangle(double a,double b) { super (a,b); } double area() { System.out.println("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

System.out.println("area is"+figref.area[]); Figref=t; System.out.println("area is"+figref.area()); } }

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; System.out.print("I: - "+i+"\n"); System.out.print("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(); System.out.println("Largest value: -\n"+large); } } class nestingtest { public static void main(String args[]) { nesting nest=new nesting(50,40); nest.display(); } }

Output: Largest value: 50

*A Program of using Increment Operator*


class incrementoperator { public static void main(String args[]) { int m=10,n=20; System.out.println("M: - "+m); System.out.println("n: - "+n); System.out.println("++m: - "+ ++m); System.out.println("n++: - "+n++); System.out.println("m: - "+m); System.out.println("n: - "+n); } }

Output: M: - 10 n: - 20 ++m: - 11 n++: - 20 m: -11 n: - 21

*A Program of creating an Applet*


import java.awt.*; import java.applet.*; 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) { g.drawString(str,10,100); } }

Output: Appletviewer hellojava.html 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++) { System.out.println("Runnable running"); } } } public class TestThreads { public static void main (String [] args) { FooRunnable r = new FooRunnable(); Thread t = new Thread(r); t.start(); } }

Output: % java TestThreads Runnable running Runnable running Runnable running Runnable running Runnable running

*A Program of Implementing Inner Classes*


class MyInner { public void seeOuter() { System.out.println("Outer x is " + x); System.out.println("Inner class ref is " + this); System.out.println("Outer class ref is " + MyOuter.this); } } class MyOuter { private int x = 7; public void makeInner() { MyInner in = new MyInner(); in.seeOuter(); } class MyInner public void seeOuter() { System.out.println("Outer x is " + x); System.out.println("Inner class ref is " + this); System.out.println("Outer class ref is " + MyOuter.this); } } public static void main (String[] args) { MyOuter.MyInner inner = new MyOuter().new MyInner(); inner.seeOuter(); } }

Output: Outer x is 7 Inner class ref is MyOuter$MyInner@113708 Outer class ref is MyOuter@33f1d7

*A Program of Handling Exceptions*


import java.io.*; public class ReadData { public static void main(String args[]) { try { RandomAccessFile raf = new RandomAccessFile("myfile.txt", "r"); byte b[] = new byte[1000]; raf.readFully(b, 0, 1000); } catch(FileNotFoundException e) { System.err.println("File not found"); System.err.println(e.getMessage()); e.printStackTrace(); } catch(IOException e) { System.err.println("IO Error"); System. err. println(e. toString()); e.printStackTrace(); } } }

Output: This will handle different exceptions

*A Program of implementing a package*

package package1; public class classA { public void displayA() { System.out.println(Class A); } } //Now save it to destination with folder name package1 import package1.classA; class PackageTest { public static void main(String args[]) { classA objectA=new classA(); object.displayA(); } }

Output: Class A

*A Program of Creating file through file object*


import java.io.*; class Writer1 { public static void main(String [] args) { try { boolean newFile = false; File file = new File; System.out.println(file.exists()); newFile = file.createNewFile(); System.out.println(newFile); System.out.println(file.exists()); } 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

You might also like