Category Archives: Class activity

Input/Output: Origami Half Areas

If you fold a square diagonally, you will create a right isosceles triangle. We will take the triangle’s area to be 1. Continue to halve this triangle again and again by placing the acute angles on top of each other. Write a java program, HalfAreas_YI.java and prompt the user how many times wants the square to be folded.

As an example, let’s say the user’s response to the prompt is 5.

Output: in full sentence display the number of folds and the area of the final triangle.

Find an origami paper on the last table and do it manually first. Do the calculations so you can check your program is doing the right thing.

Once you confirm your program is running properly, run the program for an input of 7 folds. Submit both, the hand calculation, the output for the test and the solution for the 7 folds.

NOTE: if you are aware of finding the solution by using a formula, you can add that information to your documentation. For this assignment you must use loops.

First fold. Let the area of this triangle be 1.
Second Fold
Third Fold
Fourth Fold
Fifth Fold

Arrays: Making Magic Squares

Screen Shot 2014-12-07 at 12.13.44 PM

2D Arrays

Classwork/Homework:

  1. Implement the following algorithm to construct magic n × n squares; it works only if n is odd.Set row = n – 1, column = n / 2.
    For k = 1 … n * n
    Place k at [row][column].
    Increment row and column.
    If the row or column is n, replace it with 0.
    If the element at [row][column] has already been filled
    { Set row and column to their previous values.
    Decrement row.}

    Here is the 5 × 5 square that you get if you follow this method:

    Screen Shot 2014-11-23 at 9.53.37 PM

    Write a program, MagicNSquare_YI.java whose input is the number n and whose output is the magic square of order n if n is odd. Include the code to check the new array is in fact a Magic Square. Display the sum of the columns, rows and diagonals.

  2. (Financial tsunami) Banks lend money to each other. In tough economic times, if a bank goes bankrupt, it may not be able to pay back the loan. A bank’s total assets are its current balance plus its loans to other banks. The diagram in the figure below shows five banks. The banks’ current balances are 25, 125, 175, 75, and 181 million dollars, respectively. The arrow from node 1 to node 2 indicates that bank 1 lends 40 million dollars to bank 2.

    If a bank’s total assets are under a certain limit, the bank is unsafe. The money it borrowed cannot be returned to the lender, and the lender cannot count the loan in its total assets. Consequently, the lender may also be unsafe, if its total assets are under the limit. Write a program, BankFinancing_YI.java to find all the unsafe banks. Your program reads the input as follows. It first reads two integers n and limit, where n indicates the number of banks and limit is the minimum total assets for keeping a bank safe. It then reads n lines that describe the information for n banks with IDs from 0 to n-1.
    The first number in the line is the bank’s balance, the second number indicates the number of banks that borrowed money from the bank, and the rest are pairs of two numbers. Each pair describes a borrower. The first number in the pair is the borrower’s ID and the second is the amount borrowed. For example, the input for the five banks in the figure is as follows (note that the limit is 201):

    5 201
    25 2 1 100.5 4 320.5
    125 2 2 40 3 85
    175 2 0 125 3 75
    75 1 0 125
    181 1 2 125
    

    The total assets of bank 3 are (75 + 125), which is under 201, so bank 3 is unsafe. After bank 3 becomes unsafe, the total assets of bank 1 fall below (125 + 40). Thus, bank 1 is also unsafe. The output of the program should be

    Unsafe banks are 3 1
    
    

    (Hint: Use a two-dimensional array borrowers to represent loans. borrowers[i][j] indicates the loan that bank i loans to bank j. Once bank j becomes unsafe, borrowers[i][j] should be set to 0.)

If you are done with every assignment, here is the next topics

PU Input and Output Standard classes

Book site
Screen Shot 2015-02-23 at 12.19.21 AM

Lesson’s pdf
Screen Shot 2015-11-22 at 4.48.06 PM

Create a new project:
InputOutput
You can get the following files from edmodo.com. Add them to your workspace:

StdIn.java
StdOut.java
StdDraw.java
StdAudio.java

Arrays: Connect 4 Algorithm – 2D Arrays

Let’s not just connect 4. Let’s connect what you have learned about arrays and everything that brought you to this point to develop your first AI game in java.

  1. What does “your” simplified flowchart for a game look like?
Invent With Python – https://inventwithpython.com/invent4thed/chapter7.html

2. Let’s talk about “your” algorithm.

Let’s play the game

Red is the player – Blue is the computer
Answer the following questions
1. How would your program select the next move?
2. When would your program choose to make a random move?
3. When would your program make an intelligent move?
4. If the red ball numbered 1 was the first move, can you trace the following steps up to this point?
5. Can you make an educated guess as to how many different ways?

3. Use your “Toolkit” from previous lessons:

2D Arrays Intro

  1. You learned how to add rows:

    int total = 0;
    for (int i = 0; i < COUNTRIES; i++)
    {
      total = 0;
      for (int j = 0; j < MEDALS; j++) 
      {
        total = total + counts[i][j];
      }
    }
    

  2. You learned how to add columns:
    int total = 0;
    for (int j = 0; j < MEDALS; j++)
    {
      total = 0;
      for (int i = 0; i < COUNTRIES; i++) 
      {
        total = total + counts[i][j]; 
      }
    }
    

And accessing the neighboring elements of a 2D array

and from Magic Square

You learned how to add diagonals:

sum = 0;
for (int i = 0; i < square.length; i++) {
            sum += square[i][i];
        }

sum = 0;
        for (int i = 0; i < square.length; i++) {
            sum += square[i][square.length-i];
        }

4. Add details to your simplified flowchart. Let's develop the "computer to win" a bit further. You do not want to play against an infallible player (the computer) because you will not even want to play the game. So a better approach would be a good blend of random moves with smart moves. You decide how "smart" the computer is.

5. Levels of difficulty. Choose the "Medium" level of difficulty and start the same as in the first game. Do the same for "Hard" level and compare to easy and medium. How different are the behaviors of the "computer" moves? Can you find a pattern?

Arrays: Students Show and Tell

Welcome back!!
new-year-gif
Class activity:
Discussions and students’ presentations about the following assignments:
StdIn/StdOut – Hwk 12/3 -YI_Duplicates.java
StdDraw – Hwk 12/4 – YI_TriangleTess.java
StdDraw – Clwk 12/4 – YI_MyGraphPaper.java
StdDraw – Hwk 12/7 – YI_SongLyrics.java
StdDraw – Clwk 12/8 – YI_Histogram.java

Homework:
1 Question on edmodo.com
Work on missing assignments.

OOD: Pre conditions – Person

bug report
xkcd
Assignments:
1. Enhance the BetterBankAccount_YI class by adding preconditions
For the constructor: require the amount parameter to be at least zero
For the deposit method: require the amount parameter to be at least zero
For the withdraw method: the amount to be a value between 0 and the current balance

Driver: Write enough code in the tester class to invoke (call) all the methods.
Atm machines.

  1. Write a class Person with the following specifications:

a. Constructor: it takes a name as a string, a second string argument that describes what member of the family it is and, a third string describing the member activity to create a short story. You can make any changes you feel is necessary to accomplish this.

b. Mutator Method: setAge(int anAge). It prompts the user for age and updates the class instance field.

c. Mutator Method: setWhatIamDoing(String doing). It prompts the user and updates the class instance field currentActivity. Keep the number of activities low so it doesn’t become too lengthly.

d. Accessor Method: getWhatIamDoing(). It displays the currentActivity.

Tester class: MyFamilyDriver.java

despicableme2

Create or “instantiate” 4 persons, a mother, a father, a son or a daughter and a friend.
Simulate the following set of events:
The mother keeps milk in the refrigerator.
The father keeps juice in the refrigerator.
The son keeps a can of soda in the refrigerator.
The friend is thirsty and wants to drink something.

This is a story about “encapsulation” so your ending must reflect this concept. Be creative and use your imagination. The lines above is only an example. Do not use it for your own story. You can add more methods and instance fields to the Person ADT so you can make the simulation work nicely.

Some java language help:
String a = “abc”;
String b = “abcdefg”;
System.out.println(b.contains(a)); // true

Event: Philadelphia Classic Spring 2014

December 4th, 2014

Philadelphia Classic Spring 2014 Contest Problem Set


Screen Shot 2014-12-04 at 9.05.16 AM

Do problems 1 through 4 in class and for homework.
Work with a partner. DO NOT LOOK FOR THE SOLUTIONS

Pay special attention to the following:
1. Make sure you read the instructions in the first 2 pages.
2. Read the problem many times before you get started.
3. Draft a plan on paper or a document before you start.
4. Test parts of the code before you develop the “whole” program.
5. Do not use outside help of any sort.
6. All students have to turn in the assignments to edmodo.com