220 likes | 368 Views
Intro to Programming. Your first program. Switch to BlueJ . We will write and execute our very first program!. Objectives. I can write a simple program involving the console window. I know when to use print and println I will not fully understand all the code I write today.
E N D
Intro to Programming Your first program
Switch to BlueJ • We will write and execute our very first program!
Objectives • I can write a simple program involving the console window. • I know when to use print and println • I will not fully understand all the code I write today. • I can identify methods used by an object and how to use those methods.
publicclass HelloPrinter { publicstaticvoidmain(String[] args) { // Display a greeting in the console window System.out.println("Hello, World!"); } }
Now let's look more closely at what we've done: • Java is case sensitive. Be very careful of what you capitalize! system.OUT.Println will not work! • Java has free-form layout • Consider readability
So, the basic plumbing of a program is: public class SomeName { public static void main(String[] args) { . . . } }
Inside the main method • The first line we have, //Display a greeting in the console window, is a comment. • Comments are only there to help other humans understand the code—the computer doesn't use them at all. The two slashes at the beginning of the line tell the computer to ignore whatever else is on that line of text. It's a good idea to add comments to your work, so you (and your teacher) can understand what you are trying to do. • If you have a comment that is longer than one line, you can also use /* ... */ to start and end a multiple line comment: /* Here's my long comment. See, it's two lines. */
Statements • The next line, System.out.println ("Hello, class!"), is a statement. Each statement in the body of a method is a single command for the computer. • The computer executes these lines one at a time until it reaches the end of the method. • Each statement ends with a semicolon. • **Caution: forgetting the semicolon is a common mistake! Your program won't compile without them!
A few more things about printing • We have two basic methods, print and println. • Experiment with several statements to decern the difference
What can I print? • The methods can print any strings (anything enclosed in quotation marks). • They can also print numbers: • System.out.println(53); • prints: 53 • They can print the answer to simple math problems • System.out.println(5 + 2); • prints: 7 • vs. System.out.println("5 + 2"); • prints: 5 + 2
Escape character • The backslash (\) is an escape character in strings. You can use it to print out quotation marks (which would normally close the string) or to print out backslashes. • If you want to print the line: • School is "fun!" :\ Yea! • You would type: System.out.print("School is \"fun!\" :\\ Yea!");
When things get messed up: • It is practically unheard of for someone to type in a program perfectly on the first try. • Realistically, the first time you try to run a program, you'll see lots of errors. Learn to read your errors! • The compiler can catch some of these, but you need to be on guard too!
Syntax errors • Syntax errors (compile-time errors) are mistakes the compiler will catch. • For each of these, the compiler will stop and tell you what the problem is. You must fix the problem and then compile again before the computer will go any further.
Logic errors • Once your program compiles, you have to worry about logic errors (run-time errors). This means you can run the program, but it doesn't do what it is supposed to do. • System.out.println("hgy there");
Try to make this ... / / / / / | o o | | ^ | | [--] | |--------|
Write a program with multiple statements (at least 2 no more than 6) which has a syntax error.DO NOT COMPILE!!
Write a program with multiple statements (at least 2 no more than 6) which has a logic error.*You must leave a comment about what your program should do!*DO NOT COMPILE!!
Write a program with multiple statements (at least 2 no more than 6) which has a logic error or a syntax error or both.*You must leave a comment about what your program should do!*DO NOT COMPILE!!
How to begin an object-oriented solution: • Write the solution down completely in regular English sentences. • Circle each noun (person, place, or thing). These are your objects. • Underline each verb (action word). These are your methods (the things each object can do). • When you get really good, star each adjective (describing word). These are the traits of each object (we'll eventually call them instance fields).