0% found this document useful (0 votes)
57 views

Java NewGen Day 2

Here are Java programs to solve the array problems given in the lab exercise: 1. Print an array after changing rows and columns: ```java int[][] arr = {{10,20,30},{40,50,60}}; int rows = arr.length; int cols = arr[0].length; int[][] result = new int[cols][rows]; for(int i=0; i<rows; i++){ for(int j=0; j<cols; j++){ result[j][i] = arr[i][j]; } } System.out.println(Arrays.deepToString(result));

Uploaded by

Akshay Jain
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Java NewGen Day 2

Here are Java programs to solve the array problems given in the lab exercise: 1. Print an array after changing rows and columns: ```java int[][] arr = {{10,20,30},{40,50,60}}; int rows = arr.length; int cols = arr[0].length; int[][] result = new int[cols][rows]; for(int i=0; i<rows; i++){ for(int j=0; j<cols; j++){ result[j][i] = arr[i][j]; } } System.out.println(Arrays.deepToString(result));

Uploaded by

Akshay Jain
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Java Classes & Object

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

A constructor is a set of instructions designed to initialize an object of a class the


moment it gets initialized.
A constructor is a member function of a class which has the same name as the
class Name.
The basic declaration of a constructor takes the following form:
[<modifier>] <class_name> ( <argument>* )
{ <statement>* }

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

 Every class has at least one constructor.


 If the programmer does not supply any constructor, the default constructor is
present automatically.
 The characteristics of default constructor-
 It takes no arguments.
 It has empty body.
 It helps to create and initialize an object.
 Remember a Constructor can not return any
value and hence we do not write any return
statement not even void.
Programming With Java

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:-

public Book() // Class Name & Constructor Name Should be Same.


{
Publisher=“BPB”;
}

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 keyword in Java


The static keyword is used as a modifier on variables, methods, and nested classes.
The static keyword declares the attribute or method is associated with the class as a
whole rather than any particular instance of that class.
Thus, static members are often called class members, such as class attributes or class
methods.

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;

public class FirstProg {


Look How an Array
public static void main(String[] args)
{
of Object is Created
Book[] b1=new Book[2]; b1[0]=new Book(); b1[1]=new Book();
b1[0].AcceptDetail(); b1[1].AcceptDetail();
b1[0].DisplayDetail(); b1[1].DisplayDetail();
System.out.println("Total Price of All Books="+Book.totalprice);
}
}
Use of static
class Book member
{
String BookName, AuthorName,Publisher; int price; public static int totalprice=0;
public Book() { Publisher="BPB" ; }
public void AcceptDetail()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Book Name");
BookName=sc.nextLine(); System.out.println("Enter Author Name"); AuthorName=sc.nextLine();
System.out.println("Enter Price"); price=sc.nextInt(); totalprice+=price;
}
public void DisplayDetail()
{
System.out.println("Book Name ="+BookName); System.out.println("Author Name +"+AuthorName);
System.out.println("Publisher Name ="+Publisher);System.out.println("Price = "+price);
}

}
Programming With Java

final keyword in Java

The final keyword is used for security reasons.


It is used to create classes that serve as a standard.
It implements the following restrictions:
You cannot subclass a final class.
You cannot override a final method.
A final variable is a constant.
All methods and data members in a final class are implicitly final.

Trainer will Show an example of using final keyword in Java


Programming in Java

final keyword in Java


A blank final variable is a final variable that is not initialized in its declaration. The
initialization is delayed.
A blank final instance variable must be assigned in a constructor, but it can be set
once only.
A blank final variable that is a local variable can be set at any time in the body of the
method, but it can be set once only.

For e.g. suppose we have to use a constant type variable say pi


whose value we want to set as 3.14 – so in that case we can write
as follows:-
final float pi=3.14f;
Programming in Java

Method Overloading in Java


Method Overloading is a concept in Java when within a same class we are having two
methods of similar Name.
In Method Overloading the methods are identified separately by the no. of
parameters.
Constructor Overloading is a very good example of Method Overloading.

Assignment : Apply Method Overloading so that AcceptDetail() method in the


Book Class can be overloaded- however in the overloaded Method pass Price
of the Book as a Parameter.
Programming in Java

Method Overriding in Java


Method Overriding is a concept when we are maintaining same method Names in two
different classes of which one is the Parent class and the other one is the Inherited
class.
In Method Overriding the overloaded methods are identified separately by the no. of
parameters.
Method Overriding is implemented using the concept of Inheritance.

Inheritance is a topic which will be discussed later and at that time you can
know more about Method Overriding.
Programming in Java

Method Overloading & Method Overriding: Comparison


No. Method Overloading Method Overriding
1) Method overloading is used to increase the Method overriding is used to provide the
readability of the program. specific implementation of the method that is
already provided by its super class or the
Parent Class

2) Method overloading is performed within same Method overriding occurs in two classes that


class. having Inheritance associativity.

3) In case of method overloading, parameter must In case of method overriding, parameter must


be different. be same.

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

7. Write a Java program to sort array in ascending order


Expected Output:
Original array: [-2, 3, 4, -1, -3, 1, 2, -4, 0]
Result: [-4, -3, -2, -1, 0, 1, 2, 3, 4]

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

You might also like