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

NPConstructors Programs

The document contains three Java programs that utilize non-parameterized constructors to initialize global variables for different scenarios: calculating motorbike rental fare, telephone call charges, and the net salary of a salesperson based on sales. Each program includes methods for accepting user input, performing calculations, and displaying results. The constructors set default values for the variables before user input is processed.

Uploaded by

nowduri.sk
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)
7 views6 pages

NPConstructors Programs

The document contains three Java programs that utilize non-parameterized constructors to initialize global variables for different scenarios: calculating motorbike rental fare, telephone call charges, and the net salary of a salesperson based on sales. Each program includes methods for accepting user input, performing calculations, and displaying results. The constructors set default values for the variables before user input is processed.

Uploaded by

nowduri.sk
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/ 6

QUESTION 1 OF NON – PARAMETERIZED CONSTRUCTOR

/*Program to calculate the fare on the motor bike take on rent, according to table given,
using non-parameterized constructor to initialize the global variable*/
import java.util.*;
class Mobike
{
int bno,days,charge; // global variables
// non - paramterized constructor
public Mobike()
{
bno = 0;
days=0;
charge=0;
}
public void accept()
{
Scanner sc = new Scanner(System.in);
System.out.println("enter bike number ");
bno = sc.nextInt();
System.out.println("Enter the days on rent");
days= sc.nextInt();
}
public void calculate() // calculating the fare of the motor bike taken on rent
{
if(days>=1 && days<=5)
charge = days *40;
else if(days>=6 && days<=12)
charge = days * 55;
else
charge = days * 65;
}
public void display()
{
System.out.println("The bike number is.. " + bno);
System.out.println("the number of days bike taken on rent.. " + days);
System.out.println("the charage calculated is.. " + charge);
}
public void main()
{
Mobike m1 = new Mobike(); // creating object & calling the constructor function
m1.accept(); // calling the funtions in sequential order
m1.calculate();
m1.display();
}
}// close class
QUESTION 5 OF NON – PARAMETERIZED CONSTRUCTOR

/*Program to calculate the amount payable on the number of telephone calls made,
according to the table given below using non-parameterized constructor to initialize
the global variable*/

import java.util.*;
class Telephone
{
int previous , present, call;
double amt , total; // declaring the global variables

//Non-parameterized constructor
// This constructor function is called at the
// time of OBJECT - CREATION STATEMENT

public Telephone()
{
previous =0;
present=0;
call=0;
amt=0.0;
total=0.0;
}
public void input()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the present and previous calls made");
present = sc.nextInt();
previous = sc.nextInt();
call = present - previous; // as given in the question.
}
public void cal()
{
if( call <= 100)
amt = 0.0;
else if(call>=101 && call <= 200)
amt = 100 * 0 + (call-100)* 0.90;
else if(call >= 201 && call <=400)
amt = 100*0 + 100*0.90 + (call-200)*0.80;
else if(call >= 401 && call <=800)
amt = 100*0 + 100*0.90 + 200*0.80 + (call-400)*0.50;

total = amt + 250; // 250 is rental charage given in the question

} // close cal()

public void display()


{
System.out.println("Number of previous calls made : " + previous);
System.out.println("Number of present calls made : " + present);
System.out.println("Number of calls made : " + call);
System.out.println("Total amount payable " + total);
} // close display( )

public void main()


{
// creating object. This statement calls the constructor function
Telephone t1 = new Telephone();
t1.input();
t1.cal();
t1.display();
} // close main()
} // close class
QUESTION 6 OF NON – PARAMETERIZED CONSTRUCTOR

/*Program to calculate the Net Salary of an Salesperson based on the sales made,
according to the table given below using non-parameterized constructor to initialize
the global variable*/

import java.util.*;
class Commission
{
String name;
double salary, sales, comm, netsalary;
// non-parameterized constructor initializes
// the global variables to default initial values
public Commission()
{
name = ""; // nullify
salary = 0.0;
sales = 0.0;
comm=0.0;
netsalary=0.0;
}
public void accept()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter Salesman name, Salary and Sales made");
name = sc.nextLine();
salary = sc.nextDouble();
sales = sc.nextDouble();
}
public void calculation()
{
if(sales <=15000)
comm = (double)8/100 * sales;
else if(sales >=15001 && sales <= 30000)
comm = (double)15/100 * sales;
else if(sales >=30001 && sales <= 50000)
comm = (double)20/100 * sales;
else
comm = (double)25/100 * sales;

netsalary = salary + comm; // given in the question


} // close calculation

public void display()


{
System.out.println("Name : " + name);
System.out.println("Salary : " + salary);
System.out.println("Sales made : " + sales);
System.out.println("Net Salary : " + netsalary);
} // close display()

public void main()


{
// object creation statement which calls the constructor function
Commission c1 = new Commission();
c1.accept();
c1.calculation();
c1.display();
} // close main()
} // close class

You might also like