CSE II Yr JNTU
CSE II Yr JNTU
java
/*Write a java program that prints all real solutions to the Quadratic
equation Read in a,b,c and use the Quadratic formula. If the discrminant
is b2 - 4ac is negative then display a message stating that there are
no real solutions*/
import java.lang.*;
import java.io.*;
System.out.println("Enter a");
a = Integer.parseInt(br.readLine());
System.out.println("Enter b");
b = Integer.parseInt(br.readLine());
System.out.println("Enter c");
c = Integer.parseInt(br.readLine());
disc = (b * b) - (4 * a * c);
if (disc > 0) {
System.out.println("Roots are real and unequal");
else if (disc == 0) {
System.out.println("Roots are Real and Equal");
r1 = r2 = (-b) / (2 * a);
System.out.println(" r1 = " + r1);
System.out.println(" r2 = " + r2);
}
else {
System.out.println("Roots are imaginary");
}
}
}
while (i <= n - 3)
{
c = a + b;
System.out.println(" " + c);
a = b;
b = c;
i++;
}
}
}
//FibonacciTest.java
import java.io.*;
public class FibonacciTest {
public static void main(String args[]) throws IOException
{
InputStreamReader obj = new InputStreamReader( System.in );
BufferedReader br = new BufferedReader( obj );
}
}
OUTPUT
Enter last number 7
import java.io.*;
public class FibonacciRec {
//FibonacciRecTest.java
import java.io.*;
OUTPUT
Enter last integer 4
import java.io.*;
public class Prime
{
boolean isPrime = false;
//PrimeTest.java
import java.io.*;
public class PrimeTest
{
public static void main(String args[] )throws IOException
{
InputStreamReader obj = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader( obj );
OUTPUT
Enter last number: 7
Prime Number are as follows
2
3
5
7
import java.io.*;
import java.util.Scanner;
if (n == p)
{
System.out.println("Matrix Multiplication is possible");
OUTPUT
Enter rows and columns of first matrix
2 2
Enter the col and rows of second matrix
2 2
Matrix Multiplication is possible
Product is
1 2
3 4
import java.io.*;
import java.util.*;
public class SumOfInt {
public static void main(String args[]) throws IOException
{
InputStreamReader obj = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader( obj );
System.out.println("Enter integers");
String input = br.readLine();
StringTokenizer tokens = new StringTokenizer(input);
int sum = 0;
while(tokens.hasMoreTokens())
{
String value = tokens.nextToken();
int value1 = Integer.parseInt(value);
sum += value1;
}
System.out.println("Sum of integers is: "+ sum);
}
OUTPUT
Enter integers
1 2 3 4 5 6
Sum of integers is: 21
import java.util.*;
public class Palindrome
{
public static void main(String args[])
{
String s1 = new String();
String s2 = new String();
System.out.println("Enter a string");
OUTPUT
Enter a string madam
madam is a palindrome
import java.util.*;
}
}
OUTPUT
Sorted List is
civil
electrical
mechanical
import java.util.*;
public class ReadTxt {
public static void main(String args[])
{
String s1 = new String();
System.out.println("Enter a line of text:-");
Scanner sc = new Scanner(System.in);
s1 = sc.nextLine();
int sum = 0;
StringTokenizer st = new StringTokenizer( s1 );
while(st.hasMoreTokens())
{
String value = st.nextToken();
System.out.println(value);
sum++;
}
System.out.println("Frequency counts of words is" + sum);
}
OUTPUT
Enter a line of text:- This is a line with 7 tokens
This
is
a
line
with
7
tokens
import java.io.*;
import javax.swing.*;
if (f.isFile())
{
System.out.println(f + (f.isFile() ? " is " : "isNot")
+ "aFile");
}
else
System.out.println(f + "is not a File");
}
}
OUTPUT
Enter File Name: C:\\cse\\FibDemo.java
C:\cse\FibDemo.java is aFile
C:\cse\FibDemo.java does exists
C:\cse\FibDemo.java can read
C:\cse\FibDemo.java can write
//LineNumb.java
import java.io.*;
OUTPUT
1 /*Write a java program that uses recursive function to print
2 the nth value in the Fibonnacci Sequence*/
3
4 //FibonacciRec
5
6 import java.io.*;
7 public class FibonacciRec {
8
9 public int fibonacci( int n )
10 {
11 if( n == 1 || n == 2 )
12 return n;
13 else
14 return fibonacci( n - 1 ) + fibonacci( n - 2 );
15 }
16
17 }
//DisplayFile.java
import java.io.*;
import java.util.*;
s2 = br2.readLine();
j = s2.length();
sum += j;
while (st.hasMoreTokens()) {
st.nextToken();
c++;
}
} catch (Exception e){ }
}
}
OUTPUT
total number of lines 17
Total number of words 57
Total number of characters 331
// WelcomeApplet.java
import java.awt.*;
import javax.swing.*;
public class WelcomeApplet extends JApplet {
public void paint(Graphics g)
{
g.drawString("Welcome..This string is displayed at the position
x= 25,y =25",25,25);
}
OUTPUT
//Factorial.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Factorial extends JApplet implements ActionListener {
JLabel numLabel;
JTextField numField;
JLabel factLabel;
JTextField factField;
JButton compute;
int fact ;
setSize(100,100);
//setVisible(true);
}
}
}
OUTPUT
//MouseTracker.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public MouseTracker()
{
super( "Demonstrating Mouse Events" );
}
}
//Multi.java
t0.setName("Thread-1");
t1.setName("Thread-2");
t2.setName("Thread-3");
t0.start();
t1.start();
t2.start();
// main thread will sleep for 5000ms
try
{
Thread.currentThread().sleep(5000);
System.out.println("Number of times Thread-1, Thread-2,
Thread-3 has been executed in 5000 milliseconds i.e 5sec");
System.exit(0);
}
catch (InterruptedException e)
{
System.out.println("Interrupted");
}
}
}
OUTPUT
Thread-1-->Good Morning
Thread-3-->Welcome
Thread-2-->Hello
Thread-1-->Good Morning
Thread-2-->Hello
Thread-1-->Good Morning
Thread-3-->Welcome
Thread-1-->Good Morning
Thread-2-->Hello
Thread-1-->Good Morning
// Producer.java
public Producer(SharedAccess h) {
super("ProduceInteger");
pHold = h;
}
pHold.setSharedInt(count);
System.out.println("Producer" + count);
}
}
}
// Consumer.java
// Definition of threaded class ConsumeInteger
public Consumer(SharedAccess h)
{
super("Consumer");
cHold = h;
}
do
{
val = cHold.getSharedInt();
System.out.println("Consumer:" + val);
} while (val != 5);
}
}
// SharedAccess.java
try
{
System.out.println("Producer is waiting");
System.out.println("Consumer is consuming the value produced by
producer");
wait();
}
catch (InterruptedException e)
{
System.out.println(“Interrupted”);
}
}
sharedInt = val;
producerTurn = false;
notify(); // tell a waiting thread(consumer) to become ready
}
wait();
}
catch (InterruptedException e) {
System.out.println(“Interrupted”);
}
}
producerTurn = true;
notify(); // tell a waiting thread(producer) to become ready
return sharedInt;
}
}
// ProducerConsumerTest.java
p.start();
c.start();
}
}
OUTPUT
Producer:1
Producer is waiting
Consumer is consuming the value produced by producer
Consumer:1
//Addition.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public Addition()
{
super("Addition");
Container c = getContentPane();
c.setLayout( new FlowLayout());
setSize(100,100);
setVisible(true);
}
String b = num2Field.getText();
int b1 = Integer.parseInt(b);
}
public static void main(String args[])
{
Addition add = new Addition();
add.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public SignalLight() {
Container c = getContentPane();
c.setLayout(new FlowLayout());
setSize(250, 250);
setVisible(true);
}
if (e.getSource() == redLightBtn) {
g.setColor(Color.RED);
g.fillOval(100, 100, 40, 40);
}
if (e.getSource() == greenLightBtn) {
g.setColor(Color.GREEN);
if (e.getSource() == yellowLightBtn) {
g.setColor(Color.YELLOW);
g.fillOval(100, 100, 40, 40);
}
}
}
OUTPUT
import java.awt.*;
import javax.swing.*;
OUTPUT
//Triangle.java
public class Triangle extends Shape{
public void noOfSides()
{
System.out.println("Triangle has 3 sides");
}
}
//Pentagon.java
public class Pentagon extends Shape{
public void noOfSides()
{
System.out.println("Pentagon has 5 sides");
}
}
//Hexagon.java
public class Hexagon extends Shape {
public void noOfSides()
{
System.out.println("Hexagon has 6 sides");
}
}
//ShapeTest.java
public class ShapeTest {
OUTPUT
import javax.swing.*;
import java.awt.*;
public class LoginTable extends JApplet {
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ComboBoxTest extends JFrame implements ItemListener {
public String courses[] = {"CSE","ECE","EEE"};
public JComboBox courseComBox;
public ComboBoxTest()
{//get ContentPane and set its layout
Container c =getContentPane();
c.setLayout(new FlowLayout());
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public CheckBoxTest() {
super("CheckBoxTest");
//get the container and set its layout
Container c = getContentPane();
c.setLayout(new FlowLayout());
setSize(300, 200);
setVisible(true);
}
//ListTest.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public ListTest()
{
super( "List Test" );
Container c = getContentPane();
c.setLayout( new FlowLayout() );
}
}
// KeyDemo.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public KeyDemo()
{
super( "Demonstrating Keystroke Events" );
Container c = getContentPane();
c.setLayout(new FlowLayout());
app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
}
JTextArea output;
JLabel statusBar;
public KeyAdapterDemo() {
super("A simple paint program");
Container c = getContentPane();
c.setLayout(new FlowLayout());
}
});
setSize(300, 150);
setVisible(true);
}
app.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//MouseAdapterDemo.java
public MouseAdaptersDemo()
{
super( "Mouse Events" );
Container c = getContentPane();
c.setLayout(new FlowLayout());
addMouseListener(
new MouseAdapter() {
public void mouseClicked( MouseEvent e )
{
int x = e.getX();
int y = e.getY();
outputLabel.setText("Mouse Clicked at x = " + x + " y = "
+ y );
}
}
);
addMouseMotionListener(
new MouseMotionAdapter() {
public void mouseDragged( MouseEvent e )
{
int x = e.getX();
int y = e.getY();
outputLabel.setText("Mouse Dragged at x = " + x + " y = "
+ y );
}
}
);
app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}