King Saud University
College of Computer and information Sciences
Computer Science Department
CSC 113: Java Programming 2 Tutorial # 2 Array of objects
Question 1.
From the following UML diagram
Item ShoppingCart
- name: String - itemList: Item [ ]
- price: double - numOfItem: int
- ID: String + ShoppingCart (in size: int)
+ AddItem (in item: Item): void
+ RemoveItem (in id: String): boolean
+ CalculateBill (): double
+ CountItem (in name: String): int
Implement the following methods located in the Class ShoppingCart:
AddItem it adds the received item in the first empty location of itemList.
RemoveItem it deletes the item from itemList with the same given id by shifting the array to the left. If
the given id doesn't exist then it returns false.
CalculateBill it calculates and returns the total price of the items in the list.
CountItem it counts the number of a given item name in the list.
Note: You can add any necessary constructors, setters, and getters to class Item.
Array of Objects 1
King Saud University
College of Computer and information Sciences
Computer Science Department
CSC 113: Java Programming 2 Tutorial # 2 Array of objects
Question 2.
Trace the following program:
public class Book { public class LibraryMember {
private String title; private Book[] borrowList;
private int ISBN; private int numOfBorr;
public Book (String title, int ISBN) { private String ID;
this.title = title; public LibraryMember (int size, String id)
this.ISBN = ISBN; {
} borrowList = new Book [size];
public void updateBook1 (int copy) { numOfBorr = 0;
title= "Crime and Punishment"; ID = id;
ISBN = 3362; }
copy = 5; public Book[] getborrowList() {
} return borrowList;
public void updateBook1() { }
title= "Twilight";
ISBN= 1122; public int getNumOfBorr() {
} return numOfBorr;
}
public int updateBook2 (int copy) {
title = "Pride and Prejudice"; public void addnewBook(String title, int
ISBN = 2246; isbn) {
copy = 4; if (numOfBorr < borrowList.length) {
return copy; borrowList [numOfBorr] = new Book
} (title, isbn);
public Book updateBook3() { numOfBorr++;
ISBN = 8888; }
return this; }
} }
public String toString () {
return title + " - " + ISBN;
}
}
Array of Objects 2
King Saud University
College of Computer and information Sciences
Computer Science Department
CSC 113: Java Programming 2 Tutorial # 2 Array of objects
public class Library {
public static void main(String[ ] args) {
int [ ] copies = {2, 1, 3, 1};
LibraryMember m1 = new LibraryMember (4, "9999121");
m1.addnewBook("Greate Expectations", 2289);
m1.addnewBook("Heart of Darkness", 6624);
m1.addnewBook("Dracula", 2440);
m1.addnewBook("The Wings of the Dove", 3448);
System.out.println("**The initial Borrowing List**");
System.out.println("Book title - ISBN - Copies");
Book[] borrList = m1.getborrowList();
for (int i=0 ; i< m1.getNumOfBorr() ; i++)
System.out.println(borrList[i] + " - " + copies[i]);
borrList[2].updateBook1(copies[2]);
copies[1] = borrList[1].updateBook2(copies[1]);
borrList[0].updateBook1();
borrList[3] = borrList[3].updateBook3();
System.out.println("\n**Updated Borrowing List**");
System.out.println("Book title - ISBN - Copies");
for (int i=0 ; i< m1.getNumOfBorr() ; i++)
System.out.println(borrList[i] + " - " + copies[i]);
}
}
Array of Objects 3