Wednesday, March 4, 2015

Class with Instance and Method

Write a program where you have to assign a class called Circle is to be defined as illustrated in the class diagram below. It contains two variables: radius (of type double) and color (of type String); and three methods: getRadius(), getColor(), and getArea().
Three instances of Circles called c1, c2, and c3 shall then be constructed with their respective data members, as shown in the instance diagrams.

Source Code:

package crl;
class circle
{
double radius;
String colour;
circle(double r,String c)
{
radius=r;
colour=c;
}
double getRadius()
{
return radius;
}
String getColour()
{
return colour;
}
double getArea()
{
return 3.1416*radius*radius;
}
}
public class CRL
{
public static void main(String[] args)
{
circle c1=new circle(3.4,"Pink");
circle c2=new circle(1.1,"white");
circle c3=new circle(1.1,"blue");
System.out.println("Radius :"+c1.getRadius() + "Colour :"+c1.getColour()+"Area:"+c1.getArea());
System.out.println("Radius :"+c2.getRadius() + "Colour :"+c2.getColour()+"Area:"+c2.getArea());
System.out.println("Radius :"+c3.getRadius() + "Colour :"+c3.getColour()+"Area:"+c3.getArea());
}
}

Thursday, December 19, 2013

Constructor with Overloading in JAVA : Day 8

          a) How to Overload a Constructor? Demonstrate with example.
    
      b) How to use Object as Parameter? Give example.

Tuesday, December 17, 2013

Stack Programming using consturctor in JAVA : Day 7


 
 Write a program to see a practical application of the JAVA CLASS, let’s develop the Stack. A stack stores data using first-in, last-out ordering. That is, a stack is like a stack of plates on a table. The first plate put down on the table is the last plate to be used. Stacks are controlled through two operations traditionally called push and pop. Use the Class name for this program as Stack, Stack() as constructor, push() and pop() as method.

Keyword and Constructor in JAVA : Day 6


a) What is this Keyword?

b) Write any program using Parameter with Constructor.

Tuesday, December 10, 2013

Constructor in JAVA : Day 5



 a) What happened when a constructor initialized in an Object?


b) Write a program to find the volume of a Box. Use Constructor to initialize the dimensions of a Box. Compute and Return the result using Method.

Method with return type in JAVA : Day 4



a) How Method return a value? Write necessary Syntax.

b) Write any JAVA program that takes parameters in a Method.

Method in JAVA : Day 3

a) How to access the instance variable using Method?


b) Write a program to calculate the Volume of a Box using Method.

Sunday, December 8, 2013

Object in JAVA : Day 2


a) What is Object? How to declare an Object in a JAVA program? Explain the Syntax of Object Declaration.

b) Write Syntax to assign an Object Reference Variable.

Friday, December 6, 2013

Class in JAVA : Day 1


  Explain the General Form of a Class.   --------------------------------------------------------------------------------------------------------------------------
Write a program to find the volume of a Box. Define only three instance variables: width, height and depth. Do not use Methods.
-----------------------------------------------------------------------------------------------------------------------------
package volbox;
class Bigbox
{
   double width;
   double depth;
   double height;
}
public class Volbox
{
  public static void main(String[] args)
  {
    Bigbox lbox=new Bigbox();
    lbox.width=15;
    lbox.depth=8;
    lbox.height=20;
    double vol=lbox.width * lbox.depth * lbox.height;
    System.out.println("Volume of box is:" +vol);
  }
}