Sie sind auf Seite 1von 17

Java Programming Language Java SE 6

Lab Manual

Edition 1.1 June 2012

1 | Page

Collections and Generics Framework

Upon completion of this lab, you should be able to : Use a generic collection to manage a one-to-many association

2 | Page

Exercise 1 In this exercise, you become familiar with the concept of manipulating a HashSet. Question : Create a class Employee with the following attributes : empId int, empName String, salary float. Create a class Company with the following attributes and methods. Attribute : list of employees i.e. HashSet<Employee> emplist. Methods : addEmployee(Employee empobj) - adds an employee object to the emplist. viewAllEmployee( ) : HashSet<Employee> - Displays all the employee in the shop. updateEmployee(int empid, float salary) - Updates the salary of the employee with the id passed as parameter deleteEmployee(int empid) Deletes the employee with the specified id. Task 1 Creating the Employee Class Do the following steps : 1. Create a project named HashSetDemo 2. Create the class Employee in the default package of the source folder with the attributes as given above. public class Employee { //Attributes } 3. Write the constructor, getters and setters. 4. In addition to the above, you have to override the hashcode and equals method in Object class to restrict the addition of duplicates. @Override public boolean equals(Object obj) { //Code to check the duplicate object based on id. }
3 | Page

@Override public int hashCode() { //Code to generate the hashcode } Task 2 Creating the Company Class Complete the following steps : 1. Create a class Company in the default package of the source folder of the project HashSetDemo with the attribute as given in the question. public class Company { //Attributes private HashSet<Employee> emplist=new HashSet<Employee>(); } 2. Add the following import statement to include the HashSet class in the Company Class import java.util.HashSet; 3. Write the method addEmployee which adds the employee object to the emplist public void addEmployee(Employee empobj) { //Code to add empobj } 4. Write the method viewAllEmployee which returns the list of employee in the company. public HashSet<Employee> viewAllEmployee() { //Code to return the employees in company } 5. Write the method updateEmployee which updates the salary of the employee based on the given id public void updateEmployee(int empid, float salary) { //Code to update the employees salary }

4 | Page

6. Write the method deleteEmployee which deletes the employee with the given id.

public void deleteEmployee(int empid) Code to delete the Employee Task 3 : Creating the HashSetDemo class 1. Create a class HashSetDemo in the default package of the source folder of the project HashSetDemo. 2. Write the main method in this class. 3. Create few objects of Employee class and add to the Company using the methods written. 4. Call the other methods also and validate the logic.(invoke the viewAllEmployee method and check the correctness of the program). public class HashSetDemo { public static void main(String args[]) { Employee empobj1=new Employee(1,"Bob",20000); Employee empobj2=new Employee(2,"Tom",15000); Employee empobj3=new Employee(1,"Henry",20000); // duplicate id. Employee empobj4=new Employee(4,"Peter",40000); Company companyobj=new Company(); companyobj.addEmployee(empobj1); companyobj.addEmployee(empobj2); companyobj.addEmployee(empobj3); companyobj.addEmployee(empobj4); HashSet<Employee> emplist1=companyobj.viewAllEmployee(); //iterate the list and display the details of employees companyobj.updateEmployee(3,50000); //iterate the list and display the details of employees companyobj.deleteEmployee(2); //iterate the list and display the details of employees } } }

{ //

5 | Page

Task 4 : Compile the HashSetDemo class Task 5 : Execute the HashSetDemo class.

The output would be as follows : Details of the Employees ID : 1 Name : Bob Salary : 20000.0 ID : 2 Name : Tom Salary : 15000.0 4 Name : Peter Salary : 40000.0 After updation ID : 1 Name : Bob Salary : 20000.0 : 2 Name : Tom Salary : 15000.0 4 Name : Peter Salary : 40000.0 After Deletion ID : 1 Name : Bob Salary : 20000.0 ID : 4 Name : Peter Salary : 40000.0 ID ID : ID :

6 | Page

Exercise 2 In this exercise, you become familiar with the concept of manipulating a TreeSet. This exercise is similar to the above exercise, but the HashSet said in the above example would be TreeSet with some changes in the Employee Class

Question : Create a class Employee with the following attributes : empId int, empName String, salary float. Create a class Company with the following attributes and methods. Attribute : list of employees i.e. TreeSet<Employee> emplist. Methods : addEmployee(Employee empobj) - adds an employee object to the emplist. viewAllEmployee( ) : ArrayList<Employee> - Displays all the employee in the shop. updateEmployee(int empid, float salary) - Updates the salary of the employee with the id passed as parameter deleteEmployee(int empid) Deletes the employee with the specified id. Task 1 Creating the Employee Class Do the following steps : 1. Create a project named TreeSetDemo 2. Create the class Employee in the default package of the source folder with the attributes as given above. As in TreeSet dupicates should not be there as well as sorted order is to be maintained, Employee class will implement the Comparable interface. public class Employee implements Comparable { //Attributes }
3. Write the constructor, getters and setters as said in the previous exercise.

7 | Page

4. As the Employee class implements the Comparable interface, instead of overriding the hashcode and equals method in Object class you should provide implementation for the compareTo method in the Comparable interface. @Override public int compareTo(Object obj) { //Code to check the duplicate object based on id } Task 2 Creating the Company Class Complete the following steps : 1. Create a class Company in the default package of the source folder of the project HashSetDemo with the attribute as given in the question. public class Company { //Attributes private TreeSet<Employee> emplist=new TreeSet<Employee>(); }
2.

Add the following import statement to include the TreeSet class in the Company Class import java.util.TreeSet;

3. Write the method addEmployee which adds the employee object to the emplist public void addEmployee(Employee empobj) /Code to add empobj } { /

4. Write the method viewAllEmployee which returns the list of employee in the company. public TreeSet<Employee> viewAllEmployee() { //Code to return the employees in company } 5. Write the method updateEmployee which updates the salary of the employee based on the given id

8 | Page

public void updateEmployee(int empid, float salary) Code to update the employees salary }

{ //

6. Write the method deleteEmployee which deletes the employee with the given id. public void deleteEmployee(int empid) { // Code to delete the Employee Task 3 : Creating the TreeSetDemo class 1. Create a class TreeSetDemo in the default package of the source folder of the project TreeSetDemo 2. Write the main method in this class. 3. Create few objects of Employee and add to the Company using the methods written. 4. Call the other methods also and validate the logic.(invoke the viewAllEmployee method and check the correctness of the program). public class TreeSetDemo { public static void main(String args[]) { Employee empobj1=new Employee(104,"Bob",20000); Employee empobj2=new Employee(200,"Tom",15000); Employee empobj3=new Employee(104,"Henry",20000); // duplicate id. Employee empobj4=new Employee(106,"Peter",40000); Company companyobj=new Company(); companyobj.addEmployee(empobj1); companyobj.addEmployee(empobj2); companyobj.addEmployee(empobj3); companyobj.addEmployee(empobj4); TreeSet<Employee> emplist1=companyobj.viewAllEmployee(); //iterate the list and display the details of employees companyobj.updateEmployee(3,50000); //iterate the list and display the details of employees companyobj.deleteEmployee(2); //iterate the list and display the details of employees } Task 4 : } }

9 | Page

Compile the TreeSetDemo class Task 5 : Execute the TreeSetDemo class.

The output would be as follows : Employee Details are : 104 Name : Bob Salary : 20000.0 Name : Peter Salary : 40000.0 Tom Salary : 15000.0 After updation ID : 104 Name : Bob Salary : 20000.0 106 Name : Peter Salary : 50000.0 Name : Tom Salary : 15000.0 After Deletion : 104 Name : Bob Salary : 20000.0 Name : Peter Salary : 50000.0 ID : ID : 106 ID : 200 Name :

ID : ID : 200 ID ID : 106

10 | P a g e

Exercise 3 In this exercise, you become familiar with the concept of manipulating an ArrayList. Question : Create a class Item which has the following attributes : itemId int, itemName String, quantityAvailable int, price float, category char. Note : The category of the product can be 'a' or 'b' or 'c'. Create a class Shop which has the following attributes and methods. Attribute : list of items i.e. ArrayList<Item> itemlist. Methods : addItem(Item itemobj) - adds an item object to the itemlist. viewAllItems( ) : ArrayList<Item> - Displays all the items in the shop. viewItem(id : int) : Item Returns the item with that id. viewItem(category : char) :ArrayList<Item> - Returns the items of the category specified as parameter calculateInventory() : float - Returns the total cost of the items present in the shop Task 1 Creating the Item Class Complete the following steps : 1. Create a project named CollectionsDemo 2. Create the class Item in the default package of the source folder with the attributes as given in the above question. public class Item { //Attributes

} 3. Write the constructor, getters and setters. Task 2 Creating the Shop Class Complete the following steps : 1. Create a class Shop in the default package of the source folder of the project CollectionsDemo with the attribute as given in the question.

11 | P a g e

public class Item { //Attributes private ArrayList<Item> itemlist=new ArrayList<Item>(); } 3. Add the following import statement to include the ArrayList class in the Shop Class import java.util.ArrayList; 4. Write the method addItem which adds the item to the itemlist public void addItem(Item itemobj) { //add item to list } 5. Write the method viewAllItems which returns the list of items in the shop. public ArrayList<Item> viewAllItems() { //Code to view the items in list } 6. Write the method viewItem which takes the id as parameter and returns the Item of that id in the shop. If no item of that id is available it should return null. public Item viewItem(int id) { //Code to view item by id } 7. Write the method viewItem which takes the category as parameter and returns the list if Items of that category present in the shop. public ArrayList<Item> viewItem(char category) { //Code to view items given category } 8. Write the method calculateInventory() which calculates and returns the total cost of all the items present in the shop. (total of (quantityAvailable * price) of each item in list)

12 | P a g e

public float calculateInventory() { //Code to calculate the inventory } Task 3 : Creating the ArrayListDemo class 1. Create a class ArrayListDemo in the default package of the source folder of the project CollectionsDemo 2. Write the main method in this class. 3. Create few objects of Item class and add to the ArrayList using the methods written. 4. Call the other methods also and validate the logic. public class ArrayListDemo{ public static void main(String arg[]){ Item itemobj1=new Item(1,Mouse,5,250,'a'); Item itemobj2=new Item(2,Keyboard,15,500,'b'); Item itemobj3=new Item(3,MotherBoard,3,5000,'c'); Item itemobj4=new Item(4,Speakers,12,700,'a'); Item itemobj5=new Item(5,modem,2,5000,'a'); Shop shopobj=new Shop(); shopobj.addItem(itemobj1); shopobj.addItem(itemobj2); shopobj.addItem(itemobj3); shopobj.addItem(itemobj4); shopobj.addItem(itemobj5); ArrayList<Item> itemlist=shopobj.viewAllItems(); //Code to iterate the list and display the item details Item itemobj=shopobj.viewItem(2); //Code to display the item details
13 | P a g e

ArrayList<Item> itemlist1=shopobj.viewItem('a'); //Code to iterate the list and display the item details float totalprice=shopobj.calculateInventory(); } } Task 4 : Compile the ArrayListDemo class Task 5 : Execute the ArrayListDemo class. The output would be as follows : Items available in shop are : ID : 1 Name : Mouse Qty : 5 Price : 250.0 Category : a ID : 2 Name : Keyboard Qty : 15 Price : 500.0 Category : b ID : 3 Name : MotherBoard Qty : 3 Price : 5000.0 Category : c ID : 4 Name : Speakers Qty : 12 Price : 700.0 Category : a ID : 5 Name : modem Qty : 2 Price : 5000.0 Category : a The item details are : ID : 2 Name : Keyboard Qty : 15 Price : 500.0 Category : b Items available of category a in shop are : ID : 1 Name : Mouse Qty : 5 Price : 250.0 Category : a ID : 4 Name : Speakers Qty : 12 Price : 700.0 Category : a ID : 5 Name : modem Qty : 2 Price : 5000.0 Category : a Total Inventory : 42150.0

14 | P a g e

Annotations

Upon completion of this lab, you should be able to : Understand the concepts of Annotations.

15 | P a g e

Exercise 1 In this exercise, you become familiar with the concept of Annotations. Question : Create a class AnnotationDemo with the following features : create a method getDate which returns the Date object. Make that method as deprecated create a method getMessage which returns the message Welcome to Java Annotations. Create a class AnnotationOverride which extends the class AnnotationDemo override the getMessage method by using @Override and return the message as Welcome to Annotations in Java - @Override In the main method, call the getDate() method, execute it. Now write the SuppressWarnings annotation to the main method, execute and note the difference.

Task 1 : Creating the AnnotationDemo class Do the following : 1. Create a project, JavaAnnotations. 2. Create a class AnnotationDemo, and write the method getDate as Deprecated. //make this method Deprecated getDate() } 3. Write the method getMessage which return the message as given in the question public Date { //return the date

Task 2 : Creating the AnnotationOverride class


16 | P a g e

Do the following : 1. Create a class AnnotationOverride which extends AnnotationDemo 2. Override the getMessage which returns the message as given in the question using annotation. 3. Write the main method and call the getDate() function 4. For the main method suppress the warning, deprecated using annotation

17 | P a g e

Das könnte Ihnen auch gefallen