Java Programming Objectives Develop subclasses for an existing class thereby ill
ID: 3847168 • Letter: J
Question
Java Programming
Objectives
Develop subclasses for an existing class thereby illustrating inheritance
And the usual submission and IDE objectives
General Instructions
You will develop two subclasses of the parent Place class. You will upload all relevant files to this lab for grading. You will need the code from Program 4a to complete this lab.
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 void 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
Turning in the assignment
When you are satisfied with the program (or are out of time and want some partial credit), submit the following files:
1) Place.java (unchanged from Program 4a)
2) GeoLocation.java (unchanged from Program 4a)
3) Eatery.java
4) Attraction.java
Explanation / Answer
package chegg;
//parent class
public class place {
String name;
String desc;
double latitude;
double longitude;
public void ToString()
{
System.out.println("place class");
}
}
//Attraction class...
package chegg;
public class Attraction extends place {
double at_price;
int at_type;
public Attraction(String name, String desc, double latitude, double longitude, double price, int type)
{
this.name=name;
this.desc=desc;
this.latitude=latitude;
this.longitude=longitude;
at_price=price;
at_type=type;
}
public double getPrice()
{
return at_price;
}
public int getType()
{
return at_type;
}
public boolean hasAnimal()
{
if((at_type==1)||(at_type==2))
{
return true;
}
else
return false;
}
public void ToString()
{
Attraction at=new Attraction("avf","xccv",324.4,3434.2,2000,1);
if(at.hasAnimal())
{
System.out.println("Tickets average"+"$"+at_price+" and feature exciting animals");
}
else
{
System.out.println("Tickets average"+"$"+at_price);
}
}
}
//Eatery class which inherits from place
package chegg;
public class Eatery extends place {
double cost;
String cuisine;
int starRating;
public double getCost() {
return cost;
}
public String getCuisine() {
return cuisine;
}
public int getStarRating() {
return starRating;
}
public Eatery(String name, String desc, double latitude, double longitude, String cuisine, double cost, int starRating)
{
this.name=name;
this.desc=desc;
this.latitude=latitude;
this.longitude=longitude;
this.cost=cost;
this.cuisine=cuisine;
this.starRating=starRating;
}
@SuppressWarnings("null")
public String ratingToStar()
{
String stars=null;
for(int i=0;i<starRating;i++)
{
stars.concat("*");
}
return stars;
}
@SuppressWarnings("null")
public String getCostToSymbol()
{
String symbol=null;
if(cost<25)
{
symbol.concat("$");
}
else if((cost>=25)&&(cost<50))
{
symbol.concat("$$");
}
else if((cost>=50)&&(cost<75))
{
symbol.concat("$$$");
}
else if((cost>=75)&&(cost<100))
{
symbol.concat("$$$$");
}
return symbol;
}
public void ToString()
{
Eatery e=new Eatery("dgfdsg", "ghjg", 534.75,677.9,"hukh", 78, 4);
String price=e.getCostToSymbol();
System.out.println("the price is"+price);
String symbol=e.ratingToStar();
System.out.println("the rating is "+symbol);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.