0% found this document useful (0 votes)
2 views12 pages

Unit II Java

The document covers key concepts in Java programming, including classes, objects, access modifiers, method parameter passing, recursion, constructors, and constructor overloading. It provides explanations and example Java programs for each topic, demonstrating the creation and usage of classes and objects, the effects of different access modifiers, and the implementation of recursion and constructor overloading. The document serves as a comprehensive guide for understanding fundamental Java programming principles.

Uploaded by

arobotic06
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)
2 views12 pages

Unit II Java

The document covers key concepts in Java programming, including classes, objects, access modifiers, method parameter passing, recursion, constructors, and constructor overloading. It provides explanations and example Java programs for each topic, demonstrating the creation and usage of classes and objects, the effects of different access modifiers, and the implementation of recursion and constructor overloading. The document serves as a comprehensive guide for understanding fundamental Java programming principles.

Uploaded by

arobotic06
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

UNiTE Computer Academy

JAVA Programming

UNIT – II

1) Explain the concepts of classes and objects in Java. Write a Java program to
demonstrate the creation of a class and the use of its objects.
Ans:
Class
A class is a blueprint or template used to create objects.
It contains:
• Data members (variables)
• Member functions (methods)
A class does not occupy memory until its object is created.
Syntax:
class ClassName {
// variables
// methods
}

Object :
▪ An object is an instance of a class.
▪ Objects are used to access the variables and methods of a class.
▪ Memory is allocated when an object is created.
Syntax:
ClassName obj = new ClassName();

Ex:

class Student {

// Data members

int rollNo;

String name;

// Member function

void display() {

[Link]("Roll No: " + rollNo);


1|Page
Near Jaihind Senior College Behind Dominos 1st Floor Deopur Dhule – 424002
Contact : 7249008822
UNiTE Computer Academy
[Link]("Name: " + name);

public static void main(String[] args) {

// Creating object

Student s1 = new Student();

// Assigning values

[Link] = 101;

[Link] = "Ganesh";

// Calling method

[Link]();

Explanation :

▪ Student is a class.
▪ rollNo and name are data members.
▪ display() is a member function.
▪ s1 is an object of the Student class.
▪ The object accesses variables and methods using the dot (.) operator.

2|Page
Near Jaihind Senior College Behind Dominos 1st Floor Deopur Dhule – 424002
Contact : 7249008822
UNiTE Computer Academy
2) Explain the different access modifiers in Java. Write a Java program to
demonstrate access modifier.

Ans:

Access modifiers in Java are used to control the visibility and accessibility of
classes, variables, methods, and constructors.

Java provides four types of access modifiers:

1. public
2. private
3. protected

1. Public Access Modifier

• Accessible from anywhere in the program.

• Highest visibility.

Ex:

public int age;

2. Private Access Modifier

• Accessible only within the same class.

• Used for data hiding.

Ex:

private int salary;

3. Protected Access Modifier

• Accessible within the same package and subclasses.

Ex:

protected String name;

4. Default Access Modifier

• No keyword is used.

• Accessible only within the same package.


3|Page
Near Jaihind Senior College Behind Dominos 1st Floor Deopur Dhule – 424002
Contact : 7249008822
UNiTE Computer Academy
Ex:

int marks;

Example:

class Demo

public int a = 10;

private int b = 20;

protected int c = 30;

int d = 40; // default access

void display()

[Link]("Public Variable: " + a);

[Link]("Private Variable: " + b);

[Link]("Protected Variable: " + c);

[Link]("Default Variable: " + d);

public class Main

public static void main(String[] args) {

Demo obj = new Demo();

[Link]();

[Link]("Public: " + obj.a);

// Private variable cannot be accessed directly

// [Link](obj.b);

4|Page
Near Jaihind Senior College Behind Dominos 1st Floor Deopur Dhule – 424002
Contact : 7249008822
UNiTE Computer Academy
[Link]("Protected: " + obj.c);

[Link]("Default: " + obj.d);

3) Write a Java program to demonstrate passing primitive variables and object


references to methods and compare the results.

Ans:

class Student {

int marks;

Student(int m)

marks = m;

public class PassDemo

// Method for primitive variable

static void changePrimitive(int x) {

x = 100;

[Link]("Value inside method: " + x);

// Method for object reference

static void changeObject(Student s) {

[Link] = 90;
5|Page
Near Jaihind Senior College Behind Dominos 1st Floor Deopur Dhule – 424002
Contact : 7249008822
UNiTE Computer Academy
[Link]("Marks inside method: " + [Link]);

public static void main(String[] args) {

// Primitive variable

int num = 50;

[Link]("Before method call: " + num);

changePrimitive(num);

[Link]("After method call: " + num);

[Link]();

// Object reference

Student obj = new Student(70);

[Link]("Before method call: " + [Link]);

changeObject(obj);

[Link]("After method call: " + [Link]);

Output:

Before method call: 50

Value inside method: 100

After method call: 50

Before method call: 70

Marks inside method: 90

After method call: 90

6|Page
Near Jaihind Senior College Behind Dominos 1st Floor Deopur Dhule – 424002
Contact : 7249008822
UNiTE Computer Academy
Explanation:

Primitive Variable

changePrimitive(num);

▪ Only a copy of num is passed.


▪ Original value remains unchanged.

Result:

After method call: 50

Object Reference

changeObject(obj);

▪ Copy of object reference is passed.


▪ Both references point to the same object.
▪ Changes affect the original object.

Result:

After method call: 90

7|Page
Near Jaihind Senior College Behind Dominos 1st Floor Deopur Dhule – 424002
Contact : 7249008822
UNiTE Computer Academy
4) Explain the concept of recursion, and develop a Java program that uses
recursion to compute the factorial of a number.

Ans:

Recursion is a process in which a method calls itself repeatedly to solve a


problem.

A recursive method must have:

1. Base case → stops recursion

2. Recursive call → method calls itself

import [Link];

class Main {

// Recursive method

static int factorial(int n) {

// Base case

if(n == 0 || n == 1) {

return 1;

// Recursive call

return n * factorial(n - 1);

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

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

int num = [Link]();

int result = factorial(num);

[Link]("Factorial = " + result);

8|Page
Near Jaihind Senior College Behind Dominos 1st Floor Deopur Dhule – 424002
Contact : 7249008822
UNiTE Computer Academy
[Link]();

5) Design and implement a Book class in Java that utilizes both a default
constructor and a parameterized constructor to initialize object data, and
demonstrate their usage with suitable examples.
Ans:
class Book {

// Data members
int bookId;
String bookName;
double price;

// Default Constructor
Book() {
bookId = 101;
bookName = "Java Programming";
price = 450.50;
}

// Parameterized Constructor
Book(int id, String name, double p) {
bookId = id;
bookName = name;
price = p;
}
// Method to display details
void display() {
[Link]("Book ID: " + bookId);
[Link]("Book Name: " + bookName);
[Link]("Price: " + price);
[Link]();
}

9|Page
Near Jaihind Senior College Behind Dominos 1st Floor Deopur Dhule – 424002
Contact : 7249008822
UNiTE Computer Academy
public static void main(String[] args) {

// Object using default constructor


Book b1 = new Book();

// Object using parameterized constructor


Book b2 = new Book(102, "Python Basics", 550.75);

[Link]("Using Default Constructor");


[Link]();

[Link]("Using Parameterized Constructor");


[Link]();
}
}

6) Create a class that demonstrates constructor overloading by initializing objects


with different parameter lists.
Ans:
Constructor overloading means creating multiple constructors in the
same class with different parameter lists.

Different constructors can:


• Have different number of parameters
• Have different types of parameters.

It is used to initialize objects in different ways.


Ex:
class Student {

int rollNo;
String name;
double marks;

// Default constructor

10 | P a g e
Near Jaihind Senior College Behind Dominos 1st Floor Deopur Dhule – 424002
Contact : 7249008822
UNiTE Computer Academy
Student() {
rollNo = 0;
name = "Unknown";
marks = 0.0;
}

// Constructor with one parameter


Student(int r) {
rollNo = r;
name = "Rahul";
marks = 75.5;
}

// Constructor with three parameters


Student(int r, String n, double m) {
rollNo = r;
name = n;
marks = m;
}

// Method to display data


void display() {
[Link]("Roll No: " + rollNo);
[Link]("Name: " + name);
[Link]("Marks: " + marks);
[Link]();
}

public static void main(String[] args) {

// Using default constructor


Student s1 = new Student();

// Using one-parameter constructor


Student s2 = new Student(101);

// Using parameterized constructor

11 | P a g e
Near Jaihind Senior College Behind Dominos 1st Floor Deopur Dhule – 424002
Contact : 7249008822
UNiTE Computer Academy
Student s3 = new Student(102, "Amit", 88.5);

[Link]();
[Link]();
[Link]();
}
}

Output :

Roll No: 0
Name: Unknown
Marks: 0.0

Roll No: 101


Name: Rahul
Marks: 75.5

Roll No: 102


Name: Amit
Marks: 88.5

12 | P a g e
Near Jaihind Senior College Behind Dominos 1st Floor Deopur Dhule – 424002
Contact : 7249008822

You might also like