0% found this document useful (0 votes)
5 views20 pages

Java Constructor and Method Examples

The document contains a series of Java programs demonstrating various programming concepts such as constructors, method calls, recursion, object references, and control flow statements like if-else and switch-case. Each program showcases different functionalities, including volume calculations for boxes, primitive type operations, and looping constructs. The collection serves as a comprehensive overview of basic Java programming techniques and principles.

Uploaded by

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

Java Constructor and Method Examples

The document contains a series of Java programs demonstrating various programming concepts such as constructors, method calls, recursion, object references, and control flow statements like if-else and switch-case. Each program showcases different functionalities, including volume calculations for boxes, primitive type operations, and looping constructs. The collection serves as a comprehensive overview of basic Java programming techniques and principles.

Uploaded by

palak.kumari1025
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PROGRAM 1:

class Pradeep{
Pradeep(){
[Link]("Pradeep");
}
Pradeep(int i){
[Link]("Pradeep");
}
}

class MainClass{
public static void main(String[] args){
Pradeep Pr=new Pradeep();
Pradeep Pr1=new Pradeep(1);
}
}

PROGRAM 2:
// Primitive types are passed by value.
class Test {
void meth(int i, int j) {
i *= 2;
j /= 2;
}
}
class CallByValue {
public static void main(String args[]) {
Test ob = new Test();
int a = 15, b = 20;
[Link]("a and b before call: " + a + " " + b);
[Link](a, b);
[Link]("a and b after call: " + a + " " + b);
}
}
PROGRAM 3:
/* Here, Box defines three constructors to initialize the dimensions of a box various ways. */
class Box {
double width;
double height;
double depth;
// constructor used when all dimensions specified
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions specified
Box() {
width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box
}
// constructor used when cube is created
Box(double len) {
width = height = depth = len;
}
// compute and return volume
double volume() {
return width * height * depth;
}
}
class OverloadCons {
public static void main(String args[]) {
// create boxes using the various constructors
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);
double vol;
// get volume of first box
vol = [Link]();
[Link]("Volume of mybox1 is " + vol);
// get volume of second box
vol = [Link]();
[Link]("Volume of mybox2 is " + vol);
// get volume of cube
vol = [Link]();
[Link]("Volume of mycube is " + vol);
}
}

PROGRAM 4:
class Pradeep{
public void Pradeep(){
[Link]("Pradeep");
Pradeep();
break;
}

public static void main(String args[]){


Pradeep();
}
}

PROGRAM 5:
public class ThisEx {
int x;

// Constructor with a parameter


public ThisEx(int x) {
this.x = x;
}

// Call the constructor


public static void main(String[] args) {
ThisEx myObj = new ThisEx(5);
[Link]("Value of x = " + myObj.x);
}
}

PROGRAM 6:
// Here, Box allows one object to initialize another.
class Box {
double width;
double height;
double depth;
// Notice this constructor. It takes an object of type Box.
Box(Box ob) { // pass object to constructor
width = [Link];
height = [Link];
depth = [Link];
}
// constructor used when all dimensions specified
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions specified
Box() {
width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box
}
// constructor used when cube is created
Box(double len) {
width = height = depth = len;
}
// compute and return volume
double volume() {
return width * height * depth;
}
}
class OverloadCons2 {
public static void main(String args[]) {
// create boxes using the various constructors
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);
Box myclone = new Box(mybox1); // create copy of mybox1
double vol;
// get volume of first box
vol = [Link]();
[Link]("Volume of mybox1 is " + vol);
// get volume of second box
vol = [Link]();
[Link]("Volume of mybox2 is " + vol);
// get volume of cube
vol = [Link]();
[Link]("Volume of cube is " + vol);
// get volume of clone
vol = [Link]();
[Link]("Volume of clone is " + vol);
}
}
PROGRAM 7:
// Objects are passed through their references.
class Test {
int a, b;
Test(int i, int j) {
a = i;
b = j;
}
// pass an object
void meth(Test o) {
o.a *= 2;
o.b /= 2;
}
}
class PassObjRef {
public static void main(String args[]) {
Test ob = new Test(15, 20);
[Link]("ob.a and ob.b before call: " + ob.a + " " + ob.b);
[Link](ob);
[Link]("ob.a and ob.b after call: " + ob.a + " " + ob.b);
}
}

PROGRAM 8:
public class RecursionExample1 {
static void p(){
[Link]("hello");
p();
}

public static void main(String[] args) {


p();
}
}

PROGRAM 9:
class Box {
double width;
double height;
double depth;
double volume() {
return width * height * depth;
}
void setDim(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
}

class BoxDemo5 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
[Link](10, 20, 15);
[Link](3, 6, 9);
vol = [Link]();
[Link]("Volume is " + vol);
vol = [Link]();
[Link]("Volume is " + vol);
}
}

PROGRAM 10:
class Box {
double width;
double height;
double depth;
Box() {
[Link]("Constructing Box");
width = 10;
height = 10;
depth = 10;
}
double volume() {
return width * height * depth;
}
}
class BoxDemo6 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
vol = [Link]();
[Link]("Volume is " + vol);
vol = [Link]();
[Link]("Volume is " + vol);
}
}

PROGRAM 11:
class Box {
double width;
double height;
double depth;
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
double volume() {
return width * height * depth;
}
}

class BoxDemo7 {
public static void main(String args[]) {
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box(3, 6, 9);
double vol;
vol = [Link]();
[Link]("Volume is " + vol);
vol = [Link]();
[Link]("Volume is " + vol);
}
}

PROGRAM 12:
class Box {
double width;
double height;
double depth;
}

class BoxDemo2 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
// assign values to mybox1's instance variables
[Link] = 10;
[Link] = 20;
[Link] = 15;

/* assign different values to mybox2's instance variables */


[Link] = 3;
[Link] = 6;
[Link] = 9;

// compute volume of first box


vol = [Link] * [Link] * [Link];
[Link]("Volume is " + vol);

// compute volume of second box


vol = [Link] * [Link] * [Link];
[Link]("Volume is " + vol);
}
}

PROGRAM 13:
class Box {
double width;
double height;
double depth;
// compute and return volume
double volume() {
return width * height * depth;
}
}

class BoxDemo4 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
// assign values to mybox1's instance variables
[Link] = 10;
[Link] = 20;
[Link] = 15;

/* assign different values to mybox2's


instance variables */
[Link] = 3;
[Link] = 6;
[Link] = 9;
// get volume of first box
vol = [Link]();
[Link]("Volume is " + vol);
// get volume of second box
vol = [Link]();
[Link]("Volume is " + vol);
}
}

PROGRAM 14:

class Box {
double width;
double height;
double depth;
}

// This class declares an object of type Box.


class BoxDemo {
public static void main(String args[]) {
Box mybox = new Box();
double vol;

// assign values to mybox's instance variables


[Link] = 10;
[Link] = 20;
[Link] = 15;
// compute volume of box
vol = [Link] * [Link] * [Link];
[Link]("Volume is " + vol);
}
}

PROGRAM 15:
class Box {
double width;
double height;
double depth;
// display volume of a box
void volume() {
[Link]("Volume is ");
[Link](width * height * depth);
}
}

class BoxDemo3 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
// assign values to mybox1's instance variables
[Link] = 10;
[Link] = 20;
[Link] = 15;

[Link] = 3;
[Link] = 6;
[Link] = 9;
[Link]();
[Link]();
}
}

PROGRAM 16:
class Sequential
{
public static void main(String args[])
{
int a=10;
int b=5;
[Link](a+b);
[Link](a-b);
[Link](a*b);
[Link](a/b);
[Link](a%b);
}
}

PROGRAM 17:
class switchcase {
public static void main(String[] args) {

int number=44;
String size;
switch (number) {

case 29:
size = "Small";
break;

case 42:
size = "Medium";
break;

case 44:
size = "Large";
break;

case 48:
size = "Extra Large";
break;

default:
size = "Unknown";
break;

}
[Link]("Size: " + size);
}
}

PROGRAM 18:
class For {
public static void main(String[] args) {
for(int i=1;i<=10;i++){
[Link](i);
}
}
}

PROGRAM 19:
class Foreachloop {
public static void main(String args[]){
int arr[]={12,13,14,44};
for(int i:arr){
[Link](i);
}
}
}
PROGRAM 20:
class LifeTime{
public static void main(String ar[]){
int x=10;
if(x==10){
int y=20;
x=y*2;
[Link](x);
[Link](y);
}
[Link](x);
}
}

//scope of the variable is valid inside the block of the code


//outside it can't be accessed
//block defines scope

PROGRAM 21:
class ExplicitCast{
public static void main(String arg[]){
float f=10.0f;
int i;
byte b;

i=(int)f;
b=(byte)f;

[Link](f);
[Link](i);
[Link](b);

}
}

PROGRAM 22:
class ExInt{
public static void main(String args[]){
//double i=10;
int i=0b1;
//boolean i=true;
// float, char, double, boolean
[Link](i);
}
}
PROGRAM 23:
class ExElseIf2{
public static void main(String args[]){
char ch='u';
if(ch=='a'){
[Link]("Vowel");
}
else if (ch=='e'){
[Link]("Vowel");
}
else if (ch=='i'){
[Link]("Vowel");
}
else if (ch=='o'){
[Link]("Vowel");
}
else if (ch=='u'){
[Link]("Vowel");
}
else{
[Link]("Consonant");
}
}
}

PROGRAM 24:
class ImplicitCast{
public static void main(String arg[]){
int i=10;
float f;
f=i;
[Link](i);
[Link](f);
}
}
PROGRAM 25:
class DynInt{
public static void main(String args[]){
double a=3.0, b=4.0;
double c=[Link](a*a + b*b);
[Link]("C is...."+c);
}
}
//Demo code for Dynamic Initialisation

