Documente Academic
Documente Profesional
Documente Cultură
import java.util.*;
public class n_nobubble
{
a[i]=sc.nextInt();
}
for(j=0;j<n-1;j++)
{
for(i=0;i<n-1;i++)
{
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
System.out.println(the given no is in increment form \n);
for(i=0;i<n;i++)
{
System.out.print(\t+a[i]);
}}}
Develop a java program using interface to implement a stack adt.
import java.io.*;
interface stackoperation
{
public void push(int i);
public void pop();
}
class Astack implements stackoperation
{
int stack[];
int top;
Astack()
{
stack=new int[10];
top=0;
}
public void push(int item)
{
if(stack[top]==10)
System.out.println("overflow");
else
{
stack[++top]=item;
System.out.println("item pushed");
}
}
public void pop()
{
if(stack[top]<=0)
System.out.println("underflow");
else
{
stack[top]=top--;
System.out.println("item popped");
}
}
public void display()
{
for(int i=1;i<=top;i++)
System.out.println("element:"+stack[i]);
}
}
class liststack implements stackoperation
{
node top,q;
int count;
public void push(int i)
{
node n=new node(i);
n.link=top;
top=n;
count++;
}
public void pop()
{
if(top==null)
System.out.println("under flow");
else
{
int p=top.data;
top=top.link;
count--;
System.out.println("popped element:"+p);
}
}
void display()
{
for(q=top;q!=null;q=q.link)
{
System.out.println("the elements are:"+q.data);
}
}
class node
{
int data;
node link;
node(int i)
{
data=i;
link=null;
}
}
}
class sample
{
public static void main(String args[])throws IOException
{
int ch,x=1,p=0,t=0;
DataInputStream in=new DataInputStream(System.in);
do
{
try
{
System.out.println("----------------------------------");
System.out.println("1.Arraystack 2.liststack 3.exit");
System.out.println("-----------------------------------");
System.out.println("enter ur choice:");
int c=Integer.parseInt(in.readLine());
Astack s=new Astack();
switch(c)
{
case 1:
do
{
if(p==1)
break;
System.out.println("ARRAY STACK");
System.out.println("1.push 2.pop 3.display 4.exit");
System.out.println("enter ur choice:");
ch=Integer.parseInt(in.readLine());
switch(ch)
{
case 1:System.out.println("enter the value to push:");
int i=Integer.parseInt(in.readLine());
s.push(i);
break;
case 2:
s.pop();
break;
case 3:
System.out.println("the elements are:");
s.display();
break;
case 4:
p=1;
continue;
}
}while(x!=0);
break;
case 2:
liststack l=new liststack();
do
{
if(t==1)
break;
System.out.println("LIST STACK:");
System.out.println("1.push 2.pop 3.display 4.exit");
System.out.println("enter your choice:");
ch=Integer.parseInt(in.readLine());
switch(ch)
{
case 1:
System.out.println("enter the value for push:");
int a=Integer.parseInt(in.readLine());
l.push(a);
break;
case 2:
l.pop();
break;
case 3:
l.display();
break;
case 4:
t=1;
continue;
}
}
while(x!=0);
break;
case 3:
System.exit(0);
}
}
catch(IOException e)
{
System.out.println("io error");
}
}
while(x!=0);
}
}
import java.util.Scanner;
class AddTwoMatrix
{
public static void main(String args[])
{
int m, n, c, d;
Scanner in = new Scanner(System.in);
System.out.println();
}
}
}
Develop a java program using applets that can draw basic graphical primitives in
different dimensions and colors. Use appropriate menus and buttons
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class paint extends Frame implements ActionListener
{
int x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,n=5,ch=1;
int xpoints[]=new int[n];
int ypoints[]=new int[n];
MenuBar mb;
Menu op,col;
MenuItem l,c,r,a,po,x,br,bb,bg,fr,fb,fg;
Panel p;
TextField t1,t2,t3,t4,t5,t6,t7,t8,t9,t10;
Label l1,l2,l3,l4,l5,l6,l7,l8,l9,l10;
Button b;
paint()
{
p=new Panel();
t1=new TextField(4);
t2=new TextField(4);
t3=new TextField(4);
t4=new TextField(4);
l1=new Label("x1");
l2=new Label("y1");
l3=new Label("x2");
l4=new Label("y2");
t5=new TextField(4);
t6=new TextField(4);
l5=new Label("height");
l6=new Label("width");
t7=new TextField(4);
t8=new TextField(4);
t9=new TextField(4);
t10=new TextField(4);
l7=new Label("yaxis");
l8=new Label("yaxis");
l9=new Label("yaxis");
l10=new Label("yaxis");
b=new Button("ok");
p.add(l1);
p.add(t1);
p.add(l2);
p.add(t2);
p.add(l3);
p.add(t3);
p.add(l4);
p.add(t4);
p.add(l5);
p.add(t5);
p.add(l6);
p.add(t6);
p.add(l7);
p.add(t7);
p.add(l8);
p.add(t8);
p.add(l9);
p.add(t9);
p.add(l10);
p.add(t10);
p.add(b);
b.addActionListener(this);
setLayout(new BorderLayout());
add("South",p);
mb=new MenuBar();
setMenuBar(mb);
op=new Menu("option");
op.add(l=new MenuItem("Line"));
op.add(c=new MenuItem("Circle"));
op.add(r=new MenuItem("Rectangle"));
op.add(a=new MenuItem("Arc"));
op.add(po=new MenuItem("Polygon"));
op.add(x=new MenuItem("exit"));
col=new Menu("Color");
Menu back=new Menu("Background");
back.add(br=new MenuItem("Red"));
back.add(bg=new MenuItem("Green"));
back.add(bb=new MenuItem("Blue"));
col.add(back);
Menu fore=new Menu("Foreground");
fore.add(fr=new MenuItem("Red"));
fore.add(fg=new MenuItem("Green"));
fore.add(fb=new MenuItem("Blue"));
col.add(fore);
mb.add(op);
mb.add(col);
br.addActionListener(this);
bg.addActionListener(this);
bb.addActionListener(this);
fr.addActionListener(this);
fg.addActionListener(this);
fb.addActionListener(this);
l.addActionListener(this);
c.addActionListener(this);
r.addActionListener(this);
a.addActionListener(this);
po.addActionListener(this);
x.addActionListener(this);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void paint(Graphics g)
{
switch(ch)
{
case 1: g.drawLine(x1,y1,x2,y2);
break;
case 2: g.drawOval(x1,y1,x2,y2);
break;
case 3: g.drawRect(x1,y1,x2,y2);
break;
case 4: g.drawArc(x1,y1,x2,y2,x3,y3);
break;
case 5: xpoints[0]=x1;
xpoints[1]=x2;
xpoints[2]=x3;
xpoints[3]=x4;
xpoints[4]=x5;
ypoints[0]=y1;
ypoints[1]=y2;
ypoints[2]=y3;
ypoints[3]=y4;
ypoints[4]=y5;
g.drawPolygon(xpoints,ypoints,n);
break;
}
}
public void actionPerformed(ActionEvent e)
{
String s=e.getActionCommand();
Object s1=e.getSource();
if(s1==br)
setBackground(Color.red);
else if(s1==bg)
setBackground(Color.green);
else if(s1==bb)
setBackground(Color.blue);
else if(s1==fr)
setForeground(Color.red);
else if(s1==fg)
setForeground(Color.green);
else if(s1==fb)
setForeground(Color.blue);
if(s.equals("exit"))
System.exit(0);
else if(s.equals("Line"))
{
ch=1;
}
else if(s.equals("Circle"))
ch=2;
else if(s.equals("Rectangle"))
ch=3;
else if(s.equals("Arc"))
{
ch=4;
}
else if(s.equals("Polygon"))
{
ch=5;
}
else if(s.equals("ok"))
{
x1=Integer.parseInt(t1.getText());
x2=Integer.parseInt(t2.getText());
y1=Integer.parseInt(t3.getText());
y2=Integer.parseInt(t4.getText());
x3=Integer.parseInt(t5.getText()+0);
y3=Integer.parseInt(t6.getText()+0);
x4=Integer.parseInt(t7.getText()+0);
y4=Integer.parseInt(t8.getText()+0);
x5=Integer.parseInt(t9.getText()+0);
y5=Integer.parseInt(t10.getText()+0);
repaint();
}
}
sin=new JButton("SIN");
buttonpanel.add(sin);
sin.addActionListener(this);
mr=new JButton("MR");
buttonpanel.add(mr);
mr.addActionListener(this);
seven=new JButton("7");
buttonpanel.add(seven);
seven.addActionListener(this);
eight=new JButton("8");
buttonpanel.add(eight);
eight.addActionListener(this);
nine=new JButton("9");
buttonpanel.add(nine);
nine.addActionListener(this);
clr=new JButton("AC");
buttonpanel.add(clr);
clr.addActionListener(this);
cos=new JButton("COS");
buttonpanel.add(cos);
cos.addActionListener(this);
mc=new JButton("MC");
buttonpanel.add(mc);
mc.addActionListener(this);
four=new JButton("4");
buttonpanel.add(four);
four.addActionListener(this);
five=new JButton("5");
buttonpanel.add(five);
five.addActionListener(this);
six=new JButton("6");
buttonpanel.add(six);
six.addActionListener(this);
mul=new JButton("*");
buttonpanel.add(mul);
mul.addActionListener(this);
tan=new JButton("TAN");
buttonpanel.add(tan);
tan.addActionListener(this);
mp=new JButton("M+");
buttonpanel.add(mp);
mp.addActionListener(this);
one=new JButton("1");
buttonpanel.add(one);
one.addActionListener(this);
two=new JButton("2");
buttonpanel.add(two);
two.addActionListener(this);
three=new JButton("3");
buttonpanel.add(three);
three.addActionListener(this);
min=new JButton("-");
buttonpanel.add(min);
min.addActionListener(this);
pow2=new JButton("x^2");
buttonpanel.add(pow2);
pow2.addActionListener(this);
mm=new JButton("M-");
buttonpanel.add(mm);
mm.addActionListener(this);
zero=new JButton("0");
buttonpanel.add(zero);
zero.addActionListener(this);
plmi=new JButton("+/-");
buttonpanel.add(plmi);
plmi.addActionListener(this);
poin=new JButton(".");
buttonpanel.add(poin);
poin.addActionListener(this);
plus=new JButton("+");
buttonpanel.add(plus);
plus.addActionListener(this);
pow3=new JButton("x^3");
buttonpanel.add(pow3);
pow3.addActionListener(this);
rec=new JButton("1/x");
buttonpanel.add(rec);
rec.addActionListener(this);
sqrt=new JButton("Sqrt");
buttonpanel.add(sqrt);
sqrt.addActionListener(this);
lg=new JButton("log");
buttonpanel.add(lg);
lg.addActionListener(this);
div=new JButton("/");
div.addActionListener(this);
buttonpanel.add(div);
eq=new JButton("=");
buttonpanel.add(eq);
eq.addActionListener(this);
cont.add("Center",buttonpanel);
cont.add("North",textpanel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
String s=e.getActionCommand();
if(s.equals("1"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"1");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"1");
z=0;
}
}
if(s.equals("2"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"2");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"2");
z=0;
}
}
if(s.equals("3"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"3");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"3");
z=0;
}
}
if(s.equals("4"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"4");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"4");
z=0;
}
}
if(s.equals("5"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"5");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"5");
z=0;
}
}
if(s.equals("6"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"6");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"6");
z=0;
}
}
if(s.equals("7"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"7");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"7");
z=0;
}
}
if(s.equals("8"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"8");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"8");
z=0;
}
}
if(s.equals("9"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"9");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"9");
z=0;
}
}
if(s.equals("0"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"0");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"0");
z=0;
}
}
if(s.equals("AC"))
{
jtx.setText("");
x=0;
y=0;
z=0;
}
if(s.equals("log"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
a=Math.log(Double.parseDouble(jtx.getText()));
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}
if(s.equals("1/x"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
a=1/Double.parseDouble(jtx.getText());
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}
if(s.equals("x^2"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
a=Math.pow(Double.parseDouble(jtx.getText()),2);
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}
if(s.equals("x^3"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
a=Math.pow(Double.parseDouble(jtx.getText()),3);
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}
if(s.equals("+/-"))
{
if(x==0)
{
jtx.setText("-"+jtx.getText());
x=1;
}
else
{
jtx.setText(jtx.getText());
}
}
if(s.equals("."))
{
if(y==0)
{
jtx.setText(jtx.getText()+".");
y=1;
}
else
{
jtx.setText(jtx.getText());
}
}
if(s.equals("+"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
temp=0;
ch='+';
}
else
{
temp=Double.parseDouble(jtx.getText());
jtx.setText("");
ch='+';
y=0;
x=0;
}
jtx.requestFocus();
}
if(s.equals("-"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
temp=0;
ch='-';
}
else
{
x=0;
y=0;
temp=Double.parseDouble(jtx.getText());
jtx.setText("");
ch='-';
}
jtx.requestFocus();
}
if(s.equals("/"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
temp=1;
ch='/';
}
else
{
x=0;
y=0;
temp=Double.parseDouble(jtx.getText());
ch='/';
jtx.setText("");
}
jtx.requestFocus();
}
if(s.equals("*"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
temp=1;
ch='*';
}
else
{
x=0;
y=0;
temp=Double.parseDouble(jtx.getText());
ch='*';
jtx.setText("");
}
jtx.requestFocus();
}
if(s.equals("MC"))
{
m1=0;
jtx.setText("");
}
if(s.equals("MR"))
{
jtx.setText("");
jtx.setText(jtx.getText() + m1);
}
if(s.equals("M+"))
{
if(k==1)
{
m1=Double.parseDouble(jtx.getText());
k++;
}
else
{
m1+=Double.parseDouble(jtx.getText());
jtx.setText(""+m1);
}
}
if(s.equals("M-"))
{
if(k==1)
{
m1=Double.parseDouble(jtx.getText());
k++;
}
else
{
m1-=Double.parseDouble(jtx.getText());
jtx.setText(""+m1);
}
}
if(s.equals("Sqrt"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
a=Math.sqrt(Double.parseDouble(jtx.getText()));
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}
if(s.equals("SIN"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
a=Math.sin(Double.parseDouble(jtx.getText()));
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}
if(s.equals("COS"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
a=Math.cos(Double.parseDouble(jtx.getText()));
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}
if(s.equals("TAN"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
a=Math.tan(Double.parseDouble(jtx.getText()));
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}
if(s.equals("="))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
temp1 = Double.parseDouble(jtx.getText());
switch(ch)
{
case '+':
result=temp+temp1;
break;
case '-':
result=temp-temp1;
break;
case '/':
result=temp/temp1;
break;
case '*':
result=temp*temp1;
break;
}
jtx.setText("");
jtx.setText(jtx.getText() + result);
z=1;
}
}
jtx.requestFocus();
}
public static void main(String args[])
{
scientific n=new scientific();
n.setTitle("CALCULATOR");
n.setSize(370,250);
n.setResizable(false);
n.setVisible(true);
}
}
Develop a java program that creates a string object and initializes it with your name and
perform the operations;
Find the length
Find whether the character a is present
class StringDemo
{
public static void main(String args[])
{
String name= "Java Programming";
int count=0;
System.out.prinln("The given name is"+name);
System.out.println("The length of name is"+name.length());
if(name.indexOf('a')<0)
System.out.println("The character 'a' is not present in my name");
else
{
System.out.println("The character 'a' is present in the locations");
int loc=0;
while (loc<name.lastIndexOf('a'))
{
int x=name.indexOf('a',loc);
System.out.println(x);
loc=x+1;
count++;
}
System.out.println("The character 'a' is present"+count+"times");
}
}
}
Develop a java program for generating two threads to perform following operations
Generates n prime numbers
Generates n Fibonacci numbers
import java.io.*;
import java.io.PipedWriter;
import java.io.PipedReader;
class fibonacci extends Thread
{
PipedWriter fw=new PipedWriter();
public PipedWriter getwrite()
{
return fw;
}
public void run()
{
super.run();
fibo();
}
int f(int n)
{
if(n<2)
return n;
else
return f(n-1)+f(n-2);
}
void fibo()
{
for(int i=2,fibv=0;(fibv=f(i))<100000;i++)
{
try{
fw.write(fibv);
}
catch(IOException e){
}
}
}
}
class receiver extends Thread
{
PipedReader fibr,primer;
public receiver(fibonacci fib,prime pr)throws IOException
{
fibr=new PipedReader(fib.getwrite());
primer=new PipedReader(pr.getwrite());
}
public void run()
{
int p=0,f=0;
try{
p=primer.read();
f=fibr.read();
}
catch(IOException e)
{
}
while(true)
{
try
{
if(p==f){
System.out.println ("Match:"+p);
p=primer.read();
f=fibr.read();
}
else if(f<p)
f=fibr.read();
else
p=primer.read();
}catch(IOException e)
{System.exit(-1);
}
}
}
}
class prime extends Thread
{
PipedWriter pw=new PipedWriter();
public PipedWriter getwrite()
{
return pw;
}
public void run()
{
super.run();
prim();
}
public void prim()
{
for(int i=2;i<100000;i++)
{
if(isprime(i))
{
try{
pw.write(i);
}
catch(IOException e){
}
}
}
}
boolean isprime(int n)
{
boolean p=true;
int s=(int)Math.sqrt(n);
for(int i=2;i<=s;i++)
{
if(n%i==0)
p=false;
}
return p;
}
}
class fibprime
{
public static void main (String[] args)throws IOException {
fibonacci fi=new fibonacci();
prime pri=new prime();
receiver r=new receiver(fi,pri);
fi.start();
pri.start();
r.start();
}
}
Producer
package trail;
import java.util.Random;
import java.util.Stack;
public Thread1(Stack<Integer> A) {
this.A = A;
}
this.notify();
}
@Override
public void run() {
System.out.println("Producer get started");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
while (true) {
produce();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Consumer
package trail;
import java.util.Stack;
public Thread2(Stack<Integer> A) {
this.A = A;
}
public synchronized void consume() {
while (A.isEmpty()) {
System.err.println("List is empty" + A + A.size());
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.err.println(A.pop() + " Consumed " + A);
this.notify();
}
@Override
public void run() {
System.out.println("New consumer get started");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
while (true) {
consume();
}
}
}
Main
public static void main(String[] args) {
B.start();
C.start();
}
Develop a java program to find transpose of a matrix
public class Trans {
public static void main(String args[]) {
// initialize the variable
int[][] a = { { 5, 1, 1 }, { 3, 6, 0 }, { 0, 5, 9 } };
// print the matrix
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(a[i][j]);
}
System.out.println();
}
// operation for transpose
for (int i = 0; i < 3; i++) {
for (int j = i + 1; j < 3; j++) {
int temp = a[i][j];
a[i][j] = a[j][i];
a[j][i] = temp;
}
System.out.println();
}
System.out.println("Transpose matrix:");
// After Transpose the matrix print the result
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(a[i][j]);
}
System.out.println();
}
}
}
Develop ajava program for implementing a stack data structure using linked list
.perform push and pop operations
import java.io.*;
class Node
{
public int item;
public Node next;
public Node(int val)
{
item = val;
}
public void displayNode()
{
System.out.println("[" + item + "] ");
}
}
class LinkedList
{
private Node first;
public LinkedList()
{
first = null;
}
public boolean isEmpty()
{
return (first==null);
}
public void insert(int val)//inserts at beginning of list
{
Node newNode = new Node(val);
newNode.next = first;
first = newNode;
}
public Node delete()//deletes at beginning of list
{
Node temp = first;
first = first.next;
return temp;
}
public void display()
{
System.out.println("Elements from top to bottom");
Node current = first;
while(current != null)
{
current.displayNode();
current = current.next;
}
System.out.println("");
}
}
class Stack
{
private LinkedList listObj;
public Stack()
{
listObj = new LinkedList();
}
public void push(int num)
{
listObj.insert(num);
}
public Node pop()
{
return listObj.delete();
}
public boolean isEmpty()
{
return listObj.isEmpty();
}
public void displayStack()
{
System.out.print("Stack : ");
listObj.display();
}
}
class StackLinkedListBlog
{
public static void main(String[] args) throws IOException
{
Stack demo = new Stack();
demo.push(10);
demo.push(20);
demo.displayStack();
demo.push(30);
demo.push(40);
demo.displayStack();
demo.pop();
demo.pop();
System.out.println("Two elements are popped");
demo.displayStack();
}
}
Develop a java program for implementing singly linked list data structure
import java.util.Scanner;
/* Class Node */
class Node
{
protected int data;
protected Node link;
/* Constructor */
public Node()
{
link = null;
data = 0;
}
/* Constructor */
public Node(int d,Node n)
{
data = d;
link = n;
}
/* Function to set link to next Node */
public void setLink(Node n)
{
link = n;
}
/* Function to set data to current Node */
public void setData(int d)
{
data = d;
}
/* Function to get link to next node */
public Node getLink()
{
return link;
}
/* Function to get data from current Node */
public int getData()
{
return data;
}
}
/* Class linkedList */
class linkedList
{
protected Node start;
protected Node end ;
public int size ;
/* Constructor */
public linkedList()
{
start = null;
end = null;
size = 0;
}
/* Function to check if list is empty */
public boolean isEmpty()
{
return start == null;
}
/* Function to get size of list */
public int getSize()
{
return size;
}
/* Function to insert an element at begining */
public void insertAtStart(int val)
{
Node nptr = new Node(val, null);
size++ ;
if(start == null)
{
start = nptr;
end = start;
}
else
{
nptr.setLink(start);
start = nptr;
}
}
/* Function to insert an element at end */
public void insertAtEnd(int val)
{
Node nptr = new Node(val,null);
size++ ;
if(start == null)
{
start = nptr;
end = start;
}
else
{
end.setLink(nptr);
end = nptr;
}
}
/* Function to insert an element at position */
public void insertAtPos(int val , int pos)
{
Node nptr = new Node(val, null);
Node ptr = start;
pos = pos - 1 ;
for (int i = 1; i < size; i++)
{
if (i == pos)
{
Node tmp = ptr.getLink() ;
ptr.setLink(nptr);
nptr.setLink(tmp);
break;
}
ptr = ptr.getLink();
}
size++ ;
}
/* Function to delete an element at position */
public void deleteAtPos(int pos)
{
if (pos == 1)
{
start = start.getLink();
size--;
return ;
}
if (pos == size)
{
Node s = start;
Node t = start;
while (s != end)
{
t = s;
s = s.getLink();
}
end = t;
end.setLink(null);
size --;
return;
}
Node ptr = start;
pos = pos - 1 ;
for (int i = 1; i < size - 1; i++)
{
if (i == pos)
{
Node tmp = ptr.getLink();
tmp = tmp.getLink();
ptr.setLink(tmp);
break;
}
ptr = ptr.getLink();
}
size-- ;
}
/* Function to display elements */
public void display()
{
System.out.print("\nSingly Linked List = ");
if (size == 0)
{
System.out.print("empty\n");
return;
}
if (start.getLink() == null)
{
System.out.println(start.getData() );
return;
}
Node ptr = start;
System.out.print(start.getData()+ "->");
ptr = start.getLink();
while (ptr.getLink() != null)
{
System.out.print(ptr.getData()+ "->");
ptr = ptr.getLink();
}
System.out.print(ptr.getData()+ "\n");
}
}
/* Class SinglyLinkedList */
public class SinglyLinkedList
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
/* Creating object of class linkedList */
linkedList list = new linkedList();
System.out.println("Singly Linked List Test\n");
char ch;
/* Perform list operations */
do
{
System.out.println("\nSingly Linked List Operations\n");
System.out.println("1. insert at begining");
System.out.println("2. insert at end");
System.out.println("3. insert at position");
System.out.println("4. delete at position");
System.out.println("5. check empty");
System.out.println("6. get size");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to insert");
list.insertAtStart( scan.nextInt() );
break;
case 2 :
System.out.println("Enter integer element to insert");
list.insertAtEnd( scan.nextInt() );
break;
case 3 :
System.out.println("Enter integer element to insert");
int num = scan.nextInt() ;
System.out.println("Enter position");
int pos = scan.nextInt() ;
if (pos <= 1 || pos > list.getSize() )
System.out.println("Invalid position\n");
else
list.insertAtPos(num, pos);
break;
case 4 :
System.out.println("Enter position");
int p = scan.nextInt() ;
if (p < 1 || p > list.getSize() )
System.out.println("Invalid position\n");
else
list.deleteAtPos(p);
break;
case 5 :
System.out.println("Empty status = "+ list.isEmpty());
break;
case 6 :
System.out.println("Size = "+ list.getSize() +" \n");
break;
default :
System.out.println("Wrong Entry \n ");
break;
}
/* Display List */
list.display();
System.out.println("\nDo you want to continue (Type y or
n) \n");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}
public int x;
class Queue
Queue()
front=rear=null;
temp.x=v;
temp.next=null;
if(front==null)
front=rear=temp;
else
rear.next=temp;
rear=temp;
}
}
int del()
int v=front.x;
node temp=front;
front=front.next;
temp=null;
return v;
class QueueList
q1.insert(30);
q1.insert(40);
q1.insert(50);
System.out.println(q1.del());
System.out.println(q1.del());
System.out.println(q1.del());
}
System.out.print("Enter the element to be searched in
the array: ");
x = sc.nextInt();
for(i=0;i<n;i++)
{
if(x == a[i])
{
found = true;
break;
}
}
if(found)
System.out.println("The number " + x + " is
present in the list");
else
System.out.println("The number " + x + " is not
in the list");
}
}
import java.io.*;
class binary_search
{
public static void main(String args[])throws IOException
{
int i;
InputStreamReader x=new InputStreamReader(System.in);
BufferedReader y=new BufferedReader(x);
int a[]=new int[10];
System.out.println("ENTER THE NUMBER TO BE SEARCHED");
int n=Integer.parseInt(y.readLine());
System.out.println("ENTER 10 NUMBERS FOR THE ARRAY");
for(i=0;i<10;i++)
{
a[i]=Integer.parseInt(y.readLine());
}
System.out.println("CONTENTS OF ARRAY ARE");
for(i=0;i<10;i++)
{
System.out.println(a[i]);
}
System.out.println("NUMBER TO BE SEARCHED IS "+n);
int p=-1,mid,l=0,u=9;
while(l<=u)
{
mid=(l+u)/2;
if(a[mid]==n)
{
p=mid;
break;
}
else if(n> a[mid])
{
l=mid+1;
}
else if(n<a[mid])
{
u=mid-1;
}
}
if(p==-1)
{
System.out.println("NUMBER DOES NOT EXIST IN THE ARRAY");
}
else
{
System.out.println("NUMBER EXISTS AT THE INDEX "+p);
}
}
}
class group{
public static void main(String argn[]){
Scanner data = new Scanner(System.in);
int num, i, temp, max;
// Reading numbers want to be read
System.out.print("Enter number you want to enter:");
num = data.nextInt();
System.out.println("Enter " +num+ " number");
// Reading 1st number
max = data.nextInt();
// Reading other number
for (i=1; i < num; i++) { temp = data.nextInt();
if(temp < max)
continue;
else
// largest number
max = temp;
}
System.out.println("Largest Number is "+max);
}
}
Develop a java program to copy the content of one file into another
import java.io.*;
Develop a java program for complex number include methods for all four basic
arithmetic operators
import java.io.*;
class complex
{
int a,b;
public static int c;
public complex(int x,int y)
{
a=x;b=y;
c++;
}
public static String add(complex n1,complex n2)
{
int a1=n1.a+n2.a;
int b1=n1.b+n2.b;
if(b1<0)
return (a1+" "+b1+"i");
else
return (a1+" "+b1+"i");
}
public static String sub(complex n1,complex n2)
{
int a1=n1.a-n2.a;
int b1=n1.b-n2.b;
if(b1<0)
return (a1+" "+b1+"i");
else
return (a1+" "+b1+"i");
}
public static String mul(complex n1,complex n2)
{
int a1=n1.a*n2.a;
int b1=n1.b*n2.b;
int v1=n1.a*n2.b;
int v2=n2.a*n1.b;
int vi=v1+v2;
if(vi<0)
return(a1-b1+" "+vi+"i");
else
return(a1-b1+"+"+vi+"i");
}
}
class com
{
public static void main(String a[])throws IOException
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
int x,y;
System.out.println("enter the no for complex1:");
x=Integer.parseInt(in.readLine());
y=Integer.parseInt(in.readLine());
System.out.println("enter the no for complex2:");
int m=Integer.parseInt(in.readLine());
int n=Integer.parseInt(in.readLine());
complex c1=new complex(x,y);
complex c2=new complex(m,n);
System.out.println("addition:"+complex.add(c1,c2));
System.out.println("subtraction:"+complex.sub(c1,c2));
System.out.println("multiplication:"+complex.mul(c1,c2));
System.out.println("count="+complex.c);
}
}
import java.io.*;
class MList {
System.out.println("__________________________________________________");
System.out.println("___________________________________________");
System.out.format("%1$-5d", i + 1);
System.out.format("%1$-19s", theStudents.m_studList[i].name);
System.out.format("%1$-7d", theStudents.m_studList[i].marks[0]);
System.out.format("%1$-7d", theStudents.m_studList[i].marks[1]);
System.out.format("%1$-7d", theStudents.m_studList[i].marks[2]);
//System.out.format("%1$-7d", theStudents.m_studList[i].marks[3]);
// System.out.format("%1$-7d", theStudents.m_studList[i].marks[4]);
System.out.format("%1$-7d", theStudents.m_studList[i].total);
System.out.println();
System.out.println("_______________________________________________________
________");
try
String name;
name = reader.readLine();
marks[i - 1] = Integer.parseInt(reader.readLine());
theStudents.AddRecord(name, marks);
catch (Exception e)
e.printStackTrace();
}
public static void main(String[] args) {
try
inpString = reader.readLine();
InputRecords();
ViewRecords();
catch (Exception e)
{
e.printStackTrace();
class CStudent
class CStudents
stud.name = name;
stud.marks = marks;
stud.total = 0;
stud.total += stud.marks[i];
m_studList[m_nMaxStudents++] = stud;
return 1;