KEMBAR78
Java Practical No.10 PDF | PDF | Computers
0% found this document useful (0 votes)
743 views7 pages

Java Practical No.10 PDF

The document provides two examples of using constructors in Java classes. The first example demonstrates using two constructors in an Employee class to initialize object properties from a string and integer. The second example shows using a constructor in a Student class to initialize properties by prompting the user for input within the constructor body. Both examples include display methods to output the object property values.

Uploaded by

Meer Kukreja
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)
743 views7 pages

Java Practical No.10 PDF

The document provides two examples of using constructors in Java classes. The first example demonstrates using two constructors in an Employee class to initialize object properties from a string and integer. The second example shows using a constructor in a Student class to initialize properties by prompting the user for input within the constructor body. Both examples include display methods to output the object property values.

Uploaded by

Meer Kukreja
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/ 7

10.

1)

A) DEMONSTRATE USE AT LEAST TWO CONSTRUCTORS.

PROGRAM:

class emp

String name;

int rollno;

emp(String s,int i)

name=s;

rollno=i;

void display()

System.out.println("name:"+name);

System.out.println("rollno:"+rollno);

class empdemo

public static void main(String args[])

emp e=new emp("Meer",1210);

e.display();

}
OUTPUT:
B)

PROGRAM:

import java.util.*;

class stud

String name;

int rollno;

Scanner s=new Scanner(System.in);

stud()

System.out.println("Name:");

name=s.nextLine();

System.out.println("rollno:");

rollno=s.nextInt();

void display()

System.out.println("name:"+name);

System.out.println("rollno:"+rollno);

class studDemo

public static void main(String args[])

{
stud s=new stud();

s.display();

OUTPUT:
10.2) DEVELOP A PROGRAM OF ADDDITION OF TWO COMPLEX NUMBER USING CONSTRUCTOR
OVERLOADING .

PROGRAM:

class complex

int real,img;

float real1,img1;

complex(int r,int i)

real=r;

img=i;

complex(float r,float i)

real1=r;

img1=i;

void display()

System.out.println("Equation1"+real+"+"+img+"i");

void display1()

System.out.println("Equation2"+real+"+"+img+"i");

void sum(float r,float i)

System.out.println("sum="+r+"+"+i+"i");
}

class complexDemo

public static void main(String args[])

complex c1=new complex(1,2);

complex c2=new complex(1.0f,2.0f);

float r,i;

r=c1.real+c2.real;

i=c1.img+c2.img;

c1.display();

c2.display();

c1.sum(r,i);

r=c1.real+c2.real1;

i=c1.img+c2.img1;

}
OUTPUT:

You might also like