NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

Showing posts with label Using of Input Stream class. Show all posts
Showing posts with label Using of Input Stream class. Show all posts

Sunday, August 12, 2012

Java program to read and print student information


import java.io.*;
class student
{
int slno;
String name;
student()
{
name=new String();
slno=0;
}
void get()throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the name of the student: ");
name=br.readLine();
System.out.print("\nEnter the Serial Number: ");
slno=Integer.parseInt(br.readLine());
}
void disp()
{
System.out.println("Serial Number : "+slno+"\nName : "+name);
}
}
class studmark extends student
{
int m1,m2;
studmark()
{
m1=m2=0;
}
void get()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the mark1: ");
m1=Integer.parseInt(br.readLine());
System.out.println("Enter the mark2: ");
m2=Integer.parseInt(br.readLine());
}
void disp()
{
System.out.println("Mark1 = "+m1+"\nMark2 = "+m2);
}
}
class personal extends studmark
{
int w,age;
personal()
{
w=age=0;
}
void get()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the Weight : ");
w=Integer.parseInt(br.readLine());
System.out.println("Enter the age : ");
age=Integer.parseInt(br.readLine());
}
void disp()
{
System.out.println("Weight = "+w+"\nAge = "+age);
}
}
class studinheritance
{
public static void main(String[] args)throws IOException
{
student x;
student y= new student();
x=y;
x.get();
x.disp();
studmark i;
x=i;
x.get();
x.disp();
personal p;
x=p;
x.get();
x.disp();
}
}

Java program to perform String sorting


import java.io.*;
class stringsort
{
String s[];
int n;
stringsort(int q)
{
s=new String[q];
n=q;
}
void sortstring()throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter The names: ");
for(int i=0;i<s.length;i++){
System.out.print("name: "+(i+1));
s[i]=br.readLine();}
}
void sort()
{
for(int i=0;i<s.length-1;i++)
for(int j=i+1;j<s.length;j++)
{
if(s[i].compareTo(s[j])>0)
{
String temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
System.out.println("\nThe sorted array is: ");
for(int i=0;i<s.length;i++)
System.out.println(s[i]);
}
public static void main(String[] args)throws IOException
{
BufferedReader b= new BufferedReader(new InputStreamReader(System.in));
System.out.println("\nEnter no of names : " );
int x=Integer.parseInt(b.readLine());
stringsort s=new stringsort(x);
s.sortstring();
s.sort();
}
}

Java program to read and print n student names


import java.io.*;
class stringop1
{
String s[];
int n;
stringop1(int x)
{
s= new String[x];
n=x;
}
void get()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
for(int i=0;i<n;i++)
{
System.out.print("\nName of student "+(i+1)+" : ");
s[i]=br.readLine();
}
}
void disp()
{
System.out.print("\nThe Entered names are : ");
for(int i=0;i<n;i++)
System.out.print("\nName "+(i+1)+" : " +s[i]);
}
public static void main(String[] args)throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
System.out.print("\nEnter the no.of students : ");
int x=Integer.parseInt(b.readLine());
stringop1 s=new stringop1(x);
s.get();
s.disp();
}
}

Java program to find reverse of a number


import java.io.*;
class reversenum
{
int num,dup,rev,rem;
reversenum()
{
rev=rem=0;
}
void get()throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the number : ");
num=Integer.parseInt(br.readLine());
dup=num;
while(dup>0)
{
rem=dup%10;
rev=rev*10+rem;
dup=dup/10;
}
}
void disp()
{
System.out.println("\nThe reverse of " +num+" is "+rev);
}
public static void main(String[] args)throws IOException
{
reversenum r=new reversenum();
r.get();
r.disp();
}
}

Java program to read and print n student information


import java.io.*;
class nstudio
{
String s[];
int num;
nstudio(int m)
{
s= new String[10];
num=m;
}
void getdata()throws IOException
{
BufferedReader br= new BufferedReader( new InputStreamReader(System.in));
System.out.println("Enter "+num+" Names : ");
for(int i=0;i<num;i++)
{
System.out.print(i+1+ " : ");
s[i]=br.readLine();
}
}
void dispdata()
{
System.out.println("\n The Entered Names are :");
for(int i=0;i<num;i++)
{
System.out.println(i+1+" : "+s[i]);
}
}
public static void main(String[] args)throws IOException
{
nstudio n= new nstudio(10);
n.getdata();
n.dispdata();
}
}

Java Program to implement multilevel inheritance


import java.io.*;
class xyz
{
int x,y;
void get() throws IOException
{
BufferedReader br= new BufferedReader( new InputStreamReader (System.in));
System.out.print("Enter a num : ");
x=Integer.parseInt(br.readLine());
System.out.print("Enter a num : ");
y=Integer.parseInt(br.readLine());
}
}
class abc extends xyz
{
int a;
void calc()
{
a=x+y;
System.out.println("The sum of "+x+" & "+y+" is "+ a);
}
}
class mno extends abc
{
int s;
void disp()
{
s=x-y;
System.out.println("Difference : "+ s);
}
}
class inheritance
{
public static void main(String[] args) throws IOException
{
mno d= new mno();
d.get();
d.calc();
d.disp();
}
}

Java program to Compare elements of an array


import java.io.*;
class compelarray
{
int a[],big,small,n;
compelarray(int x)
{
n=x;
a=new int[n];
}
void readarray()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter elements of array: ");
for(int i=0;i<n;i++)
a[i]=Integer.parseInt(br.readLine());
}
void compare()
{
big=small=a[0];
for(int i=0;i<n;i++)
{if(big<a[i])
big=a[i];
if(small>a[i])
small=a[i];
}
System.out.println("The biggest element is : "+big);
System.out.println("The smallest element is : "+small);
}
public static void main(String[] args)throws IOException
{
int size;
BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter size of the array : ");
size=Integer.parseInt(br2.readLine());
compelarray c= new compelarray(size);
c.readarray();
c.compare();
}
}