0% found this document useful (0 votes)
27 views30 pages

Java Program for Quadratic Equation Roots

The document contains code snippets for several Java programs that demonstrate different programming concepts: 1. The first program solves a quadratic equation and prints the real roots. 2. The second program prints the Fibonacci sequence using recursive and non-recursive functions. 3. The third program finds and prints all prime numbers up to a given integer. 4. The fourth program multiplies two matrices and prints the result. The document then continues demonstrating concepts like string tokenization, palindrome checking, sorting, file handling, and implementing a producer-consumer problem using threads. It provides full code for these examples.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views30 pages

Java Program for Quadratic Equation Roots

The document contains code snippets for several Java programs that demonstrate different programming concepts: 1. The first program solves a quadratic equation and prints the real roots. 2. The second program prints the Fibonacci sequence using recursive and non-recursive functions. 3. The third program finds and prints all prime numbers up to a given integer. 4. The fourth program multiplies two matrices and prints the result. The document then continues demonstrating concepts like string tokenization, palindrome checking, sorting, file handling, and implementing a producer-consumer problem using threads. It provides full code for these examples.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Write a java program to print all real solutions to the quadratic equation class quadratic { public static void

main(String args[]) { int a=20,b=8,c=9; double des,root1,root2; des=(b*b)-(4*a*c); if(des<0) { [Link]("no real roots"); } else { root1=(-b+[Link](des)/(2*a)); root2=(-[Link](des)/(2*a)); [Link]("the roots of quadratic equation are:" +root1+ " " +root2); } } } Save: Compile: Execute: [Link] javac [Link] java quadratic

Write a java program that uses both recursive and non-recursive functions to print nth value in the Fibonacci sequence class fibonacci1 { void series(int n) { int f0=1,f1=1,f2; [Link](f0); [Link](f1); for( int i=3;i<n;i++) { f2=f0+f1; [Link](f2); f0=f1; f1=f2; } [Link](""); } int fibo(int x) { int a; if(x==1||x==2) return 1; a=fibo(x-1)+fibo(x-2); return a; } public static void main(String a[]) { int n=10,x=6,y; fibonacci1 ob =new fibonacci1(); [Link](n); y=[Link](x); [Link]("the values are "+x +"location"+y); } }

Save: Compile: Execute:

[Link] javac [Link] java fibonacci1

Write a java program that prompts the user for an integer and than prints all the prime numbers up to that integer

class prime { public static void main(String args[]) { int i,j,count=0,n; n=[Link](args[0]); for(i=1;i<=n;i++) { count=0; for(j=1;j<=i;j++) { if(i%j==0) count++; } if(count==2) { [Link]("the prime numbers are"); [Link](i); } } } }

Save: Compile: Execute:

[Link] javac [Link] java prime 20

Write a java program to multiply two given matrices

