Sie sind auf Seite 1von 9

Day 2: Lab Session

Objects and Classes


Ex.1: WAP to calculate factorial of number
using class and object.
class Fact
{
int factorial(int b)
{
}
public static void main(String[] args)
{
}
}
Ex.2: WAP to calculate factorial of number
using class and object (different class).
class Fa // Factorial Classs
{
int factorial(int b)
{
}
}
class Facto // Class with Main Create Object in this class and calculate
//Factorial
{
public static void main(String[] args)
{

}
}
Ex.3: WAP to compute area and perimeter of
rectangle.
class Rect
{
}
class Rectangle //Class with Main
{
public static void main(String args[])
{
}
}
Ex.4: WAP to demonstrate constructor.
class Cons
{
Cons()
{ }
Cons(int a, int b)
{ }
}
class Const //Class with Main
{
public static void main(String args[])
{
}
}
Ex. 5: WAP to print name and id of a student
using constructor.
class Student
// Class with member variable, Constructor,
//member function and main
{

}
Ex.6: WAP to compute area of circle, rectangle
and square (Method Overriding).
class shapearea class Shape
{ {
void area(float r) public static void main(String args[ ])
{ {
} }
void area(int a, int b) }
{
}
void area(int a)
{
}
}
EX. 7: WAP to print student data(id and name)
implementing this keyword.
The this keyword is used to refer current class instance variable. If there is
ambiguity between the instance variable and parameter, this keyword
resolves the problem of ambiguity.
class Student
{
Student( )
{
}
void display()
{
}
public static void main(String args[ ])
{
}
}
Ex.8: Inner class demonstration. [member(s) (variable
and methods) can be directly accesses in
inner class but member(s) of inner class can not be
directly accessed in outer class]
class Out //System.out.print("Inner-y = "+inn_y);
{ //wrong
int out_x = 100; System.out.print("Inner-y = "+in.inn_y);
class Innr // correct
{ }
int inn_y = 200; }
class Outer
void display()
{
{
System.out.print("Outer-x = "+out_x);
public static void main(String args[])
{
}
Out o = new Out();
}
o.test();
void test() }
{ }
Innr in = new Innr();
in.display();

Das könnte Ihnen auch gefallen