Notice
Recent Posts
Recent Comments
Link
관리 메뉴

데이터 과학

Case Study - Magpie lab 본문

AP/AP Computer Science A

Case Study - Magpie lab

티에스윤 2023. 8. 22. 20:46

AP Comsa A 사례 학습 중에서 Magpie lab에 대한 내용입니다. 

 

사례 학습이 3개가 있는데 Magpie lab은 챗봇에 대한 내용입니다. 챗봇 내용도 간단하고 온라인 컴파일러로도 간단히 실습할 수 있습니다. 

 

컬리지보드에 가면 관련 내용에 대 PDF가 있습니다. 

 

https://secure-media.collegeboard.org/digitalServices/pdf/ap/ap-compscia-magpie-lab-student-guide.pdf

 

 

깃허브에도 관련소스가 링크 걸려 있는데 다운로드 받아서 실행해 봐도 됩니다. 전체적인 알고리즘 정도는 공부해서 알고 있어야 합니다. 

소스도 그렇게 어렵지 않고, 온라인 자바 컴파일러로도 실습이 가능하니 실행해 봅시다. 

 

activity1

activity2

activity3

activity4

 

이렇게 구성되어 있으며, activity1은 문서이며 activity2, activity3, activity4가 소스입니다. 

 

https://github.com/jvperrin/magpie-lab/tree/master/magpie-starter-code

 

 

activity2에 대한 소스는 다음과 같으며 이를 자바 컴파일러에 넣고 실행하면 됩니다. 

 

 

 

import java.util.Scanner;

/**
 * A simple class to run the Magpie class.
 * @author Laurie White
 * @version April 2012
 */
class MagpieRunner2
{

/**
 * Create a Magpie, give it user input, and print its replies.
 */
public static void main(String[] args)
{
Magpie2 maggie = new Magpie2();

System.out.println (maggie.getGreeting());
Scanner in = new Scanner (System.in);
String statement = in.nextLine();

while (!statement.equals("Bye"))
{
System.out.println (maggie.getResponse(statement));
statement = in.nextLine();
}
}

}


/**
 * A program to carry on conversations with a human user.
 * This is the initial version that:
 * <ul><li>
 *       Uses indexOf to find strings
 * </li><li>
 *      Handles responding to simple words and phrases
 * </li></ul>
 * This version uses a nested if to handle default responses.
 * @author Laurie White
 * @version April 2012
 */
class Magpie2
{
/**
 * Get a default greeting
 * @return a greeting
 */
public String getGreeting()
{
return "Hello, let's talk.";
}

/**
 * Gives a response to a user statement
 *
 * @param statement
 *            the user statement
 * @return a response based on the rules given
 */
public String getResponse(String statement)
{
String response = "";

if (statement.indexOf("no") >= 0 || statement.indexOf("No") >= 0) {
response = "Why so negative?";
} else if (statement.indexOf("Elliot") >= 0 || statement.indexOf("elliot") >= 0) {
response = "Yes, Elliot is the ruler of the observable Universe and all Domains thereof.";
} else if (statement.indexOf("Jason") >= 0 || statement.indexOf("jason") >= 0) {
response = "Jason should have added the keywords himself so that he would get something cool said about him. But he didn't!";
} else if (statement.indexOf("Sun") >= 0 || statement.indexOf("sun") >= 0) {
response = "The sun is very hot.";
} 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 if (statement.indexOf("dog") >= 0 || statement.indexOf("cat") >= 0) {
     response = "Tell me more about your pets.";
    } else if (statement.indexOf("Dr. Petach") >= 0) {
     response = "She sounds like a good teacher.";
    } else if (statement.trim().length() == 0) {
     response = "Say something, please.";
    } else {
response = getRandomResponse();
}
return response;
}

/**
 * Pick a default response to use if nothing else fits.
 * @return a non-committal string
 */
private String getRandomResponse()
{
final int NUMBER_OF_RESPONSES = 6;
double r = Math.random();
int whichResponse = (int)(r * NUMBER_OF_RESPONSES);
String response = "";

if (whichResponse == 0)
{
response = "Interesting, tell me more.";
}
else if (whichResponse == 1)
{
response = "Hmmm.";
}
else if (whichResponse == 2)
{
response = "Do you really think so?";
}
else if (whichResponse == 3)
{
response = "You don't say.";
}
else if (whichResponse == 4)
{
 response = "The HORSE is a noble animal.";
}
else if (whichResponse == 5)
{
response = "You should go give Elliot Gorokhovsky five dollars.";
}
return response;
}
}

activity3, activity4 내용이 activity2보다는 복잡해지지만 실습해 보면 됩니다. 

 

 

 

https://www.onlinegdb.com/online_java_compiler#

 

Online Java Compiler - online editor

OnlineGDB is online IDE with java compiler. Quick and easy way to run java program online.

www.onlinegdb.com

 

activity5 소스

 

MagpieRunner5.java
0.01MB

 

 

 

 

https://apcentral.collegeboard.org/courses/ap-computer-science-a/exam/past-exam-questions

 

AP Computer Science A Past Exam Questions – AP Central | College Board

 

apcentral.collegeboard.org

 

'AP > AP Computer Science A' 카테고리의 다른 글

super 예제  (1) 2024.01.05
상속 예제  (0) 2024.01.04
생성자 예제  (0) 2024.01.04
제어문 관련 예제  (1) 2024.01.04
AP computer Science A 시작  (0) 2022.03.06