COS 160 and COS 170 Structured Problem Solving
COS 160 and COS 170 Structured Problem Solving: Java Professor: Charles Welty Office: 224 Science Bldg. Office Hours: M: 1: 00 - 2: 15 pm, 5: 30 – 6: 30 W: during and directly after lab and by appointment other days: by appointment email: welty@usm. maine. edu Welty’s homepage: http: //cs. usm. maine. edu/~welty/ Tel: (207) 780 -4240 Syllabus: http: //www. cs. usm. maine. edu/~welty/cos 160/160 fall 2010/ 1
Any questions? Call roll. Ask if anyone’s name has not been called. Questions?
Building Java Programs Chapter 1: Introduction to Java Programming Copyright 2006 by Pearson Education 3
Introduction to Programming and basic Java programs with println statements reading: 1. 1 - 1. 3 4
Section 1. 1 Basic Computer Concepts program: A set of instructions to be carried out by a computer. program execution: The act of carrying out the instructions contained in a programming language: A systematic set of rules used to describe computations in a format that is editable by humans and executable by computers. This textbook teaches programming in a language named Java. Copyright 2006 by Pearson Education 5
Compile/run a program 1. Write the program. – code or source code: The set of instructions in a program. 2. Compile the program. • compile: Translate a program from one language to another. – byte code: The Java compiler converts your code into a format named byte code that runs on many computer types. 3. Run (execute) the program. – output: The messages printed to the user by a program. Console byte code source code compile run
Section 1. 2 And Now - Java System. out. println • A statement that prints a line of output on the console. – pronounced "print-linn" – sometimes called a "println statement" for short • Two ways to use System. out. println : • System. out. println("text"); Prints the given message (string) as output, then goes to the next output line. The order of operation is the same as the name: First print, then ln (line). • System. out. println(); Prints a blank line of output.
Section 1. 2 And Now - Java public class Hello { public static void main(String[] args) { System. out. println("Hello, world!"); } } code or source code: The sequence of instructions in a program. The code in this program instructs the computer to display a message of Hello, world! on the screen. output: The messages printed to the user by a program. In this case: Hello, world! console: The text box onto which output is printed. This is located at the bottom of the Eclipse window. (AKA System. out in Java. ) Copyright 2006 by Pearson Education 8
n string: A sequence of text characters that can be printed (using println()) or manipulated in a program. n sometimes also called a string literal n strings in Java start and end with quotation mark (") characters. These are called the string delimiters. n Examples: "hello" "This is a string" "This, too, is a string. It can be very long!“ public class Hello { public static void main(String[] args) { System. out. println("Hello, world!"); } } Copyright 2006 by Pearson Education 9
A string may not span across multiple lines. "This is not a legal String. “ A string may not contain a " character. (The ' character is okay) "This is not a "legal" String either. " "This is 'okay' though. ” A string can represent certain special characters by preceding them with a backslash (this is called an escape sequence). t tab character The says to treat the following character differently than it is usually " quotation mark character treated. Usually the character n just causes an n to be printed, but a n \ backslash character causes a newline to be started. Ex. System. out. println("\hellonhowtare "you"? "); new line character n Output: hello how Copyright 2006 by Pearson Education are "you"? 10
What is the output of each of the following println statements? System. out. println("tatbtc"); System. out. println("\\"); System. out. println("'"); System. out. println("""""); System. out. println("C: ninthe downward spiral"); a b c \ ' C: in he downward spiral """ Copyright 2006 by Pearson Education 11
Names and identifiers • You must give your program a name. public class Hello. World { – Program (class) naming convention: capitalize each word (e. g. My. Class. Name) – Your program's file must matchthe name exactly (Hello. World. java, My. Class. Name. java ) • includes capitalization (Java is "case-sensitive") • identifier: A name given to an item in your program. – must start with a letter or _ or $ (We will always start with a letter. ) – subsequent characters can be any of those or a number – An identifier may not contain any blank spaces. • legal: _my. Name The. Cure ANSWER_IS_42 $bling$ • Illegal: me+u 49 ers side-swipe Ph. D's Do you see why the legal ones are legal and why the illegal ones are illegal? A program (class) name is just an identifier. It is by convention that each word starts with a capital letter. A program would work if this convention were not followed but it is a good idea to follow the convention.
A program (class) name is just an identifier. It is by convention that each word starts with a capital letter. A program would work if this convention were not followed but it is a good idea to follow the convention.
identifier: A name given to a piece of data, method, class, etc. Identifiers allow us to refer to an item later in the program. Identifiers give names to: classes methods variables, constants (seen in Ch. 2) Conventions for naming in Java: - classes: capitalize each word (Class. Name) - variable names and methods: capitalize each word after the first (e. g. , method. Name variable. Name) - constants: all caps, words separated by _ (CONSTANT_NAME) Copyright 2006 by Pearson Education 14
Structure of a Java program class: a program public class Name { public static void main(String[] args) { statement; method: a named group of statement; statements. This method. . . is named “main”. statement; } statement: a command to be executed } • Every executable Java program consists of a class, – that contains a method named main, • that contains the statements (commands) to be executed. Ex. public class Hello { // class is Hello public static void main(String[] args) {// main method System. out. println("Hello, world!"); //statement } }
Ex. public class Hello { // class is Hello public static void main(String[] args) {// main method System. out. println("Hello, world!"); //statement } }
public class Hello 2 { public static void main(String[] args) { System. out. println("Hello, world!"); System. out. println("This program produces four"); System. out. println("lines of output. "); } What is the output of this program? } Syntax template for the java main method: public static void main(String[] args) { <statement>; Output of the program: <statement>; Hello, world!. . . <statement>; This program produces four lines of output. } Copyright 2006 by Pearson Education 17
keyword: An identifier that you cannot use because it already has a reserved meaning in the Java language. Complete list of Java keywords: (The keywords we will use are underlined. ) abstract boolean break byte case catch char class const continue default do double else extends finally float for goto if implements import instanceof interface long native new package private protected public return short static strictfp super switch synchronized this throws transient try void volatile while You may not use any reserved word for the name of a class, variable, method, etc. ; You could use a reserved word with different capitalization (e. g. , CHAR or While) because Java is case-sensitive. However, this could be confusing and is not recommended. Copyright 2006 by Pearson Education 18
Write a println() statement to produce the following line of output: / // \ /// \ System. out. println("/\t// \\ /// \\\");
Syntax Section 1. 3 Program Errors • syntax: The set of legal structures and commands that can be used in a particular language. – Every basic Java statement ends with a semicolon ; – The contents of a class or method occur between { and } – and lots more • syntax error (compiler error): A problem in the structure of a program that causes the compiler to fail. – Missing semicolon – Too many or too few { } braces – Illegal identifier – Class and file names do not match – and lots more
Syntax error example 1 2 3 4 5 public class Hello { pooblic static void main(String[] args) { System. owt. println("Hello, world!")_ } } • Compiler output: owt cannot be resolved or it is not a field Syntax error on token “-” ; expected Line 3 Syntax error on token “pooblic”, invalid Modifiers Line 2 ^ 3 errors – The compiler shows the line number where it found the error. – The error messages can be tough to understand!
Syntax error: A mistake that violates Java’s rules of form. • • Caught by the Java compiler Also called compile time errors. Java will tell you the line that compiler thinks the error is on and what it thinks the error is. Sometimes the line and message will not conform to the way humans think. Logic error: The program does compile but does not produce the correct answer. Example: Statement as written a * b, correct statement a + b. Runtime error: The machine cannot execute the statement as written. The usual example is having the program do a divide by 0. 22
- Slides: 23