Download presentation
Presentation is loading. Please wait.
Published byBruce Cannon Modified over 9 years ago
1
CS177 Week2: Recitation Primitive data types and Strings with code examples
2
Questions?
3
Data Types
4
int int x; //declaration of variable x x = 23; //assigns the number 23 to x Stored as 1’s and 0’s. 23d = 10111b. d stands for decimal, b for binary
5
Binary -> Decimal Conversion What does 11011110b equal in decimal?
6
222
7
Decimal -> Binary Conversion Do the following steps: Start with a decimal number x Set binary number b = 0 Until x = 0 Is x odd? If yes, put a 1 at the end of b If no, put a 0 at the end of b Divide x by 2, rounding down (truncation) Reverse the digits of b
8
Ex: x = 9
9
Ex: x = 222 11011110b = 222d, just like before
10
Positive and Negative Numbers Positive numbers start with a 0 bit Negative numbers start with a 1 bit 2’s complement – way that a negative number is represented
11
Doubles Rational numbers (can still be an integer, but it is stored differently) 64 bits double y = 1.25; //declares double y and assigns 1.25 to y double z = 3; double a = 2.0; double b = -8.8;
12
Loosely typed or strongly typed? Java is: Strongly typed double x; x = 3; What is the type of variable x? A double!
13
Strings Many letters in one type String word1 = “purdue”; String word2 = “boilermakers”; String word3 = “p”; String word4 = “”; //This is legal – called empty String
14
Chars char – one character. Could be any ASCII character String – many characters together char w = ‘a’; char x = w; char y = ‘w’; String z = “w”; Are the variables w and x equal? Are the variables x and y equal? Are the variables y and z equal?
15
String - String concatenation /************************************************************************* * Compilation: javac Ruler.java * Execution: java Ruler * * Prints the relative lengths of the subdivisions on a ruler. * * % java Ruler * 1 * 1 2 1 * 1 2 1 3 1 2 1 * 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 * 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 * *************************************************************************/
16
public class Ruler { public static void main(String[] args) { String ruler1 = "1 "; String ruler2 = ruler1 + "2 " + ruler1; String ruler3 = ruler2 + "3 " + ruler2; String ruler4 = ruler3 + "4 " + ruler3; String ruler5 = ruler4 + "5 " + ruler4; System.out.println(ruler1); System.out.println(ruler2); System.out.println(ruler3); System.out.println(ruler4); System.out.println(ruler5); }
17
Double Operation - Quadratic formula /************************************************************************* * Compilation: javac Quadratic.java * Execution: java Quadatic b c * Given b and c, solves for the roots of x*x + b*x + c. * Assumes both roots are real valued. * % java Quadratic -3.0 2.0 * 2.0 * 1.0 * % java Quadratic -1.0 -1.0 * 1.618033988749895 * -0.6180339887498949 * * Remark: 1.6180339... is the golden ratio. * * % java Quadratic 1.0 1.0 * NaN *************************************************************************/
18
public class Quadratic { public static void main(String[] args) { double b = Double.parseDouble(args[0]); double c = Double.parseDouble(args[1]); double discriminant = b*b - 4.0*c; double sqroot = Math.sqrt(discriminant); double root1 = (-b + sqroot) / 2.0; double root2 = (-b - sqroot) / 2.0; System.out.println(root1); System.out.println(root2); }
19
Char Operation /************************************************************************* * Compilation: javac Charactertest.java * Execution: java Charactertest Is this letter lowercase? true Is this letter uppercase? false Is this a letter? true /************************************************************************* public class Charactertest { public static void main(String args[]) { char letter = 'd'; boolean lowerCase = Character.isLowerCase(letter); boolean upperCase = Character.isUpperCase(letter); boolean letterOrdigit = Character.isLetter(letter); System.out.println("Is this letter lowercase? " + lowerCase); System.out.println("Is this letter uppercase? " + upperCase); System.out.println("Is this a letter? " + letterOrdigit); }
20
Questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.