Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

List

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

1. Write a Java program to do arithmetic operation on two numbers.

Input numbers as
Command line argument.

public class ArithmeticOperation {

public static void main(String[] args) {

if (args.length < 2) {
System.out.println("Please provide two numbers as command line arguments.");
return;
}

double num1 = Double.parseDouble(args[0]);


double num2 = Double.parseDouble(args[1]);

double sum = num1 + num2;


double difference = num1 - num2;
double product = num1 * num2;
double quotient = num1 / num2;

System.out.println("Sum: " + sum);


System.out.println("Difference: " + difference);
System.out.println("Product: " + product);
System.out.println("Quotient: " + quotient);

}
}
Output:
java ArithmeticOperation 5.5 2.5

Sum: 8.0
Difference: 3.0
Product: 13.75
Quotient: 2.2

2. Write a program to do the following using in-built methods in the string class of java.
a. Find the 3rd character in the string “Hello World”.
b. Find the index of character ‘o’ in string “Java Programming”.
c. Convert the string “be honest “to uppercase.
d. Replace character ‘l’; with ‘I’ in the string “hello”.

public class StringOperations {


public static void main(String[] args) {
String str1 = "Hello World";
String str2 = "Java Programming";
String str3 = "be honest";
String str4 = "hello";
// Find the 3rd character in the string "Hello World"
char thirdChar = str1.charAt(2);
System.out.println("The 3rd character in the string \"Hello World\" is: " + thirdChar);

// Find the index of character 'o' in string "Java Programming"


int index = str2.indexOf('o');
System.out.println("The index of character 'o' in string \"Java Programming\" is: " +
index);

// Convert the string "be honest" to uppercase


String upperCaseStr = str3.toUpperCase();
System.out.println("The uppercase string of \"be honest\" is: " + upperCaseStr);

// Replace character 'l' with 'I' in the string "hello"


String replacedStr = str4.replace('l', 'I');
System.out.println("The replaced string is: " + replacedStr);
}
}

Output:
The 3rd character in the string "Hello World" is: l
The index of character 'o' in string "Java Programming" is: 1
The uppercase string of "be honest" is: BE HONEST
The replaced string is: heIIo

3. Write application that declares a class named Employee. It should have instance
variables age, name &amp; salary. These should be of type int, String, float respectively.
Create the object of Employee class and set and display its instance variable.

public class Employee {


int age;
String name;
float salary;

public static void main(String[] args) {


Employee employee = new Employee();
employee.age = 25;
employee.name = "Dev Ghetia";
employee.salary = 5000.0f;

System.out.println("Employee's name: " + employee.name);


System.out.println("Employee's age: " + employee.age);
System.out.println("Employee's salary: " + employee.salary);
}
}

Output:
Employee's name: Dev Ghetia
Employee's age: 25
Employee's salary: 5000.0

4. Write a Java program which creates the Triangle class with two attributes base and
height of type float or double. Takes the two constructors of the Triangle class.
First constructor takes the default value for base and height and Second Constructor
takes base and height as a parameter. Create a method calcArea() to calculate the
area of the Triangle. Define a main method and create objects to the class and print
the area of the Triangle.

public class Triangle {


private double base;
private double height;

public Triangle() {
this.base = 0.0;
this.height = 0.0;
}

public Triangle(double base, double height) {


this.base = base;
this.height = height;
}

public double calcArea() {


return 0.5 * base * height;
}

public static void main(String[] args) {


Triangle triangle1 = new Triangle();
Triangle triangle2 = new Triangle(5.0, 8.0);

System.out.println("Area of triangle1: " + triangle1.calcArea());


System.out.println("Area of triangle2: " + triangle2.calcArea());
}
}

Output:
Area of triangle1: 0.0
Area of triangle2: 20.0

5. Create a Shape class as the abstract class with abstract method draw( ), its
Implementation is provided by the Rectangle &amp; Circle classes. Create a reference of
Shape class and if you create the instance of Rectangle class, draw() method of
Rectangle class will be invoked. And same for Circle class. (Dynamic Method
Dispatch)
abstract class Shape {
abstract void draw();
}

class Rectangle extends Shape {


void draw() {
System.out.println("Drawing a rectangle");
}
}

class Circle extends Shape {


void draw() {
System.out.println("Drawing a circle");
}
}

public class Main {


public static void main(String[] args) {
Shape shape;

shape = new Rectangle();


shape.draw(); // calls Rectangle's draw() method

shape = new Circle();


shape.draw(); // calls Circle's draw() method
}
}

Output:
Drawing a rectangle
Drawing a circle

6. Create a package MathPack having class MathDemo with method add() and sub() to
find addition and subtraction. Create another program and `import package and
invoke methods.

package MathPack;

public class MathDemo {


public static int add(int a, int b) {
return a + b;
}

public static int sub(int a, int b) {


return a - b;
}
}

import MathPack.MathDemo;

public class Main {


public static void main(String[] args) {
int a = 10;
int b = 5;

int sum = MathDemo.add(a, b);


int diff = MathDemo.sub(a, b);

System.out.println("Sum: " + sum);


System.out.println("Difference: " + diff);
}
}

Output:
Sum: 15
Difference: 5

7. Write a JAVA program that creates threads by extending Thread class .First thread
Display “Good Morning “every 1 sec, the second thread displays “Hello “every 2
Seconds and the third display “Welcome” every 3 seconds, (implementing using Runnable).

class GoodMorning extends Thread {


public void run() {
try {
while (true) {
System.out.println("Good Morning");
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

class Hello implements Runnable {


public void run() {
try {
while (true) {
System.out.println("Hello");
Thread.sleep(2000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

class Welcome implements Runnable {


public void run() {
try {
while (true) {
System.out.println("Welcome");
Thread.sleep(3000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

public class ThreadExample {


public static void main(String[] args) {
GoodMorning thread1 = new GoodMorning();
Hello runnable1 = new Hello();
Thread thread2 = new Thread(runnable1);
Welcome runnable2 = new Welcome();
Thread thread3 = new Thread(runnable2);

thread1.start();
thread2.start();
thread3.start();
}
}

Output:
Good Morning
Hello
Good Morning
Welcome
Good Morning
Hello
Good Morning
Good Morning
Welcome
Hello
Good Morning
Welcome
Good Morning
Hello
Good Morning
...

8. Create program for producer and consumer problem.

In BOOK

9. Create program for DeadLock.

class A {

synchronized void fun() {

System.out.println("A");

synchronized void m1(B b) {

System.out.println("m1");

b.fun();

// synchronized

class B {

synchronized void fun() {

System.out.println("B");

synchronized void m2(A a) {

System.out.println("m2");

a.fun();

}
public class Deadlock extends Thread {

A a = new A();

B b = new B();

void s() {

a.m1(b);

public void run() {

b.m2(a);

public static void main(String[] args) {

Deadlock dl = new Deadlock();

dl.start();

dl.s();

Output:

m1

m2

10. Create Client-Server program using socket and serverSocket.

Server Code:

import java.net.*;

import java.io.*;

public class Server {

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

ServerSocket serverSocket = null;

try {
serverSocket = new ServerSocket(5000);

} catch (IOException e) {

System.err.println("Could not listen on port: 5000.");

System.exit(1);

Socket clientSocket = null;

try {

System.out.println("Waiting for client connection...");

clientSocket = serverSocket.accept();

System.out.println("Client connected.");

} catch (IOException e) {

System.err.println("Accept failed.");

System.exit(1);

PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);

BufferedReader in = new BufferedReader(new


InputStreamReader(clientSocket.getInputStream()));

String inputLine, outputLine;

while ((inputLine = in.readLine()) != null) {

System.out.println("Received message: " + inputLine);

outputLine = "Server says: " + inputLine;

out.println(outputLine);

if (outputLine.equals("bye"))

break;

}
out.close();

in.close();

clientSocket.close();

serverSocket.close();

Client Code:

import java.net.*;

import java.io.*;

public class Client {

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

Socket echoSocket = null;

PrintWriter out = null;

BufferedReader in = null;

try {

echoSocket = new Socket("localhost", 5000);

out = new PrintWriter(echoSocket.getOutputStream(), true);

in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));

} catch (UnknownHostException e) {

System.err.println("Don't know about host: localhost.");

System.exit(1);

} catch (IOException e) {

System.err.println("Couldn't get I/O for the connection to: localhost.");

System.exit(1);

BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

String userInput;
while ((userInput = stdIn.readLine()) != null) {

out.println(userInput);

if (userInput.equals("bye"))

break;

System.out.println("Server says: " + in.readLine());

out.close();

in.close();

stdIn.close();

echoSocket.close();

Output:

Server Output:

Waiting for client connection...

Client connected.

Received message: Hello

Received message: How are you?

Received message: bye

Client Output:

Hello

Server says: Server says: Hello

How are you?

Server says: Server says: How are you?

bye

Server says: Server says: bye


11. Create a bank application.

You might also like