Using the template provided below, write a driver class Destinations java that p
ID: 3847521 • Letter: U
Question
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, getlden that returns "Program 5a, Firstname Lastname tificationStringo Tips: An ArrayList type is indexed but instead of using braces such as myArrayListCO], we use a method myArrayList.get (0) Methods can be "strung together with the dot operator. For example, o places get (0 returns a Place object for index 0 (the Original Pancake House Eatery object) places get (0) getName0 returns a Place object then for that object calls its getName0resulting in a String object original Pancake House places get (0).getName0.equals( Phil's BBQ') tests this Place object's name against "Phils BBQ" resulting in false Don't rewrite code! If a method exists to perform the task you need, use it! Casting (oh m 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 s-a Place (a Eatery is a Place and the superclass method get 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.getcuisineExplanation / Answer
import java.util.ArrayList;
class Place{
private String name;
/**
* @param name
*/
public Place(String name) {
this.name = name;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
}
class Eatery extends Place{
private String description;
private double latitude;
private double longitude;
private int rating;
private double price;
private String cuisine;
/**
* @param name
* @param description
* @param latitude
* @param longitude
* @param rating
* @param price
* @param cuisine
*/
public Eatery(String name, String description, double latitude, double longitude,String cuisine,double price,int rating) {
super(name);
this.description = description;
this.latitude = latitude;
this.longitude = longitude;
this.rating = rating;
this.price = price;
this.cuisine = cuisine;
}
/**
* @return the description
*/
public String getDescription() {
return description;
}
/**
* @param description the description to set
*/
public void setDescription(String description) {
this.description = description;
}
/**
* @return the latitude
*/
public double getLatitude() {
return latitude;
}
/**
* @param latitude the latitude to set
*/
public void setLatitude(double latitude) {
this.latitude = latitude;
}
/**
* @return the longitude
*/
public double getLongitude() {
return longitude;
}
/**
* @param longitude the longitude to set
*/
public void setLongitude(double longitude) {
this.longitude = longitude;
}
/**
* @return the rating
*/
public int getRating() {
return rating;
}
/**
* @param rating the rating to set
*/
public void setRating(int rating) {
this.rating = rating;
}
/**
* @return the price
*/
public double getPrice() {
return price;
}
/**
* @param price the price to set
*/
public void setPrice(double price) {
this.price = price;
}
/**
* @return the cuisine
*/
public String getCuisine() {
return cuisine;
}
/**
* @param cuisine the cuisine to set
*/
public void setCuisine(String cuisine) {
this.cuisine = cuisine;
}
}
class Attraction extends Place{
private double latitude;
private double longitude;
private int rating;
private double price;
private String types;
/**
* @param name
* @param latitude
* @param longitude
* @param rating
* @param price
* @param type
*/
public Attraction(String name,String types, double latitude, double longitude,double price,int rating) {
super(name);
this.latitude = latitude;
this.longitude = longitude;
this.rating = rating;
this.price = price;
this.types = types;
}
/**
* @return the latitude
*/
public double getLatitude() {
return latitude;
}
/**
* @param latitude the latitude to set
*/
public void setLatitude(double latitude) {
this.latitude = latitude;
}
/**
* @return the longitude
*/
public double getLongitude() {
return longitude;
}
/**
* @param longitude the longitude to set
*/
public void setLongitude(double longitude) {
this.longitude = longitude;
}
/**
* @return the rating
*/
public int getRating() {
return rating;
}
/**
* @param rating the rating to set
*/
public void setRating(int rating) {
this.rating = rating;
}
/**
* @return the price
*/
public double getPrice() {
return price;
}
/**
* @param price the price to set
*/
public void setPrice(double price) {
this.price = price;
}
/**
* @return the type
*/
public String getTypes() {
return types;
}
/**
* @param type the type to set
*/
public void setTypes(String types) {
this.types = types;
}
}
public class Destinations {
public static void main(String[] args) {
ArrayList<Place>places = initList();
//printing each place using enhanced for loop
for (Place place : places) {
System.out.println(place.getName());
}
Eatery teaStation = null;
Eatery tenderGreens = null;
Eatery panCakeHouse = null;
Attraction birchAquarium = null;
Attraction sdZoo = null;
//looping each place using enhanced for loop
for (Place place : places) {
// checking instance of eatery
if(place instanceof Eatery){
Eatery eatery = (Eatery) place;
System.out.print(eatery.getRating()+" ");
for(int i = 0; i<eatery.getRating();i++){
System.out.print("*");
}
System.out.println();
System.out.println(eatery.getName()+" "+eatery.getDescription());
if(eatery.getName().equals("Tea Station")){
teaStation = eatery;
}else if(eatery.getName().equals("Tender Greens")){
tenderGreens = eatery;
}else if(eatery.getName().equals("Original Pancake House")){
panCakeHouse = eatery;
}
}
// checking instance of attraction
if(place instanceof Attraction){
Attraction attraction = (Attraction) place;
if(attraction.getName().equals("Birch Aquarium at Scripps Institution of Oceanography")){
birchAquarium = attraction;
}else if(attraction.getName().equals("San Diego Zoo")){
sdZoo = attraction;
}
}
}
//comparing latitude and longitude
if(teaStation!=null && tenderGreens!=null){
if(teaStation.getLatitude()== tenderGreens.getLatitude() && teaStation.getLongitude()==tenderGreens.getLongitude()){
System.out.println("true");
}else{
System.out.println("false");
}
}
//comparing cuisines
if(panCakeHouse!=null && tenderGreens!=null){
if(panCakeHouse.getCuisine().equals(tenderGreens.getCuisine())){
System.out.println("true");
}else{
System.out.println("false");
}
}
//comparing types
if(birchAquarium != null && sdZoo !=null){
if(birchAquarium.getTypes().equals(sdZoo.getTypes())){
System.out.println("true");
}else{
System.out.println("false");
}
}
}
private static ArrayList<Place>initList(){
ArrayList<Place>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, -117.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;
}
}
--------------------------------------------------output-------------------------------------------------------------
Original Pancake House
Tea Station
Tender Greens
PetCo Park
Birch Aquarium at Scripps Institution of Oceanography
San Diego Zoo
4 ****
Original Pancake House Breakfast
3 ***
Tea Station Asian cafe with tea drinks
4 ****
Tender Greens 2400 Historic Decatur Rd
false
true
false
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.