class mulmatrix { public static void main(String args[]) { int i,j,k; int a[][]={{1,2,3},{4,5,6},{7,8,9}}; int b[][]={{1,1,1},{1,1,1},{1,1,1}}; int c[][]=new int[3][3]; [Link]("matrix 'a' is"); for(i=0;i<=2;i++) { for(j=0;j<=2;j++) { [Link](" "+a[i][j]); } [Link](); } [Link]("matrix b is"); for(i=0;i<=2;i++) { for(j=0;j<=2;j++) { [Link](" "+b[i][j]); } [Link](" "); } [Link]("performing multiplication"); for(i=0;i<=2;i++) { for(j=0;j<=2;j++) { for(k=0;k<=2;k++) { c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } } [Link]("after multiplication resultant c matrix ais"); for(i=0;i<=2;i++) { for(j=0;j<=2;j++) {

[Link](" "+c[i][j]); } [Link](" "); } } }

Save: Compile: Execute:

[Link] javac [Link] mulmatrix quadratic

Write a java program that reads a line of integers and prints sum of all the integers using string tokenizer

import [Link].*; class stoken { public static void main(String args[]) { String s="1,2,3,4,5,6,7,8,9,10"; int sum=0; StringTokenizer str=new StringTokenizer(s,","); [Link]("integer are:"); while([Link]()) { int i=[Link]([Link]()); [Link](""+i); sum=sum+i; } [Link]("sum of integer is:"+sum); } }

Save: Compile: Execute:

[Link] javac [Link] stoken quadratic

Write a java program that checks whether given string is palindrome or not

class palindrome { public static void main(String args[]) { String str="madam"; StringBuffer sb=new StringBuffer(str).reverse(); String strrev=[Link](); if([Link](strrev)) { [Link]("String is palindrome"); } else { [Link]("String is not a palindrome"); } } }

Save: Compile: Execute:

[Link] javac [Link] java palindrome

Write a java program for sorting a given list of names in ascending order

class sort { static String str[]={"hyd","delhi","banglore"}; public static void main(String a[]) { for(int i=0;i<[Link];i++) { for(int j=0;j<[Link];j++) { if((str[j].compareTo(str[i]))>0) { String t=str[i]; str[i]=str[j]; str[j]=t; } } } for(int i=0; i<[Link];i++) [Link](str[i]); } }

Save: Compile: Execute:

[Link] javac [Link] java sort

Write a java program for counting number of lines ,words and characters in a string import [Link].*; import [Link].*; import [Link].*; class txt { public static void main(String a[])throws Exception { DataInputStream d=new DataInputStream([Link]); int i,spl=0,line=0,word=0,letter=0; char j; String str; [Link]("enter a string"); str=[Link](); for(i=0;i<[Link]();i++) { j=[Link](i); if([Link](j)) { letter++; } else if([Link](j)) { word++; } else if(j=='\t') { line++; } else { spl++; }} line++; word++; letter=letter+spl; [Link]("num of lines"+line); [Link]("num of words"+word); [Link]("num of letters"+letter); }} Save: Compile: Execute: [Link] javac [Link] java txt

Write a java program that reads a file name from the user and displays information about whether the file exists, writable, readable. import [Link].*; class filedemo { public static void main(String args[]) { File f=new File("D://anusha//cse//[Link]"); [Link]("Isdirectory:"+[Link]()); [Link]("Isabsolute:"+[Link]()); [Link]("Getparent:"+[Link]()); [Link]("Getabsolutepath:"+[Link]()); [Link]("Filename:"+[Link]()); [Link]("Canread:"+[Link]()); [Link]("Canwrite:"+[Link]()); } }

Save: Compile: Execute:

[Link] javac [Link] java filedemo

Write a java program that reads a file and displays the file on the screen with a line number before each line

import [Link].*; class files { public static void main(String args[]) { String line; for(int i=0;i<[Link];i++) { try { LineNumberReader br=new LineNumberReader(new FileReader(args[i])); while((line=[Link]())!=null) { [Link]([Link]()+":"+line); } } catch(IOException e) { [Link]("error:"+e); } } } }

Save: Compile: Execute:

[Link] javac [Link] java files

Write a java program that implements stack ADT import [Link].*; class stack { int []a; int top; public stack(int n) { a=new int[n]; top=-1; } public void push(int val) { if(top==[Link]-1) { [Link]("stack overflow"); } else { top++; a[top]=val; } }//end push public void pop() { if(top==-1) { [Link]("stack underflow"); } else { [Link]("element popped"+a[top]); top--; } }//end pop public void display() { if (top==-1) { [Link]("stack empty"); } else { for(int i=top;i>=0;i--) {

[Link]("stack elements:"+a[i]); }} } }//end class public class usestack { public static void main(String a[]) { Scanner sc; sc=new Scanner([Link]); stack s; int n; [Link]("enter the size of stack"); n=[Link](); s=new stack(n); int coice; do { [Link]("[Link],[Link],[Link],[Link],enter u r coice:"); coice=[Link](); switch(coice) { case 1: int value; [Link]("enter element to push"); value=[Link](); [Link](value); break; case 2: [Link](); break; case 3: [Link](); break; case 0: break; default:[Link]("invalid choice"); } }while(coice!=0); }} Save: Compile: Execute: [Link] javac [Link] java stack

