Download 2)prime numbers

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
JNTUK VIZIANAGARAM
1) Fibanasis serice:
public class FibonaciEx{
int a;
int b;
int fib;
public void Recursion(int a,int b,int n){
fib=a+b;
if(fib<=n){
System.out.println("Using Recursion Method\t"+fib);
a=b;
b=fib;
Recursion(a,b,n);
}
}
public void Non_Recursion(int a,int b,int n){
fib=a+b;
while(fib<=n){
System.out.println("Using Non_Recursion Method\t"+fib);
a=b;
b=fib;
fib=a+b;
}
}
public static void main(String a[]){
int x=Integer.parseInt(a[0]);
FibonaciEx fib=new FibonaciEx();
System.out.println("Using Recursion Method\t"+1);
fib.Recursion(0,1,x);
System.out.println("\n");
System.out.println("Using Non_Recursion Method\t"+1);
fib.Non_Recursion(0,1,x);
}
Page:
JNTUK VIZIANAGARAM
Page:
}
2)prime numbers
import java.util.Scanner;
public class PrimeNum
{
static int count;
public static void main(String args[]){
Scanner scan=new Scanner(System.in);
System.out.println("Enter the number");
int num=scan.nextInt();
System.out.println("Prime Numbers Up To "+num+" Are");
for(int i=1;i<=num;i++){
count=0;
for(int j=1;j<=i;j++){
int rem=i%j;
if(rem==0){
count=count+1;
}
}
if(count==2){
System.out.println(i);
}
}
}
}
JNTUK VIZIANAGARAM
3)polindrome
import java.util.Scanner;
public class Palindrome
{
public static void main(String args[])
{
Palindrome p=new Palindrome();
System.out.println("Enter The String");
Scanner scan=new Scanner(System.in);
String original=scan.next();
int length=original.length();
String rev="";
for(int i=length-1;i>=0;i--)
{
rev=rev+original.charAt(i);
}
if(rev.equals(original))
{
System.out.println("Palindrome");
}
else
{
System.out.println("Not Palindrome");
}
}
}
Page:
JNTUK VIZIANAGARAM
4)sorting order
import java.util.Scanner;
public class StringSort
{
static String names[]=new String[5];
public static void main(String args[])
{
StringSort ss=new StringSort();
ss.read();
ss.sort();
ss.printing();
}
public void read()
{
Scanner scan=new Scanner(System.in);
for(int i=0;i<5;i++)
{
System.out.println("Enter Name");
names[i]=scan.next();
}
}
public void printing()
{
System.out.println("After Sorting Names");
for(int i=0;i<5;i++)
{
System.out.println(names[i]);
}
}
public void sort()
{
String temp="";
Page:
JNTUK VIZIANAGARAM
Page:
for(int i=0;i<5;i++)
{
for(int j=i+1;j<5;j++)
{
if(names[i].compareTo(names[j])>0)
{
temp=names[i];
names[i]=names[j];
names[j]=temp;
}
}
}
}
}
JNTUK VIZIANAGARAM
Page:
5)matrix mutiplication
import java.util.Scanner;
public class MatrixOperations
{
static Scanner scan=new Scanner(System.in);
static int rows=0,cols=0,rows1=0,cols1=0;
static int m1[][]=new int[10][10];
static int m2[][]=new int[10][10];
static int res[][]=new int[10][10];
public void reading(int row,int col,int m[][])
{
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
m[i][j]=scan.nextInt();
}
}
}
public void writing(int row,int col,int m[][])
{
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
System.out.print("\t"+m[i][j]);
}
System.out.println("");
}
}
JNTUK VIZIANAGARAM
Page:
public void compatibility()
{
System.out.println("Enter Rows For First Matrix");
rows=scan.nextInt();
System.out.println("Enter Cols For First Matrix");
cols=scan.nextInt();
System.out.println("Enter Rows For Second Matrix");
rows1=scan.nextInt();
System.out.println("Enter Cols For Second Matrix");
cols1=scan.nextInt();
}
public void multiply()
{
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols1;j++)
{
res[i][j]=0;
for(int k=0;k<cols;k++)
{
res[i][j]=m1[i][k]*m2[k][j]+res[i][j];
}
}
}
}
public static void main(String args[])
{
MatrixOperations mo=new MatrixOperations();
mo.compatibility();
if(cols==rows1)
JNTUK VIZIANAGARAM
Page:
{
System.out.println("Enter First Matrix");
mo.reading(rows,cols,m1);
System.out.println("Enter Second Matrix");
mo.reading(rows1,cols1,m2);
System.out.println("First Matrix");
mo.writing(rows,cols,m1);
System.out.println("Second Matrix");
mo.writing(rows1,cols1,m2);
mo.multiply();
System.out.println("Matrix Multiplication is");
mo.writing(rows,cols1,res);
mo.transpose();
}
else
{
System.out.println("Matrix Cannot Be Multiplied");
}
}
public void transpose()
{
System.out.println("Transpose of Matrix");
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols1;j++)
{
System.out.print("\t"+res[j][i]);
}
System.out.println("");
JNTUK VIZIANAGARAM
}
}
}
7) Run time ploymorfisum
import java.util.Scanner;
public class RunTimePoly
{
public static void main(String args[])
{
RunTimePoly rp=new RunTimePoly();
Scanner scan=new Scanner(System.in);
System.out.println("Enter Data");
String kind=scan.next();
Animal ref=rp.getMethod(kind);
ref.speak();
}
public Animal getMethod(String kind)
{
if(kind.equals("Cow"))
{
Cow c=new Cow();
return c;
}
elseif(kind.equals("Dog"))
{
Dog d=new Dog();
return d;
}
Page:
JNTUK VIZIANAGARAM
else
return new Cat();
}
}
interface Animal
{
public void speak();
}
class Cow implements Animal
{
public void speak()
{
System.out.println("Cow Cow");
}
}
class Dog extends Cow
{
public void speak()
{
Page:
JNTUK VIZIANAGARAM
System.out.println("Dog Dog");
}
}
class Cat extends Dog
{
public void speak()
{
System.out.println("Cat Cat");
}
}
Page:
JNTUK VIZIANAGARAM
8) Creation of a package
package pack;
public class First
{
public void hello()
{
System.out.println("Class name First");
}
}
b) package pack.pack1;
public class Second
{
public void hello()
{
System.out.println("Class name Second");
}
}
c) package pack.pack1;
public class Third
{
public void hello()
{
System.out.println("Class name Third");
}
}
d)import pack.First;
import pack.pack1.*;
public class PackageDemo
{
public static void main(String args[])
{
Page:
JNTUK VIZIANAGARAM
First f=new First();
f.hello();
Second s=new Second();
s.hello();
Third t=new Third();
t.hello();
}
}
Page:
JNTUK VIZIANAGARAM
9) StringTokenize class
import java.util.StringTokenizer;
import java.util.Scanner;
import java.io.*;
public class StringToken
{
public static void main(String args[])
throws Exception
{
Scanner scan=new Scanner(System.in);
InputStreamReader fis=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(fis);
String num=br.readLine();
StringTokenizer str=new StringTokenizer(num);
int sum=0;
while(str.hasMoreTokens())
{
String st=str.nextToken();
System.out.println(st);
sum=sum+Integer.parseInt(st);
}
System.out.println("Sum of all Integers is"+sum);
}
}
Page:
JNTUK VIZIANAGARAM
Page:
10)file oparations
import java.io.*;
import java.util.Scanner;
public class FileOperations
{
public static void main(String args[])
throws Exception
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter File Name With Extension");
String fname=scan.next();
File ff=new File(fname);
if(ff.exists())
{
System.out.println("File Existed");
if(ff.isFile())
{
System.out.println("Type : File");
long l=ff.length();
System.out.println("File Size is"+l);
}
else
{
System.out.println("Type : Directory");
}
if(ff.canRead())
{
System.out.println("File Readable");
}
if(ff.canWrite())
{
System.out.println("File Writable");
JNTUK VIZIANAGARAM
}
}
else
{
System.out.println("File Not Existed");
}
}
}
Page:
JNTUK VIZIANAGARAM
Page:
11)lenth of the word
import java.io.*;
import java.util.Scanner;
import java.util.StringTokenizer;
public class NumberOfLW
{
public static void main(String args[])
throws Exception
{
String stt="";
int clines=0;
int cwords=0;
int length=0;
int cchar=0;
Scanner scan=new Scanner(System.in);
System.out.println("Enter File Name With Extension");
String fname=scan.next();
FileReader fr=new FileReader(fname);
BufferedReader br=new BufferedReader(fr);
while((stt=br.readLine())!=null)
{
clines=clines+1;
StringTokenizer st=new StringTokenizer(stt);
while(st.hasMoreTokens())
{
cwords=cwords+1;
cchar=cchar+st.nextToken().length();
}
}
fr.close();
br.close();
JNTUK VIZIANAGARAM
System.out.println("Number of lines"+clines);
System.out.println("Number of words"+cwords);
System.out.println("Number of chars"+cchar);
}
}
Page:
JNTUK VIZIANAGARAM
12) applet
a)import java.io.*;
import java.applet.*;
import java.awt.*;
/*
<applet code=AppletFile width=100 height=100>
</applet>
*/
public class AppletFile extends Applet
{
TextArea ta=new TextArea();
public void init()
{
setLayout(new BorderLayout());
add(ta);
}
public void paint(Graphics g)
{
}
}
b)HTML CODE
<html>
<body>
<applet code="AppletFile.class" width=100 height=100 alt=AppletEx>
</applet>
</body>
</html>
Page:
JNTUK VIZIANAGARAM
13)caliculator
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="Cal" width=300 height=300>
</applet>
*/
public class Cal extends Applet
implements ActionListener
{
String msg=" ";
int v1,v2,result;
TextField t1;
Button b[]=new Button[10];
Button add,sub,mul,div,clear,mod,EQ;
char OP;
public void init()
{
Color k=new Color(120,89,90);
setBackground(k);
t1=new TextField(10);
GridLayout gl=new GridLayout(4,5);
setLayout(gl);
for(int i=0;i<10;i++)
{
b[i]=new Button(""+i);
}
add=new Button("add");
sub=new Button("sub");
mul=new Button("mul");
div=new Button("div");
Page:
JNTUK VIZIANAGARAM
mod=new Button("mod");
clear=new Button("clear");
EQ=new Button("EQ");
t1.addActionListener(this);
add(t1);
for(int i=0;i<10;i++)
{
add(b[i]);
}
add(add);
add(sub);
add(mul);
add(div);
add(mod);
add(clear);
add(EQ);
for(int i=0;i<10;i++)
{
b[i].addActionListener(this);
}
add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
mod.addActionListener(this);
clear.addActionListener(this);
EQ.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
Page:
JNTUK VIZIANAGARAM
char ch=str.charAt(0);
if ( Character.isDigit(ch))
t1.setText(t1.getText()+str);
else
if(str.equals("add"))
{
v1=Integer.parseInt(t1.getText());
OP='+';
t1.setText("");
}
else if(str.equals("sub"))
{
v1=Integer.parseInt(t1.getText());
OP='-';
t1.setText("");
}
else if(str.equals("mul"))
{
v1=Integer.parseInt(t1.getText());
OP='*';
t1.setText("");
}
else if(str.equals("div"))
{
v1=Integer.parseInt(t1.getText());
OP='/';
t1.setText("");
}
else if(str.equals("mod"))
{
v1=Integer.parseInt(t1.getText());
OP='%';
Page:
JNTUK VIZIANAGARAM
Page:
t1.setText("");
}
if(str.equals("EQ"))
{
v2=Integer.parseInt(t1.getText());
if(OP=='+')
result=v1+v2;
else if(OP=='-')
result=v1-v2;
else if(OP=='*')
result=v1*v2;
else if(OP=='/')
result=v1/v2;
else if(OP=='%')
result=v1%v2;
t1.setText(""+result);
}
if(str.equals("clear"))
{
t1.setText("");
}
}
}
JNTUK VIZIANAGARAM
14) mouse events
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class MouseHandling extends Frame implements
MouseListener,MouseMotionListener
{
Label l=new Label("Mouse Handling");
Label l1=new Label();
public MouseHandling()
{
setLayout(new GridLayout());
setResizable(false);
add(l);
l.setFont(new Font("",1,15));
l.addMouseListener(this);
l.addMouseMotionListener(this);
add(l1);
l1.setFont(new Font("",1,15));
l1.addMouseListener(this);
l1.addMouseMotionListener(this);
}
public static void main(String arg[])
{
MouseHandling mh=new MouseHandling();
mh.setVisible(true);
mh.setSize(400,300);
}
public void mouseClicked(MouseEvent me)
{
l.setText("MouseClicked");
}
Page:
JNTUK VIZIANAGARAM
public void mouseEntered(MouseEvent me)
{
l.setText("MouseEntered");
}
public void mouseExited(MouseEvent me)
{
l.setText("MouseExisted");
}
public void mousePressed(MouseEvent me)
{
l.setText("MousePressed");
}
public void mouseReleased(MouseEvent me)
{
l1.setText("Mouse Released");
}
public void mouseDragged(MouseEvent me)
{
l.setText("Mouse Dragged");
}
public void mouseMoved(MouseEvent me)
{
l1.setText("Mouse Moved");
}
}
Page:
JNTUK VIZIANAGARAM
Page:
15)thread life cycle
a)public class ThreadLC
{
public static void main(String args[])
{
Tha aa=new Tha();
aa.start();
Thb bb=new Thb();
bb.start();
}
}
class Tha extends Thread
{
public void run()
{
try{
for(int i=1;i<=10;i++)
{
if(i==2)
{
Thread.sleep(2000);
System.out.println("Thread Sleep at A2");
}
System.out.println("A"+i);
}
} catch(Exception ex)
{
}
}
}
JNTUK VIZIANAGARAM
Page:
class Thb extends Thread
{
public void run()
{
try{
for(int i=0;i<=10;i++)
{
if(i==8)
{
System.out.println("Thread Stopped at B8");
stop();
}
System.out.println("B"+i);
}
} catch(Exception ex)
{
}
}
}
b)thread sleep
public class ThreadSleep
{
public static void main(String args[])
throws Exception
{
tha aa=new tha();
aa.start();
thb bb=new thb();
bb.start();
thc cc=new thc();
JNTUK VIZIANAGARAM
cc.start();
}
}
class tha extends Thread
{
public void run()
{
try{
Thread.sleep(1000);
System.out.println("Good Morning");
} catch(Exception ex){}
}
}
class thb extends Thread
{
public void run()
{
try{
Thread.sleep(3000);
System.out.println("Hello");
} catch(Exception ex){}
}
}
class thc extends Thread
{
public void run()
{
try{
Thread.sleep(6000);
System.out.println("Welcome");
} catch(Exception ex){}
Page:
JNTUK VIZIANAGARAM
}
}
16)pie chart wit(swings & awt)
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.text.*;
import javax.swing.*;
public class PieChartExample
{
public static void main(String[] args)
{
JFrame f = new JFrame("Pie Chart");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new PieChartPanel());
f.setSize(350,300);
f.setLocation(300,300);
f.setVisible(true);
}
}
class PieChartPanel extends JPanel
{
BufferedImage image;
final int PAD = 30;
Font font;
NumberFormat numberFormat;
Page:
JNTUK VIZIANAGARAM
public PieChartPanel()
{
font = new Font("Book Antiqua", Font.BOLD, 20);
numberFormat = NumberFormat.getPercentInstance();
addMouseListener(new Visibility(this));
addComponentListener(new ComponentAdapter()
{});
}
protected void paintComponent(Graphics graphics)
{
super.paintComponent(graphics);
Graphics2D graphics2d = (Graphics2D)graphics;
graphics2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
createChartImage();
graphics2d.drawImage(image, 0, 0, this);
}
private void createChartImage()
{
int[] marks = { 98, 76, 90, 85, 75 };
int width = getWidth();
int height = getHeight();
int cp = width/2;
int cq = height/2;
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
g2.setPaint(Color.blue);
g2.fillRect(0, 0, width, height);
g2.setPaint(Color.black);
int pie = Math.min(width,height) - 2*PAD;
g2.draw(new Ellipse2D.Double(cp - pie/2, cq - pie/2, pie, pie));
double total = 0;
Page:
JNTUK VIZIANAGARAM
for(int j = 0; j < marks.length; j++)
total += marks[j];
double theta = 0, phi = 0;
double p, q;
for(int j = 0; j < marks.length; j++)
{
p = cp + (pie/2) * Math.cos(theta);
q = cq + (pie/2) * Math.sin(theta);
g2.draw(new Line2D.Double(cp, cq, p, q));
phi = (marks[j]/total) * 2 * Math.PI;
p = cp + (9*pie/24) * Math.cos(theta + phi/2);
q = cq + (9*pie/24) * Math.sin(theta + phi/2);
g2.setFont(font);
String st = String.valueOf(marks[j]);
FontRenderContext frc = g2.getFontRenderContext();
float textWidth = (float)font.getStringBounds(st, frc).getWidth();
LineMetrics lm = font.getLineMetrics(st, frc);
float sp = (float)(p - textWidth/2);
float sq = (float)(q + lm.getAscent()/2);
g2.drawString(st, sp, sq);
p = cp + (pie/2 + 4*PAD/5) * Math.cos(theta + phi/2);
q = cq + (pie/2 + 4*PAD/5) * Math.sin(theta+ phi/2);
st = numberFormat.format(marks[j]/total);
textWidth = (float)font.getStringBounds(st, frc).getWidth();
lm = font.getLineMetrics(st, frc);
sp = (float)(p - textWidth/2);
sq = (float)(q + lm.getAscent()/2);
g2.drawString(st, sp, sq);
theta += phi;
}
g2.dispose();
}
Page:
JNTUK VIZIANAGARAM
public void toggleVisibility()
{
repaint();
}
}
class Visibility extends MouseAdapter
{
PieChartPanel piechart;
public Visibility(PieChartPanel pc)
{
piechart = pc;
}
public void mousePressed(MouseEvent event)
{
if(event.getClickCount() > 1)
piechart.toggleVisibility();
}
}
Page:
JNTUK VIZIANAGARAM
17) draw the lines
import javax.swing.*;import java.awt.*;
public class cizim extends JPanel{
//www.bilgisayarkavramlari.com public void paintComponent(Graphics g)
{
g.drawLine(0,0,90,90);
} public static void main(String args[])
{
JFrame jf = new JFrame();
jf.add(new cizim());
jf.setSize(500,500);
jf.setVisible(true); }}
import javax.swing.*;
import <strong class="highlight">java</strong>.awt.*;
public class cizim extends JPanel{
//www.bilgisayarkavramlari.com
public void paintComponent(Graphics g){
g.drawLine(0,0,90,90);
}
public static void main(String args[]){
JFrame jf = new JFrame();
jf.add(new cizim());
jf.setSize(500,500);
jf.setVisible(true);
}
}
Page:
JNTUK VIZIANAGARAM
Page:
18)client & server
a) import java.io.*;
import java.awt.*;
import java.net.*;
public class Client
{
public static void main(String args[])
throws Exception
{
Socket clien=new Socket("localhost",555);
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(isr);
PrintWriter out=new PrintWriter(clien.getOutputStream());
InputStreamReader risr=new InputStreamReader(clien.getInputStream());
BufferedReader rin=new BufferedReader(risr);
String msg;
System.out.println("Enter radius");
msg=in.readLine();
out.write(msg+"\r\n");
out.flush();
System.out.println("Area of Circle is");
msg=rin.readLine();
System.out.println(""+msg);
}
}
b) import java.io.*;
import java.net.*;
import java.awt.*;
public class Server
{
public static void main(String args[])
throws Exception
JNTUK VIZIANAGARAM
Page:
{
ServerSocket serve=new ServerSocket(555);
System.out.println("Server is Running on port 555");
Socket client=serve.accept();
System.out.println("Client Connected to Server");
InputStreamReader risr=new InputStreamReader(client.getInputStream());
BufferedReader rin=new BufferedReader(risr);
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(isr);
PrintWriter out=new PrintWriter(client.getOutputStream());
int radius=0;String msg;
msg=rin.readLine();
radius=Integer.parseInt(msg);
double area=(3.14*radius*radius);
msg=String.valueOf(area);
out.write(msg+"\r\n");
out.flush();
}
}
JNTUK VIZIANAGARAM
19)random numbers
import java.util.Random;
/** Generate 10 random integers in the range 0..99. */
public final class RandomInteger {
public static final void main(String... aArgs){
log("Generating 10 random integers in range 0..99.");
//note a single Random object is reused here
Random randomGenerator = new Random();
for (int idx = 1; idx <= 10; ++idx){
int randomInt = randomGenerator.nextInt(100);
log("Generated : " + randomInt);
}
log("Done.");
}
private static void log(String aMessage){
System.out.println(aMessage);
}
}
Page:
JNTUK VIZIANAGARAM
20)geometric figure
import java.util.Scanner;
public class ShapeGoe
{
public static void main(String args[])
{
ShapeGoe rp=new ShapeGoe();
Scanner scan=new Scanner(System.in);
System.out.println("Enter Data");
String kind=scan.next();
Shape ref=rp.getMethod(kind);
ref.numbeOfSides();
}
public Shape getMethod(String kind)
{
if(kind.equals("Trapezoid"))
{
Trapezoid c=new Trapezoid();
return c;
}
else
if(kind.equals("Triangle"))
{
Triangle d=new Triangle();
return d;
}
else
return new Hexagon();
}
}
Page:
JNTUK VIZIANAGARAM
interface Shape
{
public void numbeOfSides();
}
class Trapezoid implements Shape
{
public void numbeOfSides()
{
System.out.println("4 Sides");
}
}
class Triangle extends Trapezoid
{
public void numbeOfSides()
{
System.out.println("3 Sides");
}
}
class Hexagon extends Triangle
{
public void numbeOfSides()
{
System.out.println("6 Sides");
}
}
Page:
JNTUK VIZIANAGARAM
21)que
import java.util.Scanner;
class ExcQueue extends Exception {
ExcQueue(String s) {
super(s);
}
}
public class Queue {
int front,rear;
int q[ ]=new int[3];
Queue() {
rear=-1;
front=-1;
}
void enqueue(int n) throws ExcQueue {
if (rear==2)
throw new ExcQueue("Queue is full");
rear++;
q[rear]=n;
if (front==-1)
front=0;
}
int dequeue() throws ExcQueue {
if (front==-1)
throw new ExcQueue("Queue is empty");
int temp=q[front];
if (front==rear)
front=rear=-1;
else
front++;
return(temp);
}
Page:
JNTUK VIZIANAGARAM
Page:
public static void main(String args[ ]) {
Queue q=new Queue();
try {
q.enqueue(1);
q.enqueue(4);
q.enqueue(3);
//q.enqueue(2);//If remove comments exception will be raise because q size is 3
q.dequeue();
q.dequeue();
q.dequeue();
//q.dequeue();//If remove comments exception will be raised because at this point of q is
empty
} catch (ExcQueue e) {
System.out.println(e.getMessage());
}
}
}
Related documents