Transcript slides

COMS W1004
Introduction to Computer Science
June 3, 2009
Last Time
• More object-oriented programming
– Methods
– Constructors
• Implementing Java classes
• Java library
– String class
– Math static methods
– System.out.println
Today
• Java review
• Programming environments
– Reading: Big Java sections 1.5-1.7
• Common programming errors
• In-class programming assignment
• Homework #2
Programming Environments
• Generally contain an editor where you write
your code
– Syntax highlighting of keywords, etc.
• Usually contain a compiler to compile code
– Sometimes compilation is automatic
• Also allow you to execute your code
• Debuggers for troubleshooting
Programming Environments
• Eclipse: http://www.eclipse.org
• NetBeans: http://www.netbeans.org
• JEdit: http://www.jedit.org
• JCreator: http://www.jcreator.com
Columbia computing facilities
• 251 Mudd (aka 251 Engineering Terrace)
– NetBeans, but not Eclipse
– Usually open 9am-5pm, closed weekends
• 213 Butler
– Usually open until 9pm, closed Sundays
• Support staff at the labs are not TAs!
Dealing with compiler errors
• If you get a whole bunch of errors, sometime fixing
the first error will make the others go away.
• 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.
Common programming errors
• These are some of the common errors that
students encounter when they’re just
starting
• Note that these are the error messages
you’d see in Eclipse; other programming
environments provide slightly different
wording
Class name and file name
Error: class BankAccont must be defined in its own file
Code: public class BankAccont {
• 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 resolve type
Error: Scanner cannot be resolved to a type
Code: Scanner scan = new Scanner(System.in);
• Any error that involves "cannot be resolved" means that
the compiler doesn't know what that symbol (which can
be a variable or a class name) refers to.
• In the code, if the name Scanner is underlined, 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.
Constructor is undefined
Error: The constructor Scanner() is undefined
Code: Scanner scan = new Scanner();
• This time, the method is “undefined”
because it doesn’t have the right
arguments.
• It probably means that you forgot to put
"System.in" inside the parentheses.
Method is undefined
Error: Method nextdouble is undefined for type Scanner
Code: double x = scan.nextdouble();
• Yet another “undefined" 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!
Duplicates
Error: Duplicate local variable x
Code: int x = 9;
• This means there’s already a variable
called x in your code
• Remember, you only need to declare
(state the type of) each variable the first
time you use it
Misleading errors
Error: Syntax error on sum, delete this token
Code: System.out.println("The sum is " sum);
• In this case, the error is rather misleading,
as it thinks “sum” is there inadvertently.
• However, as you can see, the error is
actually that you need a plus sign between
the two things you are concatenating.
Java Coding Conventions
• Class names should start with an upper-case
letter and use camel caps: BankAccount
• Attribute and method names should start with
a lower-case letter and use camel caps:
accountBalance, getBalance
• You should average about one single-line
comment for every 2-3 lines of code
• Use indentation
Homework #2
• Due next Monday, June 8 at beginning of
class
• Theory
– Paper submission only
• Programming
– Electronic submission and paper submission
• Can I have an extension? NO! 