LogoLogo
    Logo
    • TutoringSchools
    1. Home
    2. IB
    3. Computer Science (CS)
    4. Questions

    The programming team have decided to replace the array of `Item`objects `(pl)`and the array of `Payment`objects `(tables)`with either a linked list or an ArrayList.

    Question
    HLPaper 2

    The programming team have decided to replace the array of Itemobjects (pl)and the array of Paymentobjects (tables)with either a linked list or an ArrayList.

    The array of Itemobjects (pl)is replaced by priceLL, an object of the LinkedList library class. The Itemobjects can be created, deleted or amended.

    The method changePrice()allows an item, identified by its item code, to have its price changed. All data required by this method are passed as parameters. If the item is not found then an appropriate message is displayed.

    1.

    Outline one benefit that the use of either of these choices will have over the original array structures.

    [2]
    Verified
    Solution

    Memory space for the exact number of objects can be assigned;
    Whereas the array will (inevitably) waste space/allot more memory than is needed/may run out of allotted memory/there is no need to determine array size;

    2.

    Construct the method changePrice().

    [6]
    Verified
    Solution

    Award marks as follows, up to 6max6 max6max.
    Award 111 for correct signature.
    _Award 111 for correct initialization of the length of the loop/size of the linked list/_Boolean/Iterator object as appropriate to the solution and appropriate message__displayed at end;
    Award 111 for correct loop.
    Award 111 for correct comparison, with or without getmethods.
    Award 111 for correct updating, with or without get methods.
    Award 111 for early exit if found.
    Award 111 for answer completely correct.

    Example 1

    public void changePrice(LinkedList pll, String c,
    double newPrice)
    {
    int i = 0;
    boolean found = false;
    int size = pll.size();
    while (i < size && !found)
    {
    if (pll.get(i).getCode().equals(c)) // allow “=”
    {
    pll.get(i).setPrice(newPrice);
    found = true;
    }
    i = i + 1;
    }
    }

    Example 2

    public void changePrice(LinkedList pll, String c,
    double newPrice)
    {
    Iterator iter = pll.iterator();
    boolean found = false;
    while (iter.hasNext() && !found)
    {
    Item current = iter.next();
    if (current.getCode().equals(c)) // allow “=”
    {
    current.setPrice(newPrice);
    found = true;
    }
    }
    }

    3.

    Explain how a binary tree structure would allow a more efficient search for the Itemobject.

    [4]
    Verified
    Solution

    The plarray/Itemobjects could be read into a binary tree;
    And placed in order of the Itemcode;
    (Successive) comparisons between the search item and tree item will reduce the search space by a half (each time);
    Which results in a faster search than a linear search/Olog(n)is better than O(n);
    As a linear search might have to loop through the whole list;

    Still stuck?

    Get step-by-step solutions with Jojo AI

    FreeJojo AI

    Want more practice questions for Computer Science (CS)?

    Related topics


    Join 350k+ students using RevisionDojo today

    Footer

    General

    • About us
    • Mission
    • Tutoring
    • Blog
    • State of learning surveyNew

    • Trustpilot
    • Contact us
    • Join us We're hiring!

    Features

    • Jojo AI
    • Questionbank
    • Study notes
    • Flashcards
    • Test builder
    • Exam mode
    • Coursework
    • IB grade calculator

    Legal

    • Terms and conditions
    • Privacy policy
    • Cookie policy
    • Trust Center

    IB

    • Biology (New syllabus)
    • Business Management
    • Chemistry (New syllabus)
    • Chinese A Lang & Lit
    • Chinese B
    • Computer Science (CS)
    • Design Technology (DT)
    • Digital Society (DS)
    • Economics
    • English B
    • View more...
    Logo

    © 2022 - 2025 RevisionDojo (MyDojo Inc)

    RevisionDojo was developed independently of the IBO and as such is not endorsed by it in any way.

    SAT® is a trademark registered and owned by the College Board®, which is not affiliated with and does not endorse this product or site.

    RedditInstagramTikTokDiscord
    GDPR compliant