java 2
java 2
Parameterized Constructor in JavaA constructor is a special type of method in Java that is used to
initialize objects. A parameterized constructor is a constructor that takes arguments (parameters)
to initialize an object with specific values.Key Points about Parameterized Constructor:
// Class definition
// Method to display student details void display() { System.out.println("Name: " + name + ",
Age: " + age); } public static void main(String[] args) { // Creating objects and passing values to
the constructor
Output:
Explanation:
1. The Student class has a parameterized constructor Student(String n, int a), which initializes
the name and age attributes.
2. In the main() method, two objects (s1 and s2) are created using the parameterized
constructor, and different values are passed to them.
This approach helps in creating objects with different initial values dynamically.
Q 2---Write a program in Java to diplay terms of naturel number and their sum
Java Program
import java.util.Scanner;
public class NaturalNumberSum { public static void main(String[] args) { Scanner sc = new
Scanner(System.in);
// Taking user input for number of terms System.out.print("Enter the number of natural
numbers: "); int n = sc.nextInt();
int sum = 0;
sum += i;
sc.close();}}
Example Output
Natural numbers: 1 2 3 4 5
Explanation
This is a simple and effective way to work with natural numbers in Java.
Q 3 Explain method overloading using java program.
Method Overloading is a feature in Java that allows multiple methods in the same class to have the
same name but different parameters (different type, number, or sequence of parameters). It helps
in improving code readability and reusability. Key Rules for Method Overloading:
Output
Integer: 10
Double: 15.5
Explanation
2. The compiler determines which method to call based on the arguments passed.
3. Overloading allows the same method name to handle different types of inputs efficiently.
Q—4 What is a Multidimensional array? Wite a java program for addition of two dimensional array
int cols = sc.nextInt(); int[][] matrix1 = new int[rows][cols] int[][] matrix2 = new
int[rows][cols]; int[][] sumMatrix = new int[rows][cols];
for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) {matrix1[i][j] = sc.nextInt(); }}// Input for
Second Matrix
matrix2[i][j] = sc.nextInt();
} System.out.println();
} sc.close();
}
Q 5----Explain concept of multilevel inhertiance using a simple java program
Multilevel Inheritance is a type of inheritance where a class is derived from another derived class.
In other words, there is a chain of inheritance.
Key Points of Multilevel Inheritance----A class (Child) inherits from another class (Parent), and then
another class (Grandchild) inherits from the Child class.----The derived class gets all the features of
the base class and can also have its own additional features.---This forms a hierarchy of classes.
Example of Multilevel Inheritance in Java Let's consider a real-world example: Animal → Mammal
→ Dog Animal class represents general properties of all animals.Mammal class inherits from
Animal and adds features specific to mammals.Dog class inherits from Mammal and adds
properties of a dog. Java Code for Multilevel Inheritance// Base Class (Parent)class Animal {
// Main Class public class MultilevelInheritance {public static void main(String[] args) {
Dog myDog = new Dog(); // Creating an object of Dog // Calling methods from all levels of
inheritance myDog.eat(); // From Animal class
Explanation
2. Mammal class (Derived from Animal)Inherits eat() method from Animal.Defines its own
method walk().
3. Dog class (Derived from Mammal)Inherits eat() from Animal and walk() from
Mammal.Defines its own method bark().
Exception handling manages runtime errors to prevent program crashes and maintain smooth
execution.
Benefits:
Example:
try {
int a = 10, b = 0;
} catch (ArithmeticException e) {
} finally {
System.out.println("Always executes.");
Output:
Always executes.
Example:
JavaScript Program to Add Two Numbers Using Form, Text Box & Button Click Event
This program takes two numbers from user input, adds them, and displays the result when a
button is clicked.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script>
function addNumbers() {
if (isNaN(num1) || isNaN(num2)) {
return;
}
// Perform addition
// Display result
</script>
</head>
<body>
<form>
<br><br>
<br><br>
</form>
<h3 id="result"></h3>
</body>
</html>
Explanation:
3. The function retrieves the values, converts them to numbers, adds them, and displays the
result in an <h3> tag.