0% found this document useful (0 votes)
45 views33 pages

JAVA PRACTICLE FILE Yatin

The document contains a program written in Java to multiply two 3x3 matrices where the data is entered by the user. It defines a Matrix class with methods to get the values, show the matrices, and multiply them. In main, it creates Matrix objects, gets the values, shows the original matrices, and calls the multiplication method to output the result.

Uploaded by

Narender Rawat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views33 pages

JAVA PRACTICLE FILE Yatin

The document contains a program written in Java to multiply two 3x3 matrices where the data is entered by the user. It defines a Matrix class with methods to get the values, show the matrices, and multiply them. In main, it creates Matrix objects, gets the values, shows the original matrices, and calls the multiplication method to output the result.

Uploaded by

Narender Rawat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Yatinder singh rawat

07211402020

JAVA PRACTICLE FILE

BPIBS

BCA-4TH SEM

NAME-Yatinder Singh Rawat


ROLL NO.- 07211402020
COURSE- JAVA
PROGRAMMING
1
Yatinder singh rawat
07211402020

INDEX

S Program Name Page Date Sign


No no.
1
Write a program to print “Hello World” on
the screen.

2
Write a program to calculate simple
interest and input should be by the user

3
Write a program to find the average and
sum of n numbers using command line
argument.

4
Write a program to find the number of
arguments provided at command line.

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

6
Write a program to create a simple class to
find out the area and perimeter of
rectangle

7
Write a program to find sum of elements of
a one-dimensional array entered
dynamically by the user

8
Write a program to find multiplication of
two 3X3 matrices. Data should be entered
by user

9
Write a program to demonstrate type
casting.

10
WAP to create the Person class. Create
some objects of this class

11
Assume that a bank maintains two kinds
account for its customers

2
Yatinder singh rawat
07211402020

Programs

1. Write a program to print “Hello World” on the screen.


Code:
class Practical_1
{
public static void main(String args[])
{
System.out.println("Hello World!");
}
}
Output:

3
Yatinder singh rawat
07211402020

2. Write a program to calculate simple interest and input


should be by the user
Code:
import java.util.Scanner;
class Practical_2
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
double simpleInterest = 0;
double rate;
double principle;
double time;

System.out.print("Enter principle amount : ");


principle = input.nextDouble();
System.out.print("Enter rate: ");
rate = input.nextDouble();
System.out.print("Enter time : ");
time = input.nextDouble();
simpleInterest = (principle*rate*time)/100.0;
System.out.println("Simple Interest : " + simpleInterest);
}
}
Output:

4
Yatinder singh rawat
07211402020

3. Write a program to find the average and sum of n numbers


using command line argument.
Code:
class Practical_3
{
public static void main(String args[])
{
double sum = 0;
double average = 0;
for(int i = 0; i<args.length; i++)
{
sum = sum + Double.parseDouble(args[i]);
}
System.out.println("Sum of numbers: " + sum);
average = sum/args.length;
System.out.println("Average: " + average);
}
}
Output:

5
Yatinder singh rawat
07211402020

4. Write a program to find the number of arguments provided


at command line.
Code:
class Practical_4
{
public static void main(String args[])
{
System.out.println("Number of arguments provided: " +
args.length);
}
}
Output:

6
Yatinder singh rawat
07211402020

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


Code:
import java.util.Scanner;

class gcdClass
{

private int num1, num2;


public gcdClass()
{
num1 = num2 = 1;

}
public gcdClass(int a, int b)
{
num1 = a;
num2 = b;
}

public void showNumbers()


{
System.out.print("The two numbers are: ");
System.out.println(num1 + " " + num2);

public void getGCD()


{
int gcd = 1;

for(int i = 2; i <= (num1>num2?num1: num2); i++)


{
if(num1%i==0 && num2%i==0)
{
gcd = i;

7
Yatinder singh rawat
07211402020

break;
}
}
System.out.println("GCD of " + num1 + " and " + num2 + " =
" + gcd);

public class gcd


{
public static void main(String args[])
{
int a, b;
Scanner in = new Scanner(System.in);
System.out.println("Welcome to the program!");
System.out.print("Enter first number: ");
a = in.nextInt();

System.out.print("Enter second number: ");


b = in.nextInt();

gcdClass obj = new gcdClass(a, b);


obj.showNumbers();
obj.getGCD();

}
}

8
Yatinder singh rawat
07211402020

Output:

9
Yatinder singh rawat
07211402020

6. Write a program to create a simple class to find out the


area and perimeter of rectangle.
Code:

class Rectangle
{
private double length;
private double breadth;

public Rectangle(int a, int b)


{
length = a;
breadth = b;
}
public Rectangle()
{
length = breadth = 0;
}
public void getArea()
{
System.out.println("Area of rectangle with length " +
length + " and breadth " + breadth + " = " + (length*breadth));
}
public void getPerimeter()
{
System.out.println("Perimeter of rectangle with length " +
length + " and breadth " + breadth + " = " + 2*(length+breadth));
}

}
class Practical_6
{
public static void main(String args[])
{

10
Yatinder singh rawat
07211402020

Rectangle r1 = new Rectangle(4,5);


r1.getArea();
r1.getPerimeter();

}
}

Output:

11
Yatinder singh rawat
07211402020

7. Write a program to find sum of elements of a one-


dimensional array entered dynamically by the user
Code:
import java.util.Scanner;
class Practical_7
{
public static void main(String args[])
{
array obj = new array();
obj.getArray();
obj.getValues();
obj.showValues();
obj.add();
}
}
class array
{
private int arr[];
Scanner input = new Scanner(System.in);
public void getArray()
{
System.out.println("Enter the number of elements you
want in array: ");
int N = input.nextInt();
arr = new int[N];
}
public void getValues()
{
for(int i = 0; i<arr.length;i++)
{
System.out.print("Value for " + (i+1) + " element: ");
arr[i] = input.nextInt();
}
}
public void showValues()

12
Yatinder singh rawat
07211402020

{
for(int i = 0; i<arr.length; i++)
{
System.out.print(arr[i] + " ");
}
System.out.println();

}
public void add()
{
int addition = 0;
for(int i = 0;i<arr.length;i++)
{
addition += arr[i];
}
System.out.println("Addition = "+addition);

}
}
Output:

13
Yatinder singh rawat
07211402020

8. Write a program to find multiplication of two 3X3 matrices.


Data should be entered by user.
Code:
import java.util.Scanner;
class matrix
{
private int arr1[][] = new int[3][3];
Scanner in =new Scanner(System.in);

public matrix()
{

}
public matrix(matrix b)
{
arr1 = b.arr1;
}

void getValues()
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{

System.out.println("Enter elements for


["+(i+1)+"]["+(j+1)+"] position ");
arr1[i][j]=in.nextInt();
}
}
}

14
Yatinder singh rawat
07211402020

void show()
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{

System.out.print(arr1[i][j] + "\t");

}
System.out.println();
}

matrix multiplication_of_matrix(matrix b)
{
matrix c = new matrix();
System.out.println();
System.out.println("Multiplication Of Two Matrices : ");
for (int i = 0; i <3; i++)
{
int sum =0;
for (int j = 0; j <3; j++)
{
sum = 0;
for (int k = 0; k <3; k++)
{
sum = sum + arr1[i][k] * b.arr1[k][j];
}
c.arr1[i][j] = sum;
}
}
return c;
}

15
Yatinder singh rawat
07211402020

}
public class Practical_8
{
public static void main(String[] args) {
matrix obj=new matrix();
obj.getValues();
obj.show();
matrix obj2 = new matrix();
obj2.getValues();
obj2.show();
matrix obj3 = new matrix();
obj3 = obj.multiplication_of_matrix(obj2);
System.out.println("After multiplication: ");
obj3.show();
}
}
Output: 1)

16
Yatinder singh rawat
07211402020

2)

17
Yatinder singh rawat
07211402020

9. Write a program to demonstrate type casting.


Code:
class Practical_9
{
public static void main(String args[])
{
int a = 10;
float b = 20;
char c = 'a';
double d = 32;
d = a; //No error
a = (int)d; //Error
a = c; //No error
System.out.println(a + " " + b + " " + c + " " + d);
}
}
Output:

18
Yatinder singh rawat
07211402020

10. 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
{
private String name;
private int age;
public void setName(String a)
{
name = a;
}
public void setAge(int a)
{
age = a;
}

public String getName()


{
return name;
}
public int getAge()
{
return age;
}
public void showData()
{
System.out.println();
System.out.print("Name : " + name + " ");

19
Yatinder singh rawat
07211402020

System.out.print("Age : " + age + " ");


System.out.println();

}
class Student extends Person
{
private int rollNo;

public void setRollNo(int a)


{
rollNo = a;
}
public void showData()
{
System.out.println();
System.out.println("Student Data: ");
System.out.print("Name : " + getName() + " ");
System.out.print("Age : " + getAge() + " ");
System.out.print("RollNo: " + rollNo + " ");
System.out.println();

}
class Teacher extends Person
{
private int id;

public void setId(int a)


{
id = a;
}

20
Yatinder singh rawat
07211402020

public void showData()


{
System.out.println();
System.out.println("Teacher Data: ");
System.out.print("Name : " + getName() + " ");
System.out.print("Age : " + getAge() + " ");
System.out.print("Id: " + id);
System.out.println();
}

public class Practical_10


{
private static Scanner input = new Scanner(System.in);

public static void main(String args[])


{

Student s1 = new Student();


System.out.print("Enter student name : ");
String studentName = input.nextLine();
s1.setName(studentName);

System.out.print("Enter student age: ");


int studentAge = input.nextInt();
s1.setAge(studentAge);

int rollNo;
System.out.print("Enter roll no : ");
rollNo = input.nextInt();
s1.setRollNo(rollNo);

21
Yatinder singh rawat
07211402020

Person ptr;
ptr = s1;
ptr.showData();

Teacher t1 = new Teacher();


input.nextLine();
System.out.print("Enter teacher name : ");
String teacherName = input.nextLine();
t1.setName(teacherName);

int id;

System.out.print("Enter teacher id : ");


id = input.nextInt();
t1.setId(id);

System.out.print("Enter teacher age: ");


int teacherAge = input.nextInt();
t1.setAge(teacherAge);

ptr = t1;

ptr.showData();
}
}

22
Yatinder singh rawat
07211402020

Output:

23
Yatinder singh rawat
07211402020

11. 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 in order 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();

24
Yatinder singh rawat
07211402020

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 {


private float withdraw;
private float interest;
private float deposit;

{cheque=false;}
void deposit() {
System.out.println("Enter Amount to be Deposited");
deposit = sc.nextFloat();
Amount += deposit;

25
Yatinder singh rawat
07211402020

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 {


private float withdraw;
private float deposit;
private float penalty;
{cheque=true;}

void deposit() {
System.out.printf("Enter Amount to be Deposited = ");
deposit = sc.nextFloat();

26
Yatinder singh rawat
07211402020

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");

27
Yatinder singh rawat
07211402020

Amount -= 15011111;
System.out.println("Your Total Balance After Paying
Penalty = " + Amount);

cInterest = 5;
}
else {
System.out.println("Time Remaining.");
cInterest--;
}
}

}
}

public class Bank_Account {


public static void main(String[] args) {
char 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
India..............");
System.out.println(".......Press S for Saving and C for
Current.......... ");
ch = sc.next().charAt(0);

if (ch=='s' || ch == 'S') {
sv.get_info();
while (count != 5) {
System.out.println();
System.out.println();

28
Yatinder singh rawat
07211402020

System.out.println("1.Display\n2.Deposit\n3.Withdraw\n4.Inte
rest\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=='c'|| ch == 'C') {


cu.get_info();
cu.penalty();

29
Yatinder singh rawat
07211402020

while (count != 4) {
System.out.println();

System.out.println("\n1.Display\n2.Deposit\n3.Withdraw\n4.C
heque 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!");
}

30
Yatinder singh rawat
07211402020

}
Output:
1)

2)

31
Yatinder singh rawat
07211402020

3)

4)

5)

32
Yatinder singh rawat
07211402020

6)

7)

33

You might also like