[Go to site: main page, start]

0% found this document useful (0 votes)
3 views27 pages

Java Practical File

This document is a practical file for a Java programming course, submitted as part of the Bachelor of Computer Applications degree. It includes an index of various programming tasks and their implementations, such as calculating the area of a rectangle, checking for prime numbers, and creating classes for Student and Employee. The document serves as a comprehensive guide to the programming exercises completed by the student under the guidance of a teacher.

Uploaded by

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

Java Practical File

This document is a practical file for a Java programming course, submitted as part of the Bachelor of Computer Applications degree. It includes an index of various programming tasks and their implementations, such as calculating the area of a rectangle, checking for prime numbers, and creating classes for Student and Employee. The document serves as a comprehensive guide to the programming exercises completed by the student under the guidance of a teacher.

Uploaded by

285diksha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Practical File

of
Java
submitted in partial fulfillment of the requirement for the award of degree of
Bachelor of Computer Applications (BCA-5th)
in
Computer Applications
By
Name
(Roll Number)
Under the guidance of
Teacher name

Department of Computer Applications


J. C. BOSE UNIVERSITY OF SCIENCE & TECHNOLOGY, YMCA
SECTOR-6 FARIDABAD
HARYANA-121006
INDEX

Sr. No. List of Programs Teacher sign.

1 Print area and parameter of a rectangle


2 Print reverse of a number

3 Check even or odd


4 Print prime numbers from 1 to n

5 Print pyramid patterns

6 Print factorial of a number using recursion

7 Read n number of values in an array and display it in reverse


order
8 Multiply two given matrices

9 Check weather a string is palindrome or not

10 Sort names of an array in ascending order

11 Create a JAVA class called Student and create n Student


objects and print the Rollno, Name, Branch, Phone, and
percentage of these objects
12 Create a class Employee with a method called
calculateSalary() and create two subclasses Manager and
Programmer. In each subclass, override the calculateSalary()
method to calculate and return the salary based on their
specific roles.
13 Program using an interface called ‘Bank’ having function
‘rate_of_interest()’.
Implement this interface to create two separate bank classes
‘SBI’ and ‘PNB’ to print different rates of interest. Include
additional member variables, constructors also in classes ‘SBI’
and ‘PNB’.
14 Package program for the class book and then import the data
from the package and display the result.
[Link] a program to print area and parameter of a rectangle.
import [Link];
public class areaparameter {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);

[Link]("Enter length: ");


int length = [Link]();
[Link]("Enter bredth: ");
int bredth = [Link]();

int area = length*bredth;


[Link]("Area of rectangle is : "+area);
int parameter = 2*(length+bredth);
[Link]("Parameter of rectangle is : "+parameter);
}
}

Output:
2. Write a program to print reverse of a number.
import [Link];
public class reverse {
public static void main(String args[]) {
Scanner input = new Scanner([Link]);
[Link]("Enter a value: ");
int num = [Link]();

int reversed = 0;
[Link]("Original Number: "+ num);

while(num != 0) {
int digit = num % 10;
reversed = reversed*10 + digit;
num /= 10;
}
[Link]("Reversed Number: "+ reversed);
[Link]();
}
}

Output:
3. Write a program to check even or odd.
import [Link];
public class evenodd {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);

[Link]("Enter a number: ");


int num = [Link]();

if (num%2==0) {
[Link]("Number is even");
}
else{
[Link]("Number is odd");
}
[Link]();
}
}

Output:
4. Write a program to print prime numbers from 1 to n.
import [Link];
public class prime {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the value of n: ");
int n = [Link]();
[Link]("Prime numbers from 1 to " + n + " are:");
for (int i = 2; i <= n; i++) {
boolean isPrime = true;
for (int j = 2; j <= [Link](i); j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
[Link](i + " ");
} } }
}

Output:
5.) Write a program to print below pyramid pattern:

public class starpattern {


public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= (rows - i); j++) {
[Link](" ");
}
for (int k = 1; k <= i; k++) {
[Link]("* ");
}
[Link]();
}
}
}

Output:
6.)Write a program to print factorial of a number using recursion.
import [Link];
public class factorialRecursion {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
long fact = factorial(n); // call recursive function
[Link]("Factorial of " + n + " is: " + fact);
}
// Recursive function to find factorial
public static long factorial(int n) {
if (n == 0 || n == 1) {
return 1; // base case
} else {
return n * factorial(n - 1); // recursive call } }
}

Output:
7.)Write a java program to read n number of values in an array and display it
in reverse order.
import [Link];
public class reversearray {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the number of elements: ");
int n = [Link]();
int[] arr = new int[n];
// Input elements
[Link]("Enter " + n + " numbers:");
for (int i = 0; i < n; i++) {
arr[i] = [Link]();
}
// Display in reverse order
[Link]("Array elements in reverse order:");
for (int i = n - 1; i >= 0; i--) {
[Link](arr[i] + " ");
}
[Link](); }
}

