Prog1-36 Java
Prog1-36 Java
PROGRAM NO – 26
AIM : WAP to achieve multithreading using run(), yield(), sleep() and start()
in JAVA.
CODE :
class MyRunnable implements Runnable {
private String t1;
public MyRunnable(String name) {
this.t1 = name;
}
@Override
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(t1 + " - Count: " + i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println(t1 + " interrupted.");
}
Thread.yield();
}
}
}
thread1.start();
thread2.start();
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
System.out.println("Main thread interrupted.");
}
OUTPUT:
PROGRAM NO – 27
CODE :
class BookTicket {
int total_seats = 10;
synchronized void bookTicket(int seats) {
if (total_seats >= seats) {
System.out.println("Seat booked successfully");
total_seats = total_seats - seats;
System.out.println("Seats left: " + total_seats);
}
else {
System.out.println("Not enough seats available");
}
}
}
public class MovieTicket extends Thread {
static BookTicket b;
int seats;
public MovieTicket(int seats) {
this.seats = seats;
}
public void run() {
b.bookTicket(seats);
}
public static void main(String[] args) {
b = new BookTicket();
MovieTicket t1 = new MovieTicket(7);
t1.start();
MovieTicket t2 = new MovieTicket(10);
t2.start();
}
}
OUTPUT :
PROGRAM NO – 28
CODE :
import java.util.LinkedList;
public class Threadexample {
public static void main(String[] args)
throws InterruptedException{
final PC pc = new PC();
t1.start();
t2.start();
t1.join();
t2.join();
}
int value = 0;
while (true) {
synchronized (this){
while (list.size() == capacity)
wait();
PROGRAM NO. - 23
AIM: To implement single try-catch block in JAVA.
CODE:
class Error {
public static void main(String[] args) {
int a = 10;
int b = 5;
int c = 5;
int x,y;
try{
x = a/(b-c);
}
catch(ArithmeticException e){
System.out.println("Division by zero");
}
y = a/(b+c);
System.out.println(+y);
}
}
OUTPUT:
PROGRAM NO. - 24
AIM: To implement multiple catch blocks in JAVA.
CODE:
class Error {
public static void main(String[] args) {
int a[] = {10,5};
int b = 5;
int c;
try{
c = a[2]/a[1]-b;
int d = a[1]/a[0]-b;
}
catch(ArithmeticException e){
System.out.println("Arithmetic Exception");
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("Array Index Out Of Bound");
}
finally{
System.out.println("Exception Caught");
}
c = a[1]/(a[0]+b);
System.out.println(+c);
}
}
OUTPUT:
PROGRAM NO. - 25
AIM: To implement throwing your own exception in JAVA.
CODE:
// Define the custom exception class
class MyException extends Exception {
MyException(String message) {
super(message); // Call the base class constructor with the message
}
}
OUTPUT:
PROGRAM NO. – 17
AIM: W.A.P to use String and StringBuffer Classes.
CODE:
public class StringAndStringBufferClass {
public static void main(String[] args) {
StringBuffer sb=new StringBuffer();
System.out.println(sb.capacity());//default 16
sb.append("Hello");
System.out.println(sb.capacity());//now 16
sb.append("java is my favourite language");
System.out.println(sb.capacity());//now (16*2)+2=34 i.e (oldcapacity*2)+2
sb.ensureCapacity(10);//now no change
System.out.println(sb.capacity());//now 34
sb.ensureCapacity(50);//now (34*2)+2
System.out.println(sb.capacity());//now 70
}
}
OUTPUT:
PROGRAM NO. – 18
AIM: W.A.P. to use Wrapper Classes in JAVA.
CODE:
public class WrapperClass {
public static void main(String[] args) {
byte b=10;
short s=20;
int i=30;
long l=40;
float f=50.0F;
double d=60.0D;
char c='a';
boolean b2=true;
//Printing objects
System.out.println("---Printing object values---");
System.out.println("Byte object: "+byteobj);
System.out.println("Short object: "+shortobj);
System.out.println("Integer object: "+intobj);
System.out.println("Long object: "+longobj);
System.out.println("Float object: "+floatobj);
System.out.println("Double object: "+doubleobj);
System.out.println("Character object: "+charobj);
System.out.println("Boolean object: "+boolobj);
//Printing primitives
OUTPUT:
PROGRAM NO. – 19
AIM: W.A.P. to perform operations on Vector Class.
CODE:
import java.util.Vector;
public class VectorOperations {
public static void main(String[] args) {
//Create an empty vector with initial capacity 4
Vector<String> vec = new Vector<String>(4);
OUTPUT:
PROGRAM NO. – 20
AIM: JAVA program to initialize the values from one object to another
object.
CODE:
public class Student {
int id;
String name;
void display(){
System.out.println(id + " " + name);
}
s1.display();
s2.display();
}
}
OUTPUT:
PROGRAM NO. – 21
AIM: W.A.P. to achieve Multiple Inheritance in JAVA.
CODE:
interface Character {
void attack();
}
interface Weapon {
void use();
}
@Override
public void use() {
System.out.println("Warrior uses a sword.");
}
}
@Override
public void use() {
System.out.println("Mage uses a wand.");
}
}
OUTPUT:
PROGRAM NO. – 22
AIM: W.A.P. to achieve Multi-level Inheritance.
CODE:
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class BabyDog extends Dog{
void weep(){System.out.println("weeping...");}
}
public class TestInheritance2{
public static void main(String args[]){
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}
}
OUTPUT:
PROGRAM NO. - 11
AIM: To calculate income tax in JAVA.
CODE:
import java.util.Scanner;
public class TaxCalculation {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your income: ");
int n = sc.nextInt();
int tax;
if(n<=180000){
System.out.println("No Tax");
}
else if(n>180000 && n<=500000){
tax = (int) ((n - 180000)*0.10);
System.out.print("Tax = "+tax);
}
else if(n>500000 && n<=800000){
tax = (int) ((n - 500000)*0.20);
System.out.print("Tax = "+tax);
}
else{
tax = (int) ((n - 800000)*0.30 + (n - 500000)*0.20 + (n - 180000)*0.10);
System.out.print("Tax = "+tax);
}
}
}
OUTPUT:
PROGRAM NO. – 13
AIM: W.A.P to calculate area of different shapes using method overloading.
CODE:
public class Area {
float l,b,a;
static int area(int l,int b){
return (l*b);
}
static int area(int a){
return(a*a);
}
double area(float l, float b){
return(0.5*l*b);
}
double area(float a){
return(0.433*a*a);
}
public static void main(String[] args) {
Area obj = new Area();
System.out.println("area = "+Area.area(2,3));
}
}
OUTPUT:
PROGRAM NO. – 14
AIM: W.A.P. to sort a list of numbers.
CODE:
public class Sorting {
public static void main(String[] args)
{
OUTPUT:
PROGRAM NO. – 15
AIM: W.A.P. to multiply 3×3 matrix.
CODE:
public class MatrixMultiplication {
public static void main(String args[]){
int a[][]={{1,2,3},{4,5,6},{7,8,9}};
int b[][]={{9,8,7},{6,5,4},{3,2,1}};
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=0;
for(int k=0;k<3;k++){
c[i][j]+=a[i][k]*b[k][j];
}
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
}
OUTPUT:
PROGRAM NO. – 16
AIM: W.A.P. to check whether a given string is palindrome or not.
CODE:
import java.util.Scanner;
public class Palindrome {
public static void main(String args[]){
int r,sum=0,temp;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a no.: ");
int n = sc.nextInt();
temp=n;
while(n>0){
r=n%10; //getting remainder
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
System.out.println("Palindrome number ");
else
System.out.println("Not palindrome");
}
}
OUTPUT:
PROGRAM NO. - 9
AIM: To print Fibonacci series in JAVA.
CODE:
import java.util.Scanner;
public class Fibonacci {
int a = 0;
int b = 1;
int i = 1;
Scanner sc = new Scanner(System.in);
System.out.print("Enter no. of digits: ");
int n = sc.nextInt();
System.out.println(a);
System.out.println(b);
while(i<=n){
int nextTerm = a+b;
System.out.println(nextTerm);
a = b;
b = nextTerm;
i++;
}
}
}
OUTPUT:
PROGRAM NO. - 10
AIM: To display multiplication table of a number in JAVA.
CODE:
import java.util.Scanner;
public class Table {
OUTPUT:
PROGRAM NO. - 11
AIM: To calculate income tax in JAVA.
CODE:
import java.util.Scanner;
public class TaxCalculation {
OUTPUT:
PROGRAM NO. - 12
AIM: To convert a given number of eggs into the number of gross plus the
number of dozens plus the number of left over eggs in JAVA.
CODE:
import java.util.Scanner;
public class GrossAndDozens {
int eggs;
int gross;
int aboveGross;
int dozens;
int extras;
OUTPUT:
PROGRAM NO. - 1
AIM: To print “hello” message in JAVA.
CODE:
class Hello{
public static void main(String[] args){
System.out.println("Hello Java");
}
}
OUTPUT:
PROGRAM NO. - 2
AIM: To swap value of two variables without using third variable in JAVA.
CODE:
public class Swap {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println("Before swapping");
System.out.println("a = " + a);
System.out.println("b = " + b);
a = a + b;
b = a - b;
a = a - b;
System.out.println("\nAfter swapping");
System.out.println("a = " + a);
System.out.println("b = " + b);
}
}
OUTPUT:
PROGRAM NO. – 3
AIM: To swap value of two variables by using third variable in JAVA.
CODE:
public class Swap2 {
public static void main(String[] args) {
int a = 10;
int b = 20;
int c;
System.out.println("Before swapping");
System.out.println("a = " + a);
System.out.println("b = " + b);
c = a;
a = b;
b = c;
System.out.println("\nAfter swapping");
System.out.println("a = " + a);
System.out.println("b = " + b);
}
}
OUTPUT:
PROGRAM NO. - 4
AIM: To check whether a number is even or odd in JAVA.
CODE:
import java.util.Scanner;
public class EvenOdd {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a no.: ");
int a = sc.nextInt();
if (a%2==0)
System.out.println("Even");
else
System.out.println("Odd");
}
}
OUTPUT:
PROGRAM NO. - 5
AIM: To check whether a year is leap year or not in JAVA.
CODE:
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a year: ");
int a = sc.nextInt();
if (a%4==0)
System.out.println("Leap Year");
else
System.out.println("Non-Leap Year");
}
}
OUTPUT:
PROGRAM NO. - 6
AIM: To give grading to students according to the percentage in JAVA.
CODE:
public class Grading {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter percentage: ");
int a = sc.nextInt();
if (a>=60)
System.out.println("1st Division");
else if (a>=50)
System.out.println("2nd Division");
else if (a>=40)
System.out.println("3rd Division");
else
System.out.println("Fail");
}
}
OUTPUT:
PROGRAM NO. - 7
AIM: To implement switch statement in JAVA.
CODE:
import java.util.Scanner;
public class DaysOfWeek {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a no.: ");
int d = sc.nextInt();
switch(d){
case 1:
System.out.println("Sunday");
break;
case 2:
System.out.println("Monday");
break;
case 3:
System.out.println("Tuesday");
break;
case 4:
System.out.println("Wednesday");
break;
case 5:
System.out.println("Thursday");
break;
case 6:
System.out.println("Friday");
break;
case 7:
System.out.println("Saturday");
break;
default:
System.out.println("Invalid Day");
}
}
}
OUTPUT:
PROGRAM NO. - 8
AIM: To find factorial of a number in JAVA.
CODE:
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a no.: ");
float a = sc.nextInt();
float fact = 1;
for (int i = 1; i <= a; i++) {
fact = fact*i;
}
System.out.println(fact);
}
}
OUTPUT: