cs201 discussion 2 - university of california, berkeleyarunganesh/201discussions/discussion 2...

25
CS201 Discussion 2 TODAY’S TOPIC: A GENERAL APPROACH FOR SOLVING PROBLEMS IN CS201

Upload: others

Post on 28-Mar-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS201 Discussion 2 - University of California, Berkeleyarunganesh/201discussions/Discussion 2 Handout.pdfCS201 Discussion 2 TODAY’S TOPIC: A GENERAL APPROACH FOR SOLVING PROBLEMS

CS201 Discussion 2TODAY’S TOPIC: A GENERAL APPROACH FOR SOLVING PROBLEMS IN CS201

Page 2: CS201 Discussion 2 - University of California, Berkeleyarunganesh/201discussions/Discussion 2 Handout.pdfCS201 Discussion 2 TODAY’S TOPIC: A GENERAL APPROACH FOR SOLVING PROBLEMS

Never Lose Your Work!We are going to quickly teach you how to use Duke Box to back up your files.

This way, you should never lose your work for this class (or others) due to lost laptops, computers crashing, etc.

(Translation: No excuses)

Page 3: CS201 Discussion 2 - University of California, Berkeleyarunganesh/201discussions/Discussion 2 Handout.pdfCS201 Discussion 2 TODAY’S TOPIC: A GENERAL APPROACH FOR SOLVING PROBLEMS

Start Using BoxGo to box.duke.edu and select LOG IN

Page 4: CS201 Discussion 2 - University of California, Berkeleyarunganesh/201discussions/Discussion 2 Handout.pdfCS201 Discussion 2 TODAY’S TOPIC: A GENERAL APPROACH FOR SOLVING PROBLEMS

Free for Duke (CFO = Duke Alum)Select the “CONTINUE” option seen below, and use Shibboleth to log in with your NetID

If it’s your first time logging in, it will guide you through some basic instructions. Feel free to read through these - we’re only going to cover how to sync a folder today.

Page 5: CS201 Discussion 2 - University of California, Berkeleyarunganesh/201discussions/Discussion 2 Handout.pdfCS201 Discussion 2 TODAY’S TOPIC: A GENERAL APPROACH FOR SOLVING PROBLEMS

BoxSync: Sync with Your OSGo to https://duke.app.box.com/settings/sync and download BoxSync for your OS. Run the downloaded file and go through the installation menu. At the end, select “Launch” – when prompted, your username and password are your Duke email + password.

Once you see the word “Synced” in the window that follows, the “Box Sync” file will appear in your userspace. A shortcut will also appear on your Desktop.

Page 6: CS201 Discussion 2 - University of California, Berkeleyarunganesh/201discussions/Discussion 2 Handout.pdfCS201 Discussion 2 TODAY’S TOPIC: A GENERAL APPROACH FOR SOLVING PROBLEMS

Files Synced AutomaticallyNow, anytime you place a file in this folder, it will sync up to Box’s servers, and you should be able to access the file via box.duke.edu

You may wish to change your workspace to be inside your Box Sync folder – to do this, you can copy all the files in your current workspace into a folder within your Box Sync folder, and then access the workspace in Eclipse via File > Switch Workspace > Other… and selecting your new workspace location.

Page 7: CS201 Discussion 2 - University of California, Berkeleyarunganesh/201discussions/Discussion 2 Handout.pdfCS201 Discussion 2 TODAY’S TOPIC: A GENERAL APPROACH FOR SOLVING PROBLEMS

Prof. Hilton’s 7 Steps to Problem Solving1. Work a small example of the problem by hand2. Write down what you did3. Find patterns to scale your solution to a larger version of the problem4. Check by hand on a different example of the problem5. Translate your solution to code6. Develop and run test cases to verify your solution7. Debug your failed test cases using the scientific method

Today, we will work through Steps 1-5 with the CirclesCountry APT

Page 8: CS201 Discussion 2 - University of California, Berkeleyarunganesh/201discussions/Discussion 2 Handout.pdfCS201 Discussion 2 TODAY’S TOPIC: A GENERAL APPROACH FOR SOLVING PROBLEMS

CirclesCountryLet’s review the problem statement

Page 9: CS201 Discussion 2 - University of California, Berkeleyarunganesh/201discussions/Discussion 2 Handout.pdfCS201 Discussion 2 TODAY’S TOPIC: A GENERAL APPROACH FOR SOLVING PROBLEMS

Step 1: Work a small exampleLook at this example. How many boundaries do you need to cross to get from one point to another?

Page 10: CS201 Discussion 2 - University of California, Berkeleyarunganesh/201discussions/Discussion 2 Handout.pdfCS201 Discussion 2 TODAY’S TOPIC: A GENERAL APPROACH FOR SOLVING PROBLEMS

Step 2: What did you do?Let’s hear from you!

Page 11: CS201 Discussion 2 - University of California, Berkeleyarunganesh/201discussions/Discussion 2 Handout.pdfCS201 Discussion 2 TODAY’S TOPIC: A GENERAL APPROACH FOR SOLVING PROBLEMS

Step 3: Find patterns that scale

Circle color Is white inside?

Is black inside?

Should we cross?

Red

Yellow

Green

Blue

Try filling out this table:

Page 12: CS201 Discussion 2 - University of California, Berkeleyarunganesh/201discussions/Discussion 2 Handout.pdfCS201 Discussion 2 TODAY’S TOPIC: A GENERAL APPROACH FOR SOLVING PROBLEMS

Step 3: Find patterns that scale

Circle color Is white inside?

Is black inside?

Should we cross?

Red Yes Yes No

Yellow No No No

Green Yes No Yes

Blue No Yes Yes

You should get this:

What’s the general pattern? Is it scalable?

Page 13: CS201 Discussion 2 - University of California, Berkeleyarunganesh/201discussions/Discussion 2 Handout.pdfCS201 Discussion 2 TODAY’S TOPIC: A GENERAL APPROACH FOR SOLVING PROBLEMS

Step 4: Check by handFrom what we learned on the last slide, with a partner try developing a general solution.

Check one or two examples from https://www.cs.duke.edu/csed/newapt/circlescountry.htmland their answers to see if your solution works.

Page 14: CS201 Discussion 2 - University of California, Berkeleyarunganesh/201discussions/Discussion 2 Handout.pdfCS201 Discussion 2 TODAY’S TOPIC: A GENERAL APPROACH FOR SOLVING PROBLEMS

Step 5: Translate your solution to codeLet’s start by writing pseudo-code – abstract, high-level description of our code. This is easier to write than code, and easier to translate into code than English. ◦ In general, writing pseudo-code before starting a program is never

a bad idea.

Writing pseudo-code makes it easy to work out an algorithm without getting into specifics.

One thing to think about when writing pseudo-code – how can we determine if a point is inside a circle using Java code?

Page 15: CS201 Discussion 2 - University of California, Berkeleyarunganesh/201discussions/Discussion 2 Handout.pdfCS201 Discussion 2 TODAY’S TOPIC: A GENERAL APPROACH FOR SOLVING PROBLEMS

Step 5: Translate your solution to codeIf there is something we do often, we may want to make a helper method out of it – a smaller method our main method can call repeatedly.

Why might helper methods be considered good style?

What might be a good helper method to write here?

Page 16: CS201 Discussion 2 - University of California, Berkeleyarunganesh/201discussions/Discussion 2 Handout.pdfCS201 Discussion 2 TODAY’S TOPIC: A GENERAL APPROACH FOR SOLVING PROBLEMS

The power of helper methodsA good helper method to write would be inCirclewhich determines if a point is inside a circle. If you declare it as follows:private boolean inCircle(int x1, int y1, int x2, int y2, int r){

[more code here] //return inside; //return value you calculated

}

By calling inCircle(a, b, x, y, r) in leastBordersyou can cleanly determine if the point (a, b) is in the circle centered at (x, y) with radius r. This saves you from typing the same code over and over.

Note: there are many other

helper methods you could write

Page 17: CS201 Discussion 2 - University of California, Berkeleyarunganesh/201discussions/Discussion 2 Handout.pdfCS201 Discussion 2 TODAY’S TOPIC: A GENERAL APPROACH FOR SOLVING PROBLEMS

Step 5: Translate your solution to codeAt this point, all that’s left to do is write the body of your leastBorders method!

Some tips:◦ You will want to use a for loop and some sort of counter variable◦sqrDist = (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2);◦Math.pow(a, b) returns ab – this is useful but not necessary◦Math.sqrt(x) returns sqrt of x – this is useful but not necessary◦ You can easily represent the boolean statement “x or y but not

both” in Java as x != y (i.e, x and y are not equal).

Page 18: CS201 Discussion 2 - University of California, Berkeleyarunganesh/201discussions/Discussion 2 Handout.pdfCS201 Discussion 2 TODAY’S TOPIC: A GENERAL APPROACH FOR SOLVING PROBLEMS

Steps 6 & 7: Testing and debuggingThe APT Tester we provide you has many tests you can use to check your code, so for APTs there is no need to write your own tests (although it may be helpful).

Once you’ve finished CirclesCountry, on your own time, be sure to test and debug it until you get all greens.

Page 19: CS201 Discussion 2 - University of California, Berkeleyarunganesh/201discussions/Discussion 2 Handout.pdfCS201 Discussion 2 TODAY’S TOPIC: A GENERAL APPROACH FOR SOLVING PROBLEMS

A brief aside: Static vs non-staticWhen submitting APTs, do not make static methods. ◦ If you do, you will get a reported 0 of -1 corrected on the tests,

since the tester expects a non-static method.

Static and non-static methods are a difficult concept◦ We will cover the difference soon in lecture.

Math.sqrt and Math.pow are static so they do not require a “new Math…”◦ For now, know that you can directly call static methods (such as

helper methods) from non-static ones, but not vice-versa.

Page 20: CS201 Discussion 2 - University of California, Berkeleyarunganesh/201discussions/Discussion 2 Handout.pdfCS201 Discussion 2 TODAY’S TOPIC: A GENERAL APPROACH FOR SOLVING PROBLEMS

SoccerLeaguesLet’s get started on another APT – SoccerLeagues

This APT does not require as clever a solution, but processing the input can be tricky

Page 21: CS201 Discussion 2 - University of California, Berkeleyarunganesh/201discussions/Discussion 2 Handout.pdfCS201 Discussion 2 TODAY’S TOPIC: A GENERAL APPROACH FOR SOLVING PROBLEMS

Understanding the inputLook at the problem statement, then the input {“-DD”, “L-L”, “WD-”}. Can you tell what the result of Team 3 vs Team 1 at Team 3’s stadium is? How about Team 1 vs Team 2 at Team 1’s stadium?

Page 22: CS201 Discussion 2 - University of California, Berkeleyarunganesh/201discussions/Discussion 2 Handout.pdfCS201 Discussion 2 TODAY’S TOPIC: A GENERAL APPROACH FOR SOLVING PROBLEMS

Accessing the result of one gameGiven that the jth character of the ith element is the result of i vs j at i’s stadium, how can we access this character for any i/j pair? (The results array is named matches)

How can we check, for example, if team i got a home victory against team j? (i.e. if the character is ‘W’).

if ( ___ )

Page 23: CS201 Discussion 2 - University of California, Berkeleyarunganesh/201discussions/Discussion 2 Handout.pdfCS201 Discussion 2 TODAY’S TOPIC: A GENERAL APPROACH FOR SOLVING PROBLEMS

Accessing the result of one gameGiven that the jth character of the ith element is the result of i vs j at i’s stadium, how can we access this character for any i/j pair? (The results array is named matches)

matches[i].charAt(j)

How can we check, for example, if team i got a home victory against team j? (i.e. if the corresponding character is ‘W’).

if ( matches[i].charAt(j) == 'W' )

Page 24: CS201 Discussion 2 - University of California, Berkeleyarunganesh/201discussions/Discussion 2 Handout.pdfCS201 Discussion 2 TODAY’S TOPIC: A GENERAL APPROACH FOR SOLVING PROBLEMS

The coding approachThe way you’ll probably want to go about solving this APT is:

1. Initialize an array of each team’s points. (What size will this array be in terms of matches? What values should the array start with?)

2. For each game, figure out the result, and update each team’s points. (How could we use for loops to iterate through all i/j values? How many if statements will you need to check the result?)

3. Return the array.

Page 25: CS201 Discussion 2 - University of California, Berkeleyarunganesh/201discussions/Discussion 2 Handout.pdfCS201 Discussion 2 TODAY’S TOPIC: A GENERAL APPROACH FOR SOLVING PROBLEMS

Get started!Using all that you’ve learned, start working on the APTs! Feel free to ask your TAs for help

◦ These slides are available on Sakai if you want to look back on anything we used.