Homework on Constructors

Question 1

Define a class number described as below:
Data members:
a and b of int type.
Member methods:
a) A default constructor to initialize the data members.
b) sum() to return the sum of a and b.
c) display() to display the values of a and b.
In the main() method, create an object and call the above mentioned methods.

class number{
    int a;
    int b;
    public number(){
        a = 12;
        b = 7;
    }
    public int sum(){
        return a + b;
    }
    public void display(){
        System.out.println("a = " + a);
        System.out.println("b = " + b);
    }
    public static void main(String[] args){
        number obj = new number();
        obj.display();
        System.out.println("Sum = " + obj.sum());
    }
}

Question 2

Define a class SalaryCalculation described as below:
Data members:
name (String data type)
basicPay, specialAlw, conveyanceAlw, gross, pf, netSalary, annualSal (double data types)
Member methods:
a) SalaryCalculation(): A constructor to assign name of employee (name), basic salary (basicPay) of your choice and conveyance allowance (conveyanceAlw) as Rs. 1000.00.
b) void salaryCal(): To calculate other allowances and salaries as given below:
specialAlw = 25% of basic salary
pf = 11% of basic salary
gross = basicPay + specialAlw + conveyanceAlw
netSalary = gross – pf
annualSal = 12 months of net salary
c) void display(): To display the name and other calculations with suitable headings.
Write a main() method to call the above member methods.

class SalaryCalculation{
    String name;
    double basicPay;
    double specialAlw;
    double conveyanceAlw;
    double gross;
    double pf;
    double netSalary;
    double annualSal;
    public SalaryCalculation(){
        name = "James Gosling";
        basicPay = 110000.0;
        conveyanceAlw = 1000.0;
    }
    public void salaryCal(){
        specialAlw = 0.25 * basicPay;
        pf = 0.11 * basicPay;
        gross = basicPay + specialAlw + conveyanceAlw;
        netSalary = gross - pf;
        annualSal = 12 * netSalary;
    }
    public void display(){
        System.out.println("Employee name: " + name);
        System.out.println("Basic Pay: " + basicPay);
        System.out.println("Special Allowance: " + specialAlw);
        System.out.println("Conveyance Allowance: " + conveyanceAlw);
        System.out.println("Gross: " + gross);
        System.out.println("PF: " + pf);
        System.out.println("Net Salary: " + netSalary);
        System.out.println("Annual Salary: " + annualSal);
    }
    public static void main(String[] args){
        SalaryCalculation obj = new SalaryCalculation();
        obj.salaryCal();
        obj.display();
    }
}

Question 3

Define a class PhoneBill with the following descriptions:
Data members:
customerName of String type
phoneNo of long type
no_of_unit of int type
rent of int type
amount of double type
Methods:
PhoneBill(): to initialize with default value to each data member.
accept(): to accept customer name, phone number, number of units and rent.
calculate(): to calculate the amount as rent + cost for the units, where cost for the units is to be calculated according to the following conditions:
First 50 calls: Free
Next 100 calls: Rs. 0.80/unit
Next 200 calls: Rs. 1.00/unit
Remaining calls: Rs. 1.20/unit
display(): to display the values of all the data members on the screen.

import java.io.*;
class PhoneBill{
    String customerName;
    long phoneNo;
    int no_of_unit;
    int rent;
    double amount;
    public PhoneBill(){
        customerName = "";
        phoneNo = 0L;
        no_of_unit = 0;
        rent = 0;
        amount = 0.0;
    }
    public void accept()throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Customer's name: ");
        customerName = br.readLine();
        System.out.print("Phone Number: ");
        phoneNo = Long.parseLong(br.readLine());
        System.out.print("Number of units: ");
        no_of_unit = Integer.parseInt(br.readLine());
        System.out.print("Rent: ");
        rent = Integer.parseInt(br.readLine());
    }
    public void calculate(){
        if(no_of_unit <= 50)
            amount = 0.0;
        else if(no_of_unit <= 150)
            amount = (no_of_unit - 50) * 0.80;
        else if(no_of_unit <= 350)
            amount = 100 * 0.80 + (no_of_unit - 150) * 1.00;
        else
            amount = 100 * 0.80 + 200 * 1.00 + (no_of_unit - 350) * 1.20;
        amount += rent;
    }
    public void display(){
        System.out.println("Customer Name: " + customerName);
        System.out.println("Phone Number: " + phoneNo);
        System.out.println("Number of units: " + no_of_unit);
        System.out.println("Rent: Rs. " + rent);
        System.out.println("Amount: Rs. " + amount);
    }
    public static void main(String[] args)throws IOException{
        PhoneBill obj = new PhoneBill();
        obj.accept();
        obj.calculate();
        obj.display();
    }
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.