0% found this document useful (0 votes)
2 views8 pages

Remedial Tutorial 3 Solution

The document provides a remedial tutorial on derived syntactical constructs in Java, covering topics such as constructors, array declaration and initialization, sorting arrays, copy constructors, and the Vector class. It includes example programs demonstrating sorting, copy constructors, and operations on complex numbers. Additionally, it explains various methods of the Vector class and provides example outputs for clarity.

Uploaded by

patilvedant386
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views8 pages

Remedial Tutorial 3 Solution

The document provides a remedial tutorial on derived syntactical constructs in Java, covering topics such as constructors, array declaration and initialization, sorting arrays, copy constructors, and the Vector class. It includes example programs demonstrating sorting, copy constructors, and operations on complex numbers. Additionally, it explains various methods of the Vector class and provides example outputs for clarity.

Uploaded by

patilvedant386
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

REMEDIAL TUTORIAL: - 3 SOLUTION

CHAPTER 02:- DERIVED SYNTACTICAL CONSTRUCTS IN JAVA

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.

The syntax of initializing an array is given below.


datatype [ ] arrayName = new datatype [ size ];

3) Write a program to sort the elements of an array in ascending order.


Ans:

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

4) Write a program to show the use of copy constructor.


Ans:

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:

 Vector class is in java.util package of java.


 Vector is dynamic array which can grow automatically according to
the requirement.
 Vector does not require any fix dimension like String array and int
array.
 Vectors are used to store objects that do not have to be
homogeneous.
 Vector contains many useful methods.
Vectors are created like arrays. It has three constructor methods.

 Vector list = new Vector(); //declaring vector without size


 Vector list = new Vector(3); //declaring vector with size
 Vector list = new Vector(5,2); //create vector with initial size
and whenever it need to grows, it grows by value specified by
increment capacity
Vector Methods -

Methods Task performed

firstElement() It returns the first element of the vector.


lastElement() It returns last element.
addElement(item) Adds the item specified to the list at the end.
elementAt(int index) Gives the name of the object present at index position.
size() Gives the number of objects present.
setSize(int newSize) Sets the size of this vector.
Set(int index, object
Replaces the element at the specified position in the
element) vector with the specified element.
capacity() Returns the current capacity of this vector. By default
capacity is 10 for vector.
Boolean Contains(object Tests if the specified object is a component in the
elem) vector.
clear() Removes all of the elements from this vector
remove(Object o) Removes the first occurrence of the specified element
in this vector, If the vector does not contain the
element, it is unchanged.

remove(int index) Removes the element at the specified position in this


vector.

Example:
import java.io.*;

import java.lang.*;

import java.util.*;
class vector2

public static void main(String args[])

vector v=new vector();

Integer s1=new Integer(1);

Integer s2=new Integer(2);

String s3=new String("fy");

String s4=new String("sy");

Character s5=new Character('a');

Character s6=new Character('b');

Float s7=new Float(1.1f);

Float s8=new Float(1.2f);

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

public static void main(String args[])

Vector v = new Vector();

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));

System.out.println(v.size()); // display original size

System.out.println("Initial Vector: " + v);

v.removeElementAt(2); // remove 3rd element

System.out.println("Current Vector: " + v);

v.removeElementAt(3); // remove 4th element

System.out.println("Current Vector: " + v);

v.insertElementAt(11,2); // new element inserted at 3rd position

System.out.println("Current Vector: " + v);

System.out.println("Size of vector after insert delete operations: "


+ v.size());

Output:

Initial Vector: [10, 20, 30, 40, 10, 20]

Current Vector: [10, 20, 40, 10, 20]


Current Vector: [10, 20, 40, 20]

Current Vector: [10, 20, 11, 40, 20]

Size of vector after insert delete operations: 5

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:

// Java program to add and subtract two


// complex numbers using Class
import java.util.*;
// User Defined Complex class
class Complex
{
// Declaring variables
int real, imaginary;
// Empty Constructor
Complex()
{
}
// Constructor to accept real and imaginary part
Complex(int tempReal, int tempImaginary)
{
real = tempReal;
imaginary = tempImaginary;
}
// Defining addComp() method for adding two complex number
Complex addComp(Complex C1, Complex C2)
{
// creating temporary variable
Complex temp = new Complex();
// adding real part of complex numbers
temp.real = C1.real + C2.real;
// adding Imaginary part of complex numbers
temp.imaginary = C1.imaginary + C2.imaginary;
// returning the sum
return temp;
}
// Defining subtractComp() method for subtracting two complex number
Complex subtractComp(Complex C1, Complex C2)
{
// creating temporary variable
Complex temp = new Complex();
// subtracting real part of complex numbers
temp.real = C1.real - C2.real;
// subtracting Imaginary part of complex numbers
temp.imaginary = C1.imaginary - C2.imaginary;
// returning the difference
return temp;
}
Complex productComp(Complex C1, Complex C2)
{
// creating temporary variable
Complex temp = new Complex();
// product of of complex numbers (a + ib) (c + id)= (ac - bd) +
i(ad + bc).
temp.real = ((C1.real*C2.real)-(C1.imaginary*C2.imaginary));
temp.imaginary = ((C1.real*C2.imaginary) +
(C1.imaginary*C2.real));
// returning the difference
return temp;
}
// Function for printing complex number
void printComplexNumber()
{
System.out.println("Complex number: " + real + " + " +
imaginary + "i");
}
}
// Main Class
public class Main
{
// Main function
public static void main(String[] args)
{
// First Complex number
Complex C1 = new Complex(3, 2);
// printing first complex number
C1.printComplexNumber();
// Second Complex number
Complex C2 = new Complex(9, 5);
// printing second complex number
C2.printComplexNumber();
// for Storing the sum
Complex C3 = new Complex();
// calling addComp() method
C3 = C3.addComp(C1, C2);
// printing the sum
System.out.print("Sum of : ");
C3.printComplexNumber();
// calling subtractComp() method
C3 = C3.subtractComp(C1, C2);
// printing the difference
System.out.print("Difference of : ");
C3.printComplexNumber();
// calling productComp() method
C3 = C3.productComp(C1, C2);
// printing the product
System.out.print("product of : ");
C3.printComplexNumber();
}
}
OUTPUT:
Complex number: 3 + 2i
Complex number: 9 + 5i
Sum of Complex number: 12 + 7i
Difference of Complex number: -6 + -3i
product of Complex number: 17 + 33i

You might also like