Java 5th Sem Complete
Java 5th Sem Complete
Java 5th Sem Complete
Installation (Windows)
1. Download and Install standard Java Developement Kit (jdk-8)
from Oracle website.
2. Download Eclipse IDE form : http://goo.gl/dNKT0p.
3. Extract the zip file downloaded in previous step.
4. Run eclipse.exe in extracted folder.
Usage
1. Create new project by clicking File -> New -> Java Project,
specify the project name and click Finish.
2. Create new class by clicking File -> New -> Class, specify the
class name and click Finish.
3. Write the program and save it.
4. Run and test the code by clicking Run -> Run or Cntl+F11.
stackOperation.java
import java.io.*;
public class stackOperation {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
int top=-1,n,ch,ele;
BufferedReader ip= new BufferedReader(new InputStreamReader
(System.in));
System.out.println("Enter the size of the array");
n=Integer.parseInt(ip.readLine());
int stack[]=new int[n];
System.out.println("Stack operation\n1 Push\n2 Pop\n3 Display\n4
Exit");
while(true){
System.out.println("Enter your choice from the ");
ch=Integer.parseInt(ip.readLine());
switch(ch){
case 1: System.out.println("Enter the element to be pushed");
ele=Integer.parseInt(ip.readLine());
if(top==n-1)
System.out.println("Stack Overflow");
else
stack[++top]=ele;
break;
case 2: if(top<0) System.out.println("Stack underflow");
else System.out.println("Popped :"+stack[top--]);
break;
case 3: if(top<0)System.out.println("Stack empty");
else{
System.out.println("Stack elements are");
for(int i=top;i>=0;i--)
System.out.print(stack[i]+" ");
}
break;
default:System.exit(0);
}
}
}
}
Page 1
quadEquation.java
import java.io.*;
public class quadEquation {
public static void main(String[] args) throws IOException
{
BufferedReader ip=new BufferedReader(new InputStreamReader
(System.in));
double a,b,c,d;
System.out.print("a = ");
a=Double.parseDouble(ip.readLine());
System.out.print("b = ");
b=Double.parseDouble(ip.readLine());
System.out.print("c = ");
c=Double.parseDouble(ip.readLine());
d=b*b-4*a*c;
if(a==0){
System.out.println("Given values are not valid");
System.exit(0);
}
if(d>0){
System.out.println("Distinct roots are");
System.out.println((-1*b+(Math.sqrt(d)))/(2*a)+" "+(-1*b(Math.sqrt(d)))/(2*a));
}
else if(d==0){
System.out.println("Unique solution is");
System.out.println(-1*b/(2*a));
}
else {
System.out.println("Imaginary roots are");
System.out.println(-1*b/(2*a)+"+i"+Math.sqrt(-1*d)/(2*a));
System.out.println(-1*b/(2*a)+"-i"+Math.sqrt(-1*d)/(2*a));
}
}
}
Page 1
MultipleInheritance.java
import java.io.*;
class marks {
int no;
double mark[];
}
class Subjects extends marks {
String subname[];
}
class Students extends Subjects {
String name;
Students(int n){
no=n;
mark = new double[n];
subname = new String[n];
}
double total(){
double total_marks=0;
for(int i=0;i<no;i++)
total_marks+=mark[i];
return total_marks;
}
}
public class MultipleInheritance {
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
BufferedReader ip = new BufferedReader(new InputStreamReader
(System.in));
System.out.println("Enter the number of students");
int n=Integer.parseInt(ip.readLine());
String inp[]=new String[3];
Students std[]=new Students[n];
for(int i=0;i<n;i++){
System.out.print("Name : ");
String name=ip.readLine();
System.out.print("Number of Subjects: ");
int a=Integer.parseInt(ip.readLine());
std[i]=new Students(a);
std[i].name=name;
System.out.println("Enter data in following formate <Subject name>
<marks>");
for(int j=0;j<a;j++){
inp=ip.readLine().split(" ");
std[i].subname[j]=inp[0];
std[i].mark[j]=Double.parseDouble(inp[1]);
}
}
for(int i=0;i<n;i++)
System.out.println(std[i].name+" "+std[i].total());
}
}
Page 1
SingleInheritance.java
import java.io.*;
class Subjects {
String subname[];
int no;
double mark[];
}
class Students extends Subjects {
String name;
Students(int n){
no=n;
mark = new double[n];
subname = new String[n];
}
double total(){
double total_marks=0;
for(int i=0;i<no;i++)
total_marks+=mark[i];
return total_marks;
}
}
public class SingleInheritance {
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
BufferedReader ip = new BufferedReader(new InputStreamReader
(System.in));
System.out.println("Enter the number of students");
int n=Integer.parseInt(ip.readLine());
String inp[]=new String[3];
Students std[]=new Students[n];
for(int i=0;i<n;i++){
System.out.print("Name : ");
String name=ip.readLine();
System.out.print("Number of Subjects: ");
int a=Integer.parseInt(ip.readLine());
std[i]=new Students(a);
std[i].name=name;
System.out.println("Enter data in following formate <Subject name>
<marks>");
for(int j=0;j<a;j++){
inp=ip.readLine().split(" ");
std[i].subname[j]=inp[0];
std[i].mark[j]=Double.parseDouble(inp[1]);
}
}
for(int i=0;i<n;i++)
System.out.println(std[i].name+" "+std[i].total());
}
}
Page 1
QueueOperation.java
import java.io.*;
public class QueueOperation {
public static void main(String[] args)throws IOException {
int r,ch;
r=-1;
int queue[]=new int[100];
BufferedReader ip=new BufferedReader(new InputStreamReader
(System.in));
System.out.println("Menu \n1 Insert\n2 Delete\n3 Dispay\n4 Exit");
while(true){
System.out.println("Enter your selection from menu");
ch=Integer.parseInt(ip.readLine());
switch(ch){
case 1:
if(r==99)
System.out.println("Queue is full");
else {
System.out.println("Enter the number to be inserted");
int ele=Integer.parseInt(ip.readLine());
queue[++r]= ele;
}
break;
case 2:
if(r<0)
System.out.println("Queue is empty");
else {
System.out.println("Deleted "+queue[0]);
for(int i=0;i<r;i++)
queue[i]=queue[i+1];
r--;
}
break;
case 3:
if(r<0)
System.out.println("Queue is empty");
else{
System.out.println("Queue contains");
for(int i=0;i<=r;i++)System.out.print(queue[i]+" ");
System.out.println();
}
break;
default:System.exit(0);
}
}
}
}
Page 1
ExeptionHandle.java
import java.io.*;
public class ExeptionHandle {
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
BufferedReader ip= new BufferedReader(new InputStreamReader
(System.in));
try{
int a,b;
System.out.println("Enter two integers");
a=Integer.parseInt(ip.readLine());
b=Integer.parseInt(ip.readLine());
int c=a/b;
System.out.println("C = "+c);
}
catch(IOException e){
System.out.println("Invali Input");
}
catch(NumberFormatException e){
System.out.println("Invalid number");
}
catch(ArithmeticException e){
System.out.println("You are trying to divide a number by zero");
}
}
}
Page 1
StringOverload.java
import java.io.*;
class string{
private char s[];
private int length;
string(){
s = new char[100000];
length=0;
}
string(String a){
s = new char[100000];
for(int i=0;i<a.length();i++)
s[i]=a.charAt(i);
length=a.length();
}
public void copyValueOf(String b){
s=b.toCharArray();
length=b.length();
}
public int length(){
return length;
}
public string concat(string b){
for(int i=length;i < b.length() +length;i++)
s[i]=b.charAt(i-length);
length+=b.length();
return this;
}
int compareTo(string b){
for(int i=0; i < Math.min(this.length(),b.length()); i++)
if(this.charAt(i)!=b.charAt(i)){
//println(this.charAt(i)+" "+b.charAt(i));
return this.charAt(i)-b.charAt(i);
}
if(b.length()>this.length())
return -1;
if(b.length()<this.length())
return 1;
return 0;
}
char charAt(int i){
if(i>length){
Page 1
StringOverload.java
System. out .println("Index out of bound");
throw new ArrayIndexOutOfBoundsException();
}
return s[i];
}
}
public class StringOverload {
private static void print(String op){
System.out.print(op);
}
private static void println(String op){
System.out.println(op);
}
public static void main(String[] args) throws IOException {
BufferedReader ip=new BufferedReader(new InputStreamReader(System. in
));
println("Enter the first string");
string mystr1=new string(ip.readLine());
println("length of string1 : "+mystr1.length()+"\n");
println("Enter the another string");
string mystr2=new string(ip.readLine());
println("length of string1 : "+mystr2.length()+"\n");
println("Contatinating string2 to string1....");
mystr1.concat(mystr2);
print("string2 : ");
for(int i=0;i<mystr1.length();i++)
print(""+mystr1.charAt(i));
println("\n");
println("Copying \"new string\" to string2....");
mystr2.copyValueOf("new string");
print("string2 : ");
for(int i=0;i<mystr2.length();i++)
print(""+mystr2.charAt(i));
println("\n");
println("check charAt(n)... Enter n :");
int n=Integer.parseInt(ip.readLine());
print("Char at "+n+" in string1 is "+mystr1.charAt(n));
println("\n");
println("comparision string1 with \"hkbharath\" : "
+ mystr1.compareTo(new string("hkbharath")));
}
}
Page 2
matrixMul.java
import java.io.*;
public class matrixMul {
public static void main(String[] args) throws IOException {
BufferedReader ip= new BufferedReader(new InputStreamReader
(System.in));
System.out.println("Enter the size of matrix A");
String inp[]=new String[25];
inp=ip.readLine().split(" ");
int m1=Integer.parseInt(inp[0]);
int n1=Integer.parseInt(inp[1]);
int mat1[][]=new int[m1][];
System.out.println("Enter Matrix A");
for(int i=0;i<m1;i++){
mat1[i]=new int[n1];
inp=ip.readLine().split(" ");
for(int j=0;j<n1;j++)
mat1[i][j]=Integer.parseInt(inp[j]);
}
System.out.println("Enter the size of matrix B");
inp=ip.readLine().split(" ");
int m2=Integer.parseInt(inp[0]);
int n2=Integer.parseInt(inp[1]);
int mat2[][]=new int [m2][];
System.out.println("Enter Matrix B");
for(int i=0;i<m2;i++){
mat2[i]=new int[n2];
inp=ip.readLine().split(" ");
for(int j=0;j<n2;j++)
mat2[i][j]=Integer.parseInt(inp[j]);
}
if(m2!=n1){
System.out.println("Matrixes are not compitable for
multiplication");
System.exit(0);
}
int ans[][]=new int[m1][],sum=0;
for(int i=0;i<m1;i++){
ans[i]=new int[n2];
for(int j=0;j<n2;j++){
sum=0;
for(int k=0;k<n1;k++)
sum=sum+mat1[i][k]*mat2[k][j];
ans[i][j]=sum;
}
}
System.out.println("Solution is ");
for(int i=0;i<m1;i++){
for(int j=0;j<n2;j++)
System.out.print(ans[i][j]+" ");
System.out.println();
}
}
}
Page 1
FreqUsingStrTokens.java
import java.io.*;
public class FreqUsingStrTokens {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader ip=new BufferedReader(new InputStreamReader
(System.in));
System.out.println("Enter the text");
String text=ip.readLine();
System.out.println("Enter the world");
String w=ip.readLine();
StringTokenizer Ttext=new StringTokenizer(text);
int count=0;
while(Ttext.hasMoreTokens()){
if(Ttext.nextToken().equals(w))
count++;
}
System.out.println("Count = "+count);
}
}
Page 1
BubbleSOrt.java
import java.io.*;
public class BubbleSOrt {
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
int n;
BufferedReader ip= new BufferedReader(new InputStreamReader
(System.in));
System.out.print("Number of elements = ");
n=Integer.parseInt(ip.readLine());
System.out.println("Enter the array elemets");
int ar[]=new int[n];
for(int i=0;i<n;i++)
ar[i]=Integer.parseInt(ip.readLine());
BubbleSOrt my = new BubbleSOrt();
my.sort(ar);
System.out.println("Soted array is ");
for(int i=0;i<n;i++)
System.out.print(ar[i]+" ");
}
void sort(int[] ar){
int n=ar.length;
for(int i=0;i<n;i++)
for(int j=0;j<n-i-1;j++)
if(ar[j]>ar[j+1]){
int temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;
}
}
}
Page 1
ComplexOperation.java
import java.io.*;
class Complex{
double real;
double img;
Complex add(Complex b){
Complex c=new Complex();
c.real=this.real+b.real;
c.img=this.img+b.img;
return c;
}
Complex subt(Complex b){
Complex c=new Complex();
c.real=this.real-b.real;
c.img=this.img-b.img;
return c;
}
Complex mul(Complex b){
Complex c=new Complex ();
c.real=this.real*b.real - this.img*b.img;
c.img=this.real*b.img + this.img*b.real;
return c;
}
Complex div(Complex b){
Complex conjugate = new Complex();
conjugate.real=b.real;
conjugate.img=-1*b.img;
Complex numerator = new Complex();
numerator = this.mul(conjugate);
Complex dinominator = new Complex();
dinominator = b.mul(conjugate);
numerator.real/=dinominator.real;
numerator.img/=dinominator.real;
return numerator;
}
}
public class ComplexOperation {
String print(Complex a){
return ""+a.real+"+ i("+a.img+")";
}
public static void main(String[] args)throws IOException {
BufferedReader ip=new BufferedReader(new InputStreamReader
(System.in));
int ch;
String inp[]=new String[5];
System.out.println("Complex Overload demonstration");
System.out.println("Options\n1 Add\n2 Subtract\n"
Page 1
ComplexOperation.java
+ "3 Mutiply\n4 Divide\n5 Exit");
ComplexOperation my=new ComplexOperation();
Complex a=new Complex();
Complex b=new Complex();
while(true){
System.out.println("Enter your choice");
ch=Integer.parseInt(ip.readLine());
if(ch<5){
System.out.println("Enter two complex numbers "
+ "in defferent line "
+"example <5.2 +i -3.5>");
inp=ip.readLine().split(" ");
a.real=Double.parseDouble(inp[0]);
a.img=Double.parseDouble(inp[2]);
inp=ip.readLine().split(" ");
b.real=Double.parseDouble(inp[0]);
b.img=Double.parseDouble(inp[2]);
}
switch(ch){
case 1:System.out.println("Result = "+my.print(a.add(b)));break;
case 2:System.out.println("Result = "+my.print(a.subt(b)));break;
case 3:System.out.println("Result = "+my.print(a.mul(b)));break;
case 4:System.out.println("Result = "+my.print(a.div(b)));break;
default :System.out.println("Exiting.......");System.exit(0);
}
}
}
}
Page 2
newtonRamp.java
import java.io.*;
public class newtonRamp {
double f(int[] eq,double x){
double val=0;
for(int i=0;i<eq.length;i++){
val=val+Math.pow(x,i)*eq[i];
}
return val;
}
double solution(int[] eq,double st,int deg){
int[] deq=new int[deg];
for(int j=0;j<deg;j++){
deq[j]=(j+1)*eq[j+1];
//System.out.println(j+" "+deq[j]);
}
for(int i=0;i<100;i++){
st=st-f(eq,st)/f(deq,st);
}
return st;
}
public static void main(String[] args)throws IOException {
BufferedReader ip=new BufferedReader(new InputStreamReader
(System.in));
newtonRamp my=new newtonRamp();
System.out.println("Enetr the equation");
System.out.println("Format < 1x2-2x1-2x0 > for equation"
+ " \"x square - 2*x - 2\" (!!dont add extra spaces!!)");
String eqs=new String();
eqs=ip.readLine();
System.out.println("Enter the Expexted root");
int st=Integer.parseInt(ip.readLine());
int n=eqs.length();
int[] eq=new int[n];
int deg=0,pos,val;
for(int i=1;i<n;i++){
if(eqs.charAt(i)=='x'){
pos=Integer.parseInt(eqs.substring(i+1,i+2));
if(i-2>=0 && eqs.charAt(i-2)=='-')
val=-1*Integer.parseInt(eqs.substring(i-1,i));
else
val=Integer.parseInt(eqs.substring(i-1,i));
//System.out.println(pos+" "+val);
if(pos>deg)deg=pos;
eq[pos]=val;
}
}
System.out.println("Approximate solution = "+my.solution(eq,st,deg));
}
}
Page 1
ThreadsExample.java
class ThA extends Thread{
ThA(){
super ("Thread A");
System.out.println("Thread A : "+this+" started");
start();
}
public void run(){
try{
for(int i=1;i<=5;i++){
System.out.println("Thread A : "+i);
Thread.sleep(50);
}
}catch(InterruptedException e){
System.out.println("Thread Interrupted");
}
System.out.println("Thread A Exiting.....");
}
}
class ThB extends Thread{
ThB(){
super("Thread B");
System.out.println("Thread B : "+this+" started");
start();
}
public void run(){
try{
for(int i=1;i<=5;i++){
System.out.println("Thread B : "+i);
Thread.sleep(100);
}
}catch(InterruptedException e){
System.out.println("Thread Interrupted");
}
System.out.println("Thread B Exiting.....");
}
}
class ThC extends Thread{
ThC(){
super ("Thread C");
System.out.println("Thread C : "+this+" started");
start();
}
public void run(){
try{
for(int i=1;i<=5;i++){
System.out.println("Thread C : "+i);
Thread.sleep(200);
}
}catch(InterruptedException e){
System.out.println("Thread Interrupted");
Page 1
ThreadsExample.java
}
System.out.println("Thread C Exiting.....");
}
}
public class ThreadsExample {
public static void main(String[] args) {
new ThC();
new ThB();
new ThA();
try{
for(int i=1;i<=5;i++){
System.out.println("Main Thread "+i);
Thread.sleep(400);
}
}catch(InterruptedException e){
System.out.println("Thread Interrupted");
}
System.out.println("Main thread ended");
}
}
Page 2
BinarySearch.java
import java.io.*;
public class BinarySearch {
public static void main(String[] args) throws NumberFormatException,
IOException {
// TODO Auto-generated method stub
BufferedReader ip=new BufferedReader(new InputStreamReader
(System.in));
System.out.print("Number of elements = ");
int n=Integer.parseInt(ip.readLine());
System.out.println("Enter the elements");
int ar[]=new int[n];
for(int i=0;i<n;i++)
ar[i]=Integer.parseInt(ip.readLine());
System.out.println("Enter the key");
int key=Integer.parseInt(ip.readLine());
Arrays.sort(ar);
BinarySearch my = new BinarySearch();
System.out.println(my.Search(ar,key));
}
String Search(int[] ar,int key){
int n=ar.length;
int low=0,high=n-1;
int mid;
while(low<=high){
mid=(low+high)/2;
if(ar[mid]==key)
return "Key found in "+mid;
if(ar[mid]<key)
low=mid+1;
else high=mid-1;
}
return "Key is not found";
}
}
Page 1
interfaceExample.java
import java.io.*;
interface common{
void set_data(String n,double val);
void show_data();
}
class teacher implements common{
String name;
double sal;
public void set_data(String n,double val){
name=n;
sal=val;
}
public void show_data(){
System.out.println("Name : "+name+"\nSalary : "+sal);
}
}
class student implements common{
String name;
double per;
public void set_data(String n,double val){
name=n;
per=val;
}
public void show_data(){
System.out.println("Name : "+name+"\nAggrigate Marks : "+per);
}
}
public class interfaceExample {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader ip=new BufferedReader(new InputStreamReader
(System.in));
teacher tech = new teacher();
student stud = new student();
common inf;
inf=tech;
System.out.print("Teacher Details\nName: ");
String name=ip.readLine();
System.out.print("Salary : ");
double val=Double.parseDouble(ip.readLine());
inf.set_data(name,val);
inf=stud;
System.out.print("Student Details\nName: ");
name=ip.readLine();
System.out.print("Aggrigate Marks : ");
val=Double.parseDouble(ip.readLine());
Page 1
interfaceExample.java
inf.set_data(name,val);
while(true){
System.out.println("Enter 'S' for Student Details or "
+ "Enter 'T' for Teacher Details"
+ "Enter any other Charecter to Exit");
name=ip.readLine();
if(name.equals("S"))
inf=stud;
else if(name.equals("T"))
inf=tech;
else{
System.out.println("Invalid Input\nExiting.....");
break;
}
inf.show_data();
}
}
}
Page 2
appletDemo1.java
import java.applet.Applet;
import java.awt.*;
public class appletDemo1 extends Applet{
int p1,p2;
public void init(){
p1 = Integer.parseInt(getParameter("par1"));
p2 = Integer.parseInt(getParameter("par2"));
}
public void paint(Graphics g){
Font myfont = new Font("Arial",Font.BOLD,12);
g.setFont(myfont);
g.setColor(Color.RED);
g.drawString("parameters are "+p1+" and "+p2, 10, 30);
myfont = new Font("Times New Roman",Font.BOLD,18);
g.setFont(myfont);
g.setColor(Color.BLUE);
g.drawString("Code Base :"+getCodeBase().toString(),10,60);
g.drawString("Doucument Base : "+getDocumentBase().toString(), 10,
90);
}
}
Page 1
appletDemo2.java
import
import
import
import
java.applet.Applet;
java.awt.*;
java.awt.event.ActionEvent;
java.awt.event.ActionListener;
Page 1