Output:
8.)Write a program to multiply two given matrices.
import [Link];
public class matrixmultiply {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

// Input rows and columns


[Link]("Enter rows of first matrix: ");
int r1 = [Link]();
[Link]("Enter columns of first matrix: ");
int c1 = [Link]();
[Link]("Enter rows of second matrix: ");
int r2 = [Link]();
[Link]("Enter columns of second matrix: ");
int c2 = [Link]();

// Check multiplication condition


if (c1 != r2) {
[Link]("Matrix multiplication not possible!");
return;
}

// Declare matrices
int[][] A = new int[r1][c1];
int[][] B = new int[r2][c2];
int[][] C = new int[r1][c2];

// Input first matrix


[Link]("Enter first matrix:");
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c1; j++) {
A[i][j] = [Link]();
}
}

// Input second matrix


[Link]("Enter second matrix:");
for (int i = 0; i < r2; i++) {
for (int j = 0; j < c2; j++) {
B[i][j] = [Link]();
}
}

// Multiply
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
C[i][j] = 0;
for (int k = 0; k < c1; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}

// Print result
[Link]("Resultant Matrix:");
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
[Link](C[i][j] + " ");
}
[Link]();
}
}
}

Output:
9.)Write a program to check weather a string is palindrome or not.
import [Link];
public class palindrome {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
String original = str;
String rev = "";

// Reverse the string


for (int i = [Link]() - 1; i >= 0; i--) {
rev = rev + [Link](i);
}
// Check palindrome
if ([Link](rev)) {
[Link](original + " is a Palindrome string.");
} else {
[Link](original + " is NOT a Palindrome string.");
}
}
}

Output:
10.)Write a program to sort names of an array in ascending order.
import [Link];
public class SortNamesArray {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of names: ");
int n = [Link]();
[Link](); // to clear buffer
String[] names = new String[n];

// Input names
[Link]("Enter the names:");
for (int i = 0; i < n; i++) {
names[i] = [Link]();
}

// Sorting names (ascending)


for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (names[j].compareToIgnoreCase(names[j + 1]) > 0) {
// swap
String temp = names[j];
names[j] = names[j + 1];
names[j + 1] = temp;
}
}
}

// Print sorted names


[Link]("Sorted Names (A to Z):");
for (int i = 0; i < n; i++) {
[Link](names[i]);
}
}
}

Output:
11.) Create a JAVA class called Student with the following details as variables
within it.
a. ROLLNO, NAME, BRANCH, PHONE, PERCENTAGE
b. Write a JAVA program to create n Student objects and print the Rollno,
Name, Branch, Phone, and percentage of these objects with suitable
headings.
import [Link];

class Student {
private int rollno;
private String name;
private String branch;
private String phone;
private double percentage;

Student(int rollno, String name, String branch, String phone, double percentage) {
[Link] = rollno;
[Link] = name;
[Link] = branch;
[Link] = phone;
[Link] = percentage;
}

// Getter methods
public int getRollno() {
return [Link];
}

public String getName() {


return [Link];
}

public String getBranch() {


return [Link];
}

public String getPhone() {


return [Link];
}

public double getPercentage() {


return [Link];
}
}

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

Scanner sc = new Scanner([Link]);

[Link]("Enter number of students: ");


int n = [Link]();
[Link]();

Student[] s = new Student[n];

// Input section
for (int i = 0; i < n; i++) {
[Link]("\nEnter details of Student " + (i + 1));
[Link]("Enter Roll No: ");
int rollno = [Link]();
[Link]();

[Link]("Enter Name: ");


String name = [Link]();

[Link]("Enter Branch: ");


String branch = [Link]();

[Link]("Enter Phone: ");


String phone = [Link]();

[Link]("Enter Percentage: ");


double percentage = [Link]();
[Link]();

s[i] = new Student(rollno, name, branch, phone, percentage);


}

// Output Section
[Link]("\n\n----- STUDENT DETAILS -----");
[Link]("%-10s %-15s %-10s %-15s %-10s\n",
"ROLLNO", "NAME", "BRANCH", "PHONE", "PERCENTAGE");

for (Student student : s) {


[Link]("%-10d %-15s %-10s %-15s %-10.2f\n",
[Link](),
[Link](),
[Link](),
[Link](),
[Link]());
}
[Link]();
}
}

Output:
12.) Write a Java program to create a class Employee with a method called
calculateSalary().
Create two subclasses Manager and Programmer. In each subclass, override
the calculateSalary() method to calculate and return the salary based on
their specific roles.
import [Link].*;
class Employee {
String name;
double baseSalary;
Employee(String name, double salary) {
[Link] = name;
[Link] = salary;
}
double calculateSalary() {
return baseSalary;
}
String getName() {
return name;
}
}
class Manager extends Employee {
double bonusPercentage;
Manager(String name, double baseSalary, double bonusPercentage) {
super(name, baseSalary);
[Link] = bonusPercentage;
}
double calculateSalary() {
return baseSalary + (baseSalary * bonusPercentage);
}
}
class Programmer extends Employee {
int overtimeHours;
double hourRate;
Programmer(String name, double baseSalary, int overtimeHours, double hourRate) {
super(name, baseSalary);
[Link] = overtimeHours;
[Link] = hourRate;
}
double calculateSalary() {
return baseSalary + (overtimeHours * hourRate);
}
}

public class EmployeeDemo {


public static void main(String[] args) {

Employee generalStaff = new Employee("ABC", 20000);

Manager executive = new Manager("XYZ", 50000, 0.15);


// 20% bonus

Programmer developer = new Programmer("SH", 40000, 30, 1500);


// 30 overtime hours, Rs. 500/hour

[Link]("----- Employee Details -----");

[Link]("General Staff: " + [Link]());


[Link]("Salary: " + [Link]());
[Link]("\nManager: " + [Link]());
[Link]("Salary: " + [Link]());

[Link]("\nProgrammer: " + [Link]());


[Link]("Salary: " + [Link]());
}
}

Output:
13.) Write a Java program using an interface called ‘Bank’ having function
‘rate_of_interest()’.
Implement this interface to create two separate bank classes ‘SBI’ and ‘PNB’
to print different rates of interest. Include additional member variables,
constructors also in classes ‘SBI’ and ‘PNB’.
interface Bank {
void rate_of_interest();
}
class PNB implements Bank {
private String BranchCode;
private double MinBalance;
public PNB(String BranchCode, double MinBalance){
[Link]=BranchCode;
[Link]=MinBalance;
[Link]("PNB account created");
[Link]("Branch Code is: "+ [Link]);
[Link]("Min Balance is: "+ [Link]);
}
public void rate_of_interest(){
[Link]("Rate of interest of PNB is 7.5%. ");
}
}
class SBI implements Bank {
private String Name;
private String AccountType;
public SBI(String Name, String AccountType){
[Link]=Name;
[Link]=AccountType;
[Link]("SBI account created");
[Link]("Name is: "+ [Link]);
[Link]("Account Type is: "+ [Link]);
}
public void rate_of_interest(){
[Link]("Rate of interest of SBI is 6.8%. ");
}
}
class BankDemo {
public static void main(String[] args) {
PNB pnbAccount = new PNB("Pnb2829", 1100.0);
pnbAccount.rate_of_interest();
SBI sbiAccount = new SBI("Palak","Saving");
sbiAccount.rate_of_interest();

Bank generalBank;
generalBank = pnbAccount;
generalBank.rate_of_interest();
generalBank = sbiAccount;
generalBank.rate_of_interest();
}
}

Output:
14.) Write a Java package program for the class book and then import the data from the
package and display the result.

[Link]
package library;
public class Book {
private String title;
private String author;
private int pages;

public Book (String title, String author, int pages){


[Link]=title;
[Link]=author;
[Link]=pages;
}
public void display_info(){
[Link]("------------Book Details------------");
[Link]("Title: "+ [Link]);
[Link]("Author: "+ [Link]);
[Link]("Pages: "+ [Link]);
}
}

[Link]
import library.*;
public class BookDemo {
public static void main(String[] args) {
Book book1 = new Book("Java", "Abc", 500);
book1.display_info();
Book book2 = new Book("Python", "Xyz", 600);
book2.display_info();
}
}
Output:
15.) Write a Java program to handle ArithmeticException using try–catch–finally blocks
when dividing a number by zero.
public class ExceptionDemo {
public static void main(String[] args) {
int num=100;
int deno=0;
try {
int result = num/deno;
[Link]("Result is: "+ result);
} catch (ArithmeticException e) {
[Link]("Exception class: "+ [Link]().getName());
[Link]("Message: "+ [Link]());
}
finally{
[Link]("Finally block executed...");
}
[Link]("End of program");
}
}
Output:

You might also like