Download Session 03 - IT

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
SDJ I1, Session 3
Variables and types
Strings
User input
Life of a variable
class VariableExample
{
public static void main(String[] args)
{
x: doesn’t exist
int x = 7;
x: 7
x = 9;
Scope
x: 9
x = 2 * x;
}
}
x: 18
x: doesn’t exist
Seeing for yourself
public class PrintingVariables
PrintingVariables.java
{
public static void main(String[] args)
{
int x = 7;
System.out.println(x);
x = 9;
System.out.println(x);
x *= 2;
System.out.println(x);
}
7
}
9
18
Uninitialized variables
public class UninitializedVariables
UninitializedVariables.java
{
public static void main(String[] args)
{
int x;
x = 7;
System.out.println(x);
x = 9;
System.out.println(x);
x = 2 * x;
System.out.println(x);
}
7
}
9
18
This is discouraged in Java
Uninitialized problem
public class UninitializedProblem
UninitializedProblem.java
{
public static void main(String[] args)
{
int x;
System.out.println(x);
x = 7;
System.out.println(x);
x = 9;
System.out.println(x);
x = 2 * x;
System.out.println(x);
}
UninitializedProblem.java:6: variable x might not have been initialized
}
System.out.println(x);
^
1 error
Other kinds of variables
public class StringDouble
StringDouble.java
{
public static void main(String[] args)
{
String greeting = "Hello, World!";
System.out.println(greeting);
double pi = 3.14159265;
double radius = 10.0;
System.out.println(
"area: " + pi * radius * radius);
}
Hello, World!
}
area: 314.159265
Use meaningful names
Computer memory
0x1024000
0x1024001
0x1024002
0x1024003
0x1024004
0x1024005
0x1024006
0x1024007
0x1024008
0x1024009
Variables in memory
7
3.14159265
10.0
String?
Strings in memory
Reference
3.14159265
10.0
Object
”Hello, World!”
Garbage
Stack
Heap
0x1024000
0x1024001
0x1024002
0x1024003
0x1024004
0x1024005
0x1024006
0x1024007
0x1024008
0x1024009
”Hello, World!”
Floating point literals
• 0.0 is a double
• 1.23 is a double
• 1.23F is a float
• 1.23E23 is 1.23 x 1023 and a double
• 1.23E23F is 1.23 x 1023 and a float
• -1.23E-23 is -1.23 x 10-23 and a double
Precision mishaps
public class PrecisionMishap
PrecisionMishap.java
{
public static void main(String[] args)
{
double oneThird = 1.0/3.0;
double twoThirds = 1.0 - oneThird;
System.out.println(1.0
– oneThird
- twoThirds);
System.out.println(1.0
– twoThirds
- oneThird);
}
0.0
}
-5.551115123125783E-17
Strange division
public class StrangeDivision
StrangeDivision.java
{
public static void main(String[] args)
{
int seven = 7;
System.out.println(seven / 2);
System.out.println(seven / 2.0);
System.out.println(seven % 2);
}
}
3
3.5
1
The Math class
public class MathExamples
MathExamples.java
{
public static void main(String[] args)
{
double radius = 10.0;
System.out.println(
"area: " + Math.PI * radius * radius);
System.out.println(Math.round(28.7));
}
}
area: 314.159265
29
Exercises
• [Gaddis] Algorithm Workbench 5, 6
• Exercises 3.1 – 3.4
• [Gaddis] programming challenges 1, 4, 6
(page 109)
Comments
/**
Comments.java
* This program shows examples of using the
* Math class.
*/
public class Comments
{
public static void main(String[] args)
{
double r = 10.0; // r is the radius
System.out.println("area: " +
Math.PI * r * r);
// This is just for the sake
// of example.
System.out.println(Math.round(28.7));
}
}
Don’t Repeat Yourself
int fahrenheit, //To hold the Fahrenheit temperature
centigrade, //To hold the centigrade temperature
kelvin;
//To hold the Kelvin temperature
Tell me something I don't know
int fahrenheit, //To hold user input
centigrade, //To hold calculated value
kelvin;
//To hold calculated value
The why and how of comments
• The 6 journalistic questions:
–
–
–
–
–
–
What? – Answered in the names.
How? – Answered in the code.
Why? – Answered in the comments.
Where? – Might be answered in the comments.
Who? – Might be answered in the comments.
When? – Might be answered in the comments.
Strings
• Strings: A bunch of characters
• The string "Hello" contains the
characters 'H', 'e', 'l', 'l', and 'o'
• A string literal is enclosed in quotations
marks: "You can write anything here"
• What if I want quotation marks in the
string?
"\"Like this,\" he said."
The String class
• String is different.
• String s = "abc";
creates a reference to a String object.
• String is a class.
• Class: A cookie cutter to create objects.
Methods
• Like operators for objects.
• Operators are pre-defined.
• Methods are either pre-defined or
programmer-defined.
String methods (1)
• char charAt(int index)
Returns the char at the specified index.
String s = ”abc”;
char c = s.charAt(2);
c is ’c’
• int length()
Returns the length of the string.
int l = s.length();
l is 3.
String methods (2)
String substring(int beginIndex,
int endIndex)
Returns a new String with the characters from and
including beginIndex to but excluding
endIndex.
String word = ”developer”;
String sub = word.substring(3, 8);
sub is ”elope”
Strings in memory
public class StringInMemory
StringInMemory.java
{
public static void main(String[] args)
{
String word = "developer";
String sub = word.substring(3, 8);
String word2 = word;
}
}
Strings in memory
0x1024000
0x1024004
0x1024008
0x102400C
0x1024010
”developer”
”elope”
Object diagram
word, word2 : String
sub : String
Different strings
public class DifferentStrings
DifferentStrings.java
{
public static void main(String[] args)
{
String word = "developer";
String sub = word.substring(3, 8);
String word2 = "devel" + "oper";
}
}
Different strings diagram
word : String
sub : String
word2 : String
The Scanner class
import java.util.Scanner;
ScannerExample.java
public class ScannerExample
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Year of birth: ");
int year = keyboard.nextInt();
System.out.println("You entered " + year);
System.out.println("Height in meters: ");
double height = keyboard.nextDouble();
System.out.println("You entered " + height);
}
}
Year of Birth:
You entered
Height in meters:
You entered
1968
1968
1.84
1.84
Reading a line
import java.util.Scanner;
ScannerLine.java
public class ScannerLine
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Name: ");
String name = keyboard.nextLine();
System.out.println("Hello " + name);
}
}
Name: Ole
Hello Ole
Common errors
• Mismatched braces or parenthesis.
– You'll get errors everywhere
• Forgetting a semicolon.
– Will sometimes lead to confusing error
messages.
• Inconsistent spelling or capitalization of
variable names.
– ”What do you mean ’cannot be resolved’? It’s
right there.”
Exercises
•
•
•
•
[Gaddis] Algorithm Workbench 9
Exercises 3.5 – 3.6
(Optional) [Gaddis] Find the Error (p. 106)
[Gaddis] Programming challenges 2, 9, 10,
12, 13, (Optional) 14
Homework
• Read [Gaddis] 3.1 – 3.5
• Do the exercises you haven't done in
class.
Related documents