Download presentation
Presentation is loading. Please wait.
Published byAngela West Modified over 6 years ago
1
FATİH SULTAN MEHMET VAKIF UNIVERSITY Faculty of Engineering & Architecture Computer Engineering Department MAT217E - DİSCRETE STRUCTURES Slides for a Course Based on the Text Discrete Mathematics & Its Applications (7th Edition) by Kenneth H. Rosen MAT217E Discrete Structures Lecture 8
2
RECALL: Characters as Integers in Java
It is legal to assign a char to an int variable int i = ‘a’; // assigns 97 to i It is legal to assign an int to an char variable char c = 97; // assigns ‘a’ to c It is possible to perform arithmetic on char variables char ch = ‘a’; ch = ch + 1; // assigns ‘b’ to ch
3
The char Data Type in Java
The Java char data type provides access to single characters. char literals are enclosed in single quote marks. ‘a’, ‘Z’, ‘\n’, ‘1’ Don’t confuse char literals with String literals. char literals are enclosed in single quotes. (tek tırnakla tanımlanır.) String literals are enclosed in double quotes. (çitf tırnakla tanımlanır.) Example: " Fatih Sultan Mehmet Vakif University"
4
Intro to OOP with Java, C. Thomas Wu
ASCII Encoding For example, character 'O' is 79 (row value 70 + col value 9 = 79). O 9 70 Characters can be stored in a computer memory using the ASCII encoding. The ASCII codes range from 0 to 127. The character 'A' is represented as 65, for example. The ASCII values from 0 to 32 are called nonprintable control characters. For example, ASCII code 04 eot stands for End of Transmission. We can use this character to signal the end of transmission of data when sending data over a communication line. ©The McGraw-Hill Companies, Inc.
5
Unicode Internally, characters are stored as numbers.
Character data in Java is stored as Unicode characters. The Unicode character set can consist of (216) individual characters. This means that each character takes up 2 bytes in memory. The first 256 characters in the Unicode character set are compatible with the ASCII* character set. *American Standard Code for Information Interchange
6
Unicode A B 00 65 00 66 1 1
7
stored in memory as binary numbers.
Unicode Characters are stored in memory as binary numbers. A B 00 65 00 66 1 1
8
Unicode A B 00 65 00 66 The binary numbers represent these
decimal values. B 00 65 00 66 1 1
9
Unicode A B 00 65 00 66 The decimal values represent these characters.
1 1
10
Exercise: Character Arithmetic
Implement a method toHexDigit that takes an integer and returns the corresponding hexadecimal digit as a character. Thus, if the argument is between 0 and 9, the method should return the corresponding character between '0' and '9'. If the argument is between 10 and 15, the method should return the appropriate letter in the range 'A' through 'F'. If the argument is outside this range, the method should return '?'. public char toHexDigit(int n) { if (n >= 0 && n <= 9) { return (char) ('0' + n); } else if (n >= 10 && n <= 15) { return (char) ('A' + n - 10); } else { return '?'; }
11
Simple Encryption Variations on the following have been used to encrypt messages for thousands of years. Convert a message to capitals. Think of each letter as a number between 1 and 26. Apply an invertible modular function to each number. Convert back to letters (0 becomes 26).
12
Letter Number Conversion Table
D E F G H I J K L M 1 2 3 4 5 6 7 8 9 10 11 12 13 N O P Q R S T U V W X Y Z 14 15 16 17 18 19 20 21 22 23 24 25 26
13
Encryption example Let the encryption function be
f (a) = (3a + 9) mod 26 Encrypt “Stop Thief” STOP THIEF (capitals) 19,20,15, ,8,9,5,6 14,17,2,5 17,7,10,24,1 NQBE QGJXA
14
g (a) = 9 (a - 9) mod 26 = (9a – 3) mod 26
Decryption example Decryption works the same, except that you apply the inverse function. EG: Find the inverse of f (a) = (3a + 9) mod 26 If we didn’t have to deal with mod 26, inverse would be g (a) = 3-1 (a - 9) We’ll see that since gcd(3,26) = 1, the inverse of 3 is actually well defined modulo 26 and is the number 9. This gives: g (a) = 9 (a - 9) mod 26 = (9a – 3) mod 26 L9
15
Caesar’s Cipher f (a) = (a+3) mod 26
16
Exercise: Write a program to find the given number is Armstrong number or not.
Armstrong numbers are the sum of their own digits to the power of the number of digits. It is also known as narcissistic numbers. Example: 153 = 𝟏 𝟑 + 𝟓 𝟑 + 𝟑 𝟑
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.