Title: Computing Concepts with Java Essentials
1Computing ConceptswithJava Essentials
2Page 23 Escape Sequences
- To display a String
- System.out.println(String)
- For example
- System.out.println(Hello Karel)
- generates the output Hello Karel
3Page 23 Escape Sequences
- How is the String Hello Karel generated
- to display double quotes precede the with a
backslash, i.e. \ is used to display a single
double quote (). - System.out.println(Hello \Karel\)
4Page 23 Escape Sequences
- Other common escape sequences
- System.out.println(\\) outputs \
- System.out.println(\n) outputs endOfLine
- System.out.println(\u00E9) outputs the
corresponding unicode character(é).
5Page 42 Defining A Class
- A class defines the methods that you can apply to
its objects
6Page 42 Defining A Class
- A method definition contains the following parts
- An access specified (usually private)
- The return type of the method (void, int, )
- The name of the method
- A list of the parameters of the method, enclosed
in parentheses - The body of the method
7Page 47 Instance Fields
- A object stores its state in one or more instance
fields - An instance field declaration consists of the
following parts - An access specifier (usually private)
- The type of the variable (String, int, Robot, .)
- The name of the variable
- A possible initialization (well discuss default
initialization values)
8Page 49/50 Constructors
- A constructor specifies how an object should be
initialized - A constructor has the same name as the class
9Page 50 Constructors
- Constructors are generally declared as public
- The new operator invokes the constructor
- Constructors do not have return types
10Page 62 Common Error
- A constructor is invoked only when an object is
first created - The constructor can not be called to reset an
object
11Chapter 3, Section 1 Number Types
- The int type denotes integers without fractional
parts - The double type denotes floating point numbers.
12Chapter 3, Section 1 Number Types
- Why separate int and double types.
- int(egers) are more efficient
- less storage
- process faster
- no rounding errors
13Chapter 3, Section 1 Number Types
- Numeric Ranges
- -2,147,483,648 lt int lt 2,147,483,647
- Integer.MIN_VALUE lt int lt Integer.MAX_VALUE
- double (Floating points) store about 15
significant digits
14Chapter 3, Section 1 Other Types
The eight primitives are
- Numeric Types
- byte
- short
- int
- long
- float
- double
Types on AP exam
15Chapter 3, Section 3 Combining Assignment
Arithmetic
16Chapter 3, Section 3 Constants
- Constants are defined with the keyword final
- The form of a constant variable declaration is
- accessSpecifier final type variableName value
- The variable Name should be in ALL caps!
- Ex.private final double MIN_GPA 3.2
17Chapter 3Common Error 3.1
- Integer Division
- 15 / 4
- 15 / 4.
- 15. / 4
- 12 3 / 4
- 12 3 / 4.
- 12. / ( 7 / 8)
18Round-off Error Why?
- double f 4.35
- int n (int) (100 f)
- System.out.println(n)
- 434 prints Why?
- Remember ICT? Write out ¾ in binary. No problem.
Now, try to write out 23/47 in binary.
19Chapter 3, Section 5 Static Methods
- A static method does not operate on an object
- Static method call
- ClassName.methodName(parameters)
- Example
- Math.sqrt(4)
- Point2d.distance(x1, y1, x2, y2)
20Chapter 3, Section 6 Type Conversion
- Assignments require that both types be compatible
- In Java, assigning a double to an int(narrowing)
is an error but assigning an int to a double
is ok(widening) - In Java, one type can be cast to another type
- Cast Syntax
- int x (int) 4.3 // hey, compiler. I know more
than you, so Im going to override what you
normally do. If Im wrong, an error will occur at
run-time. (essentially, thats casting)
21Chapter 3, Section 6 Type Conversion
- Example of casting a double to int
- int numBeepers (int) (4.5)
- double price 13.6
- int dollar (int) price
- dollar (int) price 0.5
- dollar (int) (price 0.5)
- dollar Math.round(price)
- price has not been changed!
22Chapter 3, Advance Topic 3.4Binary Number
- Remember ICT, its back
- Binary numbers digits from the set 0,1
- Conversion
- binary ? hexadecimal
- binary ? decimal
- hexadecimal ? decimal
23Chapter 3, Section 7 String
- A string is a sequence of characters
- If one argument of the operator is a string,
the other argument is converted to a string - If a string contains the digits of a number, the
Integer.parseInt or Double.parseInt methods
return the numeric value - int num Integer.parseInt(245)
24Chapter 3, Section 7 String
- The substring method extracts a part of the
string - String positions are counted starting with 0.
- .substring(beginIndex, endIndex) -or-
- .substring(beginIndex)
- String str1 Hi There.substring(1,3)
- String str2 Hi There.substring(3)
25Chapter 3, Section 8 Reading Input
- What do we want to put here?????
26Chapter 3, Section 10Comparing Primitives and
Objects
- Primitive type variables hold values
- Object variables hold references
- The difference
- When a primitive type is copied, the original and
copy are independent values - When you copy a reference, both variable
reference the same object (aliasing)
27Comparing Objects
- .equals() // for all classes
- if ( str1.equals(str2)
-
- .compareTo() // for classes implementing
Comparable - if ( str1.compareTo(str2) gt 0 )
- //str1 is greater than str2
- else if ( str1.compareTo(str2) lt 0 )
- // str1 is less than str2
- else if ( str1.compareTo(str2) 0 )
- // they are equal