0% found this document useful (0 votes)
55 views6 pages

Constructor Programs

The document contains Java class definitions for Employee, Salesman, Box, ProfitLoss, and Prime, each with constructors and methods to perform specific calculations. The Employee class calculates net salary based on basic salary, while the Salesman class computes commission from sales. The Box class calculates volume, the ProfitLoss class determines profit or loss percentage, and the Prime class checks if a number is prime.

Uploaded by

snehalatars0007
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)
55 views6 pages

Constructor Programs

The document contains Java class definitions for Employee, Salesman, Box, ProfitLoss, and Prime, each with constructors and methods to perform specific calculations. The Employee class calculates net salary based on basic salary, while the Salesman class computes commission from sales. The Box class calculates volume, the ProfitLoss class determines profit or loss percentage, and the Prime class checks if a number is prime.

Uploaded by

snehalatars0007
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
You are on page 1/ 6

/* Define a java class Employee with the following members:

* Data members: code,name,basic,hra,da,PF.

* Default constructor: Initialises code, basic, hra, da and PF to zero.

* Parameterized constructor: Initialises basic, computes hra, da and PF as per the following criteria:

* hra =10% of basic

* da = 55% of basic

* PF = 1000/-

* Main method: Computes and displays the net salary

* NetSalary=basic+da+hra-PF

*/

public class Employee

int code;

String name;

double basic, da, hra, PF;

Employee ()// default constructors

code=0;

name="";

basic=0.0;

da=0.0;

hra=0.0;

PF=0.0;

Employee (double basSalary)// parametrized constructor

basic=basSalary;

da=55.0/100.0*basic;

hra=10.0/100.0*basic;

PF=1000;
}

public static void main (String args[]) //main method

int cd=1;

String n="Radheshyam";

double salary=25000.85;

Employee obj=new Employee(salary);// object creation

double netSalary;

netSalary=obj.basic+obj.da+obj.hra-obj.PF; // to calculate net salary

System.out.println("employees payslip");

System.out.println("Code "+cd);

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

System.out.println("Netsalary "+netSalary);

/* Define a class Salesman that initialises the commission of the salesman and the sale done by him
to zero,

* using constructor.

* Define main method that computes his commission if his sales is RS. 15000 and the commission in
% is 2.

*/

class salesman

double sales, commission; // instance variables

salesman() //default constructor

sales=0.0;

commission=0.0;

salesman(double s, double c) //parameterized constructor


{

sales=s;

commission=c;

public static void main(String args[])

salesman obj=new salesman(15000,2); // object parameterized constructor

double comInRupees;

comInRupees= obj.commission/100*obj.sales;

System.out.println("Commission in Rupees"+ comInRupees);

/* Write a Java class Box whose default constructor initialises the dimensions length, width and
height

* of the box to zero. The parameterized constructor is passed three double values each for its

* dimensions.

* Write a main method for the above class that creates a Box object of dimensions 3.89 cm, 2.1 cm

* and 1.5 cm. Compute the volume of this box.

*/

//import java.util.*;

public class box

double length, heigth,width; //instance variables

box() // default constructor

length=0.0;

width=0.0;z

heigth=0.0;

box(double l, double w, double h) //parameterized constructor


{

length=l;

width=w;

heigth=h;

public static void main(String args[])

/*Scanner sc=new Scanner(System.in);

double a=sc.nextDouble();

double b=sc.nextDouble();

double c=sc.nextDouble();*/

box obj=new box(3.89,2.1,1.5); // creating object only for parameterized constructor

double vol1;

vol1= obj.length*obj.width*obj.heigth; // calculating volume of the box

System.out.println("volume of the box"+ vol1);

/** Write a program by using a class with the following specifications:

* Class name: profitloss

* Data members/instance variables: int cp, sp

* Methods:

* profitloss (): default constructor to initialize cp,sp

* void input (int a, int b): to assign cp with a and sp with b

* void display(): to calculate and display either profit percent (pp) or loss percent (lp)

*/

class profitloss

int cp,sp,pr,loss; // data members

double pp,lp;

profitloss() //default constructor


{

cp=0; // default values

sp=0;

void input(int a, int b) //parameterized method

cp=a; //assigning the values

sp=b;

void display() // display method

if(sp>cp) //condition to check selling price is greater than cost price

pr=sp-cp; // finding profit

pp=(double)pr/cp*100; // calculating profit percent

System.out.println("The profit pecent"+ pp); // printing profit percent

if(cp>sp) //condition to check cost price is greater than selling price

loss=cp-sp; // finding loss

lp=(double)loss/cp*100; // calculating loss percent

System.out.println("The loss percent:"+lp); // printing loss percent

/** Write a program by using a class with the following specifications:

* Class name: prime

* Data members/ instance variables: int n

* Member methods:

* prime(): default constructor to initialize n


* void input (int x): to assign n with x

* void display(): to check whether the number is prime or not.

*/

class prime

int n;

prime()

n=0;

void input(int x)

n=x;

void display()

int c=0;

for(int i=1;i<=n;i++)

if(n%i==0)

c++;

if(c==2)

System.out.println(n+ "is a prime number");

else

System.out.println(n+ "is not a prime number");

You might also like