Java Practical File Rohit
Java Practical File Rohit
Java Practical File Rohit
PRACTICAL FILE
ON
SUBMITTED BY SUBMITTED TO
Rohit mehra Mr. Deepak Rathore
(21bca35)
2
Index
S.NO. TOPIC PG.NO. TEACHER
SIGN.
1. Write a program declaring a class 4
Rectangle with data member’s length
and breadth and member functions
Input, Output and Calculate Area?
class Rectangle {
double length;
double breadth;
void inputValues() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the length of the rectangle: ");
length = scanner.nextDouble();
System.out.print("Enter the breadth of the rectangle: ");
breadth = scanner.nextDouble();
}
void outputValues() {
System.out.println("Length: " + length);
System.out.println("Breadth: " + breadth);
}
double calculateArea() {
return length * breadth;
5
}
}
rectangle.inputValues();
rectangle.outputValues();
Q.3 Write a program to demonstrate the use of static variable, static method and
static block?
Ans:-
class StaticDemo {
static int staticVariable;
int instanceVariable;
static {
System.out.println("Inside static block");
staticVariable = 10;
}
void instanceMethod() {
System.out.println("Inside instance method");
System.out.println("Static Variable: " + staticVariable);
System.out.println("Instance Variable: " + instanceVariable);
}
}
}
}
class th
{
public static void main(String[] args)
{
student s=new student(1001,"Jestin");
s.display();
}
}
11
Output:-
12
{
void info(int acc_no,String name)
{
System.out.println("You're Name is : "+name);
System.out.println("You're Account no. is : " +acc_no);
}
}
class Saving extends Account
{
void balance(int balance)
{
System.out.println("You're balance is : "+balance);
if(balance<500)
{
System.out.println("Minimum balance should be 500 or more!");
}
else
{
System.out.println("Account Created");
}
13
}}
{
System.out.println("Minimum balance should be 1000 or more!");
}
else
{
System.out.println("Account Created");
}
}
}
class Bank
{
public static void main(String[] args)
{
int acc_no,n;
// String name;
int balance;
Scanner obj=new Scanner(System.in);
System.out.print("Enter you're account number : " );
acc_no=obj.nextInt();
14
Output:-
void get()
{
System.out.println("Class A");
}
}
class B extends A
{ int
y=20;
void display()
{
System.out.println("Class B");
}
}
16
class C extends B
{ int
z=30;
void show()
{
System.out.println("Class C");
}
}
class multilevel
{
public static void main(String[] args)
{
C obj = new C();
System.out.println("Value of x is : "+obj.x);
System.out.println("Value of y is : "+obj.y);
System.out.println("Value of z is : "+obj.z);
obj.get();
obj.display();
obj.show();
}
}
17
Output:-
18
{
public static void main(String[] args)
{
child c= new child();
c.display();
}
}
Output:-
20
class B extends A
{
void display()
{
System.out.println("Child");
}
}
class overriding
{
public static void main(String[] args)
{
A obj =new A(); A
obj1= new B();
21
obj.display();
obj1.display();
}
}
Output:-
22
class Book
{
String name;
int price;
// author details
Author auther;
Book(String n, int p, Author auther)
{
this.name = n;
this.price = p;
this.auther = auther;
23
}
public static void main(String[] args) {
Author auther = new Author("John", 42, "USA");
Book b = new Book("Java for Begginer", 800, auther);
System.out.println("Book Name: "+b.name);
System.out.println("Book Price: "+b.price);
System.out.println("------------Auther Details----------");
System.out.println("Auther Name: "+b.auther.authorName);
System.out.println("Auther Age: "+b.auther.age);
System.out.println("Auther place: "+b.auther.place);
}
Output:-
24
class bca2
{
public static void voter(int age)
{
if (age<18)
{
throw new ArithmeticException("Sorry You are not eligible to vote");
}
else
{
System.out.println("Welcome you are eligible for vote");
}
}
public static void main(String[] args)
{
int age;
Scanner obj=new Scanner(System.in);
System.out.println("Enter your age: ");
age=obj.nextInt(); voter(age);
}
}
25
Output:-
26
Q.10 Write a program to use Byte stream class to read from a text file and display
the content on the output screen?
Ans:-
import java.io.*;
import java.util.*; class
fileread
{
public static void main(String[] args)
{
try
{
File obj=new File ("C:\\Users\\jesti\\OneDrive\\Desktop\\test\\goblin\\jestin.txt");
Scanner obj2=new Scanner(obj); while(obj2.hasNextLine())
{
System.out.println(obj2.nextLine());
}
obj2.close();
}
catch(Exception e)
27
e.printStackTrace();
}
}
}
Output:- Read
Display
28
Address:
Ans:-
import java.util.*;
class employee
{
String name;
int age;
String address;
void getdata()
{
Scanner obj=new Scanner(System.in);
System.out.print("Enter the name of the Employee : ");
name=obj.next();
System.out.print("Enter the age of the Employee : "); age=obj.nextInt();
System.out.print("Enter Employee's address : "); address=obj.next();
}
void showdata()
29
Output:-
30
Q.2 Write a Java program to perform basic Calculator operations. Make a menu
driven program to select operation to perform (+ - * /). Take 2 integers and
perform operation as chosen by user?
Ans:-
import java.util.*; class
cal
{
int sum,sub,mul;
float div; void
addition(int a,int b)
{
sum=a+b;
System.out.println("Sum of " +a+ " and " +b+ " is : " +sum );
}
void subtraction(int a,int b)
{
sub=a-b;
System.out.println("Sub of " +a+ " and " +b+ " is : " +sub);
}
void division(int a, int b)
{
div=a/b;
System.out.println("division of " +a+ " and " +b+ " is : " +div);
}
void multiplication(int a, int b)
{
31
mul=a*b;
System.out.println("Multiplication of " +a+ " and " +b+ " is : " +mul);
}
}
class calculator
public static void main(String[] args)
{
int a,b;
String operator;
Scanner obj=new Scanner(System.in);
System.out.println("Enter the value of a : "); a=obj.nextInt();
System.out.println("Enter the value of b : " );
b=obj.nextInt(); cal obj2=new cal();
System.out.println("Operator available :- ");
System.out.println("+");
System.out.println("-");
System.out.println("*");
System.out.println("/");
System.out.print("Enter from the given operator : ");
operator=obj.next(); if(operator.equals("+"))
{
obj2.addition(a,b);
}
else if(operator.equals("-")){ obj2.subtraction(a,b);
}
else if(operator.equals("*")){ obj2.multiplication(a,b);
32
}
else if(operator.equals(" / ")){ obj2.division(a,b);
}
}
}
Output:-
33
Q.3 Write a program declaring a Java class called Savings Account with members
``account Number`` and ``Balance``. Provide member functions as ``deposit
Amount ()`` and ``withdraw Amount ()``. If user tries to withdraw an amount
greater than their balance then throw a user-defined exception?
Ans:-
import java.util.*; import
java.lang.Throwable; class
SavingAccount
{
double accountNumber;
static double Balance;
34
double total;
double accno,bala;
System.out.println("Enter your Account number : ");
accno=obj.nextDouble();
System.out.println("Enter your Balance : "); bala=obj.nextDouble();
SavingAccount sa=new SavingAccount(accno,bala);
System.out.println("Option we have : ");
System.out.println("1. Deposit");
System.out.println("2. Withdraw");
System.out.println("Enter your option : ");
int option=obj.nextInt(); if(option==1)
{
System.out.println("Enter the amount for deposit : "); double
deposit=obj.nextDouble(); sa.depositAmount(deposit);
}
else if(option==2)
{
System.out.println("Enter the amount to be withdrawn : ");
double amount=obj.nextDouble(); withdraw(amount);
}
else
{
System.out.println("Invalid option");
}
}
}
36
Output:-
37
sam()
{
f=new JFrame(); cut=new
JMenuItem("cut"); copy=new
JMenuItem("copy"); paste=new
JMenuItem("paste"); selectAll = new
JMenuItem("selectAll");
cut.addActionListener(this);
copy.addActionListener(this);
paste.addActionListener(this);
selectAll.addActionListener(this);
mb=new JMenuBar();
file=new JMenu("file");
edit=new JMenu("edit");
help=new JMenu("help");
38
edit.add(cut);
edit.add(copy);
edit.add(paste);
edit.add(selectAll);
mb.add(file);
mb.add(edit);
mb.add(help); ta=new
JTextArea();
ta.setBounds(5,5,360,320);
f.add(mb);
f.add(ta);
f.setJMenuBar(mb);
f.setLayout(null);
f.setSize(500,500);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==cut)
ta.cut();
if(e.getSource()==paste)
ta.paste();
if(e.getSource()==copy) ta.copy();
if(e.getSource()==selectAll)
ta.selectAll();
}
39
Border()
{
jframe.add(btn5, BorderLayout.CENTER);
jframe.setSize(300,300); jframe.setVisible(true);
}
Output:-