Remedial Tutorial 3 Solution
Remedial Tutorial 3 Solution
1) Define constructor.
Ans:
Constructor:
A constructor is a special member which initializes an object
immediately upon creation.
It has the same name as class name in which it resides, and it is
syntactically similar to any method.
When a constructor is not defined, java executes a default constructor
which initializes all numeric members to zero and other types to null or
spaces.
Once defined, constructor is automatically called immediately after the
object is created before new operator completes.
2) Write down the syntax of array declaration, initialization.
Ans:
The syntax of declaring an array in Java is given below.
datatype [ ] arrayName;
Here, the datatype is the type of element that will be stored in the
array, square bracket[] is for the size of the array, and arrayName is
the name of the array.
class arraysort
{
public static void main(String args[])
{
int a[]={85,95,78,45,12,56,78,19};
int i=0;
int j=0;
int temp=0;
int l=a.length;
for(i=0;i<l;i++)
{ //apply bubble sort
for(j=(i+1);j<l;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
System.out.println("Ascending order of numbers:");
for(i=0;i<l;i++)
System.out.println(""+a[i]);
}
}
Output:
Ascending order of numbers:
12
19
45
56
78
78
85
95
class student
{
int id;
String name;
student(int i, String n)
{
id=i;
name=n;
}
student (student s)//copy constructor
{
id=s.id;
name=s.name;
}
void display()
{
System.out.println(id+“ ”+name)
}
public static void main(String args[])
{
student s1=new student(111, “ABC”);
s1.display();
student s2= new student(s1);
s2.display();
}
}
Output
111 ABC
111 ABC
5) Explain vector with the help of example. Explain any 3 methods of vector
class.
Ans:
Example:
import java.io.*;
import java.lang.*;
import java.util.*;
class vector2
v.addElement(s1);
v.addElement(s2);
v.addElement(s3);
v.addElement(s4);
v.addElement(s5);
v.addElement(s6);
v.addElement(s7);
v.addElement(s8);
System.out.println(v);
v.removeElement(s2);
v.removeElementAt(4);
System.out.println(v);
OR
import java.util.*;
public class Main
v.addElement(new Integer(10));
v.addElement(new Integer(20));
v.addElement(new Integer(30));
v.addElement(new Integer(40));
v.addElement(new Integer(10));
v.addElement(new Integer(20));
Output:
6) Write a program to print the sum, difference, and product of two complex
numbers by creating a class named "Complex" with separate methods for
each operation whose real and imaginary parts are entered by user.
Ans: