QuestionQuestion

Transcribed TextTranscribed Text

Question#1 Suppose that a list can contain items and other lists. Therefore, the composite pattern can perfectly be used to implement such a list. For example, the list: [1 [2 3] [4 5] [6 4]] is composed of an item 1 and three lists: [2 3], [4 5] and [6 4]. In this program you will use Composite pattern and Builder pattern to implement a list structure. The following UML class diagram shows the program structure: The program will take an input in the form: [1 [2 3] [4 5] [6 4]] and store it using a ListComponent. The program should create a ListComposite and add a child of type Item for 1. Then it will add another child of type ListComposite for [2 3], in which it will add two Items 2 and 3, and so on. Item’s printValue() method only prints out its value. The following is a skeleton code for the main program: ListBuilder builder = new ListBuilder(); read in input list into a string and tokenize it. for each token in the input string if (token == “[“) builder.buildOpenBracket(); else if (token == “]” builder.buildCloseBracket(); else if (token == number) builder.buildElement(number); ListComponent list = builder.getList(); list.printValue(); Bonus marks: modify your program to work on lists in the form [1 [2 [4 5] 3] [6 4]]. You have to allow the program to support hierarchy of ListComposites. Hint: you may use an attribute in ListComposite to point to the “parent" ListComposite. This will help you to traverse back to the parent ListComposite after you finish adding items in the child ListComposite. 2 of 2 Question#2: Proxy pattern: write the following simple program to implement 3 protection proxies. The real subject is defined as following class Text { private String content; void setContent(String newContent) {this.content = newContent;} String getContent() {return content;} } Write proxies that can be set up at construction time to protect its real subject by allowing to read only, write only, or read/write (3 options) the content of the real subject. Write a main program to test your proxy objects with different protection options. Question#3: Write a simple class called ArrayListSubject that maintains an arraylist of objects. It provides two operations: void append(Object obj); //append the object to the end of the arraylist void delete(Object obj); //remove the object from the arraylist Use the observer pattern to define an observer that simply prints “an item being deleted” when the delete operation is called on ArrayListSubject. For all these questions, test your code with some inputs. You have to submit the following to the blackboard: 1-Source code. 2-Screenshots of the output. Put all the screenshots in a single word document.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import java.util.*;

/*
* ListBuilder main class
*/
public class ListBuilder {

// Fields to keep track of ListComponent object
ListComponent component;
int componentIndex;
// Fields to keep track of ListComposite object
ListComposite composite;
int compositeIndex;
// Level for hierarchy parsing
int level = 0;

// Method which processes an open bracket
public void buildOpenBracket() {
    level++;
    // Lowest level open bracket
    if (level == 1) {
      component = new ListComponent();
      componentIndex = 0;
    // Nested open bracket
    } else {
      composite = new ListComposite();
      compositeIndex = 0;
    }
}

// Method which processes an close bracket
public void buildCloseBracket() {
    level--;
    // Nested close bracket, add ListComposite to ListComponent
    if (level == 1) {
      component.addChild(componentIndex++, composite);
    }
}

// Method which processes an integer element
public void buildElement(int element) {
    // Lowest level, add to ListComponent
    if (level == 1) {
      component.addChild(componentIndex++, new Item(element));
    // Nested level, add to ListComposite
    } else if (level == 2) {
      composite.addChild(compositeIndex++, new Item(element));
    }
}

// Get ListComponent of ListBuilder
public ListComponent getList() {
    return component;
}

// Main method which runs upon execution
public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    // Initially, query user for input list
    String input = s.nextLine().replaceAll("\\s","");
    ListBuilder builder = new ListBuilder();
    // Apply ListBuilder methods to input
    for (char token: input.toCharArray()) {
      if (token == '[') {
       builder.buildOpenBracket();
      } else if (token == ']') {
       builder.buildCloseBracket...

By purchasing this solution you'll be able to access the following files:
Solution.zip and Solution1.zip.

$45.00
for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats