Category Archives: Classwork

OOD: Shannon Entropy

Classwork/Homework:

These assignments are to be developed using the basic elements of object oriented design.

Generating cryptograms. A cryptogram is obtained by scrambling English text by replacing each letter with another letter. Write a program, YI_Crypto1.java to generate a random permutation of the 26 letters and use this to map letters. Give example: Don’t scramble punctuation or whitespace.

crypto

Entropy. The Shannon entropy measures the information content of an input string and plays a cornerstone role in information theory and data compression.

Screen Shot 2015-05-05 at 8.21.45 PM            Shannon Entropy Calculator

Shannon is famous for having founded information theory.
It was proposed by Claude Shannon in 1948, borrowing upon the concept in statistical thermodynamics. Assuming each character i appears with probability p(_{i}), the entropy is defined to be H = – sum p(_{i}) log(_{2})p(_{i}), where the contribution is 0 if p(_{i}) = 0. Compute entropy of DNA sequence.

(a) Write a program, YI_Entropy1.java to read in a ASCII text string from standard input, count the number of times each ASCII character occurs, and compute the entropy, assuming each character appears with the given probabilities.

(b) Repeat part (a) but use Unicode.

entropy1

Screen Shot 2015-05-25 at 1.40.56 AM

OOD: Advanced topic: The “this” operator

Designing classes
The this operator
Make sure you read thoroughly.
1. Make the following changes to the BankAccount class:
a. Include a print statement in the getBalance() method to show the reference to the implicit parameter. Include “from method getBalance”
b. Include a print statement in both constructors, “from method default constructor” and “from constructor with parameter”.
2. Implement a test class, ThisOperatorTest. In this test, create an object of the class BankAccount and print it.
3. Include a commented paragraph at the end of the test class making a conclusion on the output. Make sure you explain what the this operator does in the two programs and compare them.
4. The following questions refer to calling by reference, by value and side effects:

• Modify your BankAccount class to include a new method, transfer. This method has two input parameters, double amount and double otherBalance from a different BankAccount object. The method takes money from the calling object’s balance and increases the balance from the other object by the “amount” value. The method also includes two print statements to display each object.

System.out.println(“From transfer( ), current object @: ” + _missing parameter);
System.out.println(“From transfer( ), otherAccount @: ” + otherBalance);

Create a test class, BankAccountTest1. In main:
• Create two BankAccount objects.
• Display the balance from both objects.
• Call the transfer method from one object to transfer money to the other object’s balance.
• Display both accounts’ balance.

A different approach to the same idea in 4:
• Modify your BankAccount class to include a new method, transfer. This method has two input parameters, double amount and BankAccount otherAccount. The method takes money from the calling object’s balance and increases the otherAccount’s balance by the “amount” value. The method also includes two print statements to display each object.

System.out.println(“From transfer( ), current object @: ” + _missing parameter);
System.out.println(“From transfer( ), otherAccount @: ” + otherAccount);

Look at edmodo.com for the different posts and due dates.

OOD Project: gpa calculator

Classwork:
Write a program, YI_GPACalculator.java to read grades from a text file. The grades will have a specific structure so the marking periods, the subjects, and categories can be identified.

The file structure given here is just an example. You should notice it has some redundancy. You can configure your own structure to enable easier implementation.

[spoiler title=’gpadata.txt’]
Q1
Subject: English
Categories start:
Homework: .30
Classwork: .30
Test: .40
Categories end
Data:
Homework:
75
90
85
Classwork:
65
75
78
Test:
100
98
99
Subject: Math
Categories start:
Homework: .25
Classwork: .25
Test: .50
Categories end
Data:
Homework:
86
88
100
Classwork:
60
70
80
Test:
100
90
91
Q1 end
Q2
Subject: English
Categories start:
Homework: .30
Classwork: .30
Test: .40
Categories end
Data:
Homework:
95
80
81
Classwork:
55
95
88
Test:
93
100
85
Subject: Math
Categories start:
Homework: .25
Classwork: .25
Test: .50
Categories end:
Homework:
86
88
100
Classwork:
60
70
80
Test:
87
96
81
Q2 end
[/spoiler]

Here is some code that might help you read the file.

  1. Using PU libraries: StdIn.java
/**
 * Calculate the GPA given a file with all the requirements.
 * Q1
 * Subject: English
 * Categories start:
 * Homework: .30
 * Classwork:
 * ....
 * ....
 * END
 * 
 * java GPACalculator1 < gpadata.txt
 * 
 */
public class GPACalculator1
{
    public static void main(String [] args)
    {

            while (StdIn.hasNextLine()) {
                String s1 = StdIn.readLine();
                System.out.println(s1);
            }
          
    
    }
    
}

  1. Using java libraries:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
 * Calculate the GPA given a file with all the requirements.
 * Subject: English
 * Catgories beginning:
 * 
 */
public class GPACalculator2
{
    public static void main(String [] args)
    {
        
        File file = new File(args[0]);
    
        try {
    
            Scanner sc = new Scanner(file);
    
            while (sc.hasNextLine()) {
                String s1 = sc.nextLine();
                System.out.println(s1);
            }
            sc.close();
        } 
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    
    }
    
}

Before you get started writing code,
1. Draft a flowchart or a concise pseudocode.
2. Trace few grades from one subject for Q1 and Q2.
3. Manually calculate the GPA for the data used in the trace.
4. Confirm that your trace and calculation coincide.

Submit all your work to edmodo.com.

OOD: ArrayList – Intro

/**
 * Test of ArrayLists basic instructions
 * 
 * @ge
 * @4/16/18
 */
import java.util.ArrayList;

public class ArrayListTest
{
   public static void main(String [] args)
   {
        
       ArrayList myList = new ArrayList();
       //ArrayList myList = new ArrayList(); // more efficient - eliminates warning
       Integer x = new Integer(45); // creates Integer 45
       
       myList.add(x); // adds Integer 45 to myList
       myList.remove(x); // removes the value 45 from myList
       x = new Integer(100); // creates Integer 100 
       myList.add(0,x); // adds Integer 100 to the beginning of myList

       System.out.println(myList.get(0)); // print the first element 
       System.out.println(myList.size()); // print the number of elements
       
       // One step further
       myList.add(5);
       myList.add(-27);
       myList.add(55);
       myList.add(0,3);
   }
}

Classwork:

Let’s use the following documentation Java™ Platform, Standard Edition 8
API Specification to create our own resources: 

ArrayList

From the documentation copy and paste the following methods’ description:

* 1
* boolean add(E e)
*
* 2
* void add(int index, E element)

* Having two methods with the same name but different argument list
* is called OVERLOADING
*
* 3
* E get(int index)
*
* 4
* E set(int index, E element)
*
* 5
* int indexOf(Object o)
*
* 6
* boolean isEmpty()
* Returns true if this list contains no elements.
*
* 7
* E remove(int index)
*
* 8
* boolean remove(Object o)
*
* 9
* int size()

Resource:
Screen Shot 2016-01-15 at 7.46.11 AM

Assignment:
ArrayListBasics_YI.java
Write a program that does the following:
Choose a meaningful and complete message for each item in the list.

  1. It creates an ArrayList, anAList of 100 random integers between any values from 1 to 1000 and find the two consecutive integers with the smallest difference between the two of them.

  2. It finds and prints with a message the largest value in anAList.

  3. It finds and prints with a message the average value in anAList.

  4. It prints all the numbers from anAList separated with a space and in one line.

  5. It prints the numbers from anAlist separated by 3 spaces in 10 columns by 10 rows format. Use printf to achieve this format. Do not use tab: “\t”.

  6. Prompt the user for an integer and replace the 10th Integer with this new one. Display a message to acknowledge wether it was found.

  7. Prompt the user for a number and search for that number. Display the number and the index where it was found.

  8. Remove 10th Integer and print a message with the number.

  9. Print the number of elements the ArrayList anAlist contains.

Note: Use printf to keep all your numbers tab properly.

/**** Make sure your documentation includes the output ****/

OOD – Basic Elements of ADTs – Cryptography – Shannon

Screen Shot 2015-05-01 at 11.18.58 AM

In.java

Screen Shot 2015-04-29 at 10.32.07 AM

Basic elements of a data type.

  • API.
  • Class.
  • Access modifiers.
  • Instance variables.
  • Constructors.
  • Instance methods.
  • Variable names in methods.

Basic components that you need to understand to be able to build data types in Java.

classcharge

 

NOTE:

You should be working on the programming assignment and gradient assignments before you start working on OOD programs.

 

Classwork/Homework: NO DUE DATE HAS BEEN ASSIGNED YET

These assignments are to be developed using the basic elements of data types.

Generating cryptograms. A cryptogram is obtained by scrambling English text by replacing each letter with another letter. Write a program, YI_Crypto1.java to generate a random permutation of the 26 letters and use this to map letters. Give example: Don’t scramble punctuation or whitespace.

crypto

Entropy. The Shannon entropy measures the information content of an input string and plays a cornerstone role in information theory and data compression.

Screen Shot 2015-05-05 at 8.21.45 PM        Screen Shot 2015-05-05 at 8.23.34 PM    Shannon Entropy Calculator

Shannon is famous for having founded information theory.
It was proposed by Claude Shannon in 1948, borrowing upon the concept in statistical thermodynamics. Assuming each character i appears with probability p(_{i}), the entropy is defined to be H = – sum p(_{i}) log(_{2})p(_{i}), where the contribution is 0 if p(_{i}) = 0. Compute entropy of DNA sequence.
(a) Write a program, YI_Entropy1.java to read in a ASCII text string from standard input, count the number of times each ASCII character occurs, and compute the entropy, assuming each character appears with the given probabilities.
(b) Repeat part (a) but use Unicode.

entropy1

Screen Shot 2015-05-25 at 1.40.56 AM

OOD: Chapter 2: VN 2.1 BlueJ

OOD Resources
BlueJ – Objects First

Chapter 2: VN 2.1 The naive ticket machine project


Chapter 2: VN 2.2 introduction to source code – fields and constructors


Chapter 2: VN 2.3 Creating, documenting and testing a new class


Chapter 3: VN 3.1 Fields of class types


Chapter 3: VN 3.2 Constructors and field initialization


Chapter 3: VN 3.3 Solving the 12-hour clock exercise

OOD Project: LFSR by steps

Animations using java
duke

Screen Shot 2015-02-23 at 12.19.21 AM

REMINDER: Ms. Newman should be coming to PHS after school

  1. Simulate one step. The step() method simulates one step of the LFSR and returns the rightmost bit as an integer (0 or 1). For example,
// LFSR lfsr1 = new LFSR("01101000010", 9);
// for this assignment the seed and tap can be hardcoded or input
String lfsr1 = "01101000010";
int tap = 9;
StdOut.println(lfsr1);
for (int i = 0; i < 10; i++) {
    // int bit = lfsr1.step();
    // code to create a sequence of 0s and 1s like the example below
    StdOut.println(lfsr1 + " " + bit);
}

outputs
01101000010
11010000101 1
10100001011 1
01000010110 0
10000101100 0
00001011001 1
00010110010 0
00101100100 0
01011001001 1
10110010010 0
01100100100 0

  1. Extracting multiple bits. The method generate() takes a positive integer k as an argument and returns a k-bit integer obtained by simulating k steps of the LFSR. This task is easy to accomplish with a little arithmetic:
  • initialize a variable to zero
  • for each bit extracted, double the variable
  • add the bit returned by step()

For example, extracting (from left to right) each bit from the sequence 1 1 0 0 1, the variable takes on the values 1, 3, 6, 12, and 25, ending with the binary representation of the bit sequence.

gen = 0
gen = gen * 2 + new_bit = 0 * 2 + 1 = 1
gen = gen * 2 + new_bit = 1 * 2 + 1 = 3
gen = gen * 2 + new_bit = 3 * 2 + 0 = 6
gen = gen * 2 + new_bit = 6 * 2 + 0 = 12
gen = gen * 2 + new_bit = 12 * 2 + 1 = 25

For example,

// LFSR lfsr2 = new LFSR("01101000010", 9); 
// for this assignment the seed and tap can be hardcoded or input
String lfsr2 = "01101000010";
int tap = 9;
StdOut.println(lfsr2);
for (int i = 0; i < 10; i++) {
    // int r = lfsr2.generate(5);
    // code to generate a number like 25 in the example above
    StdOut.println(lfsr2 + " " + r);
}

outputs
01101000010
00001011001 25
01100100100 4
10010011110 30
01111011011 27
01101110010 18
11001011010 26
01101011100 28
01110011000 24
01100010111 23
01011111101 29

Classwork:
Write a java program, YI_LFSRSim.java to generate an output like the one right above for a given seed.
NOTE: It doesn’t have to be OOP.

Cryptography – Caesar Cipher

The Caesar Cipher

The key for the Caesar Cipher will be a number from 1 to 26. Unless you know the key (that is, know the number used to encrypt the message), you won’t be able to decrypt the secret code.

The Caesar Cipher was one of the earliest ciphers ever invented. In this cipher, you encrypt a message by taking each letter in the message (in cryptography, these letters are called symbols because they can be letters, numbers, or any other sign) and replacing it with a “shifted” letter. If you shift the letter A by one space, you get the letter B. If you shift the letter A by two spaces, you get the letter C. Figure 14-1 is a picture of some letters shifted over by three spaces.

To get each shifted letter, draw out a row of boxes with each letter of the alphabet. Then draw a second row of boxes under it, but start a certain number (this number is the key) of spaces over. After the letters at the end, wrap around back to the start of the boxes. Here is an example with the letters shifted by three spaces:

Invent with Python

Making paper cryptography paper tools

A Virtual Cipher Wheel

Assignments:
1. En-Crypt: Caesar Cipher  – Cryptography

Use the cipherwheel to encrypt a message. In a piece of paper, share the encrypted message with a partner.
In this post, you will type the message, the encrypted message and the one piece of information needed for your partner to decrypt it.

2. De-Crypt: Caesar Cipher –  Cryptography
In this post type your partner’s encrypted message and the one piece of information needed to decrypt it. What is the message?

NOTE: Include your partner’s name

In this post type your partner’s encrypted message and the one piece of information needed to decrypt it. What is the message?

NOTE: Include your partner’s name

3. Your Cipher – Cryptography

Design your own cipher and a device to easily encrypt and decrypt messages. Think ahead and prepare a checklist of materials you might need.

  1. Describe your cipher.
  2. Draw a diagram for your device.
  3. Build your device.
  4. Type here decryption instructions.

4. Friend’s Cipher – Cryptography

Find a classmate and exchange ciphers, instructions and encrypted message. After you checked with your classmate that you were able to decrypt the message, comment on the following:
1. Efficiency of the cipher
2. Quality of the instructions

NOTE: If the instructions were not clear or you couldn’t follow them, help your classmate make the right changes.
Include the name of your classmate.

5. Brute Force – Cryptography

If you didn’t have a key to decipher an encrypted message, how would write a program to decrypt it?

Write the pseudocode for you decrypting program using brute force.

Submit the instructions for your encryption device in the corresponding post. Include your name in the instructions.
Print your instructions and attach it to your device. I will “glue/attach” it to a poster. Please hand everything with your name on it.

GUI: House.java

Classwork:

Exercise:

  1. Write a GUI application, YI_House.java and its JFrame class to draw a house and the sun like the one in the picture.

house
NOTE: You can have only one class.
Extra credit: Draw a person like the one in the picture.

Homework: finish up this assignment