Manavjava
Manavjava
ICT-312P
Submitted in partial fulfilment of the requirements for the award of the degree of
B.Tech.
in
Information Technology
AIM:
Program to print the reverse of the numbers, the numbers are taken as an input from the user.
CODE:-
import java.util.Scanner;
int reversedNumber = 0;
while (number != 0) {
int digit = number % 10;
reversedNumber = reversedNumber * 10 + digit;
number /= 10;
}
scanner.close();
}
}
OUTPUT :-
PRACTICAL – 2
AIM:
Program to maintain bank account. Extend Bank account details to current and saving
account.
CODE:-
class BankAccount {
private String accountNumber;
private double balance;
savingsAccount.deposit(500);
savingsAccount.addInterest();
savingsAccount.withdraw(200);
currentAccount.deposit(1000);
currentAccount.withdraw(2500);
OUTPUT :-
PRACTICAL – 3
AIM:
Program to maintain bank account using packages.
CODE:-
interface Account {
void deposit(double amount);
void withdraw(double amount);
double getBalance();
}
interface Interest {
void addInterest();
}
savingsAccount.deposit(500);
savingsAccount.addInterest();
savingsAccount.withdraw(200);
currentAccount.deposit(1000);
currentAccount.withdraw(2500);
AIM:-
Program to run the main thread and perform operations on it. Change the name and priority
of the main thread
CODE:-
class CurrentThreadDemo {
public static void main(String args[]) {
Thread t = Thread.currentThread();
System.out.println("Current thread: " + t);
// change the name of the thread
t.setName("My Thread");
System.out.println("After name change: " + t);
try {
for(int n = 5; n > 0; n--) {
System.out.println(n);
Thread.sleep(1000);
}
}catch (InterruptedException e) {
System.out.println("Main thread interrupted");
}
}
}
OUTPUT :-
PRACTICAL – 5
AIM:
Program to illustrate the working of the child threads in concurrence with the main thread.
CODE:-
class NewThread implements Runnable {
Thread t;
NewThread() {
t = new Thread(this, "Demo Thread");
System.out.println("Child thread: " + t);
t.start();
}
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}
} catch (InterruptedException e) {
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}
class ThreadDemo {
public static void main(String args[ ] ) {
new NewThread(); // create a new thread
try {
for(int i = 5; i > 0; i--) {
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}
OUTPUT :-
PRACTICAL – 6
AIM:
Program to illustrate that the high priority thread occupies more CPU cycles as compared to
a low priority thread.
CODE:-
class Clicker implements Runnable {
long click = 0;
Thread t;
private volatile boolean running = true;
public Clicker (int p) {
t = new Thread(this);
t.setPriority(p);
}
public void run() {
while(running) {
click++;
}
}
public void stop() {
running = false;
}
public void start() {
t.start();
}
}
class Priority {
public static void main(String args[]) {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
Clicker hi = new Clicker(Thread.NORM_PRIORITY + 2);
Clicker lo = new Clicker(Thread.NORM_PRIORITY - 2);
hi.start();
lo.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("main thread interrupted");
}
lo.stop();
hi.stop();
try {
hi.t.join();
lo.t.join();
} catch (InterruptedException e) {
System.out.println("interrupted exception catched in main");
}
System.out.println("low-priority thread : " + lo.click);
System.out.println("hi-priority thread : " + hi.click);
}
}
OUTPUT :-
PRACTICAL – 7
AIM:
Program to print the table of 5 and 7 using threads and in synchronized manner.
CODE:-
class MyThread extends Thread {
int num;
MyThread(int child_num){
num=child_num;
}
public void run() {
synchronized(this){
for (int i = 1; i <= 10; i++) {
System.out.println(num +"*"+ i + "=" + num*i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
}
}
class ChildThreadExample {
public static void main(String[] args) {
MyThread childThread1 = new MyThread(5);
MyThread childThread2 = new MyThread(7);
childThread1.start();
childThread2.start();
try{
childThread1.join();
childThread2.join();
}
catch(InterruptedException e){
System.out.println(e);
}
System.out.println("Main thread ended");
}
}
OUTPUT :-
PRACTICAL – 8
AIM:
Program to take a string array as "100", "10.2", "5.hello", "100hello" and check whether it
contains valid integer or double using exception handling.
CODE:-
class ValidNumber{
public static void main(String[] args) {
String[] array = {"100", "10.2", "5.hello", "100hello"};
for (int i=0;i<4;i++) {
String str=array[i];
try{
int intValue = Integer.parseInt(str);
System.out.println("\"" + str + "\" is a valid integer");
}
catch (NumberFormatException e1){
try{
double doubleValue = Double.parseDouble(str);
System.out.println("\"" + str + "\" is a valid double");
}
catch (NumberFormatException e2){
System.out.println("\"" + str + "\" is a String");
}
}
}
}
}
OUTPUT :-
PRACTICAL – 9
AIM:
Program to create a user defined exception ‘MyException’ and throw this exception when
the age entered is less than 18.
CODE:-
import java.util.Scanner;
class MyException extends Exception {
public MyException(String message) {
super(message);
}
}
class AgeValidator {
public void checkAge(int age) throws MyException {
if (age < 18) {
throw new MyException("Age cannot be less than 18.");
} else {
System.out.println("Age is valid.");
}
}
}
class UserDefinedExceptionExample {
public static void main(String[] args) {
AgeValidator v = new AgeValidator();
Scanner sc=new Scanner(System.in);
System.out.print("Enter your age: ");
int age=sc.nextInt();
try{
v.checkAge(age);
}
catch (MyException e){
System.out.println("Exception: " + e.getMessage());
}
}
}
OUTPUT :-