New Chapter2-Programms
New Chapter2-Programms
class Rectangle
{
int length, width;
void getdata (int a, int b)
{
length = a;
width = b;
}
int recArea ( )
{
int area = length * width;
return area; /*function is returning value*/
}}
// Define a class with main method.
class RectArea
{
public static void main (String args[ ])
{
int area1;
Rectangle Rec1 = new Rectangle ( );
// Accessing member variables using function
Rec1.getdata (20, 15);
area1 = Rec1. recArea ( );
System.out.println (“Area1 = “+ area1);
}
1
}
/* Write a program to create a class Book having data members as title, author
and price. Accept and display data for one object */
class Book
{
String title, author;
double price;
void getdata (String s, String s1, double d)
{
title = s;
author = s1;
price = d;
}
void putdata ()
{
System.out.println (“Title :- “ + title);
System.out.println(“Author:- “ + author);
System.out.println(“Price :- “ + price);
}
2
/* Define a class Rectangle having data members length and breadth. Accept
and display data for three objects of the class using array of object */
public class Rectangle {
int length, breadth;
void accept(int a1,int a2)
{
length =a1;
breadth = a2;
}
void display()
{
System.out.println("Length : - " + length+" and Breadth : - " + breadth );
}
public static void main (String args[ ]) {
Rectangle arr[] = new Rectangle[3];
for(int i=0; i<3; i++)
{
arr[i] = new Rectangle ();
arr[i].accept(12,14);
arr[i].display( );
}
}
}
3
/* program to shows defining and using static members*/
class MathOperation
{
static int I =10;
static float mul ( float a, float b)
{
I=70;
return (a * b);
}
static float divide ( float a, float b)
{
return (a / b);
}
}
class MathApplications
{
public static void main (String args[ ])
{
float y= (float)MathOperation.mul ((float)4.0, (float)5.0);
float y1= (float)MathOperation. divide ((float)4.5,(float) 2.0);
System.out.println (“y = “ +y);
}
}
4
/* Write a program to calculate the area of rectangle by using constructor
method to initialize an object at the time of its creation*/
class Rectangle
{
int length, width;
// Define constructor
Rectangle (int a, int b)
{
length = a;
width = b;
}
int RectArea ( )
{
return (length * width);
}
}
// class with main ( )
class RectangleArea
{
public static void main (String args[ ])
{
// Calling constructor
5
Rectangle R1 = new Rectangle (15, 11 );
int area1 = R1. RectArea ( );
System.out.println (“Area1 = “+ area1);
}
}
7
/* program to demonstrate the this() method in Constructor context (cube
class containing 3 Constructor s)*/
public class Cube2
{
int length;
int breadth;
int height;
public int getVolume()
{
return(length * breadth * height);
}
Cube2( )
{
this(10, 10);
System.out.println (“Finished with Default Constructor”);
}
Cube2(int l, int b)
{
this(l, b, 10);
System.out.println (“Finished with Parameterized Constructor having 2
params”);
}
Cube2(int l, int b, int h)
{
length = l;
breadth = b;
height = h;
8
System.out.println (“Finished with Parameterized Constructor having 3
params”);
}