This site is from a past semester! The current version is here.
CS2113/T Aug '19
  • Week 1 [Aug 12]
  • Week 2 [Aug 19]
  • Week 3 [Aug 26]
  • Week 4 [Sep 2]
  • Week 5 [Sep 9]
  • Week 6 [Sep 16]
  • Week 7 [Sep 30]
  • Week 8 [Oct 7]
  • Week 9 [Oct 14]
  • Week 10 [Oct 21]
  • Week 11 [Oct 28]
  • Week 12 [Nov 4]
  • Week 13 [Nov 11]
  • Textbook
  • Admin Info
  • Report Bugs
  • Slack
  • Forum
  • Project Info
  • Instructors
  • Announcements
  • File Submissions
  • Tutorial Schedule
  • Duke
  • Project Phase1 Dashboard
  • Java Coding Standard
  • samplerepo-things
  • Projects List
  • config.json templates for Reposense
  • PersonalAssistant-Duke
  • Project Phase2 Dashboard
  • Reference project - Addressbook
  • Repl.it classroom
  • Previous WeekNext Week

    Week 6 [Sep 16]

    Suggested tutorial questions

    Explain in your own words the interactions illustrated by this Sequence Diagram:

    Consider the code below:

    class Person{
        Tag tag;
        String name;
    
        Person(String personName, String tagName){
            name = personName;
            tag = new Tag(tagName);
        }
    }
    
    class Tag{
        Tag(String value){
            //...
        }
    }
    
    class PersonList{
        void addPerson(Person p){
            //...
        }
    }
    

    Draw a sequence diagram to illustrate the object interactions that happen in the code snippet below:

    PersonList personList = new PersonList();
    while (hasRoom){
        Person p = new Person("Adam", "friend");
        personList.addPerson(p);
    }
    

    See the chapter on Code Quality.

    Suggest ways to improve the quality of the code below.

    import java.io.FileNotFoundException;
    import java.util.*;
    
    public class CliApp {
    
        //...
        private static final String MESSAGE_COMMAND_HELP_PARAMETERS = "Parameters: %1$s";
        private static final String MESSAGE_COMMAND_HELP_EXAMPLE = "Example: %1$s";
        private static final String MESSAGE_DISPLAY_PERSON_DATA = "%1$s  Phone Number: %2$s  Email: %3$s";
        private static final String GOODBYE_MESSAGE = "Exiting Address Book... Good bye!";
        private static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format: %1$s";
        //...
    
        /**
         * List of all persons in the address book.
         */
        private static final ArrayList<String> person = new ArrayList<>();
    
    
        //...
    
        public static void main(String[] args) {
            String userCommand = "nothing"; boolean exit = false;
            if (args.length > 0) {
                showWelcomeMessage();
                processProgramArgs(args);
                loadDataFromStorage();
                while (!exit) {
                    System.out.print("Enter command: ");
                    userCommand = SCANNER.nextLine();
                    userCommand = userCommand.trim();
                    showToUser(userCommand);
                    String feedback = executeCommand(userCommand);
                    showResultToUser(feedback);
                }
            } else {
                showToUser("Incorrect usage!");
            }
        }
    
        private static void showResultToUser(String feedback) {
            switch (feedback) {
                case "0":
                    feedback = "Good (0)";
                    break;
                case "1":
                    feedback = "Better (1)";
                    break;
            }
            try {
                writeToFile(feedback);
            } catch (FileNotFoundException e) {
                //e.printStackTrace();
            }
        }
    
        private static void matcher(String s1, String s2) {
            // ...
        }
    
        /**
         * Show the {@code message} to the user
         */
        private static void showToUser(String message) {
            System.out.println(LINE_PREFIX + message.trim()); // add LINE_PREFIX in front
        }
    
    // ...
    
    }