PROGRAM 26:
class CharDemo{
public static void main(String ar[]){
char ch1,ch2;
ch1=88;
ch2='Y';

[Link]("Ch1...is..." +ch1+" Ch2...is..."+ch2);


//[Link]("Ch1...is..." + ++ch1);
}
}

PROGRAM 27:
class SequentialExample
{
public static void main(String args[])
{
int a=10;
int b=5;
[Link](a+b);
[Link](a-b);
[Link](a*b);
[Link](a/b);
[Link](a%b);
}
}

PROGRAM 28:
public class PrimitiveSizeInfo {
public static void main(String[] args) {
[Link]("Size of byte: " + [Link] + " bytes.");
[Link]("Size of short: " + [Link] + " bytes.");
[Link]("Size of int: " + [Link] + " bytes.");
[Link]("Size of long: " + [Link] + " bytes.");
[Link]("Size of char: " + [Link] + " bytes.");
[Link]("Size of float: " + [Link] + " bytes.");
[Link]("Size of double: " + [Link] + " bytes.");

// For older Java versions or using SIZE constant


[Link]("Size of int (using SIZE): " + ([Link] / 8) + " bytes.");
}
}

PROGRAM 29:
class ForEachEx2 {
public static void main(String[]args){
int a[]={11,12,13,14};
for(int i=0;i<4;i++){
[Link](a[i]);
}
}
}

PROGRAM 30:
class ForEach {
public static void main(String args[]){
int arr[]={12,13,14,44};
for(int i:arr){
[Link](i);
}
}
}

PROGRAM 31:
class Forloop {
public static void main(String[] args) {
for(int i=1;i<=10;i++){
[Link](i);
}
}
}

PROGRAM 32:
class ExElseIf1{
public static void main(String args[]){
int a=9;
if(a<10){
[Link]("Single Digit");
}
else if (a>10 && a<100){
[Link]("Double Digit");
}
else if (a>100 && a<1000){
[Link]("Triple Digit");
}
else{
[Link]("Unknown Number");
}
}
}

PROGRAM 33:
class ForEx2 {
public static void main(String[]args){
int a[][]={
{11,12,13,14},
{21,22,23,24}
};
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
[Link](a[i][j]+"\t");
}
}
}
}

PROGRAM 34:
class ForEx1 {
public static void main(String[]args){
int a[]={11,12,13,14};
for(int i=0;i<4;i++){
[Link](a[i]);
}
}
}

PROGRAM 35:
class Whileloop {
public static void main(String args[]) {
int i= 10;
while ( i > 1 ){
[Link](i);
i--;
}
}
}

PROGRAM 36:
class ExSwitch{
public static void main(String args[]){
char ch='a';

switch(ch){
case 'a':
[Link]("Vowel");
break;

case 'e':
[Link]("Vowel");
break;

case 'i':
[Link]("Vowel");
break;

case 'o':
[Link]("Vowel");
break;

case 'u':
[Link]("Vowel");
break;

default:
[Link]("Consonant");
}
}
}

PROGRAM 37:
class ExIfElse{
public static void main(String args[]){
int i=5;

if(i%2==0){
[Link]("Even");
}
else{
[Link]("Odd");
}
}
}
PROGRAM 38:
class IfElseLadder {
public static void main(String[] args) {

int examscore = 56;


char grade;

if(examscore >= 90) {


grade = 'A';
} else if (examscore >=80) {
grade = 'B';
} else if (examscore >=70) {
grade = 'C';
} else if (examscore >=60) {
grade = 'D';
} else if (examscore >=50) {
grade = 'E';
} else {
grade = 'F';
}
[Link]("Grade is = " + grade);
}
}

PROGRAM 39:
class NestedIf {
public static void main(String args[]) {
int num1 = 23;
int num2 = 45;
if( num1 == 23 ) {
if( num2 == 45 ) {
[Link]("Number 1 is :"+ num1 +" and Number 2 is :"+ num2);
}
}
}
}

PROGRAM 40:
class Ifelse {
public static void main(String args[]){
int number = 10;
if (number > 0) {
[Link]("The number is positive.");
}
else {
[Link]("The number is not positive.");
}

[Link]("Statement outside if...else block.");


}
}

PROGRAM 41:
/*
This is a simple Java program.
Call this file "[Link]".
*/
class Example
{
// Your program begins with a call to main().
public static void main(String args[]){
[Link]("This is a simple Java program.");
}
}

PROGRAM 42:
class Ex1{
public static void main(String args[]){
[Link]("Welcome to OOPs");
}
}

PROGRAM 43:
class A{
public void show()
{
[Link]("Health IT....!");
}
}

class Ex2{
public static void main(String args[]){
A obj=new A();
[Link]();
}
}

PROGRAM 44:
class Prs{
public static void main(String args[]){
[Link]("Welcome");
}
}

You might also like