0% found this document useful (0 votes)
22 views16 pages

465 Java

The document contains a series of Java practical exercises for students, covering various programming concepts such as command line arguments, constructor overloading, method overloading, inheritance, encapsulation, and interface implementation. Each exercise includes a problem statement, sample code, and expected output. The exercises aim to enhance students' understanding of Java programming through practical application.

Uploaded by

qg4m9pg7sd
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)
22 views16 pages

465 Java

The document contains a series of Java practical exercises for students, covering various programming concepts such as command line arguments, constructor overloading, method overloading, inheritance, encapsulation, and interface implementation. Each exercise includes a problem statement, sample code, and expected output. The exercises aim to enhance students' understanding of Java programming through practical application.

Uploaded by

qg4m9pg7sd
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

SY-BCA-Div-5-Sem-4 JAVA PRACTICAL Roll no : 465

Q-1 : Take your full name at run time using command line arguments and display on screen using Java
program.

public class fullname {


public static void main(String[] args) {
String name = "Manashvi";
[Link]("Your name is : " + name);
}
}

Output :

Q-2 : Write a Program to Overload 2 Constructor. First constructor having 2 argument and Second
constructor having 3 argument perform subtraction operation on first Constructor and perform addition
operation on second constructor. (Use the concept of This Keyword)

