Programs
- Write a Java program to read the radius of a circle and to find the area and circumference.
- Write a Java program to demonstrate String Operators.
- Write a Java program to find N prime numbers reading N as command line argument.
- Write a program to find factorial of N numbers reading N as command line argument.
- Write a program to read N numbers and sort them using one-dimensional arrays.
- Write a Java program to illustrate Method Overloading.
- Write a Java program to illustrate Operator Overloading.
- Write a Java program to demonstrate Single Inheritance.
- Write a program to illustrate Constructor Overloading.
- Write a program to illustrate Method Overriding.
- Write a Java program demonstrating Multithreading.
- Write a Java program demonstrating Exception Handling.
- Write a Java program to demonstrate user defined package program.
- Write an Applet program to display Geometrical Figures using objects.
- Write an Applet program which illustrate Scroll bar object.
- Write an Applet program to change the background color randomly.
- Write an Applet program to change the color of applet using combo box.
- Write an Applet program to implement Digital Clock using thread.
- Write an Applet program to implement Mouse events.
- Write an Applet program to implement Keyboard events.
Programs with Solutions
1. Write a Java program to read the radius of a circle and to find the area and circumference.
import java.io.*;
class Main {
public static void main(String args[]) {
try {
DataInputStream dis = new DataInputStream(System.in);
System.out.println("Enter r");
int r = Integer.parseInt(dis.readLine());
System.out.println("Area = " +(3.142F * r * r));
System.out.println("Circumference = " +(2 * 3.142F * r));
} catch (IOException e) {}
}
}
2. Write a Java program to demonstrate String Operators.
public class StringOperators {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "World";
// Concatenation operator
String str3 = str1 + ", " + str2 + "!";
System.out.println(str3); // Output: Hello, World!
// Equality operator
System.out.println(str1 == str2); // Output: false
System.out.println(str1.equals(str2)); // Output: false
// Length operator
System.out.println(str1.length()); // Output: 5
// Substring operator
System.out.println(str1.substring(1, 4)); // Output: ell
}
}
3. Write a Java program to find N prime numbers reading N as command line argument.
public class PrimeNumbers {
public static void main(String[] args) {
int n = 10; // number of prime numbers to find
int num = 2;
int count = 0;
while (count < n) {
boolean isPrime = true;
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.print(num + " ");
count++;
}
num++;
}
}
}
4. Write a program to find factorial of N numbers reading N as command line argument.
class Main {
public static void main (String args[]) {
int a[] = new int[5], i, n=args.length;
for(i=0; i<n; i++) {
a[i] = Integer.parseInt(args[i]);
int fact=1;
for(int j=1; j<=a[i]; j++)
fact = fact * j;
System.out.println("Factorial of " +a[i]+ " is " +fact);
}
}
}
5. Write a program to read N numbers and sort them using one-dimensional arrays.
import java.io.DataInputStream;
public class sorting {
public static void main(String[] args) {
DataInputStream ab=new DataInputStream(System.in);
int a[]=new int[10];
int n=0, i, j, temp;
try {
System.out.println("Enter N");
n=Integer.parseInt(ab.readLine());
System.out.println("Enter the array element");
for(i=0; i<n; i++)
a[i]=Integer.parseInt(ab.readLine());
for(i=0; i<n; i++) {
for(j=i+1; j<n; j++) {
if(a[i]>a[j]) {
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
System.out.println("Elements in ascending order are");
for(i=0; i<n; i++)
System.out.println(a[i]);
for(i=0; i<n; i++) {
for(j=i+1; j<n; j++) {
if(a[i]<a[j]) {
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
System.out.println("Elements in descending order are");
for(i=0; i<n; i++)
System.out.println(a[i]);
}
catch (Exception e){
System.out.println(e);
}
}
}
6. Write a Java program to illustrate Method Overloading.
class Main {
static int add(int a, int b) {
return a + b;
}
static double add(double a, double b) {
return a + b;
}
public static void main(String[] args) {
System.out.println(add(10, 20));
System.out.println(add(10.5, 20.5));
}
}
7. Write a Java program to illustrate Operator Overloading (Java Doesnt support operator overloading soo this program isnt for exams).
class ComplexNumber {
private int real;
private int imag;
public ComplexNumber(int real, int imag) {
this.real = real;
this.imag = imag;
}
public String toString() {
return real + "+" + imag + "i";
}
}
class Main {
public static void main(String[] args) {
ComplexNumber c1 = new ComplexNumber(1, 2);
ComplexNumber c2 = new ComplexNumber(3, 4);
System.out.println(c1);
System.out.println(c2);
}
}
8. Write a Java program to demonstrate Single Inheritance.
class Animal {
public void eat(String name) {
System.out.println(name + " is eating.");
}
}
class Dog extends Animal {
public void bark(String name) {
System.out.println(name + " is barking.");
}
}
class Cat extends Animal {
public void meow(String name) {
System.out.println(name + " is meowing");
}
}
class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat("Romeo");
dog.bark("Blacky");
Cat kitty = new Cat();
kitty.eat("Billy");
kitty.meow("Snowball");
}
}
9. Write a program to illustrate Constructor Overloading.
class construct {
int base;
float height;
double width;
construct() {
base = 10;
height = 10.5f;
width = 10.25d;
System.out.println("Non parameterized constructor");
}
construct(int a, float b) {
base = a;
height = b;
width = 15.25d;
System.out.println("Constructor with two parameters");
}
construct(int a, float b, double d) {
base = a;
height = b;
width = d;
System.out.println("Constructor with three parameters");
}
double disp() {
return(base*height*width);
}
}
public class main {
public static void main (String args[])
{
construct c1 = new construct();
System.out.println("Area of cube 1 = "+c1.disp());
construct c2 = new construct(20, 20.5f);
System.out.println("Area of cube 2 = "+c2.disp());
construct c3 = new construct(50, 50.5f, 60.25d);
System.out.println("Area of cube 3 = "+c3.disp());
}
}
10. Write a program to illustrate Method Overriding.
class Animal {
void eat(String name) {
System.out.println(name + " is eating.");
}
}
class Dog extends Animal {
@Override
void eat(String name) {
System.out.println(name+ " is eating dog food");
}
}
class Main {
public static void main(String args[]) {
Animal a1 = new Animal();
a1.eat("Blackie");
Dog d1 = new Dog();
d1.eat("Rufus");
}
}
11. Write a Java program demonstrating Multithreading.
class MyThread extends Thread {
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("Child Thread");
}
}
}
public class Main {
public static void main(String[] args) {
MyThread t = new MyThread();
t.start();
for (int i = 0; i < 5; i++) {
System.out.println("Main Thread");
}
}
}
12. Write a Java program demonstrating Exception Handling.
class Main {
public static void main(String args[]) {
try {
System.out.println("Result = " +(10/0));
} catch(ArithmeticException a) {
System.out.println("Cant divide by zero");
}
}
}
13. Write a Java program to demonstrate user defined package program.
package gcd;
class gcd {
int x, y;
int gcd (int a, int b) {
x=a; y=b;
while(x!=y) {
if(x>y)
x=-y;
else
y=-y;
}
return x;
}
}
14. Write an Applet program to display Geometrical Figures using objects.
import java.awt.*;
import java.applet.*;
public class Shapes extends Applet {
public void paint(Graphics g) {
g.setFont(new Font("Cambria", Font.BOLD,15));
g.drawString("Different Shapes", 15, 15);
g.drawLine(10,20,50,60);
g.drawRect(10,70,40,40);
g.setColor(Color.blue);
g.fillOval(60,20,60,60);
g.fillArc(60,135,80,40,180,180);
g.fillRoundRect(20,120,60,30,5,5);
}
}
// <applet code="Shapes" width=200 height=200></applet>
15. Write an Applet program which illustrate Scroll bar object.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class ScrollbarApplet extends Applet implements AdjustmentListener {
Scrollbar scrollbar;
TextField textField;
Label l1;
public void init() {
scrollbar = new Scrollbar(Scrollbar.VERTICAL, 1, 1, 1, 50);
l1=new Label("Select a value:");
add(l1);
add(scrollbar);
scrollbar.addAdjustmentListener(this);
textField = new TextField(3);
textField.setEditable(false);
add(textField);
}
public void adjustmentValueChanged(AdjustmentEvent e) {
textField.setText(scrollbar.getValue()+"");
}
}
//<applet code="ScrollbarApplet" width=200 height=200></applet>
16. Write an Applet program to change the background color randomly.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.Random;
public class ColorApplet extends Applet implements Runnable {
Thread t = new Thread(this);
public void init() {
t.start();
}
public void run() {
while(true) {
Random ran = new Random();
int r = ran.nextInt(255);
int g = ran.nextInt(255);
int b = ran.nextInt(255);
Color c = new Color(r, g, b);
setBackground(c);
try {
t.sleep(1000);
} catch(Exception e) {}
}
}
}
//<applet code = "ColorApplet" height = 200 width = 200></applet>
17. Write an Applet program to change the color of applet using combo box.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
//<applet code="ComboApplet" width=200 height=200></applet>
public class ComboApplet extends Applet implements ItemListener {
public void init() {
Choice c=new Choice();
Label l=new Label("Choose a Color :");
c.add("White");
c.add("Red");
c.add("Green");
c.add("Blue");
c.add("Black");
c.addItemListener(this);
add(l);
add(c);
}
public void itemStateChanged(ItemEvent e) {
Choice c=(Choice)e.getSource();
String col=(String)c.getSelectedItem();
if(col=="Red")
setBackground(Color.red);
else if(col=="Green")
setBackground(Color.green);
else if(col=="Blue")
setBackground(Color.blue);
else if(col=="Black")
setBackground(Color.black);
else if(col=="White")
setBackground(Color.white);
}
}
18. Write an Applet program to implement Digital Clock using thread.
import java.awt.*;
import java.applet.*;
import java.util.*;
import java.text.*;
public class DigitalClock extends Applet implements Runnable {
Thread t = new Thread(this);
String time;
public void init() {
t.start();
}
public void run() {
try {
while(true) {
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
Date d = new Date();
time = sdf.format(d);
repaint();
t.sleep(1000);
}
}catch(Exception e) {}
}
public void paint(Graphics g) {
g.setFont(new Font("Cambria", Font.BOLD, 15));
g.drawString(time, 50, 50);
}
}
//<applet code = "DigitalClock" height = 200 width = 200></applet>
19. Write an Applet program to implement Mouse events.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*<applet code="Mousey.class" height=300 width=400></applet>*/
public class Mousey extends Applet implements MouseListener,ActionListener {
int x1,x2,y1,y2;
Button r,g,b,bl;
Label l1;
String s,msg;
public void init() {
l1=new Label("Choose your color");
r=new Button("Red");
g=new Button("Green");
b=new Button("Black");
bl=new Button("Blue");
r.setForeground(Color.red);
g.setForeground(Color.green);
b.setForeground(Color.black);
bl.setForeground(Color.blue);
add(l1);
add(b);
add(r);
add(bl);
add(g);
r.addActionListener(this);
g.addActionListener(this);
b.addActionListener(this);
bl.addActionListener(this);
addMouseListener(this);
}
public void mousePressed(MouseEvent e) {
x1=e.getX();
y1=e.getY();
msg="Mouse Pressed";
}
public void mouseReleased(MouseEvent e) {
x2=e.getX();
y2=e.getY();
repaint( );
msg="Mouse Released";
}
public void mouseClicked(MouseEvent e) {
msg="Mouse Clicked";
}
public void mouseExited(MouseEvent e) {
msg="Mouse Exited";
}
public void mouseEntered(MouseEvent e) {
msg="Mouse Entered";
}
public void paint(Graphics g) {
if(s=="red")
g.setColor(Color.red);
if(s=="black")
g.setColor(Color.black);
if(s=="blue")
g.setColor(Color.blue);
if(s=="green")
g.setColor(Color.green);
g.drawLine(x1,y1,x2,y2);
showStatus(msg);
}
public void actionPerformed(ActionEvent ae) {
if(ae.getSource()==r)
s="red";
if(ae.getSource()==g)
s="green";
if(ae.getSource()==b)
s="black";
if(ae.getSource()==bl)
s="blue";
}
}
20. Write an Applet program to implement Keyboard events.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
//<applet code=KEvent.class width=300 height=200></applet>
public class KEvent extends Applet implements KeyListener {
String msg;
public void init() {
msg="You Typed : ";
addKeyListener(this);
}
public void keyPressed(KeyEvent e){
showStatus("Key Down");
}
public void keyReleased(KeyEvent e) {
showStatus("Key Up");
}
public void keyTyped(KeyEvent e) {
msg+=e.getKeyChar();
repaint();
}
public void paint(Graphics g) {
g.drawString(msg,20,50);
}
}