Professional Documents
Culture Documents
Name
Branch
Roll no
Program 1: Create the program to take input for two numbers and add them.
Code:
import java.util.*;
Output:
Enter the first number: 23
Enter the second number: 45
Sum of these numbers: 68
Box() {
width = height = depth = 0;
}
Box(double len) {
width = height = depth = len;
}
double volume() {
return width * height * depth;
}
}
// Driver code
public class Test {
public static void main(String args[]) {
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);
double vol;
vol = mybox1.volume();
System.out.println(" Volume of mybox1 is " + vol);
vol = mybox2.volume();
System.out.println(" Volume of mybox2 is " + vol);
vol = mycube.volume();
System.out.println(" Volume of mycube is " + vol);
}
}
Output:
Volume of mybox1 is 3000.0
Volume of mybox2 is 0.0
Volume of mycube is 343.0
Program 3: Create a customized exception and also make use of all the 5 exception
keywords.
Code:
class MyException extends Exception {
public MyException(String s) {
// Call constructor of parent Exception
super(s);
}
}
Output:
Caught
It’s an exception
Program 4: Write a program for Star pattern, switch case example, exception handling
for an age exception.
Code:
import java.util.Scanner;
Output:
Enter the number of rows needed to print the pattern
3
## Printing the pattern ##
*
**
***
Output:
20
Exception Handling:
import java.util.Scanner;
class AgeException extends Exception {
public AgeException(String str) {
System.out.println(str);
}
}
public class AgeExcDemo {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter ur age :: ");
int age = s.nextInt();
try {
if(age < 18)
throw new AgeException("Invalid age");
else
System.out.println("Valid age");
}
catch (AgeException a) {
System.out.println(a);
}
}
}
Output:
Enter ur age :: 15
Invalid age
exception.AgeException
Enter ur age :: 20
Valid age
Program 5: Demonstrate the Abstract class example, static class example, stack push
pop operation.
Code:
abstract class Bike {
abstract void run();
}
import java.util.*;
public class Stack {
public static void main(String args[])
{
// Creating an empty Stack
Stack<Integer> STACK = new Stack<Integer>();
// Use add() method to add elements
STACK.push(10);
STACK.push(15);
STACK.push(30);
STACK.push(20);
STACK.push(5);
// Displaying the Stack
System.out.println("Initial Stack: " + STACK);
// Removing elements using pop() method
System.out.println("Popped element: " +
STACK.pop());
System.out.println("Popped element: " +
STACK.pop());
// Displaying the Stack after pop operation
System.out.println("Stack after pop operation "
+ STACK);
}
}
Output: Initial Stack: [10, 15, 30, 20, 5]
Popped element: 5
Popped element: 20
Stack after pop operation [10, 15, 30]
void getAge();
}
// Class 1
// Helper class implementing methods of Age Interface
class MyClass implements Age {
// Class 2
// Main class
// AnonymousDemo
class AnonymousClassInterpretation {
// Main driver method
public static void main(String[] args) {
// Class 1 is implementation class of Age interface
MyClass obj = new MyClass();
Output:
Age is 21
Output:
Program 8: Write a java program to show dynamic polymorphism and interfaces.
Code:
public class DynamicPolymorphism
{
public static void main(String args[])
{
//assigning a child class object to a parent class reference
Fruits fruits = new Mango();
//invoking the method
fruits.color();
}
}
//parent class
class Fruits
{
public void color()
{
System.out.println("Parent class method is invoked.");
}
}
//derived or child class that extends the parent class
class Mango extends Fruits
{
//overrides the color() method of the parent class
@Override
public void color()
{
System.out.println("The child class method is invoked.");
}
}
Output:
The child class method is invoked.
Interface example:
interface printable{
void print();
}
class A6 implements printable{
public void print(){System.out.println("Hello");}
public static void main(String args[]){
A6 obj = new A6();
obj.print();
}
}
Output: Hello
Program 9: Write a program in Java to implement Queue concept.
Code:
public class Queue {
int SIZE = 5;
int items[] = new int[SIZE];
int front, rear;
Queue() {
front = -1;
rear = -1;
}
// if queue is full
if (isFull()) {
System.out.println("Queue is full");
} else {
if (front == -1) {
// mark front denote first element of queue
front = 0;
}
rear++;
// insert element at the rear
items[rear] = element;
System.out.println("Insert " + element);
}
}
// if queue is empty
if (isEmpty()) {
System.out.println("Queue is empty");
return (-1);
} else {
// remove element from the front of queue
element = items[front];
q.display();
}
}
Output:
Queue is empty
Insert 1
Insert 2
Insert 3
Insert 4
Insert 5
Queue is full
Front index-> 0
Items ->
1 2 3 4 5
Rear index-> 4
1 Deleted
Front index-> 1
Items ->
2 3 4 5
Rear index-> 4
Output:
Enter a string: Applet
Upper Case String: APPLET
Reversed String: telppA
Vowels: ae
Consonants: pplt
class Test {
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
stack.pop();
} else // an operator is encountered
{
while (!stack.isEmpty() && Prec(c) <= Prec(stack.peek())) {
result += stack.pop();
}
stack.push(c);
}
}
// Driver method
public static void main(String[] args) {
String exp = "a+b*(c^d-e)^(f+g*h)-i";
System.out.println(infixToPostfix(exp));
}
}
Output:
abcd^e-fgh*+^*+i-
Program 12: Write a java program to show multithreaded producer and consumer
application.
Code:
import java.util.LinkedList;
// t1 finishes before t2
t1.join();
t2.join();
}
System.out.println("Producer produced-"
+ value);
System.out.println("Consumer consumed-"
+ val);
// and sleep
Thread.sleep(1000);
}
}
}
}
}
Output:
Producer produced-0
Producer produced-1
Consumer consumed-0
Consumer consumed-1
Producer produced-2
ScientificCalculator() {
cont = getContentPane();
cont.setLayout(new BorderLayout());
JPanel textpanel = new JPanel();
tfield = new JTextField(25);
tfield.setHorizontalAlignment(SwingConstants.RIGHT);
tfield.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent keyevent) {
char c = keyevent.getKeyChar();
if (c >= '0' && c <= '9') {
} else {
keyevent.consume();
}
}
});
textpanel.add(tfield);
buttonpanel = new JPanel();
buttonpanel.setLayout(new GridLayout(8, 4, 2, 2));
boolean t = true;
mr = new JButton("MR");
buttonpanel.add(mr);
mr.addActionListener(this);
mc = new JButton("MC");
buttonpanel.add(mc);
mc.addActionListener(this);
mp = new JButton("M+");
buttonpanel.add(mp);
mp.addActionListener(this);
mm = new JButton("M-");
buttonpanel.add(mm);
mm.addActionListener(this);
b1 = new JButton("1");
buttonpanel.add(b1);
b1.addActionListener(this);
b2 = new JButton("2");
buttonpanel.add(b2);
b2.addActionListener(this);
b3 = new JButton("3");
buttonpanel.add(b3);
b3.addActionListener(this);
b4 = new JButton("4");
buttonpanel.add(b4);
b4.addActionListener(this);
b5 = new JButton("5");
buttonpanel.add(b5);
b5.addActionListener(this);
b6 = new JButton("6");
buttonpanel.add(b6);
b6.addActionListener(this);
b7 = new JButton("7");
buttonpanel.add(b7);
b7.addActionListener(this);
b8 = new JButton("8");
buttonpanel.add(b8);
b8.addActionListener(this);
b9 = new JButton("9");
buttonpanel.add(b9);
b9.addActionListener(this);
eq = new JButton("=");
buttonpanel.add(eq);
eq.addActionListener(this);
double fact(double x) {
int er = 0;
if (x < 0) {
er = 20;
return 0;
}
double i, s = 1;
for (i = 2; i <= x; i += 1.0)
s *= i;
return s;
}
Output:
Output:
Program 15: Create a GUI for storing the students information and write it to file &
save to database as well.
Code:
import java.awt.*;
import java.awt.event.*;
TextArea display_details;
Student() {
setBackground(Color.CYAN);
lsname = new Label("Name : ");
lsrollno = new Label("Roll No : ");
lsclass = new Label("Class : ");
lgender = new Label("Gender : ");
add(lsname);
add(lsrollno);
add(lsclass);
add(lgender);
add(lsadrs);
add(lsmob);
add(male);
add(female);
add(csclass);
add(tfsname);
add(tfsrollno);
add(tasadrs);
add(tfsmob);
add(submit);
add(display_details);
submit.addActionListener(this);
setTitle("Students Details");
setSize(460, 390);
setLayout(null);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
}
display_details.setText(sdetails);
}
}
Output:
Program 16: Convert the content of a given file into uppercase content.
Code:
import java.io.*;
Output:
ThIs Is my fIrst prOgrAm. It Is In JAvA.