Monthly Archives: October 2020

Conditionals: Counting Sundays

screen-shot-2016-10-27-at-8-36-29-am

Counting Sundays

Problem 19

You are given the following information, but you may prefer to do some research for yourself.

1 Jan 1900 was a Monday.
Thirty days has September,
April, June and November.
All the rest have thirty-one,
Saving February alone,
Which has twenty-eight, rain or shine.
And on leap years, twenty-nine.
A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.
How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?

Conditionals: RGB to CMYK

Type conversion and conditionals. Several different formats are used to represent color. For example, the primary format for LCD displays, digital cameras, and web pages, known as the RGB format, specifies the level of red (R), green (G), and blue (B) on an integer scale from 0 to 255. The primary format for publishing books and magazines, known as the CMYK format, specifies the level of cyan (C), magenta (M), yellow (Y), and black (K) on a real scale from 0.0 to 1.0.

Write a program RGBtoCMYK_YI.java that converts RGB to CMYK. Prompt the user with messages for three integers red, green, and blue, and print the input red, green, and blue, then print the equivalent CMYK, {C,M,Y,K} values using the formulas in the attached image.
More details on edmodo.com
Project 1 # 2: RGB to CMYK

Conditionals: 1000 digit Number

 

screen-shot-2016-10-27-at-8-36-29-am

Project Euler Problem 8

Largest product in a series
The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.

73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450

Write a java program, ThirteenAdj_YI.java to find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?

NOTE: Here is the code to handle the 1000-digit number

String adjStrNumbers = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
System.out.println("The length of this number is " + adjStrNumbers.length());
//The length of this number is 1000

Check what this code snippet does:

for ( int i = 0; i < 20; i++)
System.out.print( Integer.parseInt( adjStrNumbers.substring(i,i+1) ) );


WARNING:
The largest product of thirteen adjacent digits is 23514624000 if you declare the variables for product and largest of type “long”. Otherwise, when using type “int”, the largest product of thirteen adjacent digits is 2091059712 if you use int for x and product!!!!!!!!! THIS IS THE WRONG ANSWER

Conditionals: Relative prime numbers

Relative prime numbers

Write a program, RelativelyPrime_YI.java to prompt the user for an integer, n. The program displays an n-by-n table such a that there is an * in row i and column j if the gcd of i and j is 1 ( i and j are relatively prime) and a space in that position otherwise.

screen-shot-2016-10-17-at-11-16-58-am

Hint: use GCD_YI.java to finds the greatest common divisor of two integers

Example output:

>run ca_relativelyprime 2 20
   1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 
 1 * * * * * * * * * * * * * * * * * * * * 
 2 *   *   *   *   *   *   *   *   *   *   
 3 * *   * *   * *   * *   * *   * *   * * 
 4 *   *   *   *   *   *   *   *   *   *   
 5 * * * *   * * * *   * * * *   * * * *   
 6 *       *   *       *   *       *   *   
 7 * * * * * *   * * * * * *   * * * * * * 
 8 *   *   *   *   *   *   *   *   *   *   
 9 * *   * *   * *   * *   * *   * *   * * 
10 *   *       *   *   *   *       *   *   
11 * * * * * * * * * *   * * * * * * * * * 
12 *       *   *       *   *       *   *   
13 * * * * * * * * * * * *   * * * * * * * 
14 *   *   *       *   *   *   *   *   *   
15 * *   *     * *     *   * *   * *   *   
16 *   *   *   *   *   *   *   *   *   *   
17 * * * * * * * * * * * * * * * *   * * * 
18 *       *   *       *   *       *   *   
19 * * * * * * * * * * * * * * * * * *   * 
20 *   *       *   *   *   *       *   *   
> 
*/

Event: Competition – picoCTF

What is picoCTF?

picoCTF is a free computer security game with original educational content built on a capture-the-flag framework created by security and privacy experts at Carnegie Mellon University.

Gain access to a safe and unique hands-on experience where participants must reverse engineer, break, hack, decrypt, and think creatively and critically to solve the challenges and capture the flags.

Sign up now and explore picoCTF’s newest features where you can build skills in the picoGym and read about cybersecurity terminology and principles with the picoPrimer.

Conditionals: Review and re-cap

Flowchart: What is the look of this flowchart when finished?

Screen Shot 2014-10-15 at 12.51.31 PM

  1. What does the program for that flowchart do? Write the full program.
  2. What is a pseudorandom number? What is the code to generate a random number?
  3. In the Gambler’s ruin, when does the user exit the program?
  4. In the Gambler’s ruin, how many while-loops are needed? Explain.
  5. How do you use a while structure to print the integers from 34 through 41 inclusive?
  6. How do you add all the integers from 34 through 41 inclusive? Show code.
  7. Write the code to prompt the user for an integer N. Your program will display a square pattern of asterisks N by N
  8. Write the code to print all even integers from 36 through 12 inclusive.
  9. Write the code for a while loop to prompt the user if he or she wants to continue to input more integers.
  10. The following questions are related to this flowchart:

