You are on page 1of 13

Name: Kandarp Kotadia

Enrolment No: 91900103034


Branch: Computer Engineering
Class: 4TC1
Subject Code: 01CE0403
Subject Name: Object Oriented Programming with Java

Program Title: Write a program that will generate exceptions of type


NullPointerException, NegativeArraySizeException, and
IndexOutOfBoundsException. Record the catching of each exception by
displaying the message stored in the exception object and the stack trace record.

Program Code:

public class P16 {


public static void main(String[] args) {
int[] arr = null;
try {
int temp = arr[0];
} catch (NullPointerException e) {
System.out.println("Null Pointer Exception caught");
}
try {
arr = new int[-5];
} catch (NegativeArraySizeException e) {
System.out.println("Negative Array Size Exception caught");
}
try {
arr = new int[5];
int temp = arr[7];
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array Index out of bound caught");
}
}
}

Program Output:
Program Title: Write a program that calls a method that throws an exception of
type ArithmeticException at a random iteration in for loop. Catch the exception in
the method and pass the iteration count when the exception occurred to the
calling method by using an object of an exception class you define.

Program Code:

public class P17 {

int cnt = 0;
void check() throws ArithmeticException {
int a = 8, b, c;

for (cnt = 0; cnt <= 5000; cnt++) {


try {
b = (int) (Math.random() * 50) + 0;
c = (a / b);
} catch (ArithmeticException ae) {
throw ae;
}
}
}
public static void main(String[] args) {
P17 ob = new P17();

try {
ob.check();
} catch (ArithmeticException ae) {
System.out.println("Exception occure at : " + ob.cnt + "
iteration");
}
}
}

Program Output:
Program Title: Create three threads in synchronization that will reduce the input
number by one every time and displays the output as follows: Start thread_1: 15
14 13 12 11 Start thread_2: 10 9 8 7 Start thread_3: 5 4 3 2 1 End thread_3 Start
thread_1: 10 9 8 7 6 5 4 3 2 1 End thread_1 Start thread_2: 7 6 5 4 3 2 1 End
thread_2.

Program Code:

class Printing {

void printnum(int n) {
for (int j = n; j > 0; j--) {
System.out.println(j);
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
}
}
}
}

class ThreadEx implements Runnable {

int n;
Printing pt;
Thread t;

ThreadEx(Printing p, int x) {
n = x;
pt = p;
t = new Thread(this);
t.start();
}

public void run() {


pt.printnum(n);
}
}

public class P18 {

public static void main(String[] args) {

Printing ob = new Printing();


ob.printnum(5);
}
}

Program Output:

Program Title: Write a Main method that takes the name of a text file as a command
line argument and prints every line in lower case.

Program Code:

import java.io.FileInputStream;
public class P19 {
public static void main(String[] args)throws Exception{

FileInputStream fin = new


FileInputStream("C:\\Kandarp\\JavaApplication\\src\\SEM4\\demo.tx
t");

int size=fin.available();
char ch;
for(int i=0;i<size;i++){
ch = (char) fin.read();
System.out.print(Character.toLowerCase(ch));
}
fin.close();
}
}

Program Output:
Program Title: Write a main() method that counts the number of words in a text file
whose name is accepted from standard input. Also print the size of a file.

Program Code:

import java.io.FileInputStream;
import java.util.Scanner;
public class P20 {
public static void main(String[] args)throws Exception{
int counter=0;
FileInputStream fin = new
FileInputStream("C:\\Kandarp\\JavaApplication\\src\\SEM4\\demo.tx
t");
byte[] a= new byte[100];
fin.read(a);

String s = new String(a);


String b[] = s.split(" ");
for(int i=0;i<b.length;i++){
counter++;
System.out.println(b[i]);
}
System.out.println("No of words: "+counter);
}
}

Program Output:

Program Title: Write a program using BufferedInputStream, FileInputStream,


BufferedOutputStream, FileOutputStream to copy Content of one file File1.txt into
another file File2.txt.

Program Code:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class P21 {


public static void main(String[] args) throws IOException{

int ch;
FileInputStream fin = new
FileInputStream("C:\\Kandarp\\JavaApplication\\src\\SEM4\\demo.tx
t");
BufferedInputStream Bfin = new BufferedInputStream(fin);

FileOutputStream fout = new


FileOutputStream("C:\\Kandarp\\JavaApplication\\src\\SEM4\\demo
1.txt");
BufferedOutputStream Bfout = new
BufferedOutputStream(fout);

while((ch=Bfin.read()) != -1){
Bfout.write(ch);
}
Bfin.close();
Bfout.close();
System.out.println("Contents Copied Successfully!");
}
}

Program Output:

Program Title: Develop an applet that draws a circle. The dimension of the applet
should be 500 x 300 pixels. The circle should be centered in the applet and have a
radius of 100 pixels. Display your name centered in a circle. (using drawOval()
method).

Program Code:

import java.applet.Applet;
import java.awt.Graphics;

public class P22 extends Applet {

public void init(){


super.init();
setSize(250, 200);
}

public void paint(Graphics s){


s.drawOval(50, 50, 100, 100);
s.drawString("Sherlock", 100, 110);
}
}

Program Output:

Program Title: Draw ten red circles in a vertical column in the center of the applet.

Program Code:

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;

public class P23 extends Applet{

public void init(){


super.init();
setSize(400, 300);
}

public void paint(Graphics s){


for(int i=1; i<=10; i++){
s.setColor(Color.red);
s.fillOval(200, i*10, 10, 10);
}
}
}

Program Output:

Program Title: Built an applet that displays a horizontal rectangle in its center. Let
the rectangle fill with color from left to right.

Program Code:

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;

public class P24 extends Applet {

public void init() {


super.init();
setSize(400, 350);
}

public void paint(Graphics s) {


int x1 = 150, y1 = 140, y2 = 50;
s.setColor(Color.GREEN);
s.drawRect(150, 140, 100, 50);

for (x1 = 150; x1 < 250; x1 = x1 + 5) {


try{
Thread.sleep(100);
s.fillRect(x1, y1, 5, y2);
}
catch(Exception e){
e.printStackTrace();
}
}
}
}

Program Output:

Program Title: Write a java application to create a Calculator using JButton and
JTextField. Use Grid layout manager.

Program Code:

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class P25 implements ActionListener {
private static JTextField inputBox;
public static void main(String[] args) {
createWindow();
}
private static void createWindow() {
JFrame frame = new JFrame("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createUI(frame);
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static void createUI(JFrame frame) {
JPanel panel = new JPanel();
P25 calculator = new P25();
GridBagLayout layout = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
panel.setLayout(layout);
inputBox = new JTextField(10);
inputBox.setEditable(false);
JButton button0 = new JButton("0");
JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
JButton button3 = new JButton("3");
JButton button4 = new JButton("4");
JButton button5 = new JButton("5");
JButton button6 = new JButton("6");
JButton button7 = new JButton("7");
JButton button8 = new JButton("8");
JButton button9 = new JButton("9");
JButton buttonPlus = new JButton("+");
JButton buttonMinus = new JButton("-");
JButton buttonDivide = new JButton("/");
JButton buttonMultiply = new JButton("x");
JButton buttonClear = new JButton("C");
JButton buttonDot = new JButton(".");
JButton buttonEquals = new JButton("=");
button1.addActionListener(calculator);
button2.addActionListener(calculator);
button3.addActionListener(calculator);
button4.addActionListener(calculator);
button5.addActionListener(calculator);
button6.addActionListener(calculator);
button7.addActionListener(calculator);
button8.addActionListener(calculator);
button9.addActionListener(calculator);
button0.addActionListener(calculator);

buttonPlus.addActionListener(calculator);
buttonMinus.addActionListener(calculator);
buttonDivide.addActionListener(calculator);
buttonMultiply.addActionListener(calculator);
buttonClear.addActionListener(calculator);
buttonDot.addActionListener(calculator);
buttonEquals.addActionListener(calculator);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
panel.add(button1, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
panel.add(button2, gbc);
gbc.gridx = 2;
gbc.gridy = 0;
panel.add(button3, gbc);
gbc.gridx = 3;
gbc.gridy = 0;
panel.add(buttonPlus, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
panel.add(button4, gbc);
gbc.gridx = 1;
gbc.gridy = 1;
panel.add(button5, gbc);
gbc.gridx = 2;
gbc.gridy = 1;
panel.add(button6, gbc);
gbc.gridx = 3;
gbc.gridy = 1;
panel.add(buttonMinus, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
panel.add(button7, gbc);
gbc.gridx = 1;
gbc.gridy = 2;
panel.add(button8, gbc);
gbc.gridx = 2;
gbc.gridy = 2;
panel.add(button9, gbc);
gbc.gridx = 3;
gbc.gridy = 2;
panel.add(buttonDivide, gbc);
gbc.gridx = 0;
gbc.gridy = 3;
panel.add(buttonDot, gbc);
gbc.gridx = 1;
gbc.gridy = 3;
panel.add(button0, gbc);
gbc.gridx = 2;
gbc.gridy = 3;
panel.add(buttonClear, gbc);
gbc.gridx = 3;
gbc.gridy = 3;
panel.add(buttonMultiply, gbc);
gbc.gridwidth = 3;
gbc.gridx = 0;
gbc.gridy = 4;
panel.add(inputBox, gbc);
gbc.gridx = 3;
gbc.gridy = 4;
panel.add(buttonEquals, gbc);
frame.getContentPane().add(panel, BorderLayout.CENTER);
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
switch (command.charAt(0)) {
case 'C':
inputBox.setText("");
break;
case '=':
inputBox.setText(evaluate(inputBox.getText()));
break;
default:
inputBox.setText(inputBox.getText() + command);
break;
}
}

public static String evaluate(String expression) {


char[] arr = expression.toCharArray();
String operand1 = "";
String operand2 = "";
String operator = "";
double result = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] >= '0' && arr[i] <= '9' || arr[i] == '.') {
if (operator.isEmpty()) {
operand1 += arr[i];
} else {
operand2 += arr[i];
}
}
if (arr[i] == '+' || arr[i] == '-' || arr[i] == '/' || arr[i] == '*') {
operator += arr[i];
}
}
switch (operator) {
case "+":
result = (Double.parseDouble(operand1) +
Double.parseDouble(operand2));
break;
case "-":
result = (Double.parseDouble(operand1) -
Double.parseDouble(operand2));
break;
case "/":
result = (Double.parseDouble(operand1) /
Double.parseDouble(operand2));
break;
default:
result = (Double.parseDouble(operand1) *
Double.parseDouble(operand2));
break;
}
return operand1 + operator + operand2 + "=" + result;
}
}

Program Output:

You might also like