Chapter 1
Java Fundamentals
Data Structures with Java and JUnit
©Rick Mercer
1-1
Example Java Program
// Read a number and display its squared value
import java.util.Scanner; // In Java 5 only
public class ReadItAndSquareIt {
public static void main(String[] args) {
double x;
double result = 0.0;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a number: ");
x = keyboard.nextDouble();
result = x * x;
System.out.println(x + " squared = " + result);
}
}
1-2
Pieces of a Java Program
import java.util.Scanner;
public class ReadItAndSquareIt
public static void main(String[] args)
double x; // Undefined, not garbage
double result = 0.0;
1-3
Special Symbols
= < > () { ; } == . >=
1-4
Identifiers
sqrt String Integer System in out
test1 x1 aNumber MAXIMUM A_1
1-5
Valid Identifiers
'a'..'z', 'A'..'Z', '0'..'9', '_', $
a) abc e) $$$ i) a_1
b) m/h f) 25or6to4 j) student Number
c) main g) 1_time k) String
d) double h) first-name l) ______
1-6
Reserved Identifiers
boolean default for new
break do if private
case double import public
catch else instanceOf return
char extends int void
class float long while
1-7
Literals -- Java has 6 varieties
Floating-point literals
1.234 -12.5 0.0 0. .0 1e10 0.1e-5
String literals
"between quotes" "another" "_"
Integer literals
-1 0 1 -2147483648 2147483647
Boolean literals (there are only two)
true false
The null literal (one value only)
null
Character literals
'A' 'b' '\n' '1' ' '
1-8
Comments
// on one line or
/*
between slash star and star slash
you can mash lines down real far
*/
/** javadoc comments for external documentation
* @return The square root of x
*/
1-9
Output
System.out.print(expression);
System.out.println(expression);
System.out.print("Enter test 1: ");
System.out.println("Grade: " + courseGrade);
System.out.println(1 + 2 + 3); // 6
System.out.println("1"+ 2 + 3); // 123
1-10
Assignment
variable-name = expression ;
double x; // Undefined variables
int j; // not garbage or 0 (for now)
j = 1;
x = j + 0.23;
1-11
Memory before and after
Variable Initial Assigned
Name Value Value
j ? 1
x ? 1.23
x = "oooooh nooooo, you can't do that"; // <-Error
j = x; // <-Error, can't assign a float to an int
? means undefined
1-12
Differences from C
int x;
System.out.println(x); // Compile time error
int n = 0.99; // Compile time error, not 0
if(n = 1) // Compile time error, not true
// It should read if(n == 1)
1-13
Input
1-14
The new Java Scanner class
1-15
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-16
Arithmetic Expressions�
grossPay = payRate * hoursWorked;
(40 * payRate) + 1.5 * payRate * (hoursWorked - 40)
1-17
Boolean Expressions
Operator Meaning
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
== Equal to
!= Not equal to
1-18
Boolean Expressions�
double x = 4.0;
Boolean
Expression Value
x < 5.0 true
x > 5.0 false
x <= 5.0 ? ___________
5.0 == x ? ___________
x != 5.0 ? ___________
1-19
Precedence of Arithmetic Operators
* / evaluated in a left to right order
- + evaluated in a left to right order in the absence of parentheses
double C, F;
F = 212.0;
C = 5 / 9 * (F - 32);
What is the current value of C?
1-20
int Arithmetic
int anInt = 0;
int another = 123;
int NoCanDo = 1.99; // ERROR
anInt = 9 / 2; // anInt = 4, not 4.5
anInt = anInt / 5; ________ What is anInt now?
anInt = 5 / 2; ________ What is anInt now?
1-21
The integer % operation
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-22
Compilation and Execution
1-23
The integer % operation
anInt = 9 % 2; // anInt ___1___
anInt = 101 % 2; #11 What is anInt now?
anInt = 5 % 11; #12 What is anInt now?
anInt = 361 % 60; #13 What is anInt now?
int quarter;
quarter = 79 % 50 / 25; #14 What is quarter?
quarter = 57 % 50 / 25; #15 What is quarter now?
1-24
Errors
1. Compiletime errors
2. Exceptions
3. Intent errors
1-25
Compilation and Execution
Report Errors
1-26
A few common compiletime errors
int my Weight = 0; int myWeight = 0;
integer sum = 0; int sum = 0;
double x = 0.0 double x = 0.0;
System.out.print("Hi); System.out.print("Hi");
keyboard.readDouble; keyboard.readDouble();
1-27
Exceptions
1-28
Intent Errors
System.out.print("Enter sum: ");
n = keyboard.nextInt();
System.out.print("Enter n: ");
sum = keyboard.nextDouble();
average = sum / n;
1-29
Homework
1C Wholesale Cost
Write a Java program that determines the wholesale price for any item given its retail price and markup. Test your program by running it with several different inputs where you know the expected results. Given that a store has a 25% markup on compact-disc (CD) players, if the retail price (what you pay) of a CD player is $189.98, how much did the store pay for that item (the wholesale price)? Use this formula and some algebra to solve for wholesale price:
retail price = wholesale price * (1 + markup)
One dialog must look like this (user input is in italic 255.00 and 50):
Enter the retail price: 255.00
Enter the markup percentage: 50
Wholesale price = 170.0
1-30