Lecture 3, CS 302-6, September 9 1. Review

advertisement
Lecture 3, CS 302-6, September 9
1. Review
a. Structure of a program – compiled and interpreted
i. Java, Machine Code, Byte Code, JVM
b. Java - why java?
i. Portability - -trade off!
ii. Ease of programming
iii. Important/useful language
iv. Java class library
v. Error Reporting
c. Java files
i. Source.java
ii. Source.class
2. Hello World
a. Class – fundamental building blocks of java programs, more details later
b. Main method – starting point for program. When you execute a program,
it begins executing instructions here. Introduce the idea of a thread
c. Other methods – can do other things. More later
d. Statement;
e. Some hand-waving here – public static void main(String[] args)
f. Note – semi colons, curly brackets, etc
i. Semi colons – statements
ii. Curly brackets – blocks of code. More later
g. Strings vs numbers
i. Strings – made of characters
ii. Numbers – math operations
h. Case sensitivity
i. Java is case sensitive – HelloWorld is not the same as helloworld
3. Hello World collection
a. http://www.roesler-ac.de/wolfram/hello.htm
4. System.out.print()
a. Same as System.out.println, but no new line
b. Note – the new line from .println comes After the text.
5. What do the following inputs to System.out.println() display?
a. “Hello” + “World!”
b. “Hello + 1 +2”
c. 1+2
d. “1”+2
e. 1+”2”
f. "1"+2+3
g. 1+2+”3”
h. 1+2+"3"+4+5
i. “1” + (2+3)
6. Errors, exceptions, etc
a. Compile time
i. Syntax error – cannot compile due to misspelling etc.
1. System.ou.println(“Hello World”);
ii. Compiler error – cannot compile because compiler is confused
1. System.out.println(1,2,3); Can’t find method
a. We’ll examine this one more later
2. System.out.printLn(“Hello World”);
3. System.out.println(Hello World!);
b. Run time
i. Logical error
1. System.out.println(“Hello Word!”);
ii. Run-time exception
1. System.out.println(1/0);
c. Question – why can’t you test a program for run time errors if it has
compile time errors?
d. Note – line breaks don’t really mean anything to java, just semicolons
i. Not ok:
System.out.println(“Hello World”)
ii. Ok:
System.out
.println(1+2+"3"+
4+5);
iii. Exception :
System.out.print
ln(1+2+"3"+
4+5);
7. Variables – Demo - input, name and age
import javax.swing.JOptionPane;
public class NameAndAge
{
public static void main(String[] args)
{
String name=JOptionPane.showInputDialog("What is your
name?");
int age=Integer.parseInt(JOptionPane.showInputDialog("What is
your age?"));
System.out.println("Hello " + name);
System.out.println("You are " + age + " years old");
System.exit(0);
}
}
8. Variables
a. Concept – allow your program to remember things.
b. Variables store values
c. They must be declared and initialized before use
i. Declaration
1. int number;
2. Sets aside a spot in memory for a variable
3. Type
a. int, double, etc…
ii. Initialization
1. number=10;
2. Assigns a value to a variable
3. Don’t need to declare value first, though
d. Declaring a variable is a statement – need to have a ;
e. One use - long math equations. Instead of having to do it ‘by hand’, we
can just use the symbol to represent it. Like algebra.
f. Football example
i. Variables for team score, point values for different ways to score,
etc
9. HW – Read Ch 2.1-2.4
Download