© A+ Computer Science - www.apluscompsci.com Magpie Magpie is a lab that focuses on classes, randomness, and Strings. This lab will make sure that you.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

A8 – Control Structures if, if-else, switch Control of flow in Java Any sort of complex program must have some ability to control flow.
String Processing Word processing term papers, writing memoirs, sending messages, responding to surveys, placing online orders and registering.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Strings in Java 1. strings in java are handled by two classes String &
Chapter 10 Review. Write a method that returns true is s1 and s2 end with the same character; otherwise return false. Sample Answer: public boolean lastChar(String.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Chapter 5 new The Do…Loop Statement
© 2007 Lawrenceville Press Slide 1 Chapter 6 The while Statement  Loop structure that executes a set of statements as long as a condition is true  The.
Week71 APCS-AB: Java Control Structures October 17, 2005.
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Strings Mr. Smith AP Computer Science A. What are Strings? Name some of the characteristics of strings: A string is a sequence of characters, such as.
Coding Bat: Ends in ly Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri". The string.
Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the keyword loop and ends with end loop. This will create.
Lab 4 - Variables. Information Hiding General Principle: – Restrict the access to variables and methods as much as possible Can label instance variables.
CSI 3125, Preliminaries, page 1 String. CSI 3125, Preliminaries, page 2 String Class Java provides the String class to create and manipulate strings.
Chapter 3A Strings. Using Predefined Classes & Methods in a Program To use a method you must know: 1.Name of class containing method (Math) 2.Name of.
© A+ Computer Science - A reference variable stores the memory address of an object. Monster fred = new Monster(); Monster sally.
AP Java Enhancing the Chatbot. Natural Language Processing Note: What is Natural Language Processing? What are some of the opportunities? What are some.
Java String 1. String String is basically an object that represents sequence of char values. An array of characters works same as java string. For example:
© A+ Computer Science - Until I can hear the song Make it louder Loops repeat as long as something is true.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
String Processing Word processing term papers, writing memoirs, sending messages, responding to surveys, placing online orders and registering.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
© 2010 Lawrenceville Press Slide 1 Chapter 5 The Do…Loop Statement  Loop structure that executes a set of statements as long as a condition is true. 
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
The Methods and What You Need to Know for the AP Exam
Introduction to programming in java
Arrays and Strings. Arrays in PHP Arrays are made up of multiple memory blocks, each with the same name and differentiated by an index number Each block.
Section 6.1 Introduction to String Methods. Section 6.1 Introduction to String Methods.
Chapter 2 Basic Computation
AP Java Enhancing the Chatbot.
© A+ Computer Science - Magpie Chatbot Lab © A+ Computer Science -
Introduction to Python
CS 1428 Exam I Review.
Expressions and Control Flow in JavaScript
String Handling in JAVA
Engineering Innovation Center
Engineering Innovation Center
Primitive Types Vs. Reference Types, Strings, Enumerations
Exposure Java 2015 AP®CS Edition Chapter 8 Slides Manipulating Strings
A+ Computer Science AP Review 2018 AP CS A EXAM
Introduction to Programming in Java
Chapter 6 The while Statement
String Objects & its Methods
Java Strings Slides provided by the University of Washington Computer Science & Engineering department.
Chapter 7: Strings and Characters
Object Oriented Programming
Chapter 5 The Do…Loop Statement
Multiple Choice -answer the easiest question 1st
Lesson 16: Functions with Return Values
CIS16 Application Development and Programming using Visual Basic.net
AP Magpie Chatbox "If a computer could think, how could we tell?"
© A+ Computer Science - What is a LOOP? © A+ Computer Science -
Logical Operations In Matlab.
C-to-LC3 Compiler Over the course of the next two weeks, you will build a program that will compile C code to LC-3 assembly language Don't panic! You.
Coding Concepts (Data- Types)
AP Java Enhancing the Chatbot.
See requirements for practice program on next slide.
Introduction to Computer Science
String methods 26-Apr-19.
Strings in Java.
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Magpie/Chatbot Activity 5
CS 1428 Exam I Review.
Variables and Constants
String Methods Strings have actions known as method. We will review a few of the methods associated with strings that are a part of the built in Java.
Presentation transcript:

© A+ Computer Science - www.apluscompsci.com Magpie Magpie is a lab that focuses on classes, randomness, and Strings. This lab will make sure that you know how to use the String methods substring and indexOf. Both substring and indexOf have multiple forms as these methods have been overloaded. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com What is Magpie? Magpie is a lab that focuses on classes, randomness, and Strings. This lab will make sure that you know how to use the String methods substring and indexOf. Both substring and indexOf have multiple forms as these methods have been overloaded. Magpie is a lab that focuses on classes, randomness, and Strings. This lab will make sure that you know how to use the String methods substring and indexOf. Both substring and indexOf have multiple forms as these methods have been overloaded. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com What is NLP? NLP stands for Natural Language Processing. It is a field of Computer Science that studies how computers can understand human language. The Magpie Chatbot lab is designed to help us explore some of the basics of NLP. To get started with these activities, we will first review the cascading if else structure and String methods. Add stuff here © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com String letter = "C"; int ascii=0; if(letter.equals("A")) { ascii=65; } else if(letter.equals("B")){ ascii=66; else if(letter.equals("C")){ ascii=67; else if(letter.equals("D")){ ascii=68; else{ ascii=69; out.println(ascii); Cascading if else OUTPUT 67 The Magpie Chatbot labs require students to modify existing code to add additional functionality. Adding more options to the getResponse method involves modifying the existing cascading if else structure. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com public String getResponse(String statement) { String response = ""; if (statement.indexOf("no") >= 0) response = "Why so negative?"; else if (statement.indexOf("mother") >= 0 || statement.indexOf("father") >= 0 || statement.indexOf("sister") >= 0 || statement.indexOf("brother") >= 0) response = "Tell me more about your family."; } else response = getRandomResponse(); return response; The Magpie Chatbot labs require students to modify existing code to add additional functionality. Adding more options to the getResponse method involves modifying the existing cascading if else structure. Cascading if else © A+ Computer Science - www.apluscompsci.com

Methods from AP CS Subset © A+ Computer Science - www.apluscompsci.com String Methods from AP CS Subset Name Use int length() Returns length of String int indexOf(String str) Returns first position of str in the string if found, -1 if not found String substring(int from) Returns a substring of the string starting at from to length() – 1 String substring(int from, int to) Returns a substring of the string starting at from to to – 1 Add stuff here © A+ Computer Science - www.apluscompsci.com

Methods from AP CS Subset © A+ Computer Science - www.apluscompsci.com String Methods from AP CS Subset Name Use boolean equals(Object other) Returns true if the other and this String match int compareTo(String str) Returns : a positive number if this string > str, a negative number if this string < str, 0 if this string is equal to str. Add stuff here © A+ Computer Science - www.apluscompsci.com

String Methods not in the AP CS Subset Name Use String toLowerCase() Returns length of String int indexOf(String str, int startPos) Returns position of str in the string starting at startPos if found, -1 if not found String trim() Returns a substring of the string without all leading and trailing whitespaces Add stuff here © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com open Magpie Lab \ Magpie Code \ Magpie2.java MagpieRunner2.java © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Start work on Activity 2 Lab © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Activity 3 Better Keyword Detection As you complete Activity 2, you will notice that your chatbot does not distinguish between whole word matches and partial word matches. For example, if your statement is: “Catch me if you can!” and you are looking for “cat” the getResponse method will return 0 instead of -1. Add stuff here © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Activity 3 Better Keyword Detection To facilitate whole word matches, Magpie3.java adds the findKeyword method. This method uses the String method int indexof(String str, int startPos) to determine if a whole word instead of a partial word is found. Add stuff here © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Start work on Activity 3 Part One Lab © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Activity 3 Better Keyword Detection Now that you have completed Part One of Activity 3 Lab, run the new version of your chatbot to see how it has changed. Use some of the original examples from Activity 2. Add stuff here © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com open Magpie Lab \ Magpie Code \ Magpie3.java MagpieRunner3.java H © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com findKeyword method Study the findKeyword method. Use the Activity 3 Worksheet to trace calls to this method. Questions: What is the purpose of the local variables before and after? Why are they initialized to a space at the beginning of each loop iteration? Add stuff here © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com findKeyword method Questions: What is the purpose of the local variables before and after? ANSWER: To determine if the found string is a whole word or partial word. If the characters before and after goal are not letters, a whole word version of goal has been found. Add stuff here © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com findKeyword method Questions: Why are before and after initialized to a space at the beginning of each loop iteration? ANSWER: If goal is found at the beginning of statement, there are not characters before it, so before is initialized to a space. This logic is used for after, if goal comprises the last characters of the string. Add stuff here © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Continue work on Activity 3 Part Two Lab © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Activity 4 Responses that Transform Statements In this activity, your chatbot will respond to certain phrases, not just specific keywords. This revised version responds to phrases in the form of “I want to something” and “Whatever you something me…” Run the new version of your chatbot. Add stuff here © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com open Magpie Lab \ Magpie Code \ Magpie4.java MagpieRunner4.java H © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Adding to getResponse //transform statements addition to getResponse else if(findKeyword(statement, "I want to", 0) >= 0) response = transformIWantToStatement(statement); else { int pos = findKeyword(statement, "you", 0); if (pos >= 0 && findKeyword(statement, "me", pos) >= 0) response = transformYouMeStatement(statement); } response = getRandomResponse(); Discuss how the additions to getResponse make sure that “I want to” is found in the statement before calling the transformIWantToStatement method to form a response to an “I want to something” statement. Discuss how the additions to getResponse make sure that the word you is found before me before calling the transformYouMeStatement method to form a response to a “you something me” statement. Note the use of the overloaded findKeyword method that uses position of where the word you was found to start the search for me. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com transformIWantStatement method private String transformIWantToStatement(String statement) { statement = statement.trim(); String lastChar = statement.substring(statement.length() - 1); if (lastChar.equals(".")) statement = statement.substring(0, statement.length() - 1); int pos = findKeyword(statement, "I want to", 0); String restOfStatement = statement.substring(pos + 9).trim(); return "What would it mean to " + restOfStatement + "?"; } Note: The length of “I want to” is 9. Discuss how this method returns the rest of the statement found after the phrase “I want to”. You may also wish to discuss the use of the constant 9 in statement.substring(pos + 9). © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com transformYouMeStatement method private String transformIYouMeStatement(String statement) { statement = statement.trim(); String lastChar = statement.substring(statement.length() - 1); if (lastChar.equals(".")) statement = statement.substring(0, statement.length() - 1); int posOfYou = findKeyword(statement, "you", 0); int posOfMe = findKeyword(statement, "me", posOfYou + 3); String restOfStatement = statement.substring(posOfYou + 3, posOfMe).trim(); return "What makes you think that I " + restOfStatement + " you?"; } Discuss how this method makes sure that you is found before me. Discuss how restOfStatement becomes the substring found between the words you and me. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Start work on the Activity 4 Lab © A+ Computer Science - www.apluscompsci.com