0% found this document useful (0 votes)
4 views13 pages

JJ Java

The document is a worksheet for a Computer Science and Engineering course detailing various Java programming exercises. It includes source code for tasks such as summing numbers, using classes and objects, demonstrating polymorphism, and handling exceptions. Each section outlines the aim, source code, learning outcomes, and includes screenshots of code and outputs.

Uploaded by

skyuvrajsharma57
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)
4 views13 pages

JJ Java

The document is a worksheet for a Computer Science and Engineering course detailing various Java programming exercises. It includes source code for tasks such as summing numbers, using classes and objects, demonstrating polymorphism, and handling exceptions. Each section outlines the aim, source code, learning outcomes, and includes screenshots of code and outputs.

Uploaded by

skyuvrajsharma57
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

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

WORKSHEET 1

Student Name: Jug Raj Singh UID: 24BCS10745

Branch: CSE Section/Group: 708-A

Semester: 4th Date of Performance: 12/01/26

Subject Name: OOPS Using Java Subject Code: 24CSH-207

1. Aim: i) Sum Of Two Numbers

2. Source Code:
import [Link];
class AddTwoNumbers
{ static int add(int a,
int b)
{ return a + b;
}
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter value of a: ");
int a = [Link]();
[Link]("Enter value of b: ");
int b = [Link]();
int result = add(a, b);
[Link]("sum = " + result);
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

3. Screenshot of Code & Outputs:

4. Learning Outcomes
a. Understand the use of static methods in Java
b. Implement user input using the Scanner class
c. Perform basic arithmetic operations using methods
d. Demonstrate method calling and return values
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

1. Aim: ii) Sum Of First N Numbers

2. Source Code: import [Link];


class SumOfN
{ public static void
main(String[]args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter value of n: ");
int n = [Link]();
int sum = 0;
for (int i = 1; i <= n; i++)
{
sum = sum + i;
}
[Link]("Sum = " + sum);
}
}
3. Screenshot of Code & Outputs:

4. Learning Outcomes
a. Understand the use of loops in Java
b. Apply user input using the Scanner class
c. Perform cumulative arithmetic operations
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

1. Aim: iii) Java Stdin and Stdout I.

2. Source Code:
import [Link].*;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
int a = [Link]();
int b = [Link]();
int c = [Link]();
[Link](a);
[Link](b);
[Link](c);
[Link]();
}
}
3. Screenshot of Code & Outputs:

4. Learning Outcomes

a. Understand how to take integer input from the user using the Scanner
class.
b. Learn to store user input values in variables in Java.
c. Practice displaying output using [Link]().
d. Understand the basic input–process–output flow of a Java program.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

1. Aim: iv) Java Classes and object-Student


details

2. Source Code: import [Link].*;


class Student {
int id;
String name;
int age;
void displayDetails() {
[Link]("ID: " + id);
[Link]("Name: " + name);
[Link]("Age: " + age);
}
} public class Solution1 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int id = [Link]();
[Link](); // consume leftover newline
String name = [Link]();
int age = [Link]();
Student s = new Student();
[Link] = id;
[Link] = name;
[Link] = age;
[Link]();
[Link]();
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

3. Screenshot of Code & Outputs:

4. Learning Outcomes
a. Understand how to define and use a user
defined class in Java.
b. Learn to create objects and access class data
members and methods.
c. Practice taking mixed input (integer and
string) using the Scanner class.
d. Understand how to display object data using
instance methods.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

1. Aim: V) Create a class Animal with a


method makeSound() that outputs a generic
[Link] class should further contain derived
classes Dog and Cat that override the makeSound()
method to output specific sounds for each animal. Demonstrate polymorphism
by creating an Animal reference that can hold objects of both Dog and Cat,
and call the overridden makeSound() method at runtime.

2. Source Code: class Animal {


void makeSound() {
[Link]("Animal makes a sound");
}
}
class Dog extends Animal
{ void makeSound() {
[Link]("Dog barks");
}
}
class Cat extends Animal {
void makeSound() {
[Link]("Cat meows");
}
} public class Solution2 {
public static void main(String[] args) {
Animal a;
a = new Dog();
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

[Link]();
a = new Cat();
[Link]();
}
}
3. Screenshot of Code & Outputs:

4. Learning Outcomes
a. Understand the concept of inheritance using a base class and derived
classes.

b. Learn how method overriding works in Java.

c. Demonstrate runtime polymorphism using a base class reference.

d. Understand how the same method call produces different outputs for
different objects.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

1. Aim:
Vi)Write a program that takes two strings representing numbers, converts
them to wrapper classes, and performs basic arithmetic operations (addition,
subtraction, multiplication, division)
[Link] Code:
class Solution3 {
public static void main(String[] args) {
String s1 = "40";
String s2 = "20";
Integer num1 = [Link](s1);
Integer num2 = [Link](s2);
[Link]("Addition: " + (num1 + num2));
[Link]("Subtraction: " + (num1 - num2));
[Link]("Multiplication: " + (num1 * num2));
[Link]("Division: " + (num1 / num2));
}
}
3. Screenshot of Code & Outputs:

[Link] Outcomes
a. Understand the concept of inheritance using a base class and derived
classes.

b. Learn how method overriding works in Java.

c. Demonstrate runtime polymorphism using a base class reference.


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

[Link]: Vii)
Write a Java program to create an Employee class where an Overloaded
constructor initializes employee [Link] default constructor should
Initialize name as "Unknown" and salary as 0 and the second constructor
initializes the name and sets a default salary.
[Link] Code
class Employee {
String name;
double salary;
Employee() {
name = "Unknown";
salary = 0;
}
Employee(String n) {
name = n;
salary = 40000; // default salary
}
void display() {
[Link]("Name: " + name);
[Link]("Salary: " + salary);
}
} public class Solution4 {

public static void main(String[] args) {


Employee e1 = new Employee();
[Link]();
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

[Link]();
Employee e2 = new Employee("Jugraj");
[Link]();
}
}
3. Screenshot of Code & Outputs:

[Link] Outcomes
1. Understand the concept of constructor overloading in Java.
2. Learn how constructors initialize object data members.
3. Differentiate between default and parameterized constructors.
4. Understand how objects are created using different constructors.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

[Link]: Viii) Design a program for a basic banking system that calculates the
average account balance by dividing the total balance by the number of account
holders and verifies transaction IDs from an array. The program should
demonstrate robust exception handling using multiple try-catch blocks to manage
two specific scenarios: an Arithmetic Exception for division by zero when there
are no account holders and an Array Index Out Of Bounds Exception for
accessing invalid indices in the transaction ID array.
[Link] Code
class Solution5 {
public static void main(String[] args) {
double totalBalance = 50000;
int numberOfAccounts = 0;
int[] transactionIds = {101, 102, 103};
try {
double averageBalance = totalBalance / numberOfAccounts;
[Link]("Average Account Balance: " + averageBalance);
}
catch (ArithmeticException e) {
[Link]("Error: Cannot divide by zero (No account
holders).");
}
try {
[Link]("Transaction ID: " + transactionIds[5]);
}
catch (ArrayIndexOutOfBoundsException e) {
[Link]("Error: Invalid transaction index accessed.");
}
[Link]("Program execution completed.");
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

3. Screenshot of Code & Outputs:

[Link] Outcomes
1. Understand how to handle ArithmeticException when division by zero
occurs.
2. Learn to manage ArrayIndexOutOfBoundsException while accessing
array elements safely.
3. Understand the use of multiple try–catch blocks for robust exception
handling in Java.

You might also like