Jprpracc (1) (AutoRecovered)
Jprpracc (1) (AutoRecovered)
3. Create three threads and run these threads according to set priority
public class ThreadPriority {
public static void main(String[] args) {
Thread thread1 = new Thread(() -> System.out.println("Thread 1"), "Thread 1");
Thread thread2 = new Thread(() -> System.out.println("Thread 2"), "Thread 2");
Thread thread3 = new Thread(() -> System.out.println("Thread 3"), "Thread 3");
thread1.setPriority(Thread.MIN_PRIORITY);
thread2.setPriority(Thread.NORM_PRIORITY);
thread3.setPriority(Thread.MAX_PRIORITY);
thread1.start();
thread2.start();
thread3.start();
}
}
System.out.println(result);
}
5. Write any program to check switch-case statement using character data type.
public class SwitchCaseChar {
public static void main(String[] args) {
char grade = 'B';
switch (grade) {
case 'A':
System.out.println("Excellent");
break;
case 'B':
System.out.println("Good");
break;
case 'C':
System.out.println("Average");
break;
default:
System.out.println("Invalid grade");
}
}
}
int rows = 5;
System.out.print(" ");
System.out.print("* ");
System.out.println();
}
}
9 . The program calculate the sum of two Numbers inputted as command line argument
public class SumCommandLine {
if (args.length >= 2) {
} else {
}// here we added input with java commandline statement with space
Write a program to implicitly typecast lower range data type to larger storage size data type.
14. Write a program to implement different types of constructors to perform addition of complex
numbers.
class Complex {
double real, imaginary;
Complex() {
real = 0;
imaginary = 0;
}
Complex(double r, double i) {
real = r;
imaginary = i;
}
Complex add(Complex c) {
Complex temp = new Complex();
temp.real = real + c.real;
temp.imaginary = imaginary + c.imaginary;
return temp;
}
void display() {
System.out.println("Sum = " + real + " + " + imaginary + "i");
}
15. Write a program to show the use of all methods of String class.
public class StringMethodsExample {
public static void main(String[] args) {
String str = "Hello, World!";
// 1. append() method
stringBuffer.append("Hello");
// 2. insert() method
stringBuffer.insert(5, ", World");
// 3. delete() method
stringBuffer.delete(5, 11);
// 4. deleteCharAt() method
stringBuffer.deleteCharAt(5);
// 5. reverse() method
stringBuffer.reverse();
// 6. replace() method
stringBuffer.replace(0, 5, "Hola");
// 7. capacity() method
int capacity = stringBuffer.capacity();
// 8. charAt() method
char charAt = stringBuffer.charAt(0);
// 9. indexOf() method
int indexOf = stringBuffer.indexOf("la");
import java.util.*;
public class Comeg
{
public static void main(String args[])
{
Vector v1 = new Vector();
v1.addElement("Java is Robust");
System.out.println("After the AddElement method
Implementation="+v1);
v1.insertElementAt("Kotlin",0);
System.out.println("After the InsertElement
method Implementation = "+v1);
v1.removeElementAt(0);
System.out.println("After Removing the Element
="+v1);
}
}
23. Write a program to convert String value into Integer Wrapper class object
public class StringToInteger {
// isDigit() method
System.out.println("Is 'A' a digit? " + Character.isDigit(ch));
// isLetter() method
System.out.println("Is 'A' a letter? " + Character.isLetter(ch));
// toLowerCase() method
System.out.println("Lowercase of 'A': " + Character.toLowerCase(ch));
// toUpperCase() method
System.out.println("Uppercase of 'a': " + Character.toUpperCase('a'));
// isUpperCase() method
System.out.println("Is 'A' uppercase? " + Character.isUpperCase(ch));
// isLowerCase() method
System.out.println("Is 'a' lowercase? " + Character.isLowerCase('a'));
}
}
21. Write a program to convert Integer object value into primitive datatype byte, short and double
value.
public class IntegerToPrimitive {
public static void main(String[] args) {
Integer intValue = 100;
byte byteValue = intValue.byteValue();
short shortValue = intValue.shortValue();
double doubleValue = intValue.doubleValue();
System.out.println("Byte value: " + byteValue);
System.out.println("Short value: " + shortValue);
System.out.println("Double value: " + doubleValue);
}
}
22. Develop a program to display the rate of interest of banks by method overriding method.
class Bank {
double getRateOfInterest() {
return 0;
}
}
23. Develop a program to extend 'dog' from 'animal' to override 'move()' method using super
keyword.
class Animal {
void move() {
System.out.println("Animals can move");
}
}
24. Develop a program to calculate the room area and volume to illustrate the concept of single
inheritance (Assume suitable data wherever necessary)
class Room {
double length;
double width;
double height;
double calculateArea() {
return length * width;
}
double calculateVolume() {
return length * width * height;
}
}
25. Develop a program to find area of rectangle and circle using interfaces.
interface Shape {
double area();
}
@Override
public double area() {
return length * width;
}
}
Circle(double radius) {
this.radius = radius;
}
@Override
public double area() {
return Math.PI * radius * radius;
}
}
26. Define a package named myInstitute include class named as department with one method to
display the staff of that department. Develop a program to import this package in a java application
and call the method defined in the package.
package my_institute1;
public class Department
{
public void display()
{
System.out.println("Staff are :");
System.out.println("Yash , harsh , ishan , amey ,
eshan");
}
}
import my_institute1.Department;
class Main123
{
public static void main(String args[])
{
Department dep = new Department();
dep.display();
}
}
27. Develop a program which consists of the package named let_me_calculate4 with class named
calculator and a method named add to add two integer numbers. Import let_me_calculate package
in another program( class named Demo) to add two numbers.
package let_me_cal;
class Main123
{
public static void main(String args[])
{
calculator cal = new calculator();
cal.add();
}
}
28. Write a program to create two thread one to print odd number only and other to print even
numbers.
30. Develop a program to accept a password from the user and throw "Authentication Failure"
exception if the password is incorrect.
import java.util.Scanner;
class PasswordException extends Exception {
PasswordException (String msg) {
super(msg);
}
}
class PassCheck {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
try {
System.out.println("Enter Password: ");
if (scanner.nextLine().equals("abc123")) {
System.out.println("Authenticated ");
} else {
throw new PasswordException("Authentication failure");
}
} catch (PasswordException e) {
System.out.println(e);
}
}
}
31. Define an Exception called "NotMatchException" that is thrown when a string is not equal to
"India". Write a program that uses this exception.
class NotMatchException extends Exception {
NotMatchException(String message) {
super(message);
}
}
import java.applet.Applet;
import java.awt.*;
34. Write a program to create animated shape using graphics and applets. You may use following
shapes: Polygons with fill polygon method
}
}
/* <applet code="PolygonApplet.class" width=500 height=500>
</applet> */
import java.awt.*;
setBackground(Color.white);
g.setColor(Color.black);
p.addPoint(150, 135);
p.addPoint(130, 175);
p.addPoint(170, 175);
g.drawPolygon(p);
</applet> */
37. Develop a program to draw any 2 of the following shapes: a. Cone b. cone c. cylinder
import java.applet.Applet;
import java.awt.*;
import java.io.*;
class FileCopy {
public static void main(String args[]) {
int b;
FileReader infile;
FileWriter outfile;
try {
infile = new FileReader("Copy.txt");
outfile = new FileWriter("Copied.txt");
while ((b = infile.read())>0) {
outfile.write(b);
}
// Close the file streams
infile.close();
outfile.close();
System.out.println("File Copied Successfully");
} catch (IOException e) {
System.out.println("An Error occurred: " +
e.getMessage());
}
}
}
import java.io.*;
try {
System.out.println("Enter the text to be
saved in the file:");
out.write(b, 0, bytes);
System.out.println("Data written to file
successfully.");
} catch (IOException e) {
System.out.println("An error occurred: " +
e.getMessage());
} finally {
try {
// Close the output file stream
if (out != null) {
out.close();
}
} catch (IOException e) {
System.out.println("Error closing file: " +
e.getMessage());
}
}
}
}