Transcript slides

COMS W1004
Introduction to Computer Science
May 30, 2008
Last Time
•
•
•
•
Course intro
What is Computer Science?
What is an algorithm?
Examples of algorithms
– Searching: Sequential, Binary
– Sorting: Selection, Insertion
• Pseudocode
• Comparing algorithms (S&G chapter 3)
Today
•
•
•
•
•
Java compilation and execution
Intro to programming concepts
Java datatypes
Your first Java program!
Java programming environments
• Reading: L&L 1-2
Dealing with compiler errors
• If you get a whole bunch of errors, sometime
fixing the first error will make the others go away.
So always start at the top (with the first error)
and try to address that one before looking at the
others.
• You may find that fixing an error causes more
errors to appear. That is because the compiler
makes two passes through the code, and looks
for different things each time. So don't despair if
you fix something and other errors appear.
Class name and file name
Calc.java:7: class Calculator is public, should be
declared in a file named Calculator.java
public class Calculator
^
• This means that the name of the class
does not match the name of the file.
Remember, they MUST be the same,
including capitalization!
Cannot find symbol
Calculator.java:13: cannot find symbol
symbol : class Scanner
location: class Calculator
Scanner scan = new Scanner(System.in);
^
• Any error that starts "cannot find symbol" means that the
compiler doesn't know what that symbol (which can be a
variable or a class name) refers to.
• In the second line of the error, where it says "symbol:
class Scanner", that indicates that it doesn't know what
the Scanner class is.
• This probably means that you forgot to include the line
"import java.util.Scanner;" at the top of the file.
Cannot find symbol
Calculator.java:13: cannot find symbol
symbol : constructor Scanner()
location: class java.util.Scanner
Scanner scan = new Scanner();
^
• Again we see an error that says "cannot
find symbol". This time, it is "constructor
Scanner()". We will see what a constructor
is later on, but in this case it probably
means that you forgot to put "System.in"
inside the parentheses.
Cannot find symbol
Calculator.java:19: cannot find symbol
symbol : method nextdouble()
location: class java.util.Scanner
double x = scan.nextdouble();
^
• Yet another "cannot find symbol" error.
Here it doesn't know "method
nextdouble()". Can you guess why?
Because it should be "nextDouble", with a
capital D. Be very careful about
capitalization!
; expected
Calculator.java:14: ';' expected
^
• This error message is slightly deceiving.
Clearly it means that you forgot a
semicolon. However, it tells you that the
error is on line 14. But that is not correct!
(Sometimes) The forgotten semicolon
might actually belong on the PREVIOUS
line, which would be line 13 in this case.
Don't ask why...
) expected
Calculator.java:29: ')' expected
System.out.println("The sum is " sum);
^
• In this case, the error is a bit misleading
because it says that a right parenthesis is
expected. However, as you can see, the
error is actually that you need a plus sign
between the two things you are
concatenating.
} expected
Calculator.java:45: '}' expected
}
^
• This probably means that you forgot a
curly brace somewhere. The easiest way
to avoid this is to vertically line up the left
and right curly braces and indent
accordingly.
Announcements
• Homework #1 is available on the website
– Programming homework is due Weds, June 4
– Theory homework is due Monday, June 9
• Office hours start on Monday
Programming Homework #1
• Start with the “Hello, World” and Calculator
examples and build upon them
• Don’t try to do too much at once
– Write a few lines, compile, repeat
• Don’t worry about error handling… yet
Extra Credit!
• First, in your home directory, type:
cp .profile .profileOld
• This creates a backup of your .profile file.
• Now use pico or emacs to edit the file .profile
adding the following two lines at the end of the
file. Please note the single quotes and the
spacing.
export CLASSPATH=$CLASSPATH:~cdm6/retina/
alias javac='~cdm6/retina/compile'