0% found this document useful (0 votes)
6 views7 pages

Programming in Java (CC-502)

The document explains key concepts of Java programming, including the object-oriented paradigm, inheritance, encapsulation, polymorphism, method overriding, and abstract classes. It also provides Java programs to check if a year is a leap year, demonstrate type casting, and find the HCF and LCM of two numbers. Each program includes code snippets and outputs to illustrate their functionality.

Uploaded by

Sahad Ibrahim
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)
6 views7 pages

Programming in Java (CC-502)

The document explains key concepts of Java programming, including the object-oriented paradigm, inheritance, encapsulation, polymorphism, method overriding, and abstract classes. It also provides Java programs to check if a year is a leap year, demonstrate type casting, and find the HCF and LCM of two numbers. Each program includes code snippets and outputs to illustrate their functionality.

Uploaded by

Sahad Ibrahim
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

1) Define the following: a) Object oriented paradigm of Java programming b) Inheritance and

encapsula on c) Polymorphism and method overriding d) Abstract classes

Ans a) Object oriented paradigm of Java programming: Java is an object-oriented programming


language that uses the object-oriented paradigm. In the object-oriented paradigm, programs are
built around objects, which are instances of classes. Objects have proper es, such as data and
behavior, which are defined by the class. Java supports the four fundamental principles of object-
oriented programming: encapsula on, inheritance, polymorphism, and abstrac on. For example,
consider a class named Car that has proper es like make, model, and color, and methods like start()
and stop(). Objects of the Car class can be created to represent different cars, with different values
for their proper es.

b) Inheritance and encapsula on: Inheritance and encapsula on are two key concepts of object-
oriented programming. Inheritance is a mechanism in which one class inherits the proper es and
methods of another class. The class that inherits the proper es and methods is called the subclass or
derived class, and the class that provides the proper es and methods is called the superclass or base
class. For example, consider a superclass named Animal that has a method called speak(). A subclass
named Dog can inherit this method from the Animal class and provide its own implementa on of the
speak() method.

Encapsula on is the process of hiding the internal details of an object from the outside world. It
involves bundling data and methods into a single unit, known as a class. For example, consider a class
named BankAccount that has proper es like account number and balance, and methods like
deposit() and withdraw(). The data (account number and balance) are hidden from the outside
world, and can only be accessed through the methods provided by the class.

c) Polymorphism and method overriding: Polymorphism is the ability of objects of different classes to
be used interchangeably. In Java, polymorphism can be achieved through method overloading and
method overriding. Method overloading allows a class to have mul ple methods with the same
name but different parameters. Method overriding, on the other hand, allows a subclass to provide
its own implementa on of a method that is already defined in its superclass. For example, consider a
superclass named Shape that has a method called draw(). A subclass named Circle can override the
draw() method to provide its own implementa on of how to draw a circle.

Method overriding is a way of achieving polymorphism in Java. It is the process of providing a new
implementa on for a method that is already defined in a superclass. When a method is called on an
object of the subclass, the subclass's implementa on of the method is used instead of the
superclass's implementa on. This allows different objects to respond to the same method call in
different ways, depending on their class. For example, consider a superclass named Vehicle that has a
method called drive(). A subclass named Car can override the drive() method to provide its own
implementa on of how a car should be driven.
d) Abstract classes: Abstract classes are classes that cannot be instan ated directly. They are
designed to be extended by other classes and provide a set of methods that the subclass must
implement. Abstract classes can contain both abstract and non-abstract methods. Abstract methods
are declared but not implemented, and must be implemented by the subclass. Non-abstract
methods, on the other hand, are implemented in the abstract class and can be called by the subclass.
For example, consider an abstract class named Animal that has an abstract method called
makeSound(). A subclass named Cat can extend the Animal class and implement its own version of
the makeSound() method.

2) A Java Program to Check whether an input (in year format) is a Leap Year or not

import java.u [Link];

public class LeapYear {

public sta c void main(String[] args) {

// Create a scanner object to read input from the console

Scanner scanner = new Scanner([Link]);

// Prompt the user to enter a year

[Link]("Enter a year: ");

// Read the input from the user

int year = [Link]();

// Check if the year is a leap year

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {

[Link](year + " is a leap year.");

} else {

[Link](year + " is not a leap year.");

}
// Close the scanner object

[Link]();

OUTPUT :-

Enter a year: 2000

2000 is a leap year.

Enter a year: 2001

2001 is not a leap year.

Here's how the program works:

We create a Scanner object to read input from the console.

We prompt the user to enter a year and read the input from the user.

We check if the year is a leap year using the following condi ons:

If the year is divisible by 4 and not divisible by 100, or

If the year is divisible by 400

If the year is a leap year, we print a message saying so. Otherwise, we print a message saying it's not
a leap year.

We close the Scanner object to prevent any resource leaks.

3) Write a JAVA program to: a) To Demonstrate Type Cas ng b) To Find out the HCF and LCF
a)

public class TypeCas ngDemo {

public sta c void main(String[] args) {

// Widening type cas ng (implicit)

int i = 10;

long l = i;

float f = l;

[Link]("Widening Type Cas ng:");

[Link]("int value: " + i);

[Link]("long value: " + l);

[Link]("float value: " + f);

// Narrowing type cas ng (explicit)

double d = 10.5;

float g = (float) d;

long h = (long) g;

int j = (int) h;

[Link]("Narrowing Type Cas ng:");

[Link]("double value: " + d);

[Link]("float value: " + g);

[Link]("long value: " + h);

[Link]("int value: " + j);

OUTPUT:-
Widening Type Cas ng:int value: 10

long value: 10

float value: 10.0

Narrowing Type Cas ng:

double value: 10.5

float value: 10.5

long value: 10

int value: 10

This program demonstrates both widening and narrowing type cas ng. Widening type cas ng is
done implicitly, while narrowing type cas ng is done explicitly using a cast operator.

b)

import java.u [Link];

public class HCFandLCM {

public sta c void main(String[] args) {

// Create a scanner object to read input from the console

Scanner scanner = new Scanner([Link]);

// Prompt the user to enter the two numbers

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

int num1 = [Link]();

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

int num2 = [Link]();

// Find the HCF using the Euclidean algorithm

int hcf = 0;
int a = [Link](num1, num2);

int b = [Link](num1, num2);

while (b != 0) {

int remainder = a % b;

a = b;

b = remainder;

hcf = a;

// Find the LCM using the formula: LCM = (num1 * num2) / HCF

int lcm = (num1 * num2) / hcf;

// Print the results

[Link]("The HCF of " + num1 + " and " + num2 + " is " + hcf);

[Link]("The LCM of " + num1 + " and " + num2 + " is " + lcm);

// Close the scanner object

[Link]();

OUTPUT:-

Enter first number: 50

Enter second number: 40

The HCF of 50 and 40 is 10

The LCM of 50 and 40 is 200

2
Enter first number: 500

Enter second number: 250

The HCF of 500 and 250 is 250

The LCM of 500 and 250 is 500

This program uses the Euclidean algorithm to find the HCF of two numbers, and then uses the
formula LCM = (num1 * num2) / HCF to find the LCM. The program first prompts the user to enter
the two numbers, and then computes and prints the HCF and LCM.

You might also like