Develop an applet that displays a simple message import [Link].*; import [Link].*; public class simpleapplet extends Applet { public void paint(Graphics g) { [Link]("oops through java",10,10); } }

[Link](notepad) <html> <applet code=[Link] height=300 width=300></applet> </html>

Save: Compile: Execute:

[Link] javac [Link] appletviewer [Link]

Write a java program for handling mouse and key events

import [Link].*; import [Link].*; import [Link].*; /*<applet code="mouseevents"height=200 width=200></applet>*/ public class mouseevents extends Applet implements MouseListener,MouseMotionListener { String msg=" "; int x=0,y=0; public void init() { addMouseListener(this); addMouseMotionListener(this); } public void mouseClicked(MouseEvent me) { x=0;y=10; msg="mouseclicker"; repaint(); } public void mouseEntered(MouseEvent me) { x=0;y=10; msg="mouse entered"; repaint(); } public void mouseExited(MouseEvent me) { x=0;y=0; msg="mouse exited"; repaint(); } public void mousePressed(MouseEvent me) { x=0;y=0; msg="up"; repaint(); } public void mouseReleased(MouseEvent me) {

x=0;y=10; msg="down"; repaint(); } public void mouseDragged(MouseEvent me) { x=[Link](); y=[Link](); showStatus("mouse dragged"+x+" "+y); repaint(); } public void mouseMoved(MouseEvent me) { showStatus("mousemoved"+[Link]()+","+[Link]()); repaint(); } public void Paint(Graphics a) { [Link](msg,x,y); } } [Link](notepad) <html> <applet code="mouseevents"height=200 width=200></applet> </html>

Save: Compile: Execute:

[Link] javac [Link] appletviewer [Link]

Write a java program that implements producer consumer problem using inter thread communication

class Q { int n; boolean valueset=false; synchronized int get() { if(!valueset) try { wait(); } catch(InterruptedException e) { [Link]("InterruptedException caught"); } [Link]("Get:"+n); valueset=false; notify(); return n; } synchronized void put (int n) { if(valueset) try { wait(); } catch(InterruptedException e) { [Link]("Interrupted caught"); } this.n=n; valueset=true; [Link]("put:"+n); notify(); } } class producer implements Runnable { Q q; producer(Q q)

{ this.q=q; new Thread(this,"producer").start(); } public void run() { int i=0; while(true) { [Link](i++); } } } class consumer implements Runnable { Q q; consumer(Q q) { this.q=q; new Thread(this,"consumumer").start(); } public void run() { while(true) { [Link](); } } } class pCFixed { public static void main(String a[]) { Q q=new Q(); new producer(q); new consumer(q); [Link]("press control -c to stop"); } }

Save: Compile: Execute:

[Link] javac [Link] java pCFixed

Write a java program that allows the user to draw lines, rectangles and ovals

import [Link].*; import [Link].*; public class drawing extends Applet { public void paint(Graphics g) { [Link](20,10,50,40); [Link](20,60,40,60); [Link](200,60,30,80); [Link](20,200,70,60,10,10); [Link](30,210,50,40,5,5); [Link](100,10,100,140); [Link](30,10,250,250); [Link]([Link]); [Link](20,30,150,150); } } [Link](notepad) <html> <applet code=[Link] height=300 width=300></applet> </html> Save: Compile: Execute: [Link] javac [Link] appletviewer [Link]

Write a java program that creates an abstract class named shape that shows the number of sides in the geometrical figures abstract class shape { abstract void numofsides(); } class trapezoid extends shape { void numofsides() { [Link]("the num of sides oftrapezoidis 4"); } } class triangle extends shape { void numofsides() { [Link]("the num of sides of triangle is 3"); }} class hexagone extends shape { void numofsides() { [Link]("the num of sides of hxegone is 6"); }} class demo1 { public static void main(String args[]) { shape r; trapezoid ob=new trapezoid(); triangle obj=new triangle(); hexagone obj1=new hexagone(); r=ob; [Link](); r=obj; [Link](); r=obj; [Link](); }} Save: Compile: Execute: [Link] javac [Link] java demo1

