Java Record
Java Record
import [Link].*;
import [Link].*;
class Quadra
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
double a, b, c, d, r1, r2;
[Link]("Enter a, b and c values :");
a = [Link]([Link]());
b = [Link]([Link]());
c = [Link]([Link]());
d = b * b - 4 * a * c;
if(d > 0)
{
[Link]("Roots are real: ");
r1 = (-b + [Link](d)) / (2 * a);
r2 = (-b - [Link](d)) / (2 * a);
[Link]("root1 = "+r1+", root2 = "+r2);
}
else if(d == 0)
{
[Link]("Roots are real & Equal:");
r1 = -b / (2 * a);
r2 = -b / (2 * a);
[Link]("root1 = "+r1+", root2 = "+r2);
}
else
Output:
import [Link].*;
class Fibonacci
{
public static void main(String args[]) throws IOException
{
int f1 = 0, f2 = 1, f3;
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter any value for n :");
int n = [Link]([Link]());
[Link](f1+" "+f2);
do
{
f3 = f1 + f2;
if(f3 <= n)
[Link](" "+f3);
f1 = f2;
f2 = f3;
}while(f3 <= n);
}
}
Output:
import [Link].*;
class Prime
{
public static void main(String args[]) throws IOException
{
int i = 1;
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter any value for n :");
int n = [Link]([Link]());
[Link]("Prime numbers from 1 to n: ");
while(i <= n)
{
int j = 1, found = 0;
while(j <= i)
{
if(i % j == 0)
found++;
j++;
}
if(found == 2)
[Link](" "+i);
i++;
}
}
}
Output:
import [Link].*;
class MatrixMul
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter [Link] rows and cols in first matrix :");
int r1 = [Link]([Link]());
int c1 = [Link]([Link]());
int a[][] = new int [r1][c1];
[Link]("Enter the elements: ");
for(int i=0; i<r1; i++)
for(int j=0; j<c1; j++)
a[i][j]=[Link]([Link]());
[Link]("First Matrix :");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c1;j++)
[Link](" "+a[i][j]);
[Link]();
}
[Link]("Enter [Link] rows and cols in second matrix :");
int r2 = [Link]([Link]());
int c2 = [Link]([Link]());
int b[][] = new int[r2][c2];
[Link]("Enter the elements: ");
for(int i=0; i<r2; i++)
for(int j=0; j<c2; j++)
b[i][j] = [Link]([Link]());
[Link]("Matrix Two :");
for(int i=0; i<r2; i++)
{
for(int j=0; j<c2; j++)
[Link](" "+b[i][j]);
[Link]();
}
int c[][] = new int[r1][c2];
if(c1 == r2)
{
[Link]("Multiplication :");
for(int i=0; i<r1; i++)
for(int j=0; j<c2; j++)
{
c[i][j] = 0;
for(int k=0;k<c1;k++)
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
for(int i=0; i<r1; i++)
{
Output:
import [Link].*;
class StringPalindrome
{
public static void main(String args[]) throws IOException
{
int found = 1;
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter any string :");
String str = [Link]();
int l = [Link]();
for(int i=0, j=l-1; i<=j; i++, j--)
if([Link](i) != [Link](j))
found = 0;
if(found == 1)
[Link]("The given String is Palindrome.");
else
[Link]("The given String is not Palindrome.");
}
}
Output:
import [Link].*;
import [Link].*;
class StringSort
{
public static void main(String args[])throws IOException
{
String temp;
DataInputStream dis = new DataInputStream([Link]);
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter [Link] Strings :");
int n = [Link]([Link]());
String a[] = new String[n];
[Link]("Enter the strings: ");
for(int i=0; i<n; i++)
a[i] = [Link]();
for(int i=1; i<n; i++)
for(int j=0; j<n-i; j++)
if(a[j].compareTo(a[j+1])>0)
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
[Link]("The sorted string are:");
for(int i=0; i<n; i++)
[Link](a[i]+" ");
}
}
Output:
Output:
interface Car
{
int speed = 60;
public void distanceTravelled();
void display();
}
interface Bus
{
int distance = 100;
public void speed();
void display();
}
public class Vehicle implements Car, Bus
{
int distanceTravelled;
int averageSpeed;
public void distanceTravelled()
{
distanceTravelled = speed * distance;
[Link]("Total Distance Travelled is: "+distanceTravelled);
}
public void speed()
{
int averageSpeed = distanceTravelled / speed;
[Link]("Average Speed maintained is: "+averageSpeed);
}
public void display()
{
[Link]("Implementing common method from interfaces");
}
public static void main(String args[])
{
Vehicle v1 = new Vehicle();
[Link]();
[Link]();
[Link]();
}
}
Output:
class TestExceptions
{
static void myMethod(int testnum) throws Exception
{
[Link] ("Start - myMethod");
if (testnum == 12)
throw new Exception();
[Link]("End - myMethod");
return;
}
public static void main(String args[])
{
int testnum = 12;
try
{
[Link]("try - first statement");
myMethod(testnum);
[Link]("try - last statement");
}
catch (Exception ex)
{
[Link]("An Exception");
}
finally
{
System. out. println("finally") ;
}
[Link]("Out of try/catch/finally - statement");
}
}
Output:
Output:
[Link]
package pack;
public class Addition
{
private double d1, d2;
public Addition(double a,double b)
{
d1 = a;
d2 = b;
}
public void sum()
{
[Link]("The Addition is :"+(d1+d2));
}
}
[Link]
package pack;
public class Subtraction
{
private double d1, d2;
public Subtraction(double a,double b)
{
d1 = a;
d2 = b;
}
public void sub()
{
[Link]("The Subtraction is :"+(d1-d2));
}
}
[Link]
import [Link];
import [Link];
class MyPackage
{
public static void main(String args[])
{
Addition a = new Addition(10,15.5);
[Link]();
Subtraction s = new Subtraction(10,15.5);
[Link]();
}
}
Output:
import [Link].*;
import [Link].*;
class StrTokExample
{
public static void main(String[] args)throws IOException
{
String input;
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter the input: ");
input = [Link]();
StringTokenizer st = new StringTokenizer(input, ",");
int sum = 0;
[Link]("The tokens are: ");
while([Link]())
{
int n = [Link]([Link]());
[Link](n);
sum += n;
}
[Link]("Sum = "+sum);
}
}
Output:
import [Link].*;
import [Link].*;
class StackExample
{
public static void main(String args[]) throws IOException
{
int ch = 0;
Stack<Integer> st = new Stack<Integer>();
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
do
{
[Link]("[Link]()");
[Link]("[Link]()");
[Link]("[Link]()");
[Link]("[Link]()");
[Link]("[Link]()");
[Link]("[Link]()");
[Link]("Enter any choice: ");
ch = [Link]([Link]());
switch(ch)
{
case 1:
[Link]("Enter element: ");
int no = [Link]([Link]());
[Link](no);
break;
case 2:
int ele = [Link]();
[Link]("The deleted element is: "+ele);
break;
case 3:
int top = [Link]();
[Link]("The top most element is: "+top);
break;
case 4:
boolean b = [Link]();
if(b == true)
[Link]("The stack is empty.");
else
[Link]("The stack is not empry.");
break;
case 5:
int se = [Link]([Link]());
int n = [Link](se);
if(n == -1)
[Link]("The element not found.");
else
[Link]("The element found at: "+n);
break;
Output:
import [Link].*;
import [Link].*;
class HashTableExample
{
public static void main(String args[]) throws IOException
{
Hashtable<String, Long> ht = new Hashtable<String, Long>();
[Link]("Vijay", 9876543678L);
[Link]("Raju", 8765745369L);
[Link]("Bhasu", 9985123456L);
[Link]("Anand", 7789657868L);
[Link]("Srinu", 9987896754L);
[Link](ht);
Enumeration e = [Link]();
[Link]("The contacts available: ");
while([Link]())
[Link]([Link]());
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter name to search: ");
String name = [Link]();
Long phno = [Link](name);
if(phno != null)
{
long no = [Link]();
[Link](name+" Phone No is: "+no);
}
else
[Link]("Name is not found.");
}
}
Output:
try
{
[Link](2000);
}
catch(InterruptedException ie)
{
[Link]();
}
}
}
}
class MyThreadExa
{
public static void main(String args[])
{
MyThread m1 = new MyThread(10);
MyThread m2 = new MyThread(10);
MyThread m3 = new MyThread(10);
Thread t1 = new Thread(m1);
Thread t2 = new Thread(m2);
Thread t3 = new Thread(m3);
[Link]();
[Link]();
[Link]();
}
}
Output:
class Chat
{
boolean flag = false;
public synchronized void Question(String msg)
{
if (flag)
{
try
{
wait();
}
catch (InterruptedException e)
{
[Link]();
}
}
[Link](msg);
flag = true;
notify();
}
public synchronized void Answer(String msg)
{
if (!flag)
{
try
{
wait();
}
catch (InterruptedException e)
{
[Link]();
}
}
[Link](msg);
flag = false;
notify();
}
}
class T1 implements Runnable
{
Chat m;
String[] s1 = { "Hi", "How are you ?", "I am also doing fine!" };
public T1(Chat m1)
{
this.m = m1;
new Thread(this, "Question").start();
}
public void run()
{
Output:
import [Link].*;
import [Link].*;
class TrafficSignal extends Frame implements ActionListener
{
Button b1, b2, b3;
TrafficSignal()
{
[Link](null);
b1 = new Button("Red");
b2 = new Button("Yellow");
b3 = new Button("Green");
[Link](100, 100, 70, 40);
[Link](100, 160, 70, 40);
[Link](100, 220, 70, 40);
[Link](b1);
[Link](b2);
[Link](b3);
[Link](this);
[Link](this);
[Link](this);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
[Link](0);
}
});
}
public void actionPerformed(ActionEvent ae)
{
Graphics g = [Link]();
String str = [Link]();
if([Link]() == b1)
[Link]("Stop", 250, 120);
if([Link]() == b2)
[Link]("Wait",250,180);
if([Link]() == b3)
[Link]("Go",250,240);
}
public static void main(String args[])
{
TrafficSignal tc = new TrafficSignal();
[Link]([Link]);
[Link](400, 400);
[Link]("Traffic Rules: ");
[Link](true);
}
}
import [Link].*;
import [Link].*;
public class MouseExample extends Frame implements MouseMotionListener
{
MouseExample()
{
addMouseMotionListener(this);
setSize(500,500);
setLayout(null);
setVisible(true);
}
public void mouseDragged(MouseEvent e)
{
Graphics g = getGraphics();
[Link]([Link]);
[Link]([Link](), [Link](), 5, 5);
}
public void mouseMoved(MouseEvent e)
{
Graphics g = getGraphics();
[Link]([Link]);
[Link]([Link](), [Link](), 5, 5);
}
public static void main(String[] args)
{
new MouseExample();
}
}
Output:
import [Link].*;
import [Link].*;
public class MouseAdapterExample extends MouseAdapter
{
Frame f;
MouseAdapterExample()
{
f = new Frame("Mouse Adapter");
[Link](this);
[Link](300,300);
[Link](null);
[Link](true);
}
public void mouseEntered(MouseEvent e)
{
Graphics g = [Link]();
[Link]([Link]);
[Link]([Link](),[Link](),30,30);
}
public static void main(String[] args)
{
new MouseAdapterExample();
}
}
Output:
import [Link].*;
import [Link].*;
public class AppletExample extends Applet
{
String str = "";
public void init()
{
setBackground([Link]);
setForeground([Link]);
Font f = new Font("Arial", [Link], 20);
setFont(f);
str += " Welcome ";
}
public void start()
{
str += " To ";
}
public void stop()
{
str += " Applets Program ";
}
public void destroy()
{
str += "Destroy";
}
}
Output:
import [Link].*;
import [Link].*;
import [Link].*;
add(t1);
add(b7);
add(b8);
add(b9);
add(div);
add(b4);
add(b5);
add(b6);
add(mul);
add(b1);
add(b2);
add(b3);
add(dot);
add(b0);
add(eql);
add(add);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
}
public void actionPerformed(ActionEvent ae)
{
String str = [Link]();
if ([Link]("+")||[Link]("-")||[Link]("*")||[Link]("/"))
{
String str1 = [Link]();
tmp = str;
a = [Link](str1);
msg = "";
}
B V Raju Institute of Technology Department of Computer Science and Engineering
Java Programming Page No:
else if([Link]("="))
{
String str2 = [Link]();
b = [Link](str2);
int res=0;
if(tmp == "+")
res = a + b;
else if(tmp == "-")
res = a - b;
else if(tmp == "*")
res = a * b;
else if(tmp == "/")
res = a / b;
String str1=[Link](res);
[Link](""+str1);
msg = "";
}
else
{
msg += str;
[Link](""+msg);
}
}
public void paint(Graphics g)
{
[Link]([Link]);
[Link](20,20,220,270);
}
}
Output:
import [Link].*;
class MenuExample
{
JMenu menu, submenu;
JMenuItem i1, i2, i3, i4, i5;
MenuExample()
{
JFrame f = new JFrame("Menu and MenuItem Example");
JMenuBar mb =new JMenuBar();
menu = new JMenu("Menu");
submenu = new JMenu("Sub Menu");
i1 = new JMenuItem("Item 1");
i2 = new JMenuItem("Item 2");
i3 = new JMenuItem("Item 3");
i4 = new JMenuItem("Item 4");
i5 = new JMenuItem("Item 5");
[Link](i1);
[Link](i2);
[Link](i3);
[Link](i4);
[Link](i5);
[Link](submenu);
[Link](menu);
[Link](mb);
[Link](400, 400);
[Link](null);
[Link](true);
}
public static void main(String args[])
{
new MenuExample();
}
}
Output:
import [Link].*;
class JDBCStoring
{
public static void main(String args[])
{
try
{
//step1 load the driver class
[Link]("[Link]");
}//try
catch(Exception e)
{
[Link](e);
}//catch
}//main
}//JDBCStoring
Output:
import [Link].*;
class JDBCRetrieve
{
public static void main(String args[])
{
try
{
//step1 load the driver class
[Link]("[Link]");
}//try
catch(Exception e)
{
[Link](e);
}//catch
}//main
}//JDBCRetrieve
Output: