ICS 141. Programming with Objects. Lab 5.

Due Feb. 24 (accepted through March 2 via email, fax or US mail)

 

Your Name:  _____________________________________________________________

Points: 15

Points earned:

 

You must attempt to complete the lab tonight.  If you cannot, you may work on it outside and submit it by the beginning of the next class.  But I will allow that only if you work on the problem in the assigned lab time.

 

Goals: To learn basics of inheritance.

 

Part I: The Design

 

Here is a class called Product. It denotes a generic product that a company manufactures.

 

public class Product {

 

       protected String name;

       protected int id;

       protected double price;

       protected int quantitySold;

 

       public Product(String name, double price, int aID) {

              this.name = name;

              this.price = price;

              id = aID;

              quantitySold = 0;

       }

 

       public double getPrice() {

              return price;

       }

 

       public String getName() {

              return name;

       }

 

       public int getQuantitySold() {

              return quantitySold;

       }

 

       public void setPrice(double aPrice) {

              price = aPrice;

       }

 

       public int sell(int number) {

              return (quantitySold += number);

       }

 

       public String toString() {

              return "Name: " + name + " id: " + id + " price: " +

                     price + " quantity sold " + quantitySold;

       }

}

 

 


The classes, Book and DVD both extend Product. The source code for Book is given below.

 

public class Book extends Product {

 

       private String author;

private String numISBN;

 

       public Book(String aName, double aPrice, int aID,

String aAuthor, String ISBN) {

 

              super(aName, aPrice, aID);

              author = aAuthor;

              numISBN = ISBN;

       }

 

       public String getAuthor() {

              return author;

       }

 

       public String getISBN() {

              return numISBN;

       }

 

       public String toString() {

              return super.toString() + " Author: " + author +

" ISBN: " + numISBN;

       }

}

 

 


Below, we give a UML diagram that represents the three classes.

 

Fill in the details for Product and Book.

 

 

Part II: Implementation

 

1)                  Code the class DVD to reflect the above design. You must also override the toString() method appropriately.

 

2)                  Ensure that three classes, Product, Book, and DVD compile syntax error free.

 

 

Part III: Basics of Generalization, Specialization, Casting, and References

 

Below, we give the listing of Lab5, which attempts to test the implementation. However, it has some errors in it. Take a look at the code and go to the instructions that follow.

 

public class Lab5 {

 

       private void start() {

 

              Product product;

              Book book;

              DVD dvd;

 

 

              Book bookRebecca =

new Book("Rebecca", 8.95, 10,

"Daphne de Maurier", “ISBN 0-201-72214-3”);

 

              product = bookRebecca;

              System.out.println(product);

              System.out.println(bookRebecca);

              System.out.println(product.getAuthor());

//how can we do this?  You are not

//allowed to delete the above line

             

DVD DVDbicyleThieves =

new DVD("Bicyle Thieves", 11.95, 80, 90);

              product = DVDbicyleThieves;

              System.out.println(product);

              System.out.println(DVDbicyleThieves);

              System.out.println(product.getRunningTime());

//how can we do this?  You are not

//allowed to delete the above line

 

              dvd = product; //can we do this?

              dvd = (DVD) product; // can we do this?

              book = dvd; //can we do this?

       }

 

       public static void main(String[] s) {

              new Lab5().start();

       }

}

 

 

Fix the errors  (while fixing the problem, use the following guidelines)

a.                   Some statements cannot be fixed: they simply must be commented out. Comment out those lines.

b.                   Others can be fixed with casting. Cast them appropriately.

 

 

Hand to your instructor the following:

1)      A diskette with your completed solution on it.  Your diskette should have only work for this lab on it.

2)      The completed UML diagram from Part I.