Write a java program to demonstrate dynamic method dispatch class A { void show() { [Link]("show method in class A"); } } class B extends A { void show() { [Link]("show method in class B"); } } class C extends B { void show() { [Link]("show method in class C"); } } class dmd { public static void main(String args[]) { A r; A obj=new A(); B obj1=new B(); C obj2=new C(); r=obj; [Link](); r=obj1; [Link](); r=abj2; [Link](); } } Save: Compile: Execute: [Link] javac [Link] java dmd

Write a java program to demonstrate interfaces interface a { void test(); } interface b extends a { void display(); } class ex implements b { public void display() { [Link]("An example for extending interface"); } public void test() { [Link]("test method of interface a"); } } class interface1 { public static void main(String args[]) { ex obj=new ex(); [Link](); [Link](); } }

Save: Compile: Execute:

[Link] javac [Link] java interface1

Write a java program to demonstrate super keyword class rect { rect() { [Link]("superclass constructor"); } rect(double l,double b) { [Link]("area of rect is"+(l*b)); } } class rectwt extends rect { rectwt() { super(); [Link]("sub class constructor"); } rectwt(double l,double b,double wt) { super(l,b); [Link]("wt of rect is"+wt); } } class spr { public static void main(String args[]) { rectwt ob=new rectwt(); rectwt obj=new rectwt(1,5,3); } }

Save: Compile: Execute

[Link] javac [Link] java spr

Write a java program to explain method overloading class sample { int l=10,20; void area() { [Link]("Area of rect is:"+l*b); } void area(int side) { [Link]("Area of square is:+side*side); } } class methoverl { public static void main(String args[]) { sample obj=new sample(); [Link](); [Link](8); } }

Save: Compile: Execute:

[Link] javac [Link] java methover1

Write a java program to create an abstract class

abstract class a { abstract void meth(); void test() { [Link]("test method in super class"); } } class b extends a { void meth() { [Link]("defination of abstract method is given in subclass"); } } class abst { public static void main(String args[]) { b obj=new b(); [Link](); [Link](); } }

Save: Compile: Execute:

[Link] javac [Link] java abst

Write a java program to draw a human face

import [Link].*; import [Link].*; public class humanface extends Applet { public void paint(Graphics g) { [Link](40,40,120,150); [Link](57,75,30,20); [Link](110,75,30,20); [Link](68,81,10,10); [Link](121,81,10,10); [Link](60,125,80,40,180,180); [Link]([Link]); [Link](85,100,30,30); [Link]([Link]); [Link](25,92,15,30); [Link](160,92,15,30); } } [Link](notepad) <html> <applet code=[Link] height=300 width=300></applet> </html>

Save: Compile: Execute:

[Link] javac [Link] appletviewer [Link]

Write a java program for creating multiple threads import [Link].*; class NewThread implements Runnable { Thread t; NewThread() { t=new Thread(this,"Demothread"); [Link]("child thread"+t); [Link](); } public void run() { try { for(int i=5;i>0;i--) { [Link]("child thread"+t); [Link](500); } } catch(InterruptedException e) { [Link]("child Interrupted"); } } } class threaddemo { public static void main(String args[]) { new NewThread(); try { for(int i=5;i>0;i--) { [Link]("main thread"+i); [Link](1000); } }

catch(InterruptedException e) { [Link]("main thread interrupted"); } [Link]("main thread existing"); } }

Save: Compile: Execute:

[Link] javac [Link] java threaddemo

Write a java program to create a class in a package package ex; public class A { public void display() { [Link]("it is display method in package ex"); } }

Save: Compile:

[Link] javac [Link]

NOTE: place [Link] and [Link] files in ex directory.

Write a java program to import a class from a package

import ex.*; class c { public static void main(String a[]) { A ob =new A(); [Link](); } } Save: Compile: Execute: [Link] javac [Link] java c

You might also like