0% found this document useful (0 votes)
10 views

New Chapter2-Programms

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

New Chapter2-Programms

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

/* Write a program to calculate the area of rectangle */

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

public static void main (String args[ ])


{
Book b = new Book();
b. getdata(“Java Programming”, “Gaikwad”, 199);
b. putdata ();
}
}

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

/* program to demonstrate the parameterized constructor */

public class Cube1


{
int length;
int breadth;
int height;
public int getVolume()
{
return(length * breadth * height);
}
Cube1( )
{
length = 10;
breadth = 10;
height = 10;
}
Cube1(int l, int b, int h)
{
length = l;
breadth = b;
height = h;
}
public static void main (String args[ ])
{
Cube1 cubeObj1, cubeObj2;
6
cubeObj1 = new Cube1();
cubeObj2 = new Cube1(10, 20, 30) ;
System.out.println (“Volume of Cube1 is : “ + cubeObj1. getVolume());
System.out.println (“Volume of Cube1 is : “ + cubeObj2. getVolume());
}
}

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

public static void main (String args[ ])


{
Cube2 cubeObj1, cubeObj2;
cubeObj1 = new Cube2 ();
cubeObj2 = new Cube2 (10, 20, 30) ;
System.out.println (“Volume of Cube2 is : “ + cubeObj1. getVolume());
System.out.println (“Volume of Cube2 is : “ + cubeObj2. getVolume());
}
}

You might also like