Sie sind auf Seite 1von 5

Annexure I

Homework Title/No. : 1 Course Code : CSE 310

Course Instructor : MR. Amandeep Nagpal

Date of Allotment : 21/01/2011 Date of submission : 02/02/2011

Student’s Roll No. : RB27T3B39 Section No. : DE514

Declaration :

I declare that this assignment is my individual work. I have not copied from any
other student’s work or from any other source except where due acknowledgement
is made explicitly in the text,nor has any part been written for me by another
person.

Student’s Signature : Devyani Thakur

PART A
Q1. Write a program in Java to find the roots of a quadratic equation.
Ans) program:
class Quadratic
{
public static void findRoots(double a, double b, double c)
{
double disc;
disc = (b * b) - (4 * a * c);
if ( disc > 0 )
{
double r1 = (- b + Math.sqrt(disc)) / (2 * a);
double r2 = (- b - Math.sqrt(disc)) / (2 * a);
System.out.println("The roots are unequal and irrational " + r1 + " and " + r2);
}
else if ( disc == 0 )
{
double r = - b / (2 * a);
System.out.println("The roots are equal and rational " + r + " and " + r );
}
else if ( disc < 0 )
{
System.out.println("The roots are imaginary");
}
}
}

Q2. Is it possible to save a Java program with a different name than the name of the public class
that contains main method? Will that program compile and execute? Explain the scenario with an
example.
Ans) Its not possible to save java program with a different name than the name of the public
class that contains main method.
Example:
Class Test
{
Public static void main(String args[])
{
System.out.println(“hello”);
}
}

If we save this program with the name hello.java, then the program will compile successfully but
when we will run the program then an error will occur. This error is Exception in thread
"main" java.lang.NoClassDefFoundError: hello

Q3. What are nested loops? How nested loops are executed in java? Give examples.
Ans) example:
Class Factorial
{
public static void main(String args[])
{
int n= 5, fact;
for (int i = 1; i <= n; i++)
{
fact = 1;
for (int f = 2; f <= i; f++)
{
fact *= f;
}
System.out.println(i + "! is " + fact);
}
}
}

In the above program the inner for loop will execute till the condition is satisfied. After the inner
loop condition fails then the outer loop will start executing and again the inner loop will start
executing. This process will carry on until the condition of the outer loop fails. In this way the
nested loops work.

PART B
Q4. How does Java support polymorphism over methods? Write a program to explain the same
by finding area of various shapes in a single class.
Ans) program:
class Shape
{
int area;
void square(int a)
{
area= a*a;
System.out.println(area);
}
void circle(int r)
{
area= 3.14*(r*r);
System.out.println(area);
}
}

class Result
{
public static void main(String args[])
{
Shape s1=new Shape();
Shape s2=new Shape();
s1.square(3);
s2.circle(2);
}
}

In the above program, Shape class has two member functions that have same name, but they are
calling according to the parameters passed to the objects. So in this way the function overloading
occur that depends upon the parameters passed during the calling of the function. This is known
as polymorphism.

Q5. How a constructor is different from a method which is the member of the class? Can
you have more than one constructor in a class? With the help of a program example
classify various constructors.
Ans)

 There is no return type given in a constructor signature (header). The value is this object
itself so there is no need to indicate a return value.
 There is no return statement in the body of the constructor.
 The first line of a constructor must either be a call on another constructor in the same
class (using this), or a call on the superclass constructor (using super). If the first line is
neither of these, the compiler automatically inserts a call to the parameterless super class
constructor.

Yes we can have more then one constructor in a class.

Example:

class Cube
{

int l;
int b;
int h;
public int Volume()
{
return (l*b*h);
}
Cube() -------------------------------//Default constructor
{
l = 10;
b = 10;
h = 10;
}
Cube(int x, int y, int z) -------------// Parameterized constructor
{
l = x;
b = y;
h = z;
}
public static void main(String[] args)
{
Cube Obj1,Obj2;
Obj1 = new Cube();
Obj2 = new Cube(10, 20, 30);

System.out.println("Volume of Cube is : " +Obj1.Volume());


System.out.println("Volume of Cube is : " +Obj2.Volume());
}
}
Q6. Write a program in java to find the H.C.F of two numbers using Recursion.

class Hcf

{
public static int calc(int a, int b)
{
if (a == 0)
{
return a;
}
if (b == 0)
{
return b;
}
return calc(b,a%b);
}
public static void main(String args[])
{
int a = 4, b=8;
System.out.print("The hcf of " + a + " and "+ b +" is " + hcf(a,b) );
}
}

Das könnte Ihnen auch gefallen