0% found this document useful (0 votes)
15 views30 pages

Java Programs 3

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)
15 views30 pages

Java Programs 3

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/ 30

Date: - 26/08/25

Ques 1: - Write a Java program to:


1. Create a 1D integer array using the Scanner class.
2. Ask the user to enter the size of the array.
3. Take input for all array elements from the user.
4. Display the entered array elements on the screen.
Also show the sample input and output of the program.

import [Link];

public class OneDArrayExample {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]); // Create Scanner object

// Step 1: Ask the user for the size of the array


[Link]("Enter the size of the array: ");
int size = [Link]();

// Step 2: Declare the array


int[] arr = new int[size];

// Step 3: Take input values from the user


[Link]("Enter " + size + " elements: ");
for (int i = 0; i < size; i++) {
arr[i] = [Link]();
}

// Step 4: Display the array elements


[Link]("You entered the following elements:");
for (int i = 0; i < size; i++) {
[Link](arr[i] + " ");
}
}
}
Output: -
Enter the size of the array: 5
Enter 5 elements:
10
20
30
40
50
You entered the following elements:
10 20 30 40 50
Ques 2: - Write a Java program to create and display a 2D array using the
Scanner class. The program should take the number of rows and columns as
input from the user, accept the elements of the array, and then print the array
in matrix form.

import [Link];

public class TwoDArrayExample {


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

// Step 1: Take input for rows and columns


[Link]("Enter number of rows: ");
int rows = [Link]();

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


int cols = [Link]();

// Step 2: Create a 2D array


int[][] arr = new int[rows][cols];

// Step 3: Take input for each element


[Link]("Enter the elements of the array:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
[Link]("Element at [" + i + "][" + j + "]: ");
arr[i][j] = [Link]();
}
}

// Step 4: Display the array


[Link]("\nThe 2D Array is:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
[Link](arr[i][j] + " ");
}
[Link](); // for new row
}

[Link]();
}
}
Output:-
Enter number of rows: 2
Enter number of columns: 3
Enter the elements of the array:
Element at [0][0]: 10
Element at [0][1]: 20
Element at [0][2]: 30
Element at [1][0]: 40
Element at [1][1]: 50
Element at [1][2]: 60
The 2D Array is:
10 20 30
40 50 60
Ques 3: - Write a program in Java to create a jagged array, take input from
the user, and display the output.

import [Link];

public class JaggedArrayScanner {


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

// Step 1: Ask user how many rows


[Link]("Enter number of rows: ");
int rows = [Link]();

// Step 2: Create jagged array (only rows fixed)


int[][] jagged = new int[rows][];

// Step 3: Take column size for each row from user


for (int i = 0; i < rows; i++) {
[Link]("Enter number of columns in row " + (i + 1) + ": ");
int cols = [Link]();
jagged[i] = new int[cols]; // assign size to each row

// Step 4: Fill values in the current row


[Link]("Enter " + cols + " values for row " + (i + 1) + ": ");
for (int j = 0; j < cols; j++) {
jagged[i][j] = [Link]();
}
}

// Step 5: Display jagged array


[Link]("\nJagged Array Output:");
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < jagged[i].length; j++) {
[Link](jagged[i][j] + " ");
}
[Link]();
}

[Link]();
}
}
Output:-

Enter number of rows: 3


Enter number of columns in row 1: 3
Enter 3 values for row 1:
123
Enter number of columns in row 2: 5
Enter 5 values for row 2:
45678
Enter number of columns in row 3: 2
Enter 2 values for row 3:
9 10

Jagged Array Output:


123
45678
9 10
Ques 4: - WAP in Java to read ‘n’ integers from the user using the Scanner
class and store them in an anonymous array. Calculate and display the sum of
all elements entered by the user.

import [Link];

public class AnonymousArrayExample {


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

// Asking user how many elements they want


[Link]("Enter number of elements: ");
int n = [Link]();

// Creating an anonymous array directly inside the loop


// Anonymous array means we don't assign it to a variable
int sum = 0;
[Link]("Enter " + n + " elements:");

// Reading elements and using them directly


for (int x : new int[n]) { // anonymous array of size n
x = [Link](); // read each element
sum += x; // add to sum
}
[Link]("Sum of entered numbers: " + sum);

[Link]();
}
}

Output:-

Enter number of elements: 5


Enter 5 elements:
10
20
30
40
50
Sum of entered numbers: 150
Ques 5: - Write a Java program to implement single-level inheritance using
the Scanner class.
import [Link];

// Parent class
class Person {
String name;
int age;

// Method to take input for Person


void inputPersonDetails(Scanner sc) {
[Link]("Enter name: ");
name = [Link]();

[Link]("Enter age: ");


age = [Link]();
[Link](); // consume newline
}

// Method to display Person details


void displayPersonDetails() {
[Link]("Name: " + name);
[Link]("Age: " + age);
}
}
// Child class (Single-level inheritance)
class Student extends Person {
int rollNo;

// Method to take input for Student (includes parent’s details)


void inputStudentDetails(Scanner sc) {
// Call parent method to input name and age
inputPersonDetails(sc);

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


rollNo = [Link]();
}

// Method to display Student details (includes parent’s details)


void displayStudentDetails() {
displayPersonDetails(); // parent details
[Link]("Roll Number: " + rollNo);
}
}

// Main class
public class SingleLevelInheritanceExample {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

// Create student object


Student s1 = new Student();

// Input and display details


[Link](sc);
[Link]("\n--- Student Details ---");
[Link]();

[Link]();
}
}
Output:-

Enter name: Rahul


Enter age: 20
Enter roll number: 101

--- Student Details ---


Name: Rahul
Age: 20
Roll Number: 101
Ques 6:- Write a Java program to implement Multilevel Inheritance using the
Scanner class.
Create three classes: Person, Student, and Exam where:
1. Person class stores and displays name and age.
2. Student class inherits from Person and adds course information.
3. Exam class inherits from Student and adds marks.
4. Use Scanner class to take input from the user and display all details.
import [Link];

// Base class (Grandparent)


class Person {
String name;
int age;

void inputPersonDetails(Scanner sc) {


[Link]("Enter name: ");
name = [Link]();
[Link]("Enter age: ");
age = [Link]();
[Link](); // consume newline
}

void displayPersonDetails() {
[Link]("Name: " + name);
[Link]("Age: " + age);
}
}

// Derived class (Parent)


class Student extends Person {
String course;

void inputStudentDetails(Scanner sc) {


[Link]("Enter course: ");
course = [Link]();
}

void displayStudentDetails() {
displayPersonDetails(); // call base class method
[Link]("Course: " + course);
}
}

// Derived class (Child)


class Exam extends Student {
int marks;

void inputExamDetails(Scanner sc) {


[Link]("Enter marks: ");
marks = [Link]();
}

void displayExamDetails() {
displayStudentDetails(); // call parent class method
[Link]("Marks: " + marks);
}
}

// Main class
public class MultiLevelInheritanceDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

// Create object of child class


Exam exam = new Exam();

// Input details
[Link](sc); // from Person class
[Link](sc); // from Student class
[Link](sc); // from Exam class

// Display details
[Link]("\n--- Student Exam Details ---");
[Link]();
}
}

Output:-
Enter name: Rahul
Enter age: 21
Enter course: BCA
Enter marks: 85

--- Student Exam Details ---


Name: Rahul
Age: 21
Course: BCA
Marks: 85
Ques 7:- Write a Java program to demonstrate Hierarchical Inheritance using
the Scanner class.
 Create a parent class Shape with a method to display the area.
 Derive two child classes Rectangle and Circle from Shape.
 In Rectangle, calculate area using user input of length and width.
 In Circle, calculate area using user input of radius.
 Display the results using method calls.

import [Link];

// Parent class
class Shape {
double area;

// Method to display area


void displayArea() {
[Link]("The area is: " + area);
}
}

// Child class 1: Rectangle


class Rectangle extends Shape {
void calculateArea(double length, double width) {
area = length * width;
}
}

// Child class 2: Circle


class Circle extends Shape {
void calculateArea(double radius) {
area = 3.1416 * radius * radius;
}
}

// Main class
public class HierarchicalInheritanceExample {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

// Rectangle Object
Rectangle rect = new Rectangle();
[Link]("Enter length of rectangle: ");
double length = [Link]();
[Link]("Enter width of rectangle: ");
double width = [Link]();
[Link](length, width);
[Link]();

// Circle Object
Circle circle = new Circle();
[Link]("\nEnter radius of circle: ");
double radius = [Link]();
[Link](radius);
[Link]();

[Link]();
}
}

Output: -
Enter length of rectangle: 10
Enter width of rectangle: 5
The area is: 50.0

Enter radius of circle: 7


The area is: 153.9336
Ques 8 Question:
Write a Java program to demonstrate the use of the super keyword in
inheritance.
 Create a parent class Person with data members name and age, a
constructor to initialize them, and a method to display details.
 Create a child class Student that extends Person with an additional data
member course. Use the super keyword to call the parent constructor
and parent method.
 In the main method, use the Scanner class to take input for name, age,
and course, then display all details of the student.
import [Link];

// Parent Class
class Person {
String name;
int age;

// Parent class constructor


Person(String n, int a) {
name = n; // directly assigning without this
age = a;
}

// Parent class method


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

// Child Class
class Student extends Person {
String course;

// Child class constructor using super


Student(String n, int a, String c) {
super(n, a); // calling parent constructor
course = c; // directly assigning without this
}

// Child class method


void displayStudentDetails() {
[Link](); // calling parent method
[Link]("Course: " + course);
}
}

// Main Class
public class SuperKeywordExampleNoThis {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

// Taking input from user


[Link]("Enter student name: ");
String name = [Link]();

[Link]("Enter student age: ");


int age = [Link]();
[Link](); // consume newline

[Link]("Enter student course: ");


String course = [Link]();

// Creating Student object


Student s1 = new Student(name, age, course);

// Display details
[Link]("\n--- Student Details ---");
[Link]();
}
}
Output: -
Enter student name: Ramesh
Enter student age: 22
Enter student course: [Link]

--- Student Details ---


Name: Ramesh
Age: 22
Course: [Link]
Ques 9:- Write a Java program using the Scanner class to take the name
and age of a student from the user. Use this keyword inside a constructor to
initialize instance variables (name and age) and then display the student
details.

import [Link];
class Student {
String name;
int age;

// Constructor with parameters having the same names as instance variables


Student(String name, int age) {
// 'this' is used to distinguish instance variables from local variables
[Link] = name;
[Link] = age;
}

// Method to display student details


void display() {
[Link]("Student Name: " + [Link]);
[Link]("Student Age: " + [Link]);
}
}

public class ThisKeywordExample {


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

// Taking user input


[Link]("Enter Student Name: ");
String name = [Link]();

[Link]("Enter Student Age: ");


int age = [Link]();

// Creating object and passing values


Student s1 = new Student(name, age);

// Displaying student details


[Link]();

[Link]();
}
}
Output:-
Enter Student Name: Rahul
Enter Student Age: 21
Student Name: Rahul
Student Age: 21
Ques 10: - Write a Java program to demonstrate the use of an abstract
class and an abstract method.
 Create an abstract class Shape with an abstract method area().
 Derive two subclasses: Circle and Rectangle, each implementing the
area () method.
 In the main method, use the Scanner class to take input for the radius of
a circle and the length & width of a rectangle.
 Display the area of both shapes by calling their respective area()
methods.

import [Link];

// Abstract class
abstract class Shape {
// Abstract method (no body)
abstract void area();
}

// Subclass 1: Circle
class Circle extends Shape {
double radius;

Circle(double radius) {
[Link] = radius;
}
// Implementing abstract method
void area() {
double result = [Link] * radius * radius;
[Link]("Area of Circle: " + result);
}
}

// Subclass 2: Rectangle
class Rectangle extends Shape {
double length, width;

Rectangle(double length, double width) {


[Link] = length;
[Link] = width;
}

// Implementing abstract method


void area() {
double result = length * width;
[Link]("Area of Rectangle: " + result);
}
}
public class AbstractExample {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

// Circle input
[Link]("Enter radius of circle: ");
double r = [Link]();
Shape circle = new Circle(r); // Polymorphism
[Link]();

// Rectangle input
[Link]("Enter length of rectangle: ");
double l = [Link]();
[Link]("Enter width of rectangle: ");
double w = [Link]();
Shape rectangle = new Rectangle(l, w); // Polymorphism
[Link]();

[Link]();
}
}
Output:-

Enter radius of circle: 5


Area of Circle: 78.53981633974483
Enter length of rectangle: 4
Enter width of rectangle: 6
Area of Rectangle: 24.0

You might also like