[go: up one dir, main page]

0% found this document useful (0 votes)
18 views11 pages

CSE 12 Batch Question Solve 1

The document explains key concepts of object-oriented programming (OOP) in Java, including definitions of objects, classes, and access modifiers. It differentiates OOP from procedural programming, highlights features like encapsulation, inheritance, and polymorphism, and provides examples of Java code for class creation, array manipulation, and fare calculation. Additionally, it discusses constructors, getter/setter methods, and the advantages of encapsulation.
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)
18 views11 pages

CSE 12 Batch Question Solve 1

The document explains key concepts of object-oriented programming (OOP) in Java, including definitions of objects, classes, and access modifiers. It differentiates OOP from procedural programming, highlights features like encapsulation, inheritance, and polymorphism, and provides examples of Java code for class creation, array manipulation, and fare calculation. Additionally, it discusses constructors, getter/setter methods, and the advantages of encapsulation.
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/ 11

1

(a)Q: Define object..


In Java, an object is a fundamental concept in object-oriented
programming. It is an instance of a class, which serves as a blueprint.
Objects encapsulate data (fields or attributes) and behavior (methods),
allowing for modular and reusable code.

Differentiate between OOP and Procedural language


Procedural Language Object-Oriented Programming
In procedural language, the program is divided In object-oriented programming, the
into small parts called functions. program is divided into small parts
called objects.
There is no access specifier in procedural Object-oriented programming has
language. access specifiers like private, public,
protected, etc.
Adding new data and functions is not easy. Adding new data and function is easy.
Code reusability absent in procedural laguage, Code reusability present in object-
oriented programming.
Examples: C, FORTRAN, Pascal, Basic, etc. Examples: C++, Java, Python, C#, etc.

(b) Question: Describe the three key future of OOP in detile;

The three key features of object-oriented programming (OOP)


are encapsulation, inheritance, and polymorphism:
• Encapsulation: Bundles data and methods into a single unit, called
a class. This restricts direct access to some of an object's
components, which helps protect data integrity.
• Inheritance: Allows new classes to be created based on existing
ones.
• Polymorphism: Allows objects of different classes to be treated
uniformly through a common interface. This means that methods
can perform differently based on input.

(c)Question: Mention the access modifiers use in java;


The four access modifiers in Java are public, protected, default,
and private.
2. (a) what is class in java. write down the syntex of declearing a class
in java.
In Java, a class is a blueprint or template used to create objects. It
defines the structure and behavior (attributes and methods) that the
objects created from it will have.
[access_modifier] class ClassName [extends ParentClass] {
// Fields (Attributes)
dataType fieldName;
// Constructors
ClassName(parameters) {
// Constructor body
}
// Methods (Behaviors)
returnType methodName(parameters) {
// Method body
}
}
(b) consider a class named vehicle have some attributes such as colors,
brand,speed,honk() etc. create an object of this class and print at least
tow of its attributes.
Ans:
class Vehicle {
// Attributes
String color;
String brand;
int speed;

// Method
void honk() {
System.out.println(brand + " is honking!");
}
}

public class Main {


public static void main(String[] args) {
// Creating an object of the Vehicle class
Vehicle myVehicle = new Vehicle();

// Setting attributes
myVehicle.color = "Red";
myVehicle.brand = "Toyota";
myVehicle.speed = 120;
// Printing two attributes
System.out.println("Vehicle Brand: " + myVehicle.brand);
System.out.println("Vehicle Color: " + myVehicle.color);

// Calling a method
myVehicle.honk();
}
}
(c) Define string class
In Java, the String class is part of the java.lang package and is used to represent a
sequence of characters. Strings are widely used in Java for storing and
manipulating text.

3. (a) create an array containing 5 elements of integer data. read their values using
scanner class and print them as twice (double of the assigned values).
Ans:

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
// Create a Scanner object for input
Scanner scanner = new Scanner(System.in);

// Create an array to hold 5 integers


int[] numbers = new int[5];

// Read 5 integers from the user


System.out.println("Enter 5 integers:");
for (int i = 0; i < numbers.length; i++) {
numbers[i] = scanner.nextInt();
}

// Print the double of each integer


System.out.println("Doubled values:");
for (int number : numbers) {
System.out.println(number * 2);
}

// Close the scanner


scanner.close();
}
}
input: 1 2 3 4 5
output: 2 4 6 8 10
(b) A rikshaw puller demand 50 tk for first 3 kilometers (same fare for below 3 KM)
to his passenger. for additional distance, he charge 12 tk per kilometers if a
passenger rides above 3 km. now write programme in java to calculate the total
fare for the distance X (must be above 3 km).
Ans:
import java.util.Scanner;

public class Main{


public static void main(String[] args) {
// Create a Scanner object for input
Scanner scanner = new Scanner(System.in);

// Prompt the user to enter the distance


System.out.print("Enter the distance traveled (in km, must be above 3): ");
double distance = scanner.nextDouble();

// Validate the input


if (distance <= 3) {
System.out.println("Error: Distance must be greater than 3 km.");
} else {
// Calculate fare
double baseFare = 50; // Fare for the first 3 km
double additionalFarePerKm = 12; // Fare for each km beyond 3
double additionalDistance = distance - 3;
double totalFare = baseFare + (additionalFarePerKm * additionalDistance);

// Display the total fare


System.out.println("Total fare: " + totalFare + " TK");
}
// Close the scanner
scanner.close();
}
}

5a. Define Constructor. Why do we use 'get' and 'set' methods in Java?
Definition of Constructor:
A constructor is a special method in Java used to initialize objects. It is called
automatically when an object of a class is created.
The get method returns the variable value, and the set method sets the value.

5b. Write a program in Java to subtract two integers using a user-defined


method.

import java.util.Scanner;

public class Main {


// User-defined method for subtraction
public static int subtract(int a, int b) {
return a - b;
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
// Input two integers
System.out.print("Enter first integer: ");
int num1 = sc.nextInt();
System.out.print("Enter second integer: ");
int num2 = sc.nextInt();

// Call the subtraction method and display result


int result = subtract(num1, num2);
System.out.println("Result of subtraction: " + result);

sc.close();
}
}
Enter first integer: 10
Enter second integer: 4
Result of subtraction: 6

5c. Discuss the advantages of encapsulation offered in Java.


1. Data Protection:
Encapsulation ensures that sensitive data (fields) is hidden from outside
classes. Access is controlled through getters and setters.
2. Flexibility:
By using setters, we can validate the data before assigning it to fields. For
example, ensuring that age cannot be negative.
3. Code Maintainability:
Changes to the fields or methods can be done internally without affecting
external classes.
4. Modularity:
Encapsulation allows better modularity, making the code more organized
and easier to understand.
5. Increased Security:
Encapsulation ensures that only authorized methods have access to the
fields, providing an additional layer of security.

6a. Define Subclass and Superclass. Create a constructor in Java.


Definition of Subclass and Superclass:
• Superclass: A superclass is a parent class from which other classes
(subclasses) inherit properties and methods.
• Subclass: A subclass is a child class that inherits from the superclass. It can
access public and protected members of the superclass and can also
override or extend its behavior.

Java Example with Constructor:

// Superclass
class Vehicle {
String brand;

// Constructor for Superclass


public Vehicle(String brand) {
this.brand = brand;
}

// Method in Superclass
public void displayBrand() {
System.out.println("Brand: " + brand);
}
}

// Subclass
class Car extends Vehicle {
int speed;

// Constructor for Subclass


public Car(String brand, int speed) {
super(brand); // Call to superclass constructor
this.speed = speed;
}

public void displaySpeed() {


System.out.println("Speed: " + speed + " km/h");
}
}
// Main Class
public class Main {
public static void main(String[] args) {
Car car = new Car("Toyota", 120);
car.displayBrand();
car.displaySpeed();
}
}
Brand: Toyota
Speed: 120 km/h

You might also like