Professional Documents
Culture Documents
G COLLEGE
An Autonoumous,CPE and NAAC Grade ‘A’
PROJECT FILE
Object
Class
Inheritance
Polymorphism
Abstraction
Encapsulation
Example: A dog is an object because it has states like color, name, breed, etc. as well
as behaviors like wagging the tail, barking, eating, etc.
Class
Collection of objects is called class. It is a logical entity.
A class can also be defined as a blueprint from which you can create an individual
object. Class doesn't consume any space
Inheritance
Polymorphism
If one task is performed in different ways, it is known as polymorphism. For example:
to convince the customer differently, to draw something, for example, shape, triangle,
rectangle, etc.
Abstraction
Hiding internal details and showing functionality is known as abstraction. For
example phone call, we don't know the internal processing.
Encapsulation
Binding (or wrapping) code and data together into a single unit are known as
encapsulation. For example, a capsule, it is wrapped with different medicines.
A java class is the example of encapsulation. Java bean is the fully encapsulated class
because all the data members are private here.
Coupling
Coupling refers to the knowledge or information or dependency of another class. It
arises when classes are aware of each other. If a class has the details information of
another class, there is strong coupling. In Java, we use private, protected, and public
modifiers to display the visibility level of a class, method, and field.
Cohesion
Cohesion refers to the level of a component which performs a single well-defined
task. A single well-defined task is done by a highly cohesive method. The weakly
cohesive method will split the task into separate parts. The java.io package is a highly
cohesive package because it has I/O related classes and interface. However, the
java.util package is a weakly cohesive package because it has unrelated classes and
interfaces.
Association
Association represents the relationship between the objects. Here, one object can be
associated with one object or many objects. There can be four types of association
between the objects:
One country can have one prime minister (one to one), and a prime minister can have
many ministers (one to many). Also, many MP's can have one prime minister (many
to one), and many ministers can have many departments (many to many)
Aggregation
Aggregation is a way to achieve Association. Aggregation represents the relationship
where one object contains other objects as a part of its state. It represents the weak
relationship between objects. It is also termed as a has-a relationship in Java. Like,
inheritance represents the is-a relationship. It is another way to reuse objects.
Composition
The composition is also a way to achieve Association. The composition represents the
relationship where one object contains other objects as a part of its state. There is a
strong relationship between the containing object and the dependent object. It is the
state where containing objects do not have an independent existence. If you delete the
parent object, all the child objects will be deleted automatically.
3) OOPs provides the ability to simulate real-world event much more effectively. We
can provide the solution of real word problem if we are using the Object-Oriented
Programming language.
Library Functions:- These are the inbuilt functions present in Java library classes,
provided by Java system to help programmers to perform their task in an easier way.
Java.lang is for all the basic classes that are actually imported automatically
(implicitly) because it is all the basic ones (String, Integer, Double, etc)
Java.util contains all your data structures you learned in school and more.
Read the documentation, and the more you know and practice, the better
Java.io for file reading. Look into java.util.Scanner for simple file reading, but
for any more complicated, low level file reading info, use its built for efficiency,
while Scanner is for simplicity
Java.math if you ever need to use arbitrary precision values (built-in in
python, not in java)
Java.net for sockets, connections, etc
Java.swing for GUI, which is an extension of the older java.awt
EXAMPLE
class Animal {
class Main {
public static void main(String[] args) {
}
}
output
My name is Rohu
I can eat
RECURSION
A method that calls itself is known as a recursive method. And, this process
is known as recursion.
FOR EXAMPLE
1) public class Recursion1
{
static int count=0;
static void p(){
count++;
if(count<=5){
System.out.println("hello "+count);
p();
}
}
public static void main(String[] args) {
p();
}
}
2) public class Recursion2
{
static int factorial(int n){
if (n == 1)
return 1;
else
return(n * factorial(n-1));
}
public static void main(String[] args) {
System.out.println("Factorial of 5 is: "+factorial(5));
}
}
Factorial of 5 is: 120
Abstract classes allow you to create blueprints for concrete classes. But the
inheriting class should implement the abstract method.
Function Overloading in Java occurs when there are functions having the
same name but have different numbers of parameters passed to it, which can
be different in data like int, double, float and used to return different values
are computed inside the respective overloaded method. Function
overloading is used to reduce complexity and increase the efficiency of the
program by involving more functions that are segregated and can be used to
distinguish among each other with respect to their individual functionality.
Overloaded functions are related to compile-time or static polymorphism.
There is also a concept of type conversion, which is basically used in
overloaded functions used to calculate the conversion of type in variables.
EXAMPLE:-
//Java program with overloaded main()
import java.io.*;
public class Test
{
// First main method which is created
public static void main(String[] args)
{
System.out.println("Main function called");
Test.main("Everyone");
}
// First overloaded main method
public static void main(String ar1)
{
EXCEPTION HANDELLING
An exception is an unexpected event that occurs during program execution.
It affects the flow of the program instructions which can cause the program
to terminate abnormally.
Note that the hierarchy splits into two branches: Error and Exception.
Errors
Exceptions
RuntimeException
IOException
An IOException is also known as a checked exception. They are
checked by the compiler at the compile-time and the programmer is
prompted to handle these exceptions.
try...catch block
finally block
throw and throws keyword
1. try...catch block
try {
// code
}catch(Exception e) {
// code
Here, we have placed the code that might generate an exception inside
the try block. Every try block is followed by a catch block.
EXAMPLE
class Main {
try {
int divideByZero = 5 / 0;
catch (ArithmeticException e) {
2. finally block
try {
//code
// catch block
}finally {
EXAMPLE
class Main {
public static void main(String[] args) {
try {
class Main {
// throw an exception
divideByZero();
at Main.divideByZero(Main.java:5)
at Main.main(Main.java:9)
Example:
import java.io.*;
class Main {
try {
findFile();
catch (IOException e) {
System.out.println(e);
Output
program.
char ch = 'a';
int ascii = ch;
// You can also cast char to int
int castAscii = (int) ch;
System.out.println("--Before swap--");
System.out.println("First number = " + first);
System.out.println("Second number = " + second);
System.out.println("--After swap--");
System.out.println("First number = " + first);
System.out.println("Second number = " + second);
}
}
if(num % 2 == 0)
System.out.println(num + " is even");
else
char ch = 'i';
}
}
// value a, b, and c
double a = 2.3, b = 4, c = 5.6;
double root1, root2;
// year to be checked
int year = 1996;
boolean leap = false;
// year to be checked
int year = 1996;
boolean leap = false;
char c = 'A';
String output = (c >= 'a' && c <= 'z') || (c >= 'A' && c <=
'Z')
? c + " is an alphabet."
: c + " is not an alphabet.";
System.out.println(output);
}
}
int num = 5;
for(int i = 1; i <= 10; ++i)
{
System.out.printf("%d * %d = %d \n", num, i, num * i);
}
}
}
// Always true
while(true) {
if( lcm % n1 == 0 && lcm % n2 == 0 ) {
System.out.printf("The LCM of %d and %d is %d.", n1, n2,
lcm);
break;
}
++lcm;
}
char c;
while (num != 0) {
// num = num/10
num /= 10;
++count;
}
long result = 1;
while (exponent != 0) {
result *= base;
--exponent;
}
if (str.toLowerCase().equals(reverseStr.toLowerCase())) {
System.out.println(str + " is a Palindrome String.");
}
else {
System.out.println(str + " is not a Palindrome String.");
}
}
}
if (!flag)
System.out.println(num + " is a prime number.");
else
System.out.println(num + " is not a prime number.");
}
}
++low;
}
}
}
System.out.println();
}
}
}
while (k != 2 * i - 1) {
System.out.print("* ");
++k;
}
System.out.println();
}
}
}
System.out.printf("%4d", coef);
}
System.out.println();
}
}
}