Author Archives: graceliana@gmail.com

Input/Output: Origami Half Areas

If you fold a square diagonally, you will create a right isosceles triangle. We will take its 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.

Chapter 2: Assignments

Written Assignments:
T/F 1-8

Short Answers

2.1 What is encapsulation and what is the purpose?
2.2 What output is produced by the following code fragment? Explain.

System.out.print (“Test this if you are not sure.”);
System.out.print (“Another.”);
System.out.println ();

2.3 How do you print the following statement with double quotes?
“To be or not to be, that is the question.”

2.4 What output is produced by the following statement? Explain.
System.out.println (“50 plus 25 is “ + 50 + 25);

2.5 What is the output produced by the following statement? Explain.
System.out.println (“He thrusts his fists\n\tagainst” +
“ the post\nand still insists\n\the sees the \”ghost\””);

2.6 Given the following declarations, what result is stored in each of the listed assignment statements?

int iResult, num1 = 25, num2 = 40, num3 = 17, num4 = 5;
double fResult, val1 = 17.0, val2 = 12.78;

◗ iResult = num1 / num4;
◗ fResult = num1 / num4;
◗ fResult = val1 / val2;
◗ iResult = num1 / num2;
◗ fResult = (double) num1 / num2;
◗ fResult = num1 / (double) num2;
◗ fResult = (double) (num1 / num2
◗ fResult = (int) ((double) num1 / num2); ◗ iResult = num3 % num4;
◗ iResult = num 2 % num3; ◗ iResult = num3 % num2;
◗ iResult = num2 % num4;

2.7 For each of the following expressions, indicate the order in which the operators will be evaluated by writing a number beneath each operator.

◗ a – b + c – d
◗ a / b * c * d
◗ a % b / c *d
◗ a % b % c % d
◗ (a + b) * c+ d * e
◗ (a + b) * (c / d) % e

2.8 What output is produced by the following code fragment?
String m1, m2, m3;
m1 = “Quest for the Holy Grail”;
m2 = m1.toLowerCase();
m3 = m1 + “ “ + m2;
System.out.println (m3.replace(‘h’, ‘z’));

2.9 Write an assignment statement that computes the square root of the sum of num1 and num2 and assigns the result to num3.

2.10 Write a single statement that computes and prints the absolute value of total.

2.11 Assuming that a Random object has been created called generator, what is the range of the result of each of the following expressions?

generator.nextInt(20)
generator.nextInt(8) + 1

2.12 Write code to declare and instantiate an object of the Random class (call the object reference variable rand). Then write a list of expressions using the nextInt method that generates random numbers in the following specified ranges, including the endpoints. Use the version of the nextInt method that accepts a single integer parameter.

◗ 0 to 10 including both numbers

◗ 25 to 50 including both numbers

◗ 1 to 10 including both number

◗ –10 to 15 including both numbers

2.13. List all primitive data types you know.

2.14. What is the difference between a character and a string.

2.15. Do you need to cast when assigning a double to an integer? why or why not?

2.16. Do you need to cast when assigning an integer to a double? why or why not?

Self-review questions at the end of the chapter.

Coming up next week: Chapter 2 Test
10 free responses similar to the review questions
3 String ADT related MC questions – look into the String methods
1 for-loop MC question
1 short program similar to this review question:
Write a main method that prompts the use for two numbers, x and y, and it displays it back the following format: (x,y).

Chapter 2: Test Review

Answer these questions:

Chapter 2 Test Review

The Java Language
1. What is encapsulation and what is the purpose?
2. Explain abstractions. Give an example of an abstraction.
3. Give the code to concatenate two strings “hello” and “goodbye”.
4. List the escape sequences and the meaning.
5. Write the statement to print “Hello World” including the quotes.
5. List all primitive data types you know.
6. What is the difference between a character and a string.
7. Do you need to cast when assigning a double to an integer? why or why not?
8. Do you need to cast when assigning an integer to a double? why or why not?
9. Give an example of widening and narrowing type conversion for each.
10. What is the purpose of casting when using a casting operator in the case of narrowing type conversion.
11. What does “123456789”.substring(3,6) return?
12. Write a main method that prompts the use for two numbers, x and y, and it displays it back the following format: (x,y).
13. Write a code snipped to compare two strings, s1 and s2. If s1 comes first in the lexicographic order, then it will print “move two steps forward” else “move two steps backwards.
14. What is the result of 21 % 6 when evaluated in a java expression?

Object Oriented Design
1. What are classes like?
2. What is the content of an object variable?
3. Why the “= =” cannot be used to compare objects?
4. What is an instance of a class?
5. In the following code: BankAccount myAcct = new BankAccount(); Is myAcct an object of BankAccount?
6. What is the purpose of a constructor?
7. Can you have multiple constructors?
8. What is the return type of a constructor?
9. What does the operator new do?
10. What does it mean a string object is inmutable?
11. What is a wrapper class? What is autoboxing? Give clear examples.

Homework:
Do the self-review questions at the end of the chapter.
Reflect on this article:
Screen Shot 2015-09-14 at 8.55.02 PM

First Days: Cryptography: Number Encryption

#1. A company wants to transmit data through the wireless network, but they are concerned that their devices may be compromised. All of their data is transmitted as four-digit integers. They have asked you to write a program that will encrypt their data so that it may be transmitted more securely. Your application should read a four-digit integer entered by the user in an input dialog and encrypt it following this sequence of instructions:

Replace each digit by (the sum of that digit plus 7) modulus 10.
Swap the first digit with the third.
Swap the second digit with the fourth.
Finally, print the encrypted integer.

Write a java program, NumEncrypt_YI.java to encrypt the 4-digit number entered by the user. Your program must include a beginning message, “Would you like to encrypt another number?” message, and an exit message.

#2. Write a java program, NumDecrypt_YI.java that prompts the user for an encrypted four-digit integer and decrypts it to form the original number. Just as the previous assignment, your program must include a beginning message, “Would you like to encrypt another number?” message, and an exit message.

First Days: 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:

Making paper cryptography paper tools

Assignments:
1. En-Crypt: Caesar Cipher – Clwk 9/8/2017 – 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.

NOTE: Include your partner’s name

2. De-Crypt: Caesar Cipher – Clwk 9/8/2017 – 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

3. Your Cipher – Clwk 9/8/2017 – Cryptography
Turned In (105) Due: September 11, 2017 11:45 pm
Design your own cipher and a device to easily encrypt and decrypt messages.

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

4. Friend’s Cipher – Clwk 9/12/2017 – 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.

Brute Force – Clwk 9/13/17 – Cryptography
Due: September 13, 2017 11:45 pm
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.

Chapter 2: Objects and Primitives

Objects and Primitive Data

  1. The information we manage in a Java program is either primitive data or objects.
  2. An abstraction hides details. A good abstraction hides the right details at the right time.
  3. A variable is a name for a memory location used hold a value of a particular data type.
  4. A variable can store only one value of its declared type.
  5. Java is a strongly typed language. Each variable has a specific type, and we cannot assign a value of one type to a variable of another type.
  6. Constants are like variables, but they hold one particular value.
  7. Java has two kinds of numeric values: integers and floating point. The primitive type int is an integer data type and double is a floating point data type.
  8. Many programming statements involve expressions. Expressions are combinations of one or more operands and the operators used to per­ form a calculation.
  9. Java has rules that govern the order in which operators will be evaluated in an expression.
  10. Avoid narrowing conversions because they can lose information.
  11. The new operator returns a reference to a newly created object.
  12. The Java standard class library is a useful set of classes that anyone can use when writing Java programs.

First Days: Shannon Entropy – Cryptography

Classwork/Assignment:

NOTE: The following assignment doesn’t require to be Object Oriented.
Generating cryptograms. A cryptogram is obtained by scrambling English text by replacing each letter with another letter. Write a program, Crypto1_YI.java to generate a random permutation of the 26 letters and use this to map letters.
NOTE: It is not necessary to be OOD
Give example: Don’t scramble punctuation or whitespace.

// DS
// ABCDEFGHIJKLMNOPQRSTUVWXYZ
// SDTVBJGXWACIYROLUQPENKMHZF

// SM
/**
[m, n, u, h, y, c, b, s, f, a, i, k, x, r, l, z, w, t, q, o, d, e, p, g, v, j]
please give an all lowercase string to scramble (any non lowercase characters will be skipped
happy make your bed day
smzzv xmiy vldt nyh hmv
*/

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

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, Entropy1_YI.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.(Optional)

https://www.khanacademy.org/computing/computer-science/informationtheory/moderninfotheory/v/compressioncodes