Professional Documents
Culture Documents
Java Practical
Java Practical
Java Practical
INSTITUTE OF BUSINESS
STUDIES
SESSION: 2020-2023
Practica
l File
Java
Progra
mming
Submitted Submitted
to: By:
Dr. Reena Anuraag
Chandra
Roll no. :
01011402020
Aim: Write a program to print “Hello World” on the
screen.
Code:
class Ques_1{
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Output:
Aim: Write a program to calculate simple interest and
input should be by the user.
Code:
Import java.util.Scanner;
public class Ques_2 {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
double Principal,Rate_of_Interest,Simple_Interest;
int Time;
System.out.printf("Enter Principal Amount ");
Principal=s.nextDouble();
System.out.printf("Enter Rate of Interest ");
Rate_of_Interest=s.nextDouble();
System.out.printf("Enter Time_Period ");
Time=s.nextInt();
Simple_Interest=(Principal*Rate_of_Interest*Time)/(100);
System.out.printf("Simple_Interest = " + Simple_Interest);
}
}
Output:
Aim: Write a program to find the average and sum of n
numbers using command line argument.
Code:
import java.util.Scanner;
public class Ques_3 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n, sum = 0, a;
double avg;
System.out.println("Enter Value of n");
n = s.nextInt();
for (int i = 0; i < n; i++) {
System.out.println("Enter number " + (i + 1));
a = s.nextInt();
sum += a;
}
avg = ((double)sum / (double)n);
System.out.println("sum of " + n + " Numbers = " + sum);
System.out.println("Average of " + n + " Numbers= " + avg);
}
}
Output:
}
}
Output:
Aim: Assume that a bank maintains two kinds account for its
customers. one called savings account and the other current
account. The savings account provides compound interest and
withdrawal facilities but no cheque book facility. The current
account provides cheque book facility but no interest. Current
account
holders should also maintain a minimum balance and if the
balance falls below this level, a service charge is imposed.
Create a class Account that stores customer name, account
number and type of account From this derive the classes Curr-
acct and Sav-acct to make them more specific to their
requirements. Include the necessary methods inorder to achieve
the following tasks:
a) Accept deposit from a customer and update the balance.
b) Display the balance.
c) Compute and deposit interest.
(d) Permit withdrawal and update the balance.
(c) Check for the minimum balance, impose penalty, if
necessary, and update the balance .
Use constructors to initialize the class members.
Code:
import java.util.Scanner;
class Account {
public static int min = 500;
static int sInterest = 5;
static int cInterest = 5;
String name;
long Account_num;
float Amount;
boolean cheque;
Scanner sc = new Scanner(System.in);
void get_info() {
System.out.println("Enter Name:");
name = sc.nextLine();
System.out.println("Enter Account Number:");
Account_num = sc.nextLong();
System.out.println("Enter opening Ammount must>500:");
Amount = sc.nextFloat();
if (Amount < 500) {
System.out.println("Enter opening Ammount must
Greater 500:");
}
}
void show_info() {
System.out.println("");
System.out.println("Name - " + name);
System.out.println("Account_Number - " + Account_num);
System.out.println("Amount - " + Amount);
System.out.println();
}
void chequeFacility()
{
if(cheque==true)
{System.out.println("Cheque Facility Availabel.");
}
else System.out.println("Facility No Available.");
}
}
class Saving extends Account {
float withdraw;
float interest;
float deposit;
{cheque=false;}
void deposit() {
System.out.println("Enter Amount to be Deposited");
deposit = sc.nextFloat();
Amount += deposit;
System.out.println("Your Balance after Deposit is " +
Amount);
}
void withdraw() {
System.out.println("Enter Amount to Withdraw");
withdraw = sc.nextFloat();
if (withdraw < Amount) {
Amount -= withdraw;
System.out.println("Your Balance After Withdrawal is "
+ Amount);
} else {
System.out.println("Insufficient Balance!");
}
}
void compute_interest() {
interest = (Amount * 2) / 100;
Amount += interest;
System.out.println("Total Balance with Interest is = " +
Amount);
}
}
class Current extends Account {
float withdraw;
float deposit;
float penalty;
{cheque=true;}
void deposit() {
System.out.printf("Enter Amount to be Deposited = ");
deposit = sc.nextFloat();
Amount += deposit;
System.out.println("Your Balance after Deposit is = " +
Amount);
}
void withdraw() {
System.out.printf("Enter Amount to Withdraw = ");
withdraw = sc.nextFloat();
if (withdraw < Amount) {
Amount -= withdraw;
if (Amount < min) {
penalty();
}
else
{
System.out.println("After Withdrawl Your Balance is
= " + Amount);
}
}
}
void penalty() {
if (Amount < min) {
if(cInterest == 1)
{
System.out.println("Your Balance is Less than 500 After
withdrwal = "+ Amount);
System.out.println("You have to pay Penalty of 150
Ruppee");
Amount -= 15011111;
System.out.println("Your Total Balance After Paying
Penalty = " + Amount);
cInterest = 5;
}
else {
System.out.println("Time Remaining.");
cInterest--;
}
}
}
}
class Ques_11 {
public static void main(String[] args) {
String ch;
int count = 0;
Scanner sc = new Scanner(System.in);
Saving sv = new Saving();
Current cu = new Current();
System.out.println("...............Welcome to Bank of
Squid..............");
System.out.println(".......Press S for Saving and C for
Current.......... ");
ch = sc.nextLine();
if (ch.equalsIgnoreCase("s")) {
sv.get_info();
while (count != 5) {
System.out.println();
System.out.println();
System.out.println("1.Display\n2.Deposit\
n3.Withdraw\n4.Interest\n5.Cheque Faciity\n6.Exit....");
System.out.printf("Enter Choice -");
int choice = sc.nextInt();
switch (choice) {
case 1:
sv.show_info();
break;
case 2:
sv.deposit();
break;
case 3:
sv.withdraw();
break;
case 4:
sv.compute_interest();
break;
case 5:
sv.chequeFacility();
break;
case 6:
System.exit(0);
break;
default:
System.out.println("Invalid Choice");
}
}
} else if (ch.equalsIgnoreCase("c")) {
cu.get_info();
cu.penalty();
while (count != 4) {
System.out.println();
System.out.println("\n1.Display\n2.Deposit\
n3.Withdraw\n4.Cheque Facility\n5.Exit");
System.out.printf("Enter Choice -");
int choice = sc.nextInt();
switch (choice) {
case 1:
cu.show_info();
break;
case 2:
cu.deposit();
break;
case 3:
cu.withdraw();
break;
case 5:
System.exit(0);
break;
case 4:
cu.chequeFacility();
break;
default:
System.out.println("Invalid Choice");
}
}
} else {
System.out.println("Wrong choice!");
}
}
}
Output:
Aim: Write a program “DivideByZero” that takes two
numbers a and b as input, computes a/b, and invokes
Arithmetic Exception to generate a message when the
denominator is zero.
Code:
import java.util.Scanner;
public class Ques_12 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
double a,b;
double result;
System.out.println("Enter value of a");
a=sc.nextDouble();
System.out.println("Enter value of b");
b=sc.nextDouble();
try{
result=a/b;
System.out.println("Result ="+result);
}
catch(ArithmeticException e){
System.out.println("Denominator is zero " +e);
}
}
}
Output:
Aim: Write a program to show the use of nested try
statements that emphasizes the sequence of checking for
catch handler statements.
Code:
public class Ques_13 {
public static void main(String[] args) {
try{
try{
try{
int arr[]= {1,2,3,4};
System.out.println(arr[10]);
}catch(ArithmeticException e){
System.out.print("Arithmetic Exception");
}
}
catch(NullPointerException e){
System.out.print("NullPointer Exception");
}
}
catch(NumberFormatException e3){
System.out.print("NumberFormat Exception");
}
catch(ArrayIndexOutOfBoundsException e4){
System.out.print("ArrayIndexOutOfBounds Exception");
}
catch(Exception e5){
System.out.print("Exception");
System.out.println(" handled in main try-block");
}
}
Output:
Aim: Write a program to create your own exception types to
handle situation specific to your application (Hint: Define a
subclass of Exception which itself is a subclass of
Throwable).
Code:
class MyException extends Exception
{
private int ex;
MyException(int a)
{
ex = a;
}
public String toString()
{
return "MyException[" + ex +"] is less than zero";
}
}
class Ques_14
{
static void sum(int a,int b) throws MyException
{
if(a<0)
{
throw new MyException(a); //calling constructor of user-defined exception
class
}
else
{
System.out.println(a+b);
}
}
public static void main(String[] args)
{
try
{
sum(-10, 10);
}
catch(MyException me)
{
System.out.println(me); //it calls the toString() method of user-defined
Exception
}
}
}
Output: