List
List
List
Input numbers as
Command line argument.
if (args.length < 2) {
System.out.println("Please provide two numbers as command line arguments.");
return;
}
}
}
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”.
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 & salary. These should be of type int, String, float respectively.
Create the object of Employee class and set and display its instance variable.
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 Triangle() {
this.base = 0.0;
this.height = 0.0;
}
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 & 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();
}
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;
import MathPack.MathDemo;
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).
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
...
In BOOK
class A {
System.out.println("A");
System.out.println("m1");
b.fun();
// synchronized
class B {
System.out.println("B");
System.out.println("m2");
a.fun();
}
public class Deadlock extends Thread {
A a = new A();
B b = new B();
void s() {
a.m1(b);
b.m2(a);
dl.start();
dl.s();
Output:
m1
m2
Server Code:
import java.net.*;
import java.io.*;
try {
serverSocket = new ServerSocket(5000);
} catch (IOException e) {
System.exit(1);
try {
clientSocket = serverSocket.accept();
System.out.println("Client connected.");
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
out.println(outputLine);
if (outputLine.equals("bye"))
break;
}
out.close();
in.close();
clientSocket.close();
serverSocket.close();
Client Code:
import java.net.*;
import java.io.*;
BufferedReader in = null;
try {
} catch (UnknownHostException e) {
System.exit(1);
} catch (IOException e) {
System.exit(1);
String userInput;
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
if (userInput.equals("bye"))
break;
out.close();
in.close();
stdIn.close();
echoSocket.close();
Output:
Server Output:
Client connected.
Client Output:
Hello
bye