220245-MSBTE-22412-Java (Unit 2)
220245-MSBTE-22412-Java (Unit 2)
2.1.2. Constructor
Constructor in JAVA is a special type of method that is used to initialize the object.
JAVA constructor is invoked at the time of object creation.
It constructs the values i.e. provides data for the object that is why it is known as constructor.
A constructor has same name as the class in which it resides.
Constructor in JAVA cannot be abstract, static, final or synchronized.
These modifiers are not allowed for constructor.
2. Parameterized Constructor
A constructor that have parameters is known as parameterized constructor.
Parameterized constructor is used to provide different values to the distinct objects.
Example of parameterized constructor.
class Student
{
int id;
String name;
Student(int i, String n)
{
id = i;
name = n;
}
void display( )
{
System.out.println(id+" "+name);
}
public static void main(String args[ ])
{
a=20
b=30
sum=50
3) Write a program to accept number from command line and print square root of the
number?
class clsdemo3
{
public static void main(String[] args)
{
int n= Integer.parseInt(args[0]);
double ans;
ans=Math.sqrt(n);
System.out.println("Square root of "+n+"= " +ans);
}
}
>javac clsdemo3.java
>java clsdemo3 25
Square root of 25= 5.0
Type of Array
There are two types of array.
One/ Single Dimensional Array
Two/Multidimensional Array
2.3.1. One/ Single Dimensional Array
The One dimensional array can be represented as
Array a[5]
10 20 70 40 50
0 1 2 3 4
Example
class Testarray
{
public static void main(String args[])
{
int a[ ]=new int[5]; //declaration and instantiation
a[0]=10;//initialization
a[1]=20;
a[2]=30;
a[3]=40;
a[4]=50;
//traversing array
We can declare, instantiate and initialize the java array together by:
int a[ ]={33,3,4,5}; //declaration, instantiation and initialization
Let's see the simple example to print this array.
class Testarray1
{
public static void main(String args[])
{
int a[ ]={10,20,30,40,50}; //declaration, instantiation and initialization
//printing array
for(int i=0;i<a.length; i++) //length is the property of array
System.out.println(a[i]);
}
}
Output
10
20
30
40
50
2.3.2. Two dimensional Array
In such case, data is stored in row and column based index (also known as matrix form).
Example to instantiate Multidimensional Array in Java
2.4. Strings
Definition- String is a collection of characters.
In java, the string can be defined using two commonly used methods.
1) Using String class
2) Using StringBufferClass
2.4.1. String classes
The syntax of String class is
String string_variable;
Example:-
class stringDemo
{
public static void main(String args[])
{
String s=”Welcome to java programming”;
Syste.out.println(“ ”+s);
}
}
Output
Welcome to java programming
Operation on Strings using String class.
Following are some commonly defined methods by a string class.
Method Description
s1.charAt(position) Return the character present at the index position.
s1.compareTo(s2) If s1<s2 then it returns positive. If s1>s2 then it return negative and if
s1=s2 then it returns zero.
Programs
1) Performs the following string/ string buffer operations, write java program
a) Accept a password from user
b) Check if password is correct then display “Good”, else display “Wrong”.
c) Display the password in reverse order
d) Append password with “Welcome”.
import java.util.*;
class StringBufferDemo
{
public static void main(String[] args)
{
String s="MSBTE-S-21";
Scanner sc=new Scanner(System.in);
System.out.print("Enter the password: ");
String pwd=sc.nextLine();
2) Write a simple java program to find the reverse string and check the weathered the
entered string is palindrome or not?
import java.util.*;
class ReversePalindrome
{
public static void main(String[] args)
{
String s1;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the String: ");
import java.util.*;
class VectorDemo
{
public static void main(String[] args)
{
Vector v1= new Vector(7);
v1.addElement(10);
v1.add(30);
v1.add(50);
v1.add(20);
v1.add(40);
v1.add(10);
v1.add(20);
System.out.println("The elements in the vector are:"+v1);
System.out.println("The orignal size of vector : "+v1.size());
System.out.println("Removing the Third Element ");
v1.remove(2); //3rd element
System.out.println("Removing the Fouth Element ");
v1.remove(3); //4rd element
System.out.println("Now The elements in the vector are:"+v1);
Program 2:- Write a program to add 2 integer, 2 string and 2 float objects to a vector. Remove
element specified by user and display the list.
byte Byte
char Char
double Double
float Float
int Integer
long Long
short Short
void Void
class WrapperDemo
{
public static void main(String[] args)
{
System.out.println("Integer number to string Converstion:");
int i=100;
String s=Integer.toString(i);
System.out.println("int value:"+i);
System.out.println("Equivalent to string:"+s);
System.out.println(" ");
System.out.println("String to Integer Converstion:");
String s1="200";
int n=Integer.parseInt(s1);
System.out.println("String value:"+s1);
System.out.println("Equivalent to int:"+n);
}
}
Output:-
Integer number to string Converstion:
int value:100
Equivalent to string:100
End of Unit-II