You are on page 1of 4

Java Assignment-2

1. Write a method called isAllEven that takes an array of integers


as a parameter and that returns whether or not all of the
values are even numbers (true for yes, false for no).

2. 1.Create a class with a method that prints "This is parent


class" and its subclass with another method that prints "This
is child class". Now, create an object for each of the class and
call
1 - method of parent class by object of parent class
2 - method of child class by object of child class
3 - method of parent class by object of child class

3. Create a class named 'Member' having the following


members:
Data members
1 - Name
2 - Age
3 - Phone number
4 - Address
5 - Salary
It also has a method named 'printSalary' which prints the
salary of the members.
Two classes 'Employee' and 'Manager' inherits the 'Member'
class. The 'Employee' and 'Manager' classes have data
members 'specialization' and 'department' respectively. Now,
assign name, age, phone number, address and salary to an
employee and a manager by making an object of both of
these classes and print the same.
4. Create a class named 'Rectangle' with two data members
'length' and 'breadth' and two methods to print the area and
perimeter of the rectangle respectively. Its constructor having
parameters for length and breadth is used to initialize length
and breadth of the rectangle. Let class 'Square' inherit the
'Rectangle' class with its constructor having a parameter for
its side (suppose s) calling the constructor of its parent class as
'super(s,s)'. Print the area and perimeter of a rectangle and a
square.

5. Create a class named 'Shape' with a method to print "This is


This is shape". Then create two other classes named
'Rectangle', 'Circle' inheriting the Shape class, both having a
method to print "This is rectangular shape" and "This is
circular shape" respectively. Create a subclass 'Square' of
'Rectangle' having a method to print "Square is a rectangle".
Now call the method of 'Shape' and 'Rectangle' class by the
object of 'Square' class.

6. Write a program by creating an 'Employee' class having the


following methods and print the final salary.
1 - 'getInfo()' which takes the salary, number of hours of work
per day of employee as parameter
2 - 'AddSal()' which adds $10 to salary of the employee if it is
less than $500.
3 - 'AddWork()' which adds $5 to salary of employee if the
number of hours of work per day is more than 6 hours.

7. Create a class called Invoice that a hardware store might use


to represent an invoice for an item sold at the store. An
Invoice should include four pieces of information as instance
variables‐a part number(type String),a part description(type
String),a quantity of the item being purchased (type int) and a
price per item  (double). Your class should have a constructor
that initializes the four instance variables. Provide a set and a
get method for each instance variable. In addition, provide a
method named getInvoice Amount that calculates the invoice
amount (i.e., multiplies the quantity by the price per item),
then returns the amount as a double value. If the quantity is
not positive, it should be set to 0. If the price per item is not
positive, it should be set to 0.0. Write a test application
named InvoiceTest that demonstrates class Invoice’s
capabilities.

8. Consider the UML conceptual model shown below. You will be


writing class definitions for each of the concepts and
additionally for the test driver program following the
instructions below.

Add the class definitions of OrderItem, Order and RushOrder


classes in their separate .java files but all within the package.
Note that all of these classes are public.
OrderItem class has a String upc, an integer quantity and an
integer price, all private. The getCost() method returns the
multiplication of its quantity and price.
Order class has a list of OrderItem objects. The
addOrderItem() method takes an object of OrderItem as the
parameter and stores it in the list. The getTotal() method
returns the total cost of all order items in the order. The
printOrderItems() method prints information about each
order item via the toString() method of the OrderItem class.
RushOrder class extends the Order class. It has an integer
instance variable deliveryDay, a protected member, which
represents in how many days the order should be delivered.
The getTotal() method overrides the definition of the super
class in the following way: It first invokes the getTotal() of the
super class to find the total for all items in the order. It then
adds the delivery charge. The delivery charge for one day
delivery is $25, for two day delivery is $15, for three day
delivery is $10. It is free for four or more days. Note that, the
delivery charge should be added only if there are items in the
order. It means if the getTotal() of the super class returns 0,
do not add delivery charge, but just return 0.

You might also like