Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Based on the class BC it y in the last question, write a new Java class named BSFCity for modeling Better Smoke-Free Cities with the

Based on the class BC it y in the last question, write a new Java class named BSFCity for modeling "Better Smoke-Free" Cities with the following requirements.

a) This BSFCity class is a public subclass of the given class BCity.

- This class has a specific instance field of string type, named sFreeRu1e, representing a specific "Smoke-Free" rule governing the "Better Smoke-Free" city.

- It has one constructor with the same set of parameters as its direct superclass.

- The constructor body simply calls the corresponding constructor of its superclass properly.

b) It has 2 public methods:

  • The first method setSFreeRu1e ( ) is for setting the specific field, the "Smoke-Free" rule.
    • The method accepts one input argument rule of string type and has no return.
    • This method body only sets the specific field with the input parameter value.
  • The second method overrides the method of its direct superclass for displaying information.
    • The method body first calls the overridden method of the superclass.
    • The method finally displays an extra line to show the specific "Smoke-Free" rule, as sample program output below.

Sample Program Output

(Suppose the specific "Smoke-Free" rule string is: "LAW of SMOKEFREE ")

City: BSF City, Pop: 8

Rule 21

Rule 22

Smoke-Free Rule : LAW of SMOKE-FREE

c) This class includes the main method, the entry point for starting the program.

  • This main method body first creates an array of City which contains 2 related city objects representing the given two cities listed in the table below.
  • This main method then calls the related method to set the smoke-free city object in the array a specific "Smoke-Free" rule string with format: "LAW of "

E.g. "LAW of CHAN Tai Man" if your full name is CHAN Tai Man.

  • This main method finally calls the related method to display the smoke-free city object in the array, as sample program output below.
  • NO more than 9 statement lines are allowed in the main ( ) method body.

Given Two Cities

Index Name Population Type List of rules
0 "A City" 2 City
1 "BSF City" 8 BSFCITY "Rule 21", "Rule 22"

Sample Program Output

image text in transcribed

Reference

image text in transcribed Based on the given references above and the class City in the last question, define a new Java class BC i ty for modeling "Better" Cities with the following requirements.

  1. This BCity class is a public subclass of the given class City o This class has a specific instance field named bRList (type ArrayList of string) representing the list of better rules (of string type) governing the better city.

o This field is initialized with an empty ArrayList of string.

  1. It has one constructor with 3 parameters (String cN, int c P, String C ] rules) : o The first and second parameters are for the name and population of the city object, while the third parameter is for the rules.
  • The body of this constructor first calls the corresponding constructor of its superclass with the two related input parameters.
  • The constructor body then adds all elements of the input array parameter into its own specific field bRList.

c) It has 2 public methods:

  • The method hasRu1e ( ) is for checking if the city includes a specific rule string.
  • The method accepts one input argument rule of string type and returns int value.
  • This method checks if the input argument rule appears in the field bRList and matches the string content of an element in the list bRLi st.
  • The method returns the index number of the first matched element found in the list.

The method returns -1 if no element found in the list matches the input argument rule o The method dispRList ( ) is for displaying information of the object on console.

  • The method has no input argument and no return value.
  • The method body first displays the general information of the city, then displays all its rules (one rule in a line) as sample program output below.

Sample Program Output

(Suppose City has: name "B Ci ty", Population 4, Rules: "Rule 11 " and "Rule 12")

City: BSF City, Pop: 8 Rule 21 Rule 22 Smoke-Free Rule: LAW of CHAN Tai Man O * Given reference, useful methods for accessing Java class ArrayList: add (elt): Add element to the end of the list, e.g. arrList.add(elt) get (index): Get an element (with index) in list, e.g. arrList.get(0) size(): Size (total number of elements) of the list, e.g. arrList.size() O o

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions