Selection Sort in Java

Code :
import java.io.*;

class select

{
public static void main(String args[])throws IOException
{
int[] array = new int[100];
int max = 9;
int low , temp;
int a = 0;

try
{
BufferedReader keyb = new BufferedReader (new
InputStreamReader(System.in));
System.out.print("Enter max number to be sort:");
max = Integer.parseInt(keyb.readLine());
for (int x = 0 ; x < max ; x++)
{
System.out.print("Enter number:");
array[x] = Integer.parseInt(keyb.readLine());
}
for (int m = 0 ; m < max ; m++)
{
System.out.print(array[m] + " ");
}
System.out.print(" ");
//for (int a = max - 1; a > 0; a--)

{

for( int b = 0 ; b < max ; b++)
{
low = array[b];
for (int c = b ; c < max ; c++)
{
if (array[c] < low)
{
low = array[c]; //
System.out.print("<" + b + "><" + c + "><" + low + ">

");
}
else
{
// System.out.print("<" + b + "><" + c + "><" + low + ">");
}


for (int d = b ; d <>
{
if (array[d] == low)
{
temp = array[b];
array[b] = array[d];
array[d] = temp;
//System.out.print("*" + c + "*");
}
}
}

System.out.print("
");
for (int m = 0 ; m <>
{

System.out.print(array[m] + " ");
}

System.out.print("
");


}




}



}
catch (IOException e)
{
System.out.println("System Error");
}
catch (NumberFormatException er)
{
System.out.println("Kindly enter a number");
}

}
}



How to sort an array in java

import java.util.Arrays;


public class Main
{


public void sortIntArray()
{

int[] arrayToSort = new int[] {48, 5, 89, 80, 81, 23, 45, 16, 2};

Arrays.sort(arrayToSort);

for (int i = 0; i <>
System.out.println(arrayToSort[i]);
}


public void sortStringArray() {

String[] arrayToSort = new String[] {"Oscar", "Charlie", "Ryan", "Adam", "David"};

Arrays.sort(arrayToSort);

for (int i = 0; i <>
System.out.println(arrayToSort[i]);
}


public static void main(String[] args)
{

Main main = new Main();
main.sortIntArray();
main.sortStringArray();
}
}

Using the Comparable interface to compare and sort objects

import java.util.Arrays;


public class Main {


public void comparableExample() {

//Creating the objects that implements the Comparable interface
Car car1 = new Car("Toyota", 2006, 5000);
Car car2 = new Car("BMW", 2007, 5000);
Car car3 = new Car("Chrysler", 2007, 4000);

//Comparing the objects by calling the compareTo method on one of them
//passing another object as argument.
System.out.println("Car 1 equals Car 2: " + car1.compareTo(car2));
System.out.println("Car 1 equals Car 3: " + car1.compareTo(car3));
System.out.println("Car 2 equals Car 3: " + car2.compareTo(car3));
System.out.println();

//To sort them we create an array which is passed to the Arrays.sort()
//method.
Car[] carArray = new Car[] {car1, car2, car3};
Arrays.sort(carArray);

//Print out the sorted array
for (Car car : carArray)
System.out.println(car.toString());
}

//The Car class used to compare and sort objects.
class Car implements Comparable
{

private String make;
private int year;
private int mileage;

public Car(String make, int year, int mileage)
{

this.make = make;
this.year = year;
this.mileage = mileage;
}

//Mandatory method when implementing the
//Comparable interface. In this method we
//compare the mileage of the two car objects.
public int compareTo(Object obj)
{

if (obj instanceof Car)
{

Car car = (Car) obj;
if (this.mileage > car.getMileage())
return 1;
else if (this.mileage < car.getMileage())
return -1;
}
return 0;
}

public void setMake(String make)
{
this.make = make;
}

public void setYear(int year)
{
this.year = year;
}

public void setMileage(int mileage)
{
this.mileage = mileage;
}

public String getMake()
{
return make;
}

public int getYear()
{
return year;
}

public int getMileage()
{
return mileage;
}

public String toString()
{

StringBuffer buffer = new StringBuffer();
buffer.append("Make: " + make + "\n");
buffer.append("Year: " + year + "\n");
buffer.append("Mileage: " + mileage + "\n");

return buffer.toString();
}
}


public static void main(String[] args)
{
new Main().comparableExample();
}
}

Program to copy an array

public class Main
{


public void copyArrayExample()
{


int[] intArray = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};

int[] arrayCopy = new int[intArray.length];

System.arraycopy(intArray, 0, arrayCopy, 0, intArray.length);

for (int i = 0; i <>
System.out.println(arrayCopy[i]);

}


public static void main(String[] args)
{

new Main().copyArrayExample();
}
}

Program to Extend the size of an array

public class Main
{

/**
* Extends the size of an array.
*/
public void extendArraySize()
{


String[] names = new String[] {"Joe", "Bill", "Mary"};

//Create the extended array
String[] extended = new String[5];

//Add more names to the extended array
extended[3] = "Carl";
extended[4] = "Jane";

//Copy contents from the first names array to the extended array
System.arraycopy(names, 0, extended, 0, names.length);

//Ouput contents of the extended array
for (String str : extended)
System.out.println(str);

}
public static void main(String[] args)
{
new Main().extendArraySize();
}
}

Extend the size of an array

public class Main
{

public void extendArraySize()
{


String[] names = new String[] {"Joe", "Bill", "Mary"};

//Create the extended array
String[] extended = new String[5];

//Add more names to the extended array
extended[3] = "Carl";
extended[4] = "Jane";

//Copy contents from the first names array to the extended array
System.arraycopy(names, 0, extended, 0, names.length);

//Ouput contents of the extended array
for (String str : extended)
System.out.println(str);

}
public static void main(String[] args)
{
new Main().extendArraySize();
}
}

BINARY SEARCH TREE

/* PROGRAM TO IMPLEMENT BINARY SEARCH TREE IN C*/

#include
struct BT
{
int data;
struct BT *right,*left;
};

void insert(struct BT ** ptr,int d)
{
if((*ptr)==NULL)
{
(*ptr)=(struct BT*)malloc(sizeof(struct BT));
(*ptr)->data=d;
(*ptr)->left=(*ptr)->right=NULL;
}
else
{
if((*ptr)->data>d)
insert(&((*ptr)->left),d);
else
insert(&((*ptr)->right),d);
}
return;
}

int search(struct BT *ptr,int no)
{
if(ptr==NULL)
return(0);
if(ptr->data==no)
return(1);
if(ptr->data>no)
return(search(ptr->left,no));
else
return(search(ptr->right,no));
}

void inorder(struct BT *ptr)
{
if(ptr==NULL)
return;
else
{
inorder(ptr->left);
printf("\t%d",ptr->data);
inorder(ptr->right);
}
}

void preorder(struct BT*ptr)
{
if(ptr==NULL)
return;
else
{
printf("\t%d",ptr->data);
preorder(ptr->left);
preorder(ptr->right);
}
}
void postorder(struct BT*ptr)
{
if(ptr==NULL)
return;
else
{
postorder(ptr->left);
postorder(ptr->right);
printf("\t%d",ptr->data);
}
}
main()
{
struct BT*root;
int ch,d,no,f;

root=NULL;
while(ch!=6)
{
printf("\n 1.Insert\n 2.Search\n 3.Inorder\n 4.Preorder\n 5.Postorder\n 6.Exit\n");
printf("\n Enter the choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter the data:");
scanf("%d",&d);
insert(&root,d);
break;
case 2: printf("Enter the node:");
scanf("%d",&no);
f=search(root,no);
if(f==0)
printf("Node is not present");
else
printf("Node is present");
break;
case 3: inorder(root);
break;
case 4: preorder(root);
break;
case 5: postorder(root);
break;
case 6: break;
}
}
}

Program to Create Threads In Java.

class A extends Thread
{

public void run()
{

System.out.println("Thread A has started");
for (int i=0;i<=5;i++)
{

System.out.println("From thread A:i =" +i);
}
System.out.println("Exit from A");
}
}
class B extends Thread
{

public void run()
{

System.out.println("Thread B has started");
for (int j=0;j<=5;j++)
{

System.out.println("From thread B:j =" +j);
if(j==3)
stop();
}
System.out.println("Exit from B");
}
}
class C extends Thread
{

public void run()
{

System.out.println("Thread C has started");
for (int k=0;k<=5;k++)
{

System.out.println("From thread C:k =" +k);
if(k==1)
try{
sleep(100);
}catch(Exception e)
{


}
}
System.out.println("Exit from C");
}
}
class ThreadMethods
{

public static void main(String args[])
{

A ta = new A();
B tb = new B();
C tc = new C();
tc.setPriority(Thread.MAX_PRIORITY);
ta.setPriority(Thread.MIN_PRIORITY);
System.out.println("Start thread A");
ta.start();
System.out.println("Start thread B");
tb.start();
System.out.println("Start thread C");
tc.start();
System.out.println("End of main Thread");
}
}

Function Overloding In Java.

class FunctionOverloading
{

int a;
FunctionOverloading()
{

a=10;
}
void volume()
{

System.out.println("Valume = " +(a * a * a));
}
int volume(int i)
{

return(i*i*i);
}
float volume(float i)
{

return(i*i*i);
}
public static void main(String args[])
{

int intVolume;
float floatVolume;
FunctionOverloading f=new FunctionOverloading();
f.volume();
intVolume=f.volume(5);
System.out.println("Valume = " +intVolume);
floatVolume=f.volume(2.3f);
System.out.println("Valume = " +floatVolume);
}
}

Array Examples In Java.

class ArrayExample
{
public static void main(String args[]) throws Exception
{
int arr[]=new int[5];
arr[0]=45;
arr[1]=24;
arr[2]=39;
arr[3]=25;
arr[4]=12;
System.out.println("Array elements are :");
for(int i=0;i<5;i++)
{
System.out.println("Arr["+ i +"] = " +arr[i]);
}
}
}

Program to Find Square Root of Number.

import java.lang.Math;
public class SquareRoot
{

public static void main(String args[])
{

double x=9;
double y;
y=Math.sqrt(x);
System.out.println("Y = "+y);
}
}

Program To Find Area Of Room in Java

class Room
{
float length;
float breadth;
public void getData(float a, float b)
{
length=a;
breadth=b;
}
}
public 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 = "+area);
}
}

Program to Reading data from Keyboard

import java.io.DataInputStream;
class Reading
{

public static void main(String args[])
{

DataInputStream in =new DataInputStream(System.in);
int intNumber=0;
float floatNumber=0.0f;
try
{

System.out.println("Enter a Integer : ");
intNumber =Integer.parseInt(in.readLine());
System.out.println("Enter a Float number : ");
floatNumber = Float.parseFloat(in.readLine());
}catch(Exception ex)
{

ex.printStackTrace();
}
System.out.println("intNumber = " +intNumber);
System.out.println("floatNumber = "+floatNumber);
}
}

Program In Java which uses commend line arguments as input.

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{
string=args[i];
i=i+1;
System.out.println(i+" : "+"Java is "+ string+ "!");
}
}
}

//input :- java ComLineTest Simple Object_Oriented Distributed Robust Secure Portable MultiThreaded Dynamic

Program In Java that will read a float type

/* Its Program that will read a float type value from the keyboard and print the following output.

->Small Integer not less than the number.

->Given Number.

->Largest Integer not greater than the number.

*/

class ValueFormat{

public static void main(String args[])
{


double i = 34.32; //given number


System.out.println("Small Integer not greater than the number : "+Math.ceil(i));


System.out.println("Given Number : "+i);

System.out.println("Largest Integer not greater than the number : "+Math.floor(i));

}

Program to display a greet message according to Marks obtained by student.

class SwitchDemo
{


public static void main(String args[])
{


int marks = Integer.parseInt(args[0]); //take marks as command line argument.

switch(marks/10)
{


case 10:

case 9:

case 8:

System.out.println("Excellent");

break;

case 7:

System.out.println("Very Good");

break;

case 6:

System.out.println("Good");

break;

case 5:

System.out.println("Work Hard");

break;

case 4:

System.out.println("Poor");

break;

case 3:

case 2:

case 1:

case 0:

System.out.println("Very Poor");

break;

default:

System.out.println("Invalid value Entered");

}

}

}

Program to find sum of all integers greater than 100 and less than 200 that are divisible by 7 In Java.

class SumOfDigit
{


public static void main(String args[])
{


int result=0;

for(int i=100;i<=200;i++) {

if(i%7==0)

result+=i;

}

System.out.println("Output of Program is : "+result);

}

}

program to find SUM AND PRODUCT of a given Digit In Java.

class Sum_Product_ofDigit
{

public static void main(String args[])
{

int num = Integer.parseInt(args[0]);
//taking value as command line argument.


int temp = num,result=0;

//Logic for sum of digit

while(temp>0)
{


result = result + temp;

temp--;

}


System.out.println("Sum of Digit for "+num+" is : "+result);


//Logic for product of digit

temp = num;

result = 1;

while(temp > 0)
{


result = result * temp;

temp--;

}


System.out.println("Product of Digit for "+num+" is : "+result);


}

}

Program to Reverse a given number in Java.

class Reverse
{


public static void main(String args[])
{


int num = Integer.parseInt(args[0]);
//take argument as command line


int remainder, result=0;

while(num>0)
{


remainder = num%10;

result = result * 10 + remainder;

num = num/10;

}

System.out.println("Reverse number is : "+result);

}

}

Program to generate 5 Random nos. between 1 to 100, and it should not follow with decimal point In Java.

class RandomDemo
{


public static void main(String args[])
{


for(int i=1;i<=5;i++)
{


System.out.println((int)(Math.random()*100));

}

}

}

Program to Find Minimum of 2 nos. using conditional operator in Java

class Minof2
{


public static void main(String args[])
{


//taking value as command line argument.

//Converting String format to Integer value

int i = Integer.parseInt(args[0]);

int j = Integer.parseInt(args[1]);

int result = (i

System.out.println(result+" is a minimum value");

}

}

Program to Find Maximum of 2 numbers in Java.

class Maxof2
{


public static void main(String args[])
{


//taking value as command line argument.

//Converting String format to Integer value

int i = Integer.parseInt(args[0]);

int j = Integer.parseInt(args[1]);

if(i > j)

System.out.println(i+" is greater than "+j);

else

System.out.println(j+" is greater than "+i);

}

}

program to find Fibonacci series of a given number in Java.

class Fibonacci
{


public static void main(String args[])
{


int num = 5; //taking no. as command line argument.

System.out.println("*****Fibonacci Series*****");

int f1, f2=0, f3=1;

for(int i=1;i<=num;i++)
{


System.out.print(" "+f3+" ");

f1 = f2;

f2 = f3;

f3 = f1 + f2;

}

}

}


****************************************************
O/p :

Input - 8

Output - 1 1 2 3 5 8 13 21

Program to Find Factorial of Given no In JAVA

class Factorial
{
public static void main(String args[])
{
int num = 5;
int result = 1;
int result2 = 1;
//using while loop
while(num>0)
{
result = result * num;
num--;
}
//using for loop
num = 5;
for(int i=num;i>=1;i--)
{
result2=result2*i;
}
System.out.println("Factorial of Given no. is : "+result);
System.out.println("Factorial of Given no. is : "+result2);
}
}

PROGARM FOR CREATE A EMPLOYEE DETAILS

import java.lang.*;
import java.io.*;
class Employee
{
int empno;
String name;
Employee()
{
try
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("name of the employee
);
name=d.readLine();
System.out.println("Employy no:");
empno=Integer.parseInt(d.readLine());
}
catch(IOException e)
{
System.out.println("ioerror");
}
}
void Display()
{
}
}

class manager extends Employee
{
String desig;
int bp;
int hra;
int ta=100,da,inc;
manager()
{
try
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("salary is
);
bp=Integer.parseInt(d.readLine());
System.out.println("desig of the employee
);
desig=d.readLine();
hra=(bp*10)/100;
da=(bp*5)/100;
inc=(bp*15)/100;
}
catch(IOException e1)
{

System.out.println("error");
}
}
void Display()
{


System.out.println(name+" "+empno+" "+desig+" "+" "+(bp+hra+da+ta+inc)
);

}
}
class Typist extends Employee
{

int bp;
String desig;
int hra;
int ta=0,da,inc;
Typist()
{
try
{
DataInputStream d=new
DataInputStream(System.in);
System.out.println("salary is
);
bp=Integer.parseInt(d.readLine());
System.out.println("desig of the employee
);
desig=d.readLine();
hra=(bp*8)/100;
da=(bp*4)/100;
inc=(bp*10)/100;
}
catch(IOException e1)
{
System.out.println("error");
}
}
void Display()
{

System.out.println(name+" "+empno+" "+desig+" "+" "+(bp+hra+da+ta+inc)
);

}
}

public class lab1
{
public static void main(String args[]) throws IOException
{
manager m=new manager();
Typist t=new Typist();
System.out.println(" EMPLOYEE DETAILS" );
System.out.println(" ~~~~~~~~~~~~~~~~" );
System.out.println("Name Code Designation
Salary ");
System.out.println("~~~~ ~~~~ ~~~~~~~~~~~
~~~~~~ ");
m.Display();
t.Display();

}
}

Is it possible to stop an object from being created during construction?

Yes, have the constructor throw an exception. Formally, an object _will_ be created (since the constructor is a method invoked after the actual method creation), but nothing useful will be returned to the program, and the dead object will be later reclaimed by Garbage Collector.

But the clean way is that you leave calls to the constructor to a static factory method which can check the parameters and return null when needed.

Note that a constructor - or any method in general - throwing an exception will not "return null", but will leave the "assign target" as it was.

Selection Sort

import java.io.*;
class select
{
public static void main(String args[])throws IOException
{
int[] array = new int[100];
int max = 9;
int low , temp;
int a = 0;

try
{
BufferedReader keyb = new BufferedReader (new
InputStreamReader(System.in));
System.out.print("Enter max number to be sort:");
max = Integer.parseInt(keyb.readLine());
for (int x = 0 ; x <>
{
System.out.print("Enter number:");
array[x] = Integer.parseInt(keyb.readLine());
}
for (int m = 0 ; m <>
{

System.out.print(array[m] + " ");
}
System.out.print("
);

//for (int a = max - 1; a > 0; a--)
{

for( int b = 0 ; b <>
{
low = array[b];
for (int c = b ; c <>
{
if (array[c] <>
{
low = array[c];
//System.out.print("<" + b + "><" + c + "><" + low + ">
);

}
else
{
// System.out.print("<" + b + "><" + c + "><" + low + ">
);

}


for (int d = b ; d <>
{
if (array[d] == low)
{
temp = array[b];
array[b] = array[d];
array[d] = temp;
//System.out.print("*" + c + "*");
}
}
}

System.out.print("
);

for (int m = 0 ; m <>
{

System.out.print(array[m] + " ");
}

System.out.print("
);

}
}
}
catch (IOException e)
{
System.out.println("System Error");
}
catch (NumberFormatException er)
{
System.out.println("Kindly enter a number");
}

Simple Program in Java to Implement Multithreading

import java.io.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;

class test extends Frame implements ActionListener,Runnable
{
int lower,upper;
Label l1=new Label("ODD");
Label l2=new Label("EVEN");
List lb1=new List();
List lb2=new List();
Button b2=new Button("EXIT");
test(int low,int up)
{
lower = low;
upper = up;
setLayout(new FlowLayout());
setSize(700,700);
setTitle("Thread Demo");
setVisible(true);
add(l1);add(lb1);add(l2);add(lb2);add(b2);
b2.addActionListener(this);
Thread t=new Thread(this);
t.start();


addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ System.exit(0); }
}
);

}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b2)
System.exit(0);
}
public void run()
{
try
{
if((lower % 2) != 0)
{
lower = lower + 1;
}
while(lower <= upper)
{
Thread.sleep(1000);
lb2.add(String.valueOf(lower));
lower += 2;
Thread.sleep(500);
}
}catch(Exception e){}
}

public static void main(String args[])
{
int lower,upper;
lower = Integer.parseInt(args[0]);
upper = Integer.parseInt(args[1]);
test ob = new test(lower,upper);

if((lower % 2) == 0)
{
lower = lower + 1;
}

try
{
while(lower <= upper)
{
Thread.sleep(1000);
ob.lb1.add(String.valueOf(lower));
lower = lower + 2;
Thread.sleep(500);
}
}
catch(Exception e){}
}

}

Thread Method

class A extends Thread{
public void run(){
System.out.println("Thread A has started");
for (int i=0;i<=5;i++){
System.out.println("From thread A:i =" +i);
}
System.out.println("Exit from A");
}
}
class B extends Thread{
public void run(){
System.out.println("Thread B has started");
for (int j=0;j<=5;j++){
System.out.println("From thread B:j =" +j);
if(j==3)
stop();
}
System.out.println("Exit from B");
}
}
class C extends Thread{
public void run(){
System.out.println("Thread C has started");
for (int k=0;k<=5;k++){
System.out.println("From thread C:k =" +k);
if(k==1)
try{
sleep(100);
}catch(Exception e){

}
}
System.out.println("Exit from C");
}
}
class ThreadMethods{
public static void main(String args[]){
A ta = new A();
B tb = new B();
C tc = new C();
tc.setPriority(Thread.MAX_PRIORITY);
ta.setPriority(Thread.MIN_PRIORITY);
System.out.println("Start thread A");
ta.start();
System.out.println("Start thread B");
tb.start();
System.out.println("Start thread C");
tc.start();
System.out.println("End of main Thread");
}
}

Creating marklist In Java

import java.io.*;
public class Stud1{
private int mark1,mark2,mark3,result;
private float per;
private String name;
public Stud1(String name,int mark1,int mark2,int mark3){
this.name=name;
this.mark1=mark1;
this.mark2=mark2;
this.mark3=mark3;
}
public void calculate(){
result=mark1+mark2+mark3;
per=(float)result/300*100;
System.out.println("Name Of Student : "+this.name);
System.out.println("Result : "+result);
System.out.println("Percentage : "+per);
}
public static void main(String args[]){
Stud1 s=new Stud1("Avinash",79,78,79);
s.calculate();
Demo2 n= new Demo2();
n.defBelong();
}
}

Program to find Vowels

public class SwitchDemo1{
public void isVowel(char c){
switch(c){
case 'a' :
case 'e' :
case 'o' :
case 'i' :
case 'u' :
case 'A' :
case 'E' :
case 'O' :
case 'I' :
case 'U' :
System.out.println("Entered Charecter is Vowel");
break;
default :
System.out.println("Entered Charecter is Consonent");
break;
}
}
public static void main(String args[]){

SwitchDemo1 s= new SwitchDemo1();
s.isVowel('a');
s.isVowel('e');
s.isVowel('d');
s.isVowel('s');
s.isVowel('o');
}
}

Program to calculate area of rectangle

class RectArea{
float length,breadth;
double area;
void setValue(float l,float b){
length=l;
breadth=b;
}
void calculate(){
area=(double)length*breadth;
System.out.println("Area of Rectangle : "+area);
}
}
class Rect{
public static void main(String args[]) {
RectArea r=new RectArea();
r.setValue(12,20);
r.calculate();
}
}

Operator In Java

class OperatorUse{
public static void main(String args[]){
int a=5,b=3;
int c=1010;
System.out.println("a & b = "+(a &b));
System.out.println("a | b = "+(a |b));
System.out.println("a ^ b = "+(a ^b));
System.out.println("a << b = "+(c <<2));
System.out.println("a >> b = "+(c >>2));
System.out.println("a >>> b = "+(c >>>2));
}
}

Function Overloading In Java

class FunctionOverloading{
int a;
FunctionOverloading(){
a=10;
}
void volume(){
System.out.println("Valume = " +(a * a * a));
}
int volume(int i){
return(i*i*i);
}
float volume(float i){
return(i*i*i);
}
public static void main(String args[]){
int intVolume;
float floatVolume;
FunctionOverloading f=new FunctionOverloading();
f.volume();
intVolume=f.volume(5);
System.out.println("Valume = " +intVolume);
floatVolume=f.volume(2.3f);
System.out.println("Valume = " +floatVolume);
}
}

Parent & Child class in JAVA

class Parent{
int i,j;
Parent(int a,int b){
i=a;
j=b;
}
void show(){
System.out.println("Value of i = "+i);
System.out.println("Value of j = "+j);
}
}
class Child extends Parent
{
int k=52;
Child(int n,int m,int o) {
super(m,o);
k=n;
}
void show(){
super.show();
System.out.println("Value of k = "+k);
}
public static void main(String args[]){
Child b=new Child(12,45,32);
b.show();
}
}

Creating Bill Program

//class buuldin g with set and get type command and
//use of Default Constructor
public class Bill{
private int bno;
private int bamt;
public Bill(){
bno=20;
bamt=3200;
}
public void setBillNo(int i){
if(i>0 && i<1000){
bno=i;
}else{
System.out.println("invalid Bill Number");
}
}public void getBillNo(){
System.out.println("Bill No. : \t"+bno);
}
public void setBillAmt(int i){
if(i>500 && i<5000){
bamt=i;
}
else{
System.out.println("Invalide Amount");
}
}
public void getBillAmt(){
System.out.println("Bill Amount : \t"+bamt);
}
public static void main(String args[]){
Bill b1=new Bill(),b2=new Bill();
b2.getBillNo();
b2.getBillAmt();
b1.setBillNo(54);
b1.getBillNo();
b1.setBillAmt(4000);
b1.getBillAmt();
}
}

Arrays Input In Java

import java.io.*;
class ArrayInput{
public static void main(String args[]) throws IOException {
int arr1[]=new int[5];
int arr2[]=new int[5];
int arr3[][]=new int[5][5];
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
String str;
System.out.println("Enter arr1 elements for arr1[0] to arr1[4]");
for(int i=0;i<5;i++){
str=br.readLine();
arr1[i]=Integer.parseInt(str);
}
System.out.println("Enter arr2 elements for arr2[0] to arr2[4]");
for(int i=0;i<5;i++){
str=br.readLine();
arr2[i]=Integer.parseInt(str);
}
for(int i=0 ;i<5;i++){
for(int j=0;j<5;j++){
arr3[i][j]=arr1[i]*arr2[j];
}
}
System.out.println("Ent1ered arr3 elements are ");
for(int i=0 ;i<5;i++){
for(int j=0;j<5;j++){
System.out.print(" "+arr3[i][j]);
System.out.print("\t");
}
System.out.println();
}
}
}

Arrays In Java

class ArrayExample
{
public static void main(String args[]) throws Exception
{
int arr[]=new int[5];
arr[0]=45;
arr[1]=24;
arr[2]=39;
arr[3]=25;
arr[4]=12;
System.out.println("Array elements are :");
for(int i=0;i<5;i++)
{
System.out.println("Arr["+ i +"] = " +arr[i]);
}
}
}

FOR LOOP In Java

A for loop is used to repeat a statement until a condition is met. Although for loops fre-
quently are used for simple iteration in which a statement is repeated a certain number of
times, for loops can be used for just about any kind of loop.
The for loop in Java looks roughly like the following:
for (initialization; test; increment) {
statement;
}
The start of the for loop has three parts:
1. initialization is an expression that initializes the start of the loop. If you have a
loop index, this expression might declare and initialize it, such as int i = 0.
Variables that you declare in this part of the for loop are local to the loop itself;
they cease to exist after the loop is finished executing. You can initialize more than
one variable in this section by separating each expression with a comma. The state-
ment int i = 0, int j = 10 in this section would declare the variables i and j,
and both would be local to the loop.
2. test is the test that occurs before each pass of the loop. The test must be a
Boolean expression or a function that returns a boolean value, such as i <>
the test is true, the loop executes. When the test is false, the loop stops execut-
ing.
3. increment is any expression or function call. Commonly, the increment is used to
change the value of the loop index to bring the state of the loop closer to returning
false and stopping the loop. The increment takes place after each pass of the loop.
Similar to the initialization section, you can put more than one expression in
this section by separating each expression with a comma.




/*Write a program to Find Factorial of Given no. */
class Factorial{
public static void main(String args[]){
int num = 5;
int result = 1;
int result2 = 1;
//using while loop
while(num>0){
result = result * num;
num--;
}
//using for loop
num = 5;
for(int i=num;i>=1;i--){
result2=result2*i;
}
System.out.println("Factorial of Given no. is : "+result);
System.out.println("Factorial of Given no. is : "+result2);
}
}

Display DIGIT Triangle

0

1 0

1 0 1

0 1 0 1 */

class Output2{

public static void main(String args[]){

for(int i=1;i<=4;i++){

for(int j=1;j<=i;j++){

System.out.print(((i+j)%2)+" ");

}

System.out.print("\n");

}

}

}

program to find average of consecutive N Odd numbers and Even numbers

class EvenOdd_Avg{

public static void main(String args[]){

int n = Integer.parseInt(args[0]);

int cntEven=0,cntOdd=0,sumEven=0,sumOdd=0;

while(n > 0){

if(n%2==0){

cntEven++;

sumEven = sumEven + n;

}

else{

cntOdd++;

sumOdd = sumOdd + n;

}

n--;

}

int evenAvg,oddAvg;

evenAvg = sumEven/cntEven;

oddAvg = sumOdd/cntOdd;

System.out.println("Average of first N Even no is "+evenAvg);

System.out.println("Average of first N Odd no is "+oddAvg);



}

}

program to generate Harmonic Series.

Example :

Input - 5

Output - 1 + 1/2 + 1/3 + 1/4 + 1/5 = 2.28 (Approximately) */

class HarmonicSeries{

public static void main(String args[]){

int num = Integer.parseInt(args[0]);

double result = 0.0;

while(num > 0){

result = result + (double) 1 / num;

num--;

}

System.out.println("Output of Harmonic Series is "+result);

}

}

switch case Program

Example :

Input - 124

Output - One Two Four */



class SwitchCaseDemo{

public static void main(String args[]){

try{

int num = Integer.parseInt(args[0]);

int n = num; //used at last time check

int reverse=0,remainder;

while(num > 0){

remainder = num % 10;

reverse = reverse * 10 + remainder;

num = num / 10;

}

String result=""; //contains the actual output

while(reverse > 0){

remainder = reverse % 10;

reverse = reverse / 10;

switch(remainder){

case 0 :

result = result + "Zero ";

break;

case 1 :

result = result + "One ";

break;

case 2 :

result = result + "Two ";

break;

case 3 :

result = result + "Three ";

break;

case 4 :

result = result + "Four ";

break;

case 5 :

result = result + "Five ";

break;

case 6 :

result = result + "Six ";

break;

case 7 :

result = result + "Seven ";

break;

case 8 :

result = result + "Eight ";

break;

case 9 :

result = result + "Nine ";

break;

default:

result="";

}

}

System.out.println(result);

}catch(Exception e){

System.out.println("Invalid Number Format");

}

}

}

We Are Founder..