breaking code - unm computer science

32
CS-152: Computer Programming Fundamentals in Java Neal Holtschulte June 15, 2013

Upload: others

Post on 09-Feb-2022

3 views

Category:

Documents


0 download

TRANSCRIPT

CS-152: ComputerProgramming

Fundamentals in JavaNeal Holtschulte

June 15, 2013

How we broke the code

I Remove a forward slash from the

comment

I Take the quotes off of ”Hello, World”

I Remove ”static” from the method header

I Remove public from the method header

I Remove a closing bracket

I Put an actual line break in the print

statement

I ...

Error messages discoveredI Syntax error on token(s), misplaced

construct(s)I Hello cannot be resolved to a variable

World cannot be resolved to a variableI java.lang.NoSuchMethodErrorI Main method not public.I Syntax error, insert “}” to complete

ClassBodyI String literal is not properly closed by a

double-quoteI ...

Syntax error on token(s), misplacedconstruct(s)

People get this error for a variety of reasons,

mostly due to objectionable formatting.

Luckily eclipse helps us isolate the error.

Always seek the earliest occurrence of the

error to find the root of the problem.

Definition: Token

In a Java program, all characters are grouped

into symbols called tokens.

I identifiers

I keywords

I separators / special characters

I operators

I literals

I commentsSource: http://www.cs.cmu.edu/~pattis/15-1XX/15-200/lectures/tokens/lecture.html

comments: line or block

comments: line or block

identifiers: names a programmer chose

identifiers: names a programmer chose

keywords: names already in theprogramming language

keywords: names already in theprogramming language

literalsLiterals are specified by their type. For

example: int, double, boolean, char, and

String

literalsLiterals are specified by their type. For

example: int, double, boolean, char, and

String

separators: aka punctuatorsbut not necessarily the same punctuation as

English

separators: aka punctuatorsbut not necessarily the same punctuation as

English

operators: symbols that operate onarguments and produce results

operators: symbols that operate onarguments and produce results

Definition: Construct

A language construct is a syntactically

allowable part of a program that may be

formed from one or more lexical tokens in

accordance with the rules of a programming

language. http://en.wikipedia.org/

wiki/Language_construct

It’s a correctly formatted sequence of one or

more tokens.

Hello cannot be resolved to a variable

Java thinks Hello and World are variables,

but they have not been defined so Java is

confused.

This error can also occur when a variable is

used out of scope.

Definition: Variable

A variable is a storage location and an

associated symbolic name (an identifier)

which contains some known or unknown

quantity or information, a value.

http://en.wikipedia.org/wiki/

Variable_(computer_science)

Definition: Variable

Definition: Scope

Scope is the context within a computer

program in which a variable name or other

identifier is valid and can be used.

http://en.wikipedia.org/wiki/

Scope_(computer_science)

Scope

Go to Eclipse and show some examples.

java.lang.NoSuchMethodError

The use of “static” decares that a method or

variable can be invoked without creating an

instance of the class.

Ok, so what’s a class?

A class is a construct that can be used to

create instances of itself.

Platonic ideal.

A class defines the properties and

functionalities of its instances.

Ok, so what’s a class?

A coin class might have properties such

name and value. The coin class might have

the functionality spend.

Penny instance

String name = “penny”;

double value = 0.01;

Tree Class

What are some of the properties of the Tree

Class?

What are some of the functionalities of the

Tree Class?

Main method not public.

The main method must be public because it

is called by code outside of its class when it

is started.Source:http://playjava.wordpress.com/question-and-answers/explain-public-static-void-main-string-args/

What is all this? public static voidmain(String[] args)

I public - declares that the method is

accessible by other classes

I static - declares that the method can be

invoked without creating an instance of

the class

I void - declares that the method does not

return any valuehttp://www.allinterview.com/showanswers/1889.html

What is all this? public static voidmain(String[] args)

I main - defines the name of the method

I String[] args - defines a parameter to the

main method which will contain any

command line options passed by the user

when invoking the progam.http://www.allinterview.com/showanswers/1889.html

Syntax error, insert “}” to completeClassBody

This means the class is incomplete. Eclipse

even suggests a solution, insert “}”.

String literal is not properly closed by adouble-quote

This error says it all. There is a String literal

not properly closed by a double-quote.