How would a program like this look writtenout? Implement two classes: Location,
ID: 3613625 • Letter: H
Question
How would a program like this look writtenout?Implement two classes: Location, State. These will be in separatejava files (Location.java, State.java).
Location is a superclass of State. Thismeans that State should "extend" the Location class.
Location should have the datafields: name: a String indicating thename of the location population: an int indicatingthe population of the location
Location should have get and set methodsfor both name and population, as well as a constructor to set nameand population.
State should extend Location and have anadditional field: flower: a String indicating thestate flower
State should have get and set methods forthe state flower, as well as a constructor to set name,population, and flower.
Write a class StateTester class with asingle main method that does the following: - Construct a Location objectwith name "Earth" and population 6,706,993,152 - Construct a State object withname "Louisiana", population 4,410,796, and state flower"Magnolia" - print out the name of Earthand Louisiana using the getName method. - print out the flower ofLouisiana using the getFlower method.
How would a program like this look writtenout?
Implement two classes: Location, State. These will be in separatejava files (Location.java, State.java).
Location is a superclass of State. Thismeans that State should "extend" the Location class.
Location should have the datafields: name: a String indicating thename of the location population: an int indicatingthe population of the location
Location should have get and set methodsfor both name and population, as well as a constructor to set nameand population.
State should extend Location and have anadditional field: flower: a String indicating thestate flower
State should have get and set methods forthe state flower, as well as a constructor to set name,population, and flower.
Write a class StateTester class with asingle main method that does the following: - Construct a Location objectwith name "Earth" and population 6,706,993,152 - Construct a State object withname "Louisiana", population 4,410,796, and state flower"Magnolia" - print out the name of Earthand Louisiana using the getName method. - print out the flower ofLouisiana using the getFlower method.
Location should have the datafields: name: a String indicating thename of the location population: an int indicatingthe population of the location
Location should have get and set methodsfor both name and population, as well as a constructor to set nameand population.
State should extend Location and have anadditional field: flower: a String indicating thestate flower
State should have get and set methods forthe state flower, as well as a constructor to set name,population, and flower.
Write a class StateTester class with asingle main method that does the following: - Construct a Location objectwith name "Earth" and population 6,706,993,152 - Construct a State object withname "Louisiana", population 4,410,796, and state flower"Magnolia" - print out the name of Earthand Louisiana using the getName method. - print out the flower ofLouisiana using the getFlower method.
Explanation / Answer
publicclass Location { // Location should havethe data fields: // name: a Stringindicating the name of the location // population: an intindicating the population of the location // instancevariables privateString name; privateint population; //constructors // Location should haveget and set methods for both name and population, as well as aconstructor to set name and population. publicLocation(String name, int population) { setName(name); setPopulation(population); } // accessors publicString getName() { return name; } publicint getPopulation() { return population; } // modifiers publicvoid setName(String n) { name=n; } publicvoid setPopulation(int p) { population=p; } } // State shouldextend Location public class State extendsLocation { // and have anadditional field: // flower: a Stringindicating the state flower // instancevariables privateString flower; // State should have getand set methods for the state flower, as well as a constructor toset name, population, and flower. //constructors publicState(String flower, String name, intpopulation) { super(name,population); setFlower(flower); } // accessors publicString getFlower() { return flower; } // modifiers publicvoid setFlower(String f) { flower=f; } } // Writea class StateTester class with a single main method that does thefollowing: public class StateTester { publicstatic void main(String[]args) { //Construct a Location object with name "Earth" and population6,706,993,152 Location earth= newLocation("Earth", 6706993152); //Construct a State object with name "Louisiana", population4,410,796, and state flower "Magnolia" State lousiana= newState("Magnolia", "Louisiana", 4410796); //print out the name of Earth and Louisiana using the getNamemethod. System.out.println(earth.getName()); System.out.println(louisiana.getName()); //print out the flower of Louisiana using the getFlowermethod. System.out.println(louisiana.getFlower()); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.