Spread the word.

Share the link on social media.

Share
  • Facebook
Have an account? Sign In Now

Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here
Sign InSign Up

SIKSHAPATH

SIKSHAPATH Navigation

  • Home
  • Questions
  • Blog
    • Computer Science(CSE)
    • NPTEL
    • Startup
  • Shop
    • Internshala Answers
Search
Ask A Question

Mobile menu

Close
Ask A Question
  • Home
  • Questions
  • Blog
    • Computer Science(CSE)
    • NPTEL
    • Startup
  • Shop
    • Internshala Answers
Home/ Questions/Q 10157
Next
In Process

SIKSHAPATH Latest Questions

Berlin
  • 1
  • 1
Berlin
Asked: December 30, 20212021-12-30T20:51:28+05:30 2021-12-30T20:51:28+05:30In: Programming Language

Given reference, useful methods for accessing Java class ArrayList: add (elt): Add element to the end of the list, e.g. …

  • 1
  • 1
Given reference, useful methods for accessing Java class ArrayList: add (elt): Add element to the end of the list, e.g. …

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()

Based on the given references above and the class City in the last question, define a new Java class BCity for modeling “Better” Cities with the following requirements.

a) This BCity class is a public subclass of the given class City

  • 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.
  • This field is initialized with an empty ArrayList of string.

b) It has one constructor with 3 parameters (String CN, int CP, String[] rules):

  • 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.
javajava class arraylist
  • 1 1 Answer
  • 269 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    1 Answer

    • Voted
    • Oldest
    • Recent
    1. I'M ADMIN
      I'M ADMIN
      2021-12-30T20:57:48+05:30Added an answer on December 30, 2021 at 8:57 pm
      package com.company;
      
      //BCity is a public subclass of City
      public class BCity extends City{
          //bRList is a variable of type ArrayList and is initialied with an empty list
          List<String> bRlist = new ArrayList<>();
          //The constructor has 3 arguments as mentioned
          BCity(String cN, int cP, String[] rules){
              //super keyword calls the parent constructor with the input arguments
              super(cN, cP);
              this.bRlist = Arrays.asList(rules);
          }
          //hasRules has returnType of int and accepts an input parameter of type String
          public int hasRules(String rule){
              int flag = -1;
              //loop over brList
              for (int i = 0; i< this.bRlist.size(); i++) {
                  //whenever the element matches assign the value of flag and break the loop
                  if(this.bRlist.get(i) == rule){
                      flag = i;
                      break;
                  }
              }
              //return the value of flag
              return flag;
          }
          //dispRList has returnType of void and donot have any input parameters
          public void dispRList(){
              //this will display the city and population value
              System.out.println("City "+ this.cN + "Pop "+ this.cP);
              //loop over bRList to display as mentioned
              for(String rule: this.bRlist){
                  System.out.println(rule);
              }
          }
      }
        • 0
      • Reply
      • Share
        Share
        • Share on WhatsApp
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn

    Leave an answer
    Cancel reply

    You must login to add an answer.

    Forgot Password?

    Need An Account, Sign Up Here

    Sidebar

    store ads

    Stats

    • Questions 1k
    • Answers 1k
    • Posts 150
    • Best Answers 89
    • Cloud Computing Notes 101: Learn Fundamentals, Service Models, and Virtualization!
    • This Free AI Tool Translates Entire Books in Minute !
    • AI News: 🎬 Hollywood’s AI Studios, 🎓 OpenAI’s Latest Gift to Educators, 🚚 Class8 Bags $22M, 🧠 Google Gemini’s Memory Upgrade
    • AI NEWS: Legal Action Against OpenAI, $16M Paid, & Elon Musk’s Praise from Investor 🤖💰📑 | AI Boosts Cloud Seeding for Water Security 🌱💧
    • AI News: 🎬AI Video Tool Scam Exposed🤯, 🛰️ AI-Powered Drones to Ukraine 😱, Google’s $20M AI Push, Sam Altman Joins SF’s Leadership Team

    Explore

    • Recent Questions
    • Questions For You
    • Answers With Time
    • Most Visited
    • New Questions
    • Recent Questions With Time

    Footer

    SIKSHAPATH

    Helpful Links

    • Contact
    • Disclaimer
    • Privacy Policy Notice
    • TERMS OF USE
    • FAQs
    • Refund/Cancellation Policy
    • Delivery Policy for Sikshapath

    Follow Us

    © 2021-24 Sikshapath. All Rights Reserved

    Insert/edit link

    Enter the destination URL

    Or link to existing content

      No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.