1 / 20

Intro to Programming

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.

quanda
Télécharger la présentation

Intro to Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Intro to Programming Your first program

  2. Switch to BlueJ • We will write and execute our very first program!

  3. 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.

  4. publicclass HelloPrinter { publicstaticvoidmain(String[] args) { // Display a greeting in the console window System.out.println("Hello, World!"); } }

  5. 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

  6. So, the basic plumbing of a program is: public class SomeName { public static void main(String[] args) { . . . } }

  7. 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. */

  8. 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!

  9. Now it starts to get fun . . .

  10. A few more things about printing • We have two basic methods, print and println. • Experiment with several statements to decern the difference

  11. 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

  12. 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!");

  13. 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!

  14. 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.

  15. 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");

  16. Try to make this ... / / / / / | o o | | ^ | | [--] | |--------|

  17. Write a program with multiple statements (at least 2 no more than 6) which has a syntax error.DO NOT COMPILE!!

  18. 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!!

  19. 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!!

  20. 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).

More Related