Lab 02
Lab 02
Lab 02
1.1 Using BufferedReader for text based user input from the keyboard - Type the
following code in Example1.java and save it in D:\Your ID.
Compile the above code. [Note down what happens during compilation].
Exercise1: Write a program in java to take 10 integer numbers as user input using
the BufferedReader and print the sum of these numbers [name the file as
Exercise1.java].
[CAREFUL: when reading input with BufferedReader, the input is always read as
String, therefore you are required to parse the input to its correct type. In this
Exercise use Integer.parseInt() method]
1.2 Using the Scanner class for text based user input from the keyboard - Type the
following code in Example2.java and save it in D:\Your ID.
import java.util.Scanner;
class Example2 {
public static void main(String args[]){
//variable declaration
int num1;
double double1;
String numStr1, numStr2;
Compile and run the above code and observe the output?
Exercise2: Write the program description given in Exercise1 in java using the Scanner
class [name the file as Exercise2.java]
2.1 Write java implementation for a class named ‘Item’ which encapsulates the details
of items to be purchased by the customer of the XYZ shop. The class Item is described
as follows:
Attributes description:
a) itemName: String [Name of the ordered Item of the Customer]
b) itemidNo: String [unique identification number of the ordered Item of the Customer]
c) itemQuantity: int [quantity of the ordered Item of the Customer]
d) itemPrice: double [price of the ordered Item of the Customer]
Methods description:
The class supplies the methods(s) as per the following specification:
a) Any Item instance can be created either by supplying the value for all the instance fields
in the order of itemName, itemidNo, itemQuantity and itemPrice OR by supplying the
value for itemName, itemidNo and itemQuantity fields only OR by supplying the
value for itemName, and itemidNo fields only . If an Item instance is created by
providing the value for itemName, itemidNo and itemQuantity fields only then value
for itemPrice is by default initialized to 500. If an Item instance is created by providing
the value for itemName and itemidNo fields only then value for itemPrice is by default
initialized to 500 and value for itemQuantity is by default initialized to 1.
b) Accessor and mutator methods are provided for every instance field.
c) All instance field(s) have a private visibility and all methods have a public visibility.
2.2 Write the java implementation for a class named ‘Customer’ which encapsulates
the details of registered customers of the XYZ shop who buy Items (class is described
above in 2.1) online. The class Customer is described as follows:
Attributes description:
a) name:String [ Name of the Customer]
b) idNo:String [unique identification number of the Customer]
c) balance:double [balance amount of the Customer]
d) item: Item [item purchased by the Customer]
Methods description:
The class supplies the methods(s) as per the following specification:
a) Any Customer instance can be created either by supplying the value for all the instance
fields in the order of name, idNo and balance OR by supplying the value for name
Note: Assume only one item (any number of quantities) can be ordered at a time by
the customer.
Attributes:
(i) salary: double // salary to calculate tax
(ii) isPANsubmitted: boolean // PAN submission status
Methods:
The class supplies the operation(s) as per the following specification:
(i) A TaxOnSalary instance can be created either by supplying the value for the
instance field isPANsubmitted OR without supplying value for any field. If
TaxOnSalary instance is created by providing the value for isPANsubmitted then
the value for salary is initialized with 1000.00 however it can be reinitialized
through the method inputSalary()[which is described below] . If TaxOnSalary
instance is created without supplying value for any field, then value for salary and
isPANsubmitted is by default initialized to 0.0 and false respectively.
(ii) Accessor methods(s) are provided for every instance field.
(iii) A method for computing the tax based on salary [caculateTax() : double] is
supplied. The tax is calculated as per the rules shown below:
a. if salary < 180000 and isPANsubmitted = true, then tax payable is zero
Exercise 4:
A. Define a class Car which encapsulates following attributes and methods
Attributes: private scope
year - The year field is an int that holds a car's year model (e.g. 2010)
make - The make field is a String object that holds the make of the car (e.g. "TATA")
speed - The speed field is an double that holds a car's current speed (e.g. 25.0)
Accelerate()
/* This method adds 1 to the speed attribute each time it is called. For example: if the car
was going 3 mph, accelerate would set the speed to 4 mph */
Accelerate(int increment)
/* This method adds the increment into current speed.
Break(int b)
/* This method decrements the current speed by square root of b. For example: if the car
was going 30 mph, break is called with 4 then speed would set the speed to 28 mph */
B. Define a RaceTrack class that has main method do the following activities.
Create a new Car object (using the Car constructor method), passing in the year, make,
and speed.
Display the current status of the car object using the getter methods getYear(), getMake(),
and getSpeed()
Call the car's Accelerate method and then re-display the car's speed using getSpeed()
Call the car's Accelerate(10) method and then re-display the car's speed using getSpeed().