Java Fundamentals

Download Report

Transcript Java Fundamentals

Input
1-1
 There are many options for reading numbers
 1. use the decorator design pattern with classes
such as BufferedReader


has complex code
2. use the new Java 5 Scanner class


Relatively easy to use (less code to write)
Can read from complicated files (variety of types)
1-2
The new Java Scanner class
 Need to import java.util.Scanner
 Construct a Scanner object with new
 Then you can send messages (call functions)
such as nextInt, nextDouble, and nextLine
1-3
Read 3 doubles
import java.util.Scanner;
public class ThreeNumbers {
public static void main(String[] args) {
double x = 0.0, y = 0.0, z = 0.0, average = 0.0;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter 3 numbers: ");
x = keyboard.nextDouble();
y = keyboard.nextDouble();
z = keyboard.nextDouble();
average = (x + y + z) / 3.0;
System.out.println("Average: " + average);
}
}
1-4
Arithmetic Expressions
 Arithmetic expressions consist of operators
such as + - / * and operands such as 40, 1.5,
payRate, and hoursWorked
 Example expression used in an assignment:
grossPay = payRate * hoursWorked;
 Another example expression:
(40 * payRate) + 1.5 * payRate * (hoursWorked - 40)
1-5
Boolean Expressions
 Boolean expressions evaluate to true or false
 Will often see relational operators
Operator
Meaning
<
>
<=
>=
==
!=
Less than
Greater than
Less than or equal to
Greater than or equal to
Equal to
Not equal to
1-6
Boolean Expressions
 Some boolean expressions and their resulting
values
double x = 4.0;
Boolean
Expression Value
x < 5.0
x > 5.0
true
false
x <= 5.0
? ___________
? ___________
? ___________
5.0 == x
x != 5.0
Precedence of Arithmetic
Operators
1-7
 Expressions with more than one operator require
some sort of precedence rules:
*
-
/
+
evaluated in a left to right order
evaluated in a left to right order in the absence of parentheses
 Use (parentheses) for readability or to intentionally
alter an expression:
double C, F;
F = 212.0;
C = 5 / 9 * (F - 32);
What is the current value of C?
1-8
int Arithmetic
variables are similar to double, except they can
only store whole numbers (integers)
 int
int anInt = 0;
int another = 123;
int NoCanDo = 1.99;
// ERROR
 Division with integers is also different

Perfoms quotient remainder whole numbers only
anInt = 9 / 2;
// anInt = 4, not 4.5
anInt = anInt / 5;
________ What is anInt now?
anInt = 5 / 2;
________ What is anInt now?
1-9
The integer % operation
 The Java % operator returns the remainder
anInt = 9 % 2;
// anInt ___1___
anInt = 101 % 2;
_____ What is anInt now?
anInt = 5 % 11;
_____ What is anInt now?
anInt = 361 % 60; _____ What is anInt now?
int quarter;
quarter = 79 % 50 / 25; ______ What is quarter?
quarter = 57 % 50 / 25; ______
What is quarter now?
1-10
Compilation and Execution
class TestMain
{ public static void
main(String[] argv)
{ double x = 2.5;
x = x * x;
}
}
Source Code
Compiler
0101010
1011001
0100101
0100100
1010100
1001010
1001010
10010
Java Byte Code
Java Virtual
Machine
Machine
dependent
executing
program
1-11
Errors
 Categories of errors and warnings are detected
during implementation
1. Compiletime errors
2. Exceptions
3. Intent errors
 You will experience many errors
1-12
Compilation and Execution
class TestMain
{ public static void
main(String[] argv)
{ double x = 2.5;
x = x * x
}
}
Source Code
Compiler
0101010
1011001
0100101
Java Virtual
0100100
Machine
1010100
1001010
1001010
10010
Java Byte Code
Report Errors
Machine
dependent
executing
program
1-13
A few common compiletime errors
 Splitting an identifier
int my Weight = 0;
int myWeight = 0;
 Misspelling a keyword
integer sum = 0;
int sum = 0;
 Leaving off a semicolon
double x = 0.0
double x = 0.0;
 Not closing a string constant
System.out.print("Hi);
System.out.print("Hi");
 Forgetting parentheses
keyboard.readDouble;
keyboard.readDouble();
1-14
Exceptions
 Exceptions are errors that occur while the
program is running
 Exceptions are thrown when the program
encounters something it could not handle well
 Example



Type in an invalid number during a nextInt message
Division by 0 with integers (3.0/0.0 is Infinity)
Attempt to open a file that is not present
1-15
Intent Errors
 When the program does what you typed, not what
you intended.
 Imagine this code
System.out.print("Enter sum: ");
n = keyboard.nextInt();
System.out.print("Enter n: ");
sum = keyboard.nextDouble();
average = sum / n;