Java NewGen Day 2
Java NewGen Day 2
What is a Class?
A class represents a general model basis on which we create some real life
objects. For example when we say that “We are coming By Car” then we don’t
specify the detail of the car and yet people understand that A car means it must
be a 4 wheeler ,it must have seating arrangements inside it, it must be running
on fuel…etc. So the word car may represent a general model.
What is an object?
An Object is referred as a real life example of a class. In the above example if we
say “We are Coming by Bolero Car” so here we are not specifying a general
model; instead we are actually identifying a particular type of a car and in this
scenario it will be an Object.
Programming With Java
Constructor
Role of a Constructor
Remember even if You construct a class without any Constructor but while
compiling the program compiler automatically adds one default constructor
for each class.
Programming With Java
Constructor
Constructor
So let us try to understand the working of Book class created on the previous day. Now
suppose all the Books are published by a common publisher say it is “BPB” – then do
you think that there is a need of entering the Publisher Name for each & every Book ?
Or rather it will be convenient to apply a constructor so that whenever an object of
Book class is created automatically we can store “BPB” with the class member variable
publisher. So in the Book class we can implement a constructor as follows:-
Since the constructor is not taking any value as a parameter so it must be treated as a
default constructor of the class.
Programming With Java
Constructor
But Then what will Happen if we have some Books which are pubished by some other
Publishers. In that case the default constructor will not be helpful so we can construct
another constructor where the name of the publisher can be sent as a parameter- in
that case it is called as Constructor Overloading means A class is having more than one
constructor.
public Book() // Class Name & Constructor Name Should be Same.
{
publisher=“BPB”;
}
public Book(String pname)
{ publisher=pname;
}
In the above case the second constructor is also known as overloaded constructor or
parameterised constructor.
Programming in Java
Static attribute:
A public static class attribute can be accessed from outside the class without an instance of
the class.
Static method:
A static method can be invoked without creating the instance of the class.
Static methods can not access instance variables.
Static initializers:
A class can contain code in a static block that does not exist within a method body.
Static block code executes once only, when the class is loaded.
Usually, a static block is used to initialize static (class) attributes.
Programming With Java
import java.util.Scanner;
}
Programming With Java
Inheritance is a topic which will be discussed later and at that time you can
know more about Method Overriding.
Programming in Java
4) Method overloading is the example of compile Method overriding is the example of run time
time polymorphism. polymorphism.
Programming With Java
Lab Exercise :
1. Write a Java program to print an array after changing the rows and columns of a given two-
dimensional array.
Original Array:
10 20 30
40 50 60
After changing the rows and columns of the said array:
10 40
20 50
30 60
2. Write a Java program to find the k largest elements in a given array. Elements in the array can
be in any order.
Expected Output:
Original Array:
[1, 4, 17, 7, 25, 3, 100]
3 largest elements of the said array are:
100 25 17
3. Write a Java program to find the k smallest elements in a given array. Elements in the array
can be in any order.
Expected Output:
Original Array:
[1, 4, 17, 7, 25, 3, 100]
3 smallest elements of the said array are:
Programming With Java
Lab Exercise :
4. Write a Java program to find the kth smallest and largest element in a given array. Elements
in the array can be in any order.
Expected Output:
Original Array:
[1, 4, 17, 7, 25, 3, 100]
Enter value of K :
2
K'th smallest element of the said array:
3
K'th largest element of the said array:
25
5. Write a Java program to find the numbers greater than the average of the numbers of a given
array.
Expected Output:
Original Array:
[1, 4, 17, 7, 25, 3, 100]
The average of the said array is: 22.0
The numbers in the said array that are greater than the average are:
25
100
Programming With Java
Lab Exercise :
6. Write a Java program that will accept an integer and convert it into a binary representation.
Now count the number of bits which is equal to zero of the said binary representation.
Expected Output:
Input first number: 25
Binary representation of 25 is: 11001
Number of zero bits: 2
8. Write a Java program to reverse the content of a sentence (assume a single space between
two words) without reverse every word.
Input a string: The quick brown fox jumps over the lazy dog
Result: dog lazy the over jumps fox brown quick The
9. Write a Java program to accept two string and test if the second string contains the first one.
Input first string: Once in a blue moon
Input second string: See eye to eye
If the second string contains the first one? false
Programming With Java
End of Day 2