Instructions for Attraction.java Write a Java program Attraction.java that has t
ID: 3727506 • Letter: I
Question
Instructions for Attraction.java
Write a Java program Attraction.java that has the following characteristics.
extends Place
New attributes:
type (int)
price (double)
New Methods:
public Attraction(String name, String desc, double latitude, double longitude, double price, int type) where for type values:
- 0 is an amusement park
- 1 is an aquarium
- 2 is a zoo
public double getPrice() -- returns current price
public int getType() -- returns type
public boolean hasAnimals() -- returns true if type contains "zoo" or "aquarium"
public String toString() -- overrides superclass toString() method so that:
- second line: print tab character, "Tickets average $xx.xx" and, if hasAnimals() is true, "and feature exciting animals". Examples:
Instructions for Eatery.java
Write a Java program Eatery.java that has the following characteristics:
extends Place
New attributes:
cost (double)
cuisine (String)
starRating (int)
New Methods:
public Eatery(String name, String desc, double latitude, double longitude, String cuisine, double cost, int starRating)
public double getCost() -- returns current cost
public String getCuisine() -- returns current cuisine
public int getRating() -- returns current starRating
public String ratingToStars() -- returns string containing one '*' character for each value (1 to 5)
public String getCostToSymbols() -- returns '$' characters that correspond to cost
[Note: no spaces--ZyBooks MarkDown text doesn't allow four dollar signs in a row. Grrr! ]
- if cost is less than 25.0, $
- if cost is less than 50.0, $$
- if cost is less than 75.0, $$$
- if cost is less than 100.0, $$ $$
- if cost is greater than or equal to 100.0, $$ $$+
public String toString() -- overrides superclass toString() method so that:
- second line: print tab character, "Price: $xxx" where xxx corresponds to cost
- third line: print tab character, print "Rating: *xxx" where xxx corresponds to starRating
***********
THIS PART IS FOR REFERENCE
--------------------------------------
Location
--------------------------------------
- EARTH_RADIUS_MILES : double = 3963.1676
- latitude : double
- longitude : double
--------------------------------------
+ Location()
+ Location(lat : double, lon : double)
+ setLatitude(lat : double) : void
+ getLatitude() : double
+ setLongitude(lon : double) : void
+ getLongitude() : double
+ distanceFrom( o : Location) : double
--------------------------------------
Coding distanceFrom()
The distance from one location to another can be found by the Spherical Law of Cosines.
Let point x be latitude1, longitude1 and point y be latitude2, longitude2. Then:
cosC = sin(latitude1) * sin(latitude2) + cos(latitude1) * cos(latitude2) * cos(longitude1-longitude2)
arcLenC = acos(cosC)
Now, using the Math class, we can plug in our formula (where point x is this location and point y is the other location provided as the method parameter).
Testing
Writing a program to test your code is a common practice. YOU WILL NOT TURN THE TEST PROGRAM IN!
A simple way to do this is to open another file, called TestLocation (or whatever), write a main() method that
instantiates a Location object using the no-arg constructor
print getLatitude() and getLongitude() return values (should be zeros)
call the setters
print again to show values were modified to whatever values you passed in
repeat Steps 1 - 4 but use the Location two-arg constructor
end part 1
part 2
------------------------------------------------------------------------
Place
------------------------------------------------------------------------
- name: String
- description : String
- location : Location
------------------------------------------------------------------------
+ Place(name : String, desc : String, latitude : double, longitude : double)
+ setName(name : String) : void
+ getName() : String
+ setDescription(desc : String) : void
+ getDescription() : String
+ setLocation( latitude : double, longitude : double) : void
+ setLocation( location : Location) : void
+ getLocation() : Location + toString() : String
+ getIdentificationString() : String
------------------------------------------------------------------------
A call to print a Place object System.out.println(myPlaceObject); should return a string of the format:
Location.java Instructions
Retrieve the Location.java class from Prog4a. Keeping all methods and structure, add the two methods described below.
----------------------------------
Location
----------------------------------
+ equals( o : Location) : boolean
+ toString() : String
---------------------------------------------------------
A call to print a Location object System.out.println(myLocationObject); should return a string of the format:
A call to determine if two Location objects are equal System.out.println(myLocationObject.equals(someOtherLocationObject); will return true if the latitude and longitude are the same (within 0.0001 degrees difference). Therefore, if:
loc1 is at latitude: 32.735316,longitude: -117.149046
loc2 is at latitude: 32.735328,longitude: -117.14910
loc3 is at latitude: 32.735328,longitude: -117.14920
loc1 is equal to loc2
since absolute value of the difference of latitudes is less than 0.0001 and same goes for longitudes
loc1 is not equal to loc3
though absolute value of the difference of latitudes is less than 0.0001, longitudes are greater than 0.0001 difference
Testing
Writing a program to test your code is a common practice. YOU WILL NOT TURN THE TEST PROGRAM IN!
A simple way to do this is to open another file, called TestPlace, write a main() method that:
instantiates a Place object using the constructor
print the "getters" and verify data is accurate
call the setters
print again to show values were modified to whatever values just set
repeat Steps 1 - 4 but use different data, possibly something that might cause an error
Explanation / Answer
public class Attraction extends Place {
private int type;
private double price;
public Attraction() {
}
public Attraction(String name, String desc, double latitude, double longitude, double price, int type) {
super(name, desc, latitude, longitude);
this.type = type;
this.price = price;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public boolean hasAnimals() {
if (this.type == 1 || this.type == 2) {
return true;
}
return false;
}
@Override
public String toString() {
if (this.hasAnimals())
return super.toString() + " Tickets average $" + this.price + " and feature exciting animals";
else
return super.toString() + " Tickets average $" + this.price;
}
}
Place.java
public class Place {
private String name;
private String desc;
private double latitude;
private double longitude;
Place() {
}
public Place(String name, String desc, double latitude, double longitude) {
this.name = name;
this.desc = desc;
this.latitude = latitude;
this.longitude = longitude;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
@Override
public String toString() {
return this.name+" (latitude:"+this.latitude+",longitude:"+this.longitude+")";
}
}
Eatery.java
public class Eatery extends Place {
private double cost;
private String cuisine;
private int starRating;
public Eatery(String name, String desc, double latitude, double longitude, String cuisine, double cost,
int starRating) {
super(name, desc, latitude, longitude);
this.cost = cost;
this.cuisine = cuisine;
this.starRating = starRating;
}
public double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost;
}
public String getCuisine() {
return cuisine;
}
public void setCuisine(String cuisine) {
this.cuisine = cuisine;
}
public int getStarRating() {
return starRating;
}
public void setStarRating(int starRating) {
this.starRating = starRating;
}
public String ratingToStars() {
String s = "";
for (int i = 0; i < this.starRating; i++) {
s = s + "*";
}
return s;
}
public String getCostSymbols() {
if (cost < 25.0) {
return "$";
} else if (cost > 25.0 && cost < 50.0) {
return "$$";
} else if (cost > 50.0 && cost < 75.0) {
return "$$$";
} else if (cost > 75.0 && cost < 100.0) {
return "$$ $$";
} else {
return "$$ $$+";
}
}
@Override
public String toString() {
return super.toString() + " Price: " + this.getCostSymbols()+" Rating: "+this.ratingToStars();
}
}
PlaceDriver.java
public class PlaceDriver {
public static void main(String[] args) {
Attraction atr1 = new Attraction("Seaport Village", "Nice Place to see", 32.70872, -117.171, 55, 0);
Attraction atr2 = new Attraction("San Diego Zoo", "Nice Place to see", 32.73607, -117.14927, 55, 2);
Eatery eats = new Eatery("The Cottage", "nice place to eat", 32.84341, -117.275, "Chinese", 74, 4);
System.out.println(atr1);
System.out.println(atr2);
System.out.println(eats);
}
}
Sample Run:-
Seaport Village (latitude:32.70872,longitude:-117.171)
Tickets average $55.0
San Diego Zoo (latitude:32.73607,longitude:-117.14927)
Tickets average $55.0 and feature exciting animals
The Cottage (latitude:32.84341,longitude:-117.275)
Price: $$$
Rating: ****