class math_func {
private int result;

// Constructor with two arguments (Performs subtraction)


math_func(int a, int b) {
[Link] = a - b;

public static void main(String[] args) {

// Calling first constructor


math_func obj1 = new math_func(10, 5);

// Calling second constructor


math_func obj2 = new math_func(10, 5, 3);

Output :

Page | 1
SY-BCA-Div-5-Sem-4 JAVA PRACTICAL Roll no : 465

Q-3 : Create a one class, create method calculate_area( ) and find the area of Parallelogram (P = B * H)
and find the area of tringle (T = 1/2 * B * H). [Use the Concept of method Overloading]

class AreaCalculator {

// Method to calculate the area of a parallelogram

void calculate_area(int base, int height) {

int area = base * height;

[Link]("Area of Parallelogram: " + area);

// Method to calculate the area of a triangle

void calculate_area(double base, double height) {

double area = 0.5 * base * height;

[Link]("Area of Triangle: " + area);

public class ConstructorOverloadExample {

public static void main(String[] args) {

AreaCalculator obj = new AreaCalculator();

// Calculate the area of a parallelogram

obj.calculate_area(10, 5);

// Calculate the area of a triangle

obj.calculate_area(10.0, 5.0);

Page | 2
SY-BCA-Div-5-Sem-4 JAVA PRACTICAL Roll no : 465

Output :

Q-4 : Write a program which show the calling sequence of default constructor in multilevel inheritance.

class Grandparent {
Grandparent() {
[Link]("Grandparent Constructor Called");
}
}

class Parent extends Grandparent {


Parent() {
[Link]("Parent Constructor Called");
}
}

class Child extends Parent {


Child() {
[Link]("Child Constructor Called");
}
}

public class inheritance {


public static void main(String[] args) {

// Creating an object of Child class to demonstrate calling sequence


Child obj = new Child();
}
}

Output :

Page | 3
SY-BCA-Div-5-Sem-4 JAVA PRACTICAL Roll no : 465

Q-5 : Create a one class and give the name student, this class holds Enroll No, Name, Gender and
Standard/Course.(Check the condition if Call the school class then display a standard otherwise display
college) [Use Concept of inheritance]

class Student {
int enrollNo;
String name;
String gender;
String course;

Student(int enrollNo, String name, String gender, String course) {


[Link] = enrollNo;
[Link] = name;
[Link] = gender;
[Link] = course;
}

void displayDetails() {
[Link]("Enroll No: " + enrollNo);
[Link]("Name: " + name);
[Link]("Gender: " + gender);
}
}

class School extends Student {


School(int enrollNo, String name, String gender, String

standard) {
super(enrollNo, name, gender, standard);

}
void display() {
[Link]("Student is in School. Standard: " + course);
}
}

class College extends Student {


College(int enrollNo, String name, String gender, String course) {
super(enrollNo, name, gender, course);
}

void display() {
[Link]("Student is in College. Course: " + course);

Page | 4
SY-BCA-Div-5-Sem-4 JAVA PRACTICAL Roll no : 465

}
}

public class stud {


public static void main(String[] args) {

[Link]("Example 1: School Student");


School schoolStudent = new School(101, "Alice", "Female", "10th Grade");
[Link]();
[Link]();
[Link]();

[Link]("Example 2: College Student");


College collegeStudent = new College(202, "Bob", "Male", "Computer Science");
[Link]();
[Link]();
}
}

Output :

Q-6 : Write a Java program to create a class called Person with private instance variables name, age. and
country. Provide public getter and setter methods to access and modify these variables. [Use concept of
encapsulation]

Page | 5
SY-BCA-Div-5-Sem-4 JAVA PRACTICAL Roll no : 465

class Person {
// Private instance variables
private String name;
private int age;
private String country;

// Constructor

public Person(String name, int age, String country) {


[Link] = name;
[Link] = age;
[Link] = country;
}

// Getter and Setter methods


public String getName() {
return name;
}

public void setName(String name) {


[Link] = name;
}

public int getAge() {


return age;
}

public void setAge(int age) {


if (age > 0) {

[Link] = age;
} else {
[Link]("Age must be a positive number.");
}
}

public String getCountry() {


return country;
}

public void setCountry(String country) {


[Link] = country;
}

// Method to display person details


public void displayDetails() {
[Link]("Name: " + name);
[Link]("Age: " + age);
[Link]("Country: " + country);

Page | 6
SY-BCA-Div-5-Sem-4 JAVA PRACTICAL Roll no : 465

public class Person_ex {


public static void main(String[] args) {
// Creating a Person object
Person person1 = new Person("Alice", 25, "USA");

// Display initial details


[Link]();

[Link]();

// Modify values using setter methods


[Link]("Bob");
[Link](30);
[Link]("Canada");

// Display updated details


[Link]("Updated Details:");
[Link]();
}
}

Output :

Q-7 : Write a Java program to create a class called Shape with a method called getArea(). Create a
subclass called Rectangle that overrides the getArea() method to calculate the area of a rectangle and area
of square. [Use concept of Overriding]

class Shape {
public double getArea() {

Page | 7
SY-BCA-Div-5-Sem-4 JAVA PRACTICAL Roll no : 465

return 0;
}
}

class Rectangle extends Shape {


private double length;
private double width;

// Constructor for Rectangle


public Rectangle(double length, double width) {
[Link] = length;
[Link] = width;
}

// Overriding getArea() method for Rectangle


@Override
public double getArea() {
return length * width;
}
}

class Square extends Shape {


private double side;

// Constructor for Square


public Square(double side) {
[Link] = side;
}

// Overriding getArea() method for Square


@Override
public double getArea() {
return side * side;
}
}

public class Shape_ex {

public static void main(String[] args) {

// Creating a Rectangle object


Rectangle rectangle = new Rectangle(10, 5);
[Link]("Area of Rectangle: " + [Link]());

// Creating a Square object


Square square = new Square(4);
[Link]("Area of Square: " + [Link]());
}
}

Page | 8
SY-BCA-Div-5-Sem-4 JAVA PRACTICAL Roll no : 465

Output :

Q-8 : Write a program in Java which show that interface can inherit another interface. Take interface A
Which have input method and another interface B which have display method. Create one child class C
Which implements the both methods.

interface A {
void input();
}

interface B extends A {
void display();
}

class C implements B {
private String data;

@Override
public void input() {
data = "Interface Inheritance Example";
}

@Override
public void display() {
[Link]("Data: " + data);
}
}

public class Interface {


public static void main(String[] args) {
C obj = new C();
[Link]();
[Link]();
}
}

Output :

Page | 9
SY-BCA-Div-5-Sem-4 JAVA PRACTICAL Roll no : 465

Q-9 : Write a java application which accept two strings. Merge both the string using alternate characters
Of each strings. Eg. “Hello” and “Good”. Result should be, “HGeololdo”.

import [Link];
public class string_merge {

public static String mergeAlternate(String str1, String str2) {

StringBuilder mergedString = new StringBuilder();


int length1 = [Link](), length2 = [Link]();
int maxLength = [Link](length1, length2);

for (int i = 0; i < maxLength; i++) {


if (i < length1) {
[Link]([Link](i));
}
if (i < length2) {

[Link]([Link](i));
}
}
return [Link]();
}

public static void main(String[] args) {


Scanner scanner = new Scanner([Link]);

[Link]("Enter first string: ");


String str1 = [Link]();

[Link]("Enter second string: ");


String str2 = [Link]();

String result = mergeAlternate(str1, str2);


[Link]("Merged String: " + result);

[Link]();
}
}

Output :

Page | 10
SY-BCA-Div-5-Sem-4 JAVA PRACTICAL Roll no : 465

Q-10 : Write a program for searching a sub-string from the given sentence entered by user. If found then
also calculate number of times given sub string occur in given sentence. Also replace it with some other
sub string entered by user.

For example. Enter string : “This is my car. That is your car. Where is your brother car?”

Enter sub-string : “is” Occur : 3 times Enter replace sub-string : “was” New string : “This was my car.
That was your car. Where was your brother car?”

import [Link];

public class sub_string {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);

// Get user input


[Link]("Enter a sentence: ");
String sentence = [Link]();

[Link]("Enter the sub-string to search: ");


String searchString = [Link]();

// Count occurrences of the substring


int count = [Link](searchString, -1).length - 1;
[Link]("Occurrences: " + count);

if (count > 0) {
[Link]("Enter the replacement sub-string: ");

String replaceString = [Link]();

// Replace occurrences
String newSentence = [Link](searchString, replaceString);
[Link]("New String: " + newSentence);
} else {
[Link]("Substring not found.");
}

Page | 11
SY-BCA-Div-5-Sem-4 JAVA PRACTICAL Roll no : 465

[Link]();
}
}

Output :

Q-11 : Create your own User defined checked exception MobileNumberException if user entered mobile
number is not valid. (*Note : mobile number length must be 10 digits and only 0 to 9 digits are allowed.
Other characters or symbols are not allowed.)

import [Link];
// Custom checked exception
class MobileNumberException extends Exception {
public MobileNumberException(String message) {
super(message);
}
}

public class Number_validator {

public static void validateMobileNumber(String mobileNumber) throws MobileNumberException {


if (![Link]("\\d{10}")) {
throw new MobileNumberException("Invalid mobile number! It must contain exactly 10 digits (0-9)
only.");
}
[Link]("Valid mobile number: " + mobileNumber);
}

Page | 12
SY-BCA-Div-5-Sem-4 JAVA PRACTICAL Roll no : 465

public static void main(String[] args) {


Scanner scanner = new Scanner([Link]);

[Link]("Enter your mobile number: ");

String mobileNumber = [Link]();

try {
validateMobileNumber(mobileNumber);
} catch (MobileNumberException e) {
[Link]("Error: " + [Link]());

[Link]();
}
}

Output :

Q-12 : Write a program to make a package Balance in which has Account class with Display_Balance
method in it. Import Balance package in another program to access Display_Balance method of Account
class.

Package file : (balance)

package Balance;

public class Account {


private double balance;

Page | 13
SY-BCA-Div-5-Sem-4 JAVA PRACTICAL Roll no : 465

// Constructor
public Account(double balance) {
[Link] = balance;
}

// Method to display balance


public void Display_Balance() {
[Link]("Current Balance: $" + balance);
}
}

Java file : (Bank)

import [Link];

public class Bank {


public static void main(String[] args) {
// Creating an Account object
Account myAccount = new Account(1000.50);

// Displaying the balance


myAccount.Display_Balance();
}
}

Output :

Q-13 : Write a program to accept 10 names from the user. Find all the names which start from ‘J’ and
display them at interval of 2 sec. (*Note : take at least 4 to 5 names which start from ‘J’)

import [Link];

public class name_filter {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
String[] names = new String[10];

Page | 14
SY-BCA-Div-5-Sem-4 JAVA PRACTICAL Roll no : 465

// Accepting 10 names from the user


[Link]("Enter 10 names:");
for (int i = 0; i < 10; i++) {
names[i] = [Link]();
}

[Link]("\nNames starting with 'j':");

for (String name : names) {


if ([Link]("j")) {
[Link](name);
try {
[Link](2000); // Pause for 2 seconds
} catch (InterruptedException e) {
[Link]();
}
}
}

[Link]();
}
}

Output :

Page | 15
SY-BCA-Div-5-Sem-4 JAVA PRACTICAL Roll no : 465

Page | 16

You might also like