NPConstructors Programs
NPConstructors Programs
/*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;
} // close cal()
/*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;