Java Practical

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 25

BHAI PARMANAND

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:

Aim: Write a program to find the number of arguments provided at


command line.
Code:
import java.util.Scanner;
public class Ques_4 {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter String");
args[]=sc.nextLine();
System.out.println(args);

}
}
Output:

Aim: Write a program to find GCD of a number input by the user.


Code:
import java.util.Scanner;
public class Ques_5 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int a, b, GCD = 1;
System.out.println("Enter two positive integers");
a = s.nextInt();
b = s.nextInt();
for (int i = 1; i < a && i < b; i++) {
if (a % i == 0 && b % i == 0)
{
GCD = i;
}
}
System.out.println("GCD of " + a + " and " + b + " is " + GCD);
}
}
Output:

Aim: Write a program to create a simple class to find out the


area and perimeter of rectangle.
Code:
class Rectangle {
int length, breadth;
int area() {
return length * breadth;
}
int perimeter() {
return 2 * (length + breadth);
}
}
public class Ques_6 {
public static void main(String[] args) {
Rectangle obj = new Rectangle();
obj.length = 5;
obj.breadth = 7;
System.out.println("Area of Rectangle= " + obj.area());
System.out.println("Perimeter of Rectangle =" + obj.perimeter());
}
}Output:
Aim: Write a program to find sum of elements of a one
dimensional array entered dynamically by the user.
Code:
import java.util.Scanner;
public class Ques_7 {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int n,sum=0;
System.out.println("Enter Value of n");
n=s.nextInt();
int arr[]=new int[n];
for (int i = 0; i < n; i++) {
System.out.println("Enter number " + (i+1));
arr[i]=s.nextInt();
sum+=arr[i];
}
System.out.println("sum of "+ n +" Numbers = "+sum);
}
}
Output:

Aim: Write a program to demonstrate type casting.


Code:
public class Ques_9 {

public static void main(String[] args) {


int a=5;
double b=9.4d;
System.out.println("Before Typecast vale of a");
System.out.println(a);
System.out.println("After Typecast vale of a");
System.out.println((double)a);
System.out.println("Before Typecast vale of b");
System.out.println(b);
System.out.println("After Typecast vale of b");
System.out.println((int)b);
}
}
Output:

Aim: WAP to create the Person class. Create some objects


of this class (by taking information from the user). Inherit
the class Person to create two classes Teacher and Student
class. Maintain the respective information in the classes
and create, display and delete objects of these two classes
(Use Runtime Polymorphism).
Code:
import java.util.Scanner;
class Person {
Scanner s= new Scanner(System.in);
String Name;
String Address;
Person() {
System.out.printf("Enter Name :");
this.Name = s.nextLine();
System.out.printf("Enter Address :");
this.Address = s.nextLine();
}
void display() {
System.out.println("Name - " + Name);
System.out.println("Address - " + Address);
System.out.println();
}
}
class Teacher extends Person {
String Department;
long Phone;
Teacher() {
System.out.printf("Enter Department of Teacher :");
this.Department = s.nextLine();
System.out.printf("Enter Phone_No of Teacher :");
this.Phone = s.nextLong();
System.out.println();
}
void display() {
System.out.println("Name of Teacher - " + Name);
System.out.println("Address of Teacher - " + Address);
System.out.println("Department of Teacher - " + Department);
System.out.println("Phone of Teacher - " + Phone);
System.out.println("");
}
}
class Student extends Person {
int Roll_no;
String Program;
Student() {
System.out.printf("Enter Program of Student :");
this.Program = s.nextLine();
System.out.printf("Enter Roll_No of Student :");
this.Roll_no = s.nextInt();
System.out.println();
}
void display() {
System.out.println("Name of Student - " + Name);
System.out.println("Address of Student - " + Address);
System.out.println("Roll_no of Student - " + Roll_no);
System.out.println("Program of Student - " + Program);
System.out.println("");
}
}
public class Ques_10 {
public static void main(String[] args) {
// Scanner s = new Scanner(System.in);
Teacher obj1=new Teacher();
Student obj2=new Student();
Person r;
r=obj1;
obj1.display();
r=obj2;
obj2.display();
}
}
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:

You might also like