Screen Shot 2014-10-15 at 12.51.23 PM

a. What is the oval shape used for?

b. What is the code for each instruction in the flowchart?

c. Draw the flowchart to find the two digits in the middle of a 4-digit integer.

Write the code to calculate and display the following output:

 “     1        1”  
 “     2        4” 
 “     3        9”  
 “     4       16”  
 “     5       25”      

Mystery: What does this program do?

for ( int i =1; i < 21 ; i++) 
  {
   System.out.printf( "%10d" ,(int)(1 + Math.random( ) * 6));

   if ( i % 5 == 0 )   # print newline every 5 rolls
    System.out.println();
  }

Simulation: What does this program do? How would you finish it up?

int frequency1 = 0
int frequency2 = 0
int frequency3 = 0
int frequency4 = 0
int frequency5 = 0
int frequency6 = 0

for ( int i =1; i < 6001 ; i++)     // 6000 die rolls
  {
   face = (int)(1 + Math.random( ) * 6);
    
   if (face == 1)                       
    frequency1 += 1                 // frequency counted
   else
   if ( face == 2 )
    ...
   ...
  }



Loop 1: What structure can be used to repeat a block of statements when the termination of the loop is not known? Explain your answer.

Loop 2: What structure can be used to repeat a block of statements when the termination of the loop is known? Explain your answer.

Advanced Topic: Static Methods and the String class

Static Methods:

The Math class:
Math.pow(10, 3)

    In Java, numbers as primitive data types are not objects, so you can never invoke a method on a number.
    Instead, you pass a number as an explicit parameter to a method
    ClassName.methodName(parameters)
  1. What happens to this code fragment when you compile it?

int x = 2;
System.out.println(x.pow(3));

  1. What is the difference between an ADT and a primitive data type? What can you create with an ADT?

The String Class
In Java, strings are objects that belong to the class String.
You do not need to call a constructor to create a string object.

A string is a sequence of characters. Strings are objects of the String class.

The substring method computes substrings of a string.

The call s.substring(start, pastEnd)

returns a string that is made up of the characters in the string s, starting at position start, and containing all characters up to, but not including, the position pastEnd.

Here is an example:
String greeting = “Hello, World!”;
String sub = greeting.substring(0, 5); // sub is “Hello”

H e l l o ,   W o r  l  d  !
0 1 2 3 4 5 6 7 8 9 10 11 12


The position number of the last character (12 for the string “Hello, World!”) is always 1 less than the length of the string.

Let us figure out how to extract the substring “World”.
Count characters starting at 0, not 1. You find that W, the eighth character, has position number 7. The first character that you don’t want, !, is the character at position 12 .

               <-----5---->
H e l l o ,   |W o r  l  d | !
0 1 2 3 4 5 6 |7 8 9 10 11 | 12


String sub2 = greeting.substring(7, 12);

You can easily compute the length of the substring: It is pastEnd – start. For example, the string “World” has length 12 – 7 = 5.

If you omit the second parameter of the substring method, then all characters from the starting position to the end of the string are copied.

For example,
String tail = greeting.substring(7); // tail is “World!”

  1. Why can you have multiple substring methods doing something different?

If you supply an illegal string position (a negative number, or a value that is larger than the length of the string), then your program terminates with an error message.

  1. Will it be an error during compilation or at run time?

Strings and the char Type
Strings are sequences of Unicode characters.
Character literals look like string literals, but we use single quotes: ‘M’ is a character, “M” is a string containing a single character.

Characters have integer values.
Screen Shot 2015-02-11 at 9.53.31 AM

You can use escape sequences inside character literals. For example, ‘\n’ is the newline character, and ‘\u00E9’ is the character é.

“When Java was first designed, each Unicode character was encoded as a two-byte quantity. The char type was intended to hold the code of a Unicode character. However, as of 2003, Unicode had grown so large that some characters needed to be encoded as pairs of char values. Thus, you can no longer think of a char value as a character. Technically speaking, a char value is a code unit in the UTF-16 encoding of Unicode. That encoding represents the most common characters as a single char value, and less common or supplementary characters as a pair of char values.” Clay Horstmann

Screen Shot 2015-02-11 at 12.59.39 PM

The charAt method of the String class returns a code unit from a string. As with the substring method, the positions in the string are counted starting at 0.
For example, the statement

String greeting = “Hello”;
char ch = greeting.charAt(0);

sets ch to the value ‘H’.

  1. How do you find out the number of characters a string has?
  2. How can you create an array of char type containing all the characters of a string?

Visit edmodo.com to answer these questions.