If it is a DiscountedItem, then you will count it.Īt the end of the loop, the method will return the count. In the loop, you will test if each Item is a DiscountedItem by using the instanceof keyword ( object instanceof Class returns true or false) similar to its use in the add(Item) method. This method will use a loop to traverse the ArrayList of Items called order. In this challenge, you will write a method called int countDiscountedItems() in the ShoppingCart class. TheĭiscountedItem class you wrote in the last lesson adds on a discount amount. Item class keeps track of the name and the price of each Item. You can use to add Items or DiscountedItems to the shopping cart. The ShoppingCart contains a polymorphic ArrayList called order that The Active Code window below (or in repl or another IDE) before completing this Please copy your solutions from the last lesson into In the last lesson, you created a class called DiscountedItem as part of a Programming Challenge : Shopping Cart 2 ¶ Casting to Dictionary means that the compiler will check the Dictionary class for the getDefinition method.ĩ.6.1.At compile time the declared type is Book and the Book class does not have or inherit a getDefintion method.The b object is actually a Dictionary object which inherits the getISBN method from Book.Polymorphic array and ArrayList types such as Shape shapeArray = Polymorphic parameters such as print(Shape) being called with different subclass types. Polymorphic assignment statements such as Shape s = new Rectangle() In the last lesson on inheritance hierarchies, we were actually seeing polymorphic behavior at run-time in the following ways. If not, the parent of that class will be checked and so on until the method is found. If the method is found there it will be executed. When a method is called at run-time the first place that is checked for that method is the class that created the object. Remember that an object keeps a reference to the class that created it (an object of the class called Class). At run-time, the actual method that is called depends on the actual type of the object. The code won’t compile if the methods don’t exist in that class or some parent class of that class. indexOf ( "h" ) // ERROR!! Objects don't have indexOf!Īt compile time, the compiler uses the declared type to check that the methods you are trying to use are available to an object of that type. Object message = new String ( "hi" ) message. The method will be found, since otherwise the code would not have compiled. It may go up all the way to the Object class. If it doesn’t find it there it will look in the parent class and keep looking up the inheritance tree until it finds the method. At run-time the execution environment will first look for the add method in the ArrayList class since that is the actual or run-time type. The List interface does have an add method so this code will compile. The complier will check if the declared type has the methods or inherits the methods being used in the code and give an error if it doesn’t find the method(s). The variable nameList declared below has a declared type of List and an actual or run-time type of ArrayList. The actual (run-time) type is the class that actually creates the object using new. The declared (compile-time) type of a variable is the type that is used in the declaration. In Java an object variable has both a declared (compile-time) type and an actual (run-time) type.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |