0% found this document useful (0 votes)
36 views15 pages

Java File-1-15

The document outlines various Java programming exercises focusing on matrix addition, jagged arrays, command line arguments for prime and palindrome checks, OOP concepts with student and bank account classes, and inheritance and polymorphism examples. Each program includes source code and expected outputs, demonstrating fundamental Java programming skills. The exercises cover topics such as method overloading, method overriding, encapsulation, and interface implementation.

Uploaded by

charmingmanalt5
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)
36 views15 pages

Java File-1-15

The document outlines various Java programming exercises focusing on matrix addition, jagged arrays, command line arguments for prime and palindrome checks, OOP concepts with student and bank account classes, and inheritance and polymorphism examples. Each program includes source code and expected outputs, demonstrating fundamental Java programming skills. The exercises cover topics such as method overloading, method overriding, encapsulation, and interface implementation.

Uploaded by

charmingmanalt5
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

PROGRAM: 1

OBJECTIVE - Use Java compiler and eclipse platform to write and execute java program.
1.a: Write a program to print addition of 2 matrices in java.
Source code:
public class MatrixAddition {
public static void main(String[] args) {
int[][] matrix1 = {
{1, 2, 3}, {4, 5, 6}, {7, 8, 9}
};
int[][] matrix2 = {
{9, 8, 7},{6, 5, 4},{3, 2, 1}
};
int[][] result = new int[[Link]][matrix1[0].length];
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < matrix1[0].length; j++) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
[Link]("Resultant Matrix after Addition:");
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < result[0].length; j++) {
[Link](result[i][j] + " ");
}
[Link]();
}
}
}
Output:
1.b: Write a program in java which creates the variable size array (Jagged Array) and print all
the values using loop statement.

Source code:

public class JaggedArrayExample {


public static void main(String[] args) {
int[][] jaggedArray = new int[3][];

jaggedArray[0] = new int[]{1, 2, 3};


jaggedArray[1] = new int[]{4, 5}
jaggedArray[2] = new int[]{6, 7, 8, 9};

[Link]("Jagged Array Elements:");


for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < jaggedArray[i].length; j++) {

[Link](jaggedArray[i][j] + " ");


}
[Link](); // Move to next line after each row
}
}
}
Output:
PROGRAM: 2

OBJECTIVE - Creating simple java programs using command line arguments.


2.a: WAP that takes input from user through command line argument and then prints whether a
number is prime or not.

Source code:

public class PrimeCheck {

public static void main(String[] args) {

if ([Link] == 0) {

[Link]("Please provide a number as a command line argument.");


return;
}
int num = [Link](args[0]);
boolean isPrime = true;
if (num < 2) {
isPrime = false;
}
else {
for (int i = 2; i <= [Link](num); i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime) {
[Link](num + " is a Prime number.");
} else {
[Link](num + " is NOT a Prime number.");

}
}
}
Output:

2.b: Write a program to enter number through command line and check whether it is palindrome
or not.

Source code:

class PalindromeCheck {

public static void main(String[] args) {

if ([Link] == 0) {

[Link]("Please provide a number as a command line argument.");


return;
}
String number = args[0];
String reversed = new StringBuilder(number).reverse().toString();
if ([Link](reversed)) {
[Link](number + " is a palindrome.");
} else {
[Link](number + " is not a palindrome.");
}
}
}
Output:

PROGRAM: 3

OBJECTIVE: Understand OOP concepts and basics of Java programming


3.a: Write a Java program to create a Student class, initialize student details using a
parameterized constructor, and display the student information using class objects and
member functions.
Source code:
class Student {
private int rollNumber;

private String name;

private String course;

public Student(int rollNumber, String name, String course) {

[Link] = rollNumber;

[Link] = name;

[Link] = course;

}
public void displayInfo() {

[Link]("Student Details:");

[Link]("Roll Number: " + rollNumber);

[Link]("Name: " + name);

[Link]("Course: " + course);

[Link]("---------------------------");

public class StudentDemo { public

static void main(String[] args) {

Student s1 = new Student(101, "Pallavi", "Computer Science");

Student s2 = new Student(102, "Rahul", "Mechanical Engineering");

Student s3 = new Student(103, "Sneha", "Electrical Engineering") [Link]();

[Link]();

[Link]();

Output:
3.b: Write a Java program to create a BankAccount class using encapsulation (private data
members) and perform operations such as deposit, withdraw, and display balance using
menu-driven choices.
Source code:
import [Link]
class BankAccount {
private String accountHolder;
private double balance
public BankAccount(String accountHolder, double initialBalance) {
[Link] = accountHolder;
[Link] = initialBalance;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
[Link]("Deposited: " + amount);
} else {
[Link]("Invalid deposit amount!");
}
}
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
[Link]("Withdrawn: " + amount);
}
else {
[Link]("Invalid withdrawal amount or insufficient
balance!");
}
}
public void displayBalance() {
[Link]("Account Holder: " + accountHolder);
[Link]("Current Balance: " + balance);
}
}
public class BankApp {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter account holder name: ");
String name = [Link]();
[Link]("Enter initial balance: ");
double initialBalance = [Link]();
BankAccount account = new BankAccount(name, initialBalance);
int choice;
do {
[Link]("\n--- Bank Menu ---");
[Link]("1. Deposit");
[Link]("2. Withdraw");
[Link]("3. Display Balance");
[Link]("4. Exit");
[Link]("Enter your choice: ");
choice = [Link]();
switch (choice) {
case 1:
[Link]("Enter deposit amount: ");
double depositAmount = [Link]();
[Link](depositAmount);
break;
case 2:
[Link]("Enter withdrawal amount: ");
double withdrawAmount = [Link]();
[Link](withdrawAmount);
break;
case 3:
[Link]();
break;
case 4:
[Link]("Exiting... Thank you!");
break;
default:
[Link]("Invalid choice! Please try again.");
}
} while (choice != 4);
[Link]();
}
}
Output:

PROGRAM: 4

OBJECTIVE: Create Java programs using inheritance and polymorphism


4.a: Write a Java program to demonstrate Compile-time Polymorphism (Method
Overloading) by creating a class Calculator that performs addition of integers, floating
values, and three numbers using overloaded methods.
Source code:
class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
public static void main(String[] args) {
Calculator cal = new Calculator();
[Link]("Addition of 2 integers: " + [Link](10, 20));
[Link]("Addition of 2 doubles: " + [Link](5.5, 2.5));
[Link]("Addition of 3 integers: " + [Link](10, 20, 50));
}
}
Output:

4.b: Write a Java program to demonstrate Single Inheritance by creating a Person class and
deriving a Student class from it, and display the details of the student using inherited
properties and methods.
Source code:
class Student {
private int rollNo;
private String name;
private double marks;
public Student(int rollNo, String name, double marks) {
[Link] = rollNo;
[Link] = name;
[Link] = marks;
}
public void display() {
[Link]("\n--- Student Details ---");
[Link]("Roll No: " + rollNo);
[Link]("Name : " + name);
[Link]("Marks : " + marks);
}
}

public class StudentDemo {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter Roll No: ");

int rollNo = [Link]();

[Link]();

[Link]("Enter Name: ");


String name = [Link]();
[Link]("Enter Marks: ");
double marks = [Link]();
Student s = new Student(rollNo, name, marks);
[Link]();
[Link]();
}
}

Output:

4.c: Write a Java program to demonstrate Runtime Polymorphism (Method Overriding) by


creating a base class Shape and derived classes Circle and Rectangle to override the area()
method and display the area of each shape.

Source code:
class Shape { public void area() {

[Link]("Area of Shape is undefined.");


}
}

class Circle extends Shape {

private double radius;

public Circle(double radius) {

[Link] = radius;

@Override public void area() {


double result = [Link] * radius * radius;
[Link]("Area of Circle: " + result);
}
}
class Rectangle extends Shape {
private double length;
private double width;
public Rectangle(double length, double width) {
[Link] = length;
[Link] = width;
}
@Override public void area() {
double result = length * width;
[Link]("Area of Rectangle: " + result);
}
}

public class ShapeDemo { public static void main(String[] args) {

Shape shape;

shape = new Circle(5.0);


[Link]();
shape = new Rectangle(4.0, 6.0);
[Link]();
}
}
Output:

4.d Write a Java program to justify that Java does not support multiple inheritance using
classes, but multiple inheritance can be achieved using interfaces by implementing more than
one interface in a single class.
Source code:

// Interface A

interface A {

void showA();

// Interface B

interface B {

void showB();

// Class implementing multiple

interfaces

class C implements A, B {

public void showA() {


[Link](“Interface A

method”);

public void showB() {

[Link]("Interface B

method");

public static void main(String[]

args) {

C obj = new C();

[Link]();

[Link]();

Output:

You might also like