Sie sind auf Seite 1von 5

P&CS Program #1: Hello World

// Program #1: Hello World // <your name>, <the date>, p&cs class HelloWorld { public static void main(String[] args) { System.out.println("Hello world!"); System.out.println("This is my first program!"); } }

Learning Points: 1. A program is really a class with a main method. 2. The only part you really care about is the code inside the main. 2a. The rest, you do not need to understand, but you do need to type in. 3. Java is very particular about the syntax. 3a. Java is case-sensitive (you must get upper and lower case exactly correct) 3b. Semicolons must follow statements 3c. Your code can only go inside the main. Anywhere else is an error. 4. Good programmers (like you!) are very particular about style. 4a. Use squiggly braces and indent your code exactly as noted above 4b. Use comments to explain what the code does, who wrote it, etc. 4c. Many more style rules to follow 5. A line beginning with // is a comment, and Java ignores the line. 6. System.out.println("foo") prints the word "foo" followed by a newline. P&CS Program #2: The Sum of Two Numbers
// Program #2: The Sum of Two Numbers // <your name>, <the date>, p&cs class SumOfTwoNumbers { public static void main(String[] args) { // declare two integer variables int x, y; // assign their values x = 10; y = 4; // print out the variable values System.out.println(x); System.out.println(y); // now print out their sum System.out.println(x + y); } }

P&CS Program #7: While Statements

// Program #7: While Statements // <your name>, <the date>, p&cs class WhileStatements { public static void main(String[] args) { // This program uses a while statement to print out // the numbers from 1 to 10, inclusive. // declare the counter variable, which runs from 1 to 10 int counter; // initialize the counter to 1 counter = 1; // now, so long as the counter is <= 10, loop, printing // out the current value of the counter, then increasing // the value of counter by 1. while (counter <= 10) { // print out the current counter value System.out.println(counter); // and increment the counter by one counter = counter + 1; } // Let's confirm that counter is not <= 10. In particular, // we expect its value here to be 11. Let's prove it: System.out.println("Counter final value: " + counter); }

Learning Points: 1. A "while" statement is of the form: while (test) { BODY } 2. A "while" statement repeatedly executes its body so long as its test is true 3. Important note: There is no semicolon after the test!!! P&CS Program #8: While Statements and Running Sums
// Program #8: While Statements and Running Sums // <your name>, <the date>, p&cs class RunningSums { public static void main(String[] args) { // This program uses a while statement to compute the sum // from 0 to 100. // declare the counter variable and the sum variable. int counter, sum; // initialize the counter to 0 and the sum to 0 counter = 0;

sum = 0; // now use a while statement to step the counter from 0 // to 100. At each step, increment the sum by the counter. while (counter <= 100) { // increment the sum by the counter sum = sum + counter; // and increment the counter by one counter = counter + 1; } // and report our results System.out.println("The sum from 1 to 100 is: " + sum); } }

P&CS Program #9: while (true) -- Infinite Loops


// Program #9 while (true) -- Infinite Loops // <your name>, <the date>, p&cs class InfiniteLoop { public static void main(String[] args) { // This program uses a while (true) statement to // loop until the user enters the number 0. // To do something interesting, it prints out the // sum of all the numbers entered. // declare the input variable and the sum variable. int n, sum; // initialize the sum to 0 sum = 0; // now loop "forever" while (true) { System.out.print("Enter an integer [0 to quit]: "); n = readInt(); if (n == 0) { // n is zero, so break out of the enclosing while loop break; } sum = sum + n; } // and report our results System.out.println("The sum of the numbers is: " + sum); } // NOTE: insert readInt() code here... }

Learning Points: 1. A program is really a class with a main method. 2. The only part you really care about is the code inside the main. a. The rest, you do not need to understand, but you do need to type in. 3. Java is very particular about the syntax.

b. Java is case-sensitive (you must get upper and lower case exactly correct) c. Semicolons must follow statements d. Your code can only go inside the main. Anywhere else is an error. 4. Good programmers (like you!) are very particular about style. e. Use squiggly braces and indent your code exactly as noted above f. Use comments to explain what the code does, who wrote it, etc. g. Many more style rules to follow 5. A line beginning with // is a comment, and Java ignores the line. 6. System.out.println("foo") prints the word "foo" followed by a newline. 7. Variables have names (like "x" and "y") and hold values (like 10 and 4). 8. Integer variables have integer values a. Some legal integer values: 2, 0, -20, 1234321 b. Some illegal values: 3.14, "hello" 9. You must declare variables before you use them. a. This tells Java that these are variables, and what type (here, integers) b. The line "int x, y;" declares the variables x and y to be integers 10. You can assign values to variables a. "x = 10;" assigns the value 10 to the variable "x" 11. You can print out variables using System.out.println 12. You can even print out their sum using System.out.println 13. To run programs which call readInt(), you must include the readInt() code from program #3. 14. In general, we should prompt the user ("Enter an integer: "), so the user knows what the program is asking. 15. Use System.out.print instead of System.out.println to stay on the same line. 16. In general, we should also print out the answer in a prettier format. 17. print(x) prints the value of the variable x 18. print("foo") prints whatever is inside the quotes. 19. All your Java programs for now on should use prompts and pretty output. 20. An "if" statement is of the form: if (test) { BODY } 21. An "if" statement executes its body only if its test is true 22. Important note: In an "if" statement, there is no semicolon after the test!!! 23. A test for equality uses double-equals, as in (x == y) 24. A test for inequality uses exclamation-equals, as in (x != y) 25. Following an "if", you may have an "else if" which provides another test 26. The last "else" can omit the test, in which case its body is executed only if all the preceding tests fail. 27. In any case, at most one body will be executed within in if-else statement. Think about this! 28. A "while" statement is of the form: while (test)

{ BODY } 29. A "while" statement repeatedly executes its body so long as its test is true 30. Important note: In a "while" statement, there is no semicolon after the test!!! 31. Study the code in Program #8 carefully to understand how the "sum" variable holds a running tally and in the end holds the sum of the numbers from 0 to 100. 32. while (true) loops forever. 33. To exit a loop (including an infinite loop), use the break statement. 34. Math.abs(x) is the absolute value of x 35. x % y, pronounced "x mod y", is the remainder of x divided by y. 36. Math.max(x,y) is the larger of x and y. 37. Math.min(x,y) is the smaller of x and y.

How do you write a Java program that sums the digits of an integer?
public static final int sumDigits(final int n) { int _n = n; int sum = 0; while( _n > 0 ) { sum += (_n % 10); // add last digit of _n to sum _n /= 10; // divide _n by 10 to "chop off" the last digit } return sum; }

Das könnte Ihnen auch gefallen