Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using the template provided below, write a driver class Destinations.java that performs the 'ToDo' tasks in the order indicated. Your code will be tested for

image text in transcribedimage text in transcribed

Using the template provided below, write a driver class Destinations.java that performs the 'ToDo' tasks in the order indicated. Your code will be tested for output and additional unit tests will be performed. Include, but do not call, getldentificationString() that returns "Program 5a, Firstname.Lastname" tips: An ArrayList type is indexed but instead of using braces such as myArrayList[0], we use a method myArrayList.get(0) Methods can be "strung together' with the dot operator. For example, places.get(0) returns a Place object for index 0(the Original Pancake House Eatery object) places.get(0).getName() returns a Place object then for that object calls its getName() resulting in a String object 'Original Pancake House' places.get(0).getName().equals("Phil's BBQ") tests this Place object's name against "Phil's BBQ'' resulting in false Don't rewrite code! If a method exists to perform the task you need, use it! Casting (oh, my!) We have an ArrayList of Place objects. Now, recall this parent class can be a reference to its subclasses. So why can't we write this? System.out.println(places.get(0).getName());//This works System.out.println(places.get(0).getCuisine());//This doesn't work! getName() is a method in the superclass Place. So the object at index 0 is-a Place (a Eatery is-a Place) and the superclass method getName() is inherited. Yay! getCuisine(), however, is a method in the subclass Eatery. A Place is not necessarily an Eatery, that is, it could be an Attraction or a Place. The compiler cannot guarantee that the getCuisine() method exists, so it indicates an error at compile time. The solution is to cast the retrieved object to the subclass that contains the method. Eatery pancake = (Eatery)places.get(0); System.out.println(pancake.getCuisine()); import java.util.ArrayList; public class Destinations { public static void main(String[] args) { ArrayList places = initList();//ToDo: Print each Place using enhanced for-loop showing polymorphism//ToDo: For each Eatery, print numeric rating followed by a space followed by appropriate 'stars'//ToDo: For each Restaurant, print its name followed by a space followed by its description//ToDo: Test Tea Station's latitude and longitude against Tender Greens', print true or false//ToDo: Compare Original Pancake House and Tender Greens cuisine, print true or false//ToDo: Test Birch Aquarium and SD Zoo types, print true or false } private static ArrayList initList(){ ArrayList list = new ArrayList (); Eatery[] restaurants = { new Eatery("Original Pancake House", "Breakfast", 32.815274, -117.1546500, "casual dining", 14.0, 4), new Eatery("Tea Station", "Asian cafe with tea drinks", 32.76049, -117.06739, "casual dining", 8.0, 3), new Eatery("Tender Greens", "2400 Historic Decatur Rd", 32.73547, -177.21601, "casual dining", 26.0, 4) }; Attraction[] fun = { new Attraction("PetCo Park", "Padres Baseball", 32.708129, -117.157036, 37.0, 0), new Attraction("Birch Aquarium at Scripps Institution of Oceanography", "Fish Aquarium", 32.865776, -117.250518, 18.50, 1), new Attraction("San Diego Zoo", "Animal Park", 32.735316, -117.149046, 52.0, 2) }; for (Eatery e: restaurants) list.add(e); for (Attraction a: fun) list.add(a); return list; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spomenik Monument Database

Authors: Donald Niebyl, FUEL, Damon Murray, Stephen Sorrell

1st Edition

0995745536, 978-0995745537

More Books

Students also viewed these Databases questions