Computer Applications
DTDC a courier company charges for the courier based on the weight of the parcel. Define a class with the following specifications:
Class name: courier
Member variables:
name – name of the customer
weight – weight of the parcel in kilograms
address – address of the recipient
bill – amount to be paid
type – 'D'- domestic, 'I'- international
Member methods:
void accept ( ) — to accept the details using the methods of the Scanner class only.
void calculate ( ) — to calculate the bill as per the following criteria:
Weight in Kgs | Rate per Kg |
---|---|
First 5 Kgs | Rs.800 |
Next 5 Kgs | Rs.700 |
Above 10 Kgs | Rs.500 |
An additional amount of Rs.1500 is charged if the type of the courier is I (International)
void print ( ) — To print the details
void main ( ) — to create an object of the class and invoke the methods
Answer
import java.util.Scanner;
public class courier
{
private String name;
private double weight;
private String address;
private double bill;
private char type;
public void accept() {
Scanner in = new Scanner(System.in);
System.out.print("Enter customer's name: ");
name = in.nextLine();
System.out.print("Enter recipient's address: ");
address = in.nextLine();
System.out.print("Enter parcel weight (in Kgs): ");
weight = in.nextDouble();
System.out.println("Enter courier type");
System.out.print("D (Domestic), I (International): ");
type = in.next().charAt(0);
}
public void calculate() {
if (weight <= 5) {
bill = weight * 800;
} else if (weight <= 10) {
bill = (5 * 800) + ((weight - 5) * 700);
} else {
bill = (5 * 800) + (5 * 700) + ((weight - 10) * 500);
}
if (type == 'I') {
bill += 1500;
}
}
public void print() {
System.out.println("Customer's Name: " + name);
System.out.println("Parcel Weight: " + weight + " Kgs");
System.out.println("Recipient's Address: " + address);
System.out.println("Type of Courier: " + type);
System.out.println("Bill Amount: Rs." + bill);
}
public static void main(String[] args) {
courier obj = new courier();
obj.accept();
obj.calculate();
obj.print();
}
}
Variable Description Table
Program Explanation
Output
Related Questions
Consider the given program and answer the questions given below:
class temp { int a; temp() { a=10; } temp(int z) { a=z; } void print() { System.out.println(a); } void main() { temp t = new temp(); temp x = new temp(30); t.print(); x.print(); } }
(a) What concept of OOPs is depicted in the above program with two constructors?
(b) What is the output of the method main()?
Primitive data types are built in data types which are a part of the wrapper classes. These wrapper classes are encapsulated in the java.lang package. Non primitive datatypes like Scanner class are a part of the utility package for which an object needs to be created.
(a) To which package the Character and Boolean classes belong?
(b) Write the statement to access the Scanner class in the program.
Define a class to accept a number from user and check if it is an EvenPal number or not.
(The number is said to be EvenPal number when number is palindrome number (a number is palindrome if it is equal to its reverse) and sum of its digits is an even number.)
Example: 121 – is a palindrome number
Sum of the digits – 1+2+1 = 4 which is an even number