Mandatory Questions

advertisement
King Saud University
College of Computer and Information Sciences
Information Technology Department
IT212: Data Structures
First Semester 1434/1435
Tutorial # 1
Mandatory Questions
1- If myList is an empty list of strings, what does it contain after the following statements execute?
myList.add("alpha");
myList.add(1, "beta");
myList.add("gamma");
myList.add(2, "delta");
myList.add(4, "alpha");
myList.remove(2);
myList.remove(2);
myList.replace(3, "delta");
2- Given the following class:
public class Car
{
private String make;
private int year;
private int mileage;
private String color;
public Car(String make, int year, int mileage, String color)
{
this.make = make;
this.year = year;
this.mileage = mileage;
this.color = color;
}
}
abc-
Make the objects of class Car comparable. The comparison should be based on cars’ mileage.
Add a method equals to class Car that determines if two Cars are equal or not. The method returns true if
the two Cars has the same make, year and mileage. Otherwise, it will return false.
Create a List of Cars that uses an ExpandableArray implementation.
3- Write a method getPosition that returns the position of a given object in a list myList. Assume that the object is in
the list.
a- Write the method getPosition at the client level.
b- Write the method getPosition at the implementation level, where myList is implemented using an array.
1
4- Suppose that you want an operation for the ADT list that reverses the order of the elements in the list.
public void reverse();
a. Write an implementation of this method for each of the two classes described in the lectures (AList and
LList).
b. Suppose that nameList is a list of Name objects, where Name is as defined in Chapter 1. Write Java
statements at the client level that reverses the order of the elements in nameList.
i. By using the added method reverse.
ii. Without using the method reverse.
5- Suppose that you want an operation for the ADT list that moves the first item in the list to the end of the list. The
header of the method could be as follows:
public void moveToEnd()
Write an implementation of this method for the class LList.
6- Write the implementation of the method removeAll(T obj) that will remove all the occurrence of the given object in
the list:
a) As part of class LList.
b) At the client level.
7- Consider an Items Store application and a class Item that represents an Item to be sold in that store. The class has
methods such as getPrice, makeDiscount, and isDiscounted. The method getPrice returns the price of an item.
The method makeDiscount returns a random discount on the item ranges from 0% to 75%. The method
isDiscounted returns true if an item has a discount. This class implemented for you. You should complete the
methods in the application class ItemsStore as following:
public class Item{
private String itemName;
private double itemPrice;
private int itemDiscount;
public Item(String n, double p){
itemName = n;
itemPrice = p;
itemDiscount = 0;
}
public double getPrice() {…}
public void makeDiscount() {…}
public boolean isDiscounted() {…}
}// end class Item
2
public class ItemsStore{
private ListInterface<Item> itemList ;
public ItemsStore(){
itemList = new AList< Item >();
}
public ListInterface<Item> moveDiscountedItems(){
// TO DO
}
public double totalDiscountedPrices(ListInterface<Item> L){
// TO DO
}
a)
b)
}
Write a method moveDiscountedItems that will calls the method makeDiscount for each store’s items and
moves all the items with a discount to another list called DiscountedItemsList; otherwise, leave the item in
the original list. This method should return the DiscountedItemsList.
Write a method totalDiscountedPrices that computes and returns the total prices of the items that have been
discounted.
Extra Questions:
1- Add a constructor to each of the classes AList, and ExpandableArrayList that creates a list from a given array of
objects.
2- Suppose that a list contains Comparable objects. Implement a client method getAllLessThan that returns a new list
of items that are less than some given item. Make sure that your method does not affect the state of the original list.
3- Suppose that you want an operation for the ADT list that adds an array of items to the end of the list.
a. Write an implementation of this method for the class LList.
b. Write an implementation of this method for the class AList "Note if you don't have enough space display proper
message for example: "x items were added from y because there is no enough space".
3
Download