Presentation is loading. Please wait.

Presentation is loading. Please wait.

Building Java Programs Chapter 1

Similar presentations


Presentation on theme: "Building Java Programs Chapter 1"— Presentation transcript:

1 Building Java Programs Chapter 1
Introduction to Java Programming Copyright (c) Pearson All rights reserved.

2 Compile/run a program Write it. Compile it. Run (execute) it.
code or source code: The set of instructions in a program. Compile it. 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. Run (execute) it. output: The messages printed to the user by a program. source code compile byte code run output

3 A Java program Its output:
public class Hello { public static void main(String[] args) { System.out.println("Hello, world!"); System.out.println(); System.out.println("This program produces"); System.out.println("four lines of output"); } Its output: Hello, world! This program produces four lines of output console: Text box into which the program's output is printed.

4 Structure of a Java program
class: a program public class name { public static void main(String[] args) { statement; ... } Every executable Java program consists of a class, that contains a method named main, that contains the statements (commands) to be executed. method: a named group of statements statement: a command to be executed

5 Names and identifiers You must give your program a name.
public class GangstaRap { Naming convention: capitalize each word (e.g. MyClassName) Your program's file must match exactly (GangstaRap.java) includes capitalization (Java is "case-sensitive") identifier: A name given to an item in your program. must start with a letter or _ or $ subsequent characters can be any of those or a number legal: _myName TheCure ANSWER_IS_42 $bling$ illegal: me+u ers side-swipe Ph.D's

6 Keywords keyword: An identifier that you cannot use because it already has a reserved meaning in Java. abstract default if private this boolean do implements protected throw break double import public throws byte else instanceof return transient case extends int short try catch final interface static void char finally long strictfp volatile class float native super while const for new switch continue goto package synchronized

7 Static methods (also called class methods)

8 Static methods static method: A named group of statements.
denotes the structure of a program eliminates redundancy by code reuse procedural decomposition: dividing a problem into methods Writing a static method is like adding a new command to Java. class method A statement method B method C

9 Why Static Methods?

10 Class Methods (from King’s book)
Uses of class methods: To provide a service to other classes. Methods in this category are declared public. To help other methods in the same class. “Helper” methods provide assistance to other methods in the same class. Helper methods should be private. To provide access to hidden class variables. If a class variable is private, the only way for a method in a different class to access the variable or to change its value is to call a class method that has permission to access the variable. Methods in this category are declared public.

11 Class Methods (from King’s book)
Class methods have another important purpose: to specify where a program begins execution. Every Java application must have a main method. main is a class method, so every Java application has at least one class method. Many classes in the Java API provide class methods, including Math, System, and String.

12 Methods called by static method (from King’s book)
Any methods called by a static method has to be a static method. Otherwise, the complier will give an error “cannot make a static reference to the non-static method…” In particular, any methods called by the main method need to be defined as static. Similarly, any outside variables used by a class method need to be static variable.

13 Using static methods 1. Design the algorithm.
Look at the structure, and which commands are repeated. Decide what are the important overall tasks. 2. Declare (write down) the methods. Arrange statements into groups and give each group a name. 3. Call (run) the methods. The program's main method executes the other methods to perform the overall task.

14 Gives your method a name so it can be executed
Declaring a method Gives your method a name so it can be executed Syntax: public static void name() { statement; statement; statement; } Example: public static void printWarning() { System.out.println("This product causes cancer"); System.out.println("in lab rats and humans."); }

15 Executes the method's code
Calling a method Executes the method's code Syntax: name(); You can call the same method many times if you like. Example: printWarning(); Output: This product causes cancer in lab rats and humans.

16 Program with static method
public class FreshPrince { public static void main(String[] args) { rap(); // Calling (running) the rap method System.out.println(); rap(); // Calling the rap method again } // This method prints the lyrics to my favorite song. public static void rap() { System.out.println("Now this is the story all about how"); System.out.println("My life got flipped turned upside-down"); Output: Now this is the story all about how My life got flipped turned upside-down

17 Design of an algorithm // This program displays a delicious recipe for baking cookies. public class BakeCookies2 { public static void main(String[] args) { // Step 1: Make the cake batter. System.out.println("Mix the dry ingredients."); System.out.println("Cream the butter and sugar."); System.out.println("Beat in the eggs."); System.out.println("Stir in the dry ingredients."); // Step 2a: Bake cookies (first batch). System.out.println("Set the oven temperature."); System.out.println("Set the timer."); System.out.println("Place a batch of cookies into the oven."); System.out.println("Allow the cookies to bake."); // Step 2b: Bake cookies (second batch). // Step 3: Decorate the cookies. System.out.println("Mix ingredients for frosting."); System.out.println("Spread frosting and sprinkles."); }

18 Final cookie program // This program displays a delicious recipe for baking cookies. public class BakeCookies3 { public static void main(String[] args) { makeBatter(); bake(); // 1st batch bake(); // 2nd batch decorate(); } // Step 1: Make the cake batter. public static void makeBatter() { System.out.println("Mix the dry ingredients."); System.out.println("Cream the butter and sugar."); System.out.println("Beat in the eggs."); System.out.println("Stir in the dry ingredients."); // Step 2: Bake a batch of cookies. public static void bake() { System.out.println("Set the oven temperature."); System.out.println("Set the timer."); System.out.println("Place a batch of cookies into the oven."); System.out.println("Allow the cookies to bake."); // Step 3: Decorate the cookies. public static void decorate() { System.out.println("Mix ingredients for frosting."); System.out.println("Spread frosting and sprinkles.");

19 Methods calling methods
public class MethodsExample { public static void main(String[] args) { message1(); message2(); System.out.println("Done with main."); } public static void message1() { System.out.println("This is message1."); public static void message2() { System.out.println("This is message2."); System.out.println("Done with message2."); Output: This is message1. This is message2. Done with message2. Done with main. What will be the output of this program?

20 Control flow When a method is called, the program's execution...
"jumps" into that method, executing its statements, then "jumps" back to the point where the method was called. public class MethodsExample { public static void main(String[] args) { message1(); message2(); System.out.println("Done with main."); } ... public static void message1() { System.out.println("This is message1."); } public static void message2() { System.out.println("This is message2."); message1(); System.out.println("Done with message2."); } public static void message1() { System.out.println("This is message1."); }

21 Class Methods (from King’s book)
If a class method has been declared public, it can be called as follows: class . method-name ( arguments ) When a class method is called by another method in the same class, the class name and dot can be omitted.

22 Class Methods (from King’s book)
Some Sample Class methods: SimpleIO.prompt SimpleIO.readLine Convert.toDouble Integer.parseInt Math.abs Math.max Math.min Math.pow Math.round Math.sqrt

23 Building Java Programs Chapter 2
Primitive Data and Definite Loops Copyright (c) Pearson All rights reserved.

24 Data types type: A category or set of data values.
Constrains the operations that can be performed on data Many languages ask the programmer to specify types Examples: integer, real number, string Internally, computers store everything as 1s and 0s 104  "hi"  ask them, how might the computer store "hi" using binary digits? (some kind of mapping; ASCII)

25 Java's primitive types primitive types: simple types for numbers, text, etc. Java also has object types, which we'll talk about later Name Description Examples int integers (up to ) 42, -3, 0, double real numbers (up to 10308) 3.1, , 9.4e3 char single text characters 'a', 'X', '?', '\n' boolean logical values true, false We're basically going to manipulate letters and numbers. We make the integer / real number distinction in English as well. We don't ask, "How many do you weigh?" or, "How much sisters do you have?" Part of the int/double split is related to how a computer processor crunches numbers. A CPU does integer computations and a Floating Point Unit (FPU) does real number computations. Why does Java separate int and double? Why not use one combined type called number? 25

26 Variables 26

27 Declaration variable declaration: Sets aside memory for storing a value. Variables must be declared before they can be used. Syntax: type name; The name is an identifier. int x; double myGPA; x myGPA

28 Assignment assignment: Stores a value into a variable. Syntax:
The value can be an expression; the variable stores its result. Syntax: name = expression; int x; x = 3; double myGPA; myGPA = ; x 3 myGPA 3.25

29 Using variables Once given a value, a variable can be used in expressions: int x; x = 3; System.out.println("x is " + x); // x is 3 System.out.println(5 * x - 1); // 5 * 3 - 1 You can assign a value more than once: int x; x = 3; System.out.println(x + " here"); // 3 here x = 4 + 7; System.out.println("now x is " + x); // now x is 11 x 11 x 3

30 Compiler errors A variable can't be used until it is assigned a value.
int x; System.out.println(x); // ERROR: x has no value You may not declare the same variable twice. int x; int x; // ERROR: x already exists int x = 3; int x = 5; // ERROR: x already exists How can this code be fixed?

31 The for loop 31

32 for loop syntax for (initialization; test; update) { statement; ... }
Perform initialization once. Repeat the following: Check if the test is true. If not, stop. Execute the statements. Perform the update. body header

33 Initialization Tells Java what variable to use in the loop
for (int i = 1; i <= 6; i++) { System.out.println("I am so smart"); } Tells Java what variable to use in the loop Performed once as the loop begins The variable is called a loop counter can use any name, not just i can start at any value, not just 1

34 Test Tests the loop counter variable against a limit
for (int i = 1; i <= 6; i++) { System.out.println("I am so smart"); } Tests the loop counter variable against a limit Uses comparison operators: < less than <= less than or equal to > greater than >= greater than or equal to

35 Loop walkthrough Output: 1 squared = 1 2 squared = 4 3 squared = 9
for (int i = 1; i <= 4; i++) { System.out.println(i + " squared = " + (i * i)); } System.out.println("Whoo!"); Output: 1 squared = 1 2 squared = 4 3 squared = 9 4 squared = 16 Whoo! 4 5 1 2 4 3 5

36 Nested for loops

37 Nested loops nested loop: A loop placed inside another loop. Output:
for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 10; j++) { System.out.print("*"); } System.out.println(); // to end the line Output: ********** The outer loop repeats 5 times; the inner one 10 times. "sets and reps" exercise analogy How would we print a multiplication table? try printing each of the following inside the inner loop: System.out.print(i + " "); System.out.print(j + " "); System.out.print((i * j) + " ");

38 Nested for loop exercise
What is the output of the following nested for loops? for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) { System.out.print(i); } System.out.println(); Output: 1 22 333 4444 55555 How about print(j)?

39 Common errors Find error in the following sets of code:
for (int i = 1; i <= 5; i++) { for (int j = 1; i <= 10; j++) { System.out.print("*"); } System.out.println(); for (int j = 1; j <= 10; i++) { Both cases produce infinite loops. Both of them produce infinite loops 39

40 Variable’s scope 40

41 Limitations of variables
Idea: Make a variable to represent the size. Use the variable's value in the methods. Problem: A variable in one method can't be seen in others. public static void main(String[] args) { int size = 4; topHalf(); printBottom(); } public static void topHalf() { for (int i = 1; i <= size; i++) { // ERROR: size not found ... public static void bottomHalf() { for (int i = size; i >= 1; i--) { // ERROR: size not found 41

42 Scope scope: The part of a program where a variable exists. i's scope
From its declaration to the end of the { } braces A variable declared in a for loop exists only in that loop. A variable declared in a method exists only in that method. public static void example() { int x = 3; for (int i = 1; i <= 10; i++) { System.out.println(x); } // i no longer exists here } // x ceases to exist here x's scope i's scope

43 Scope implications Variables without overlapping scope can have same name. for (int i = 1; i <= 100; i++) { System.out.print("/"); } for (int i = 1; i <= 100; i++) { // OK System.out.print("\\"); int i = 5; // OK: outside of loop's scope A variable can't be declared twice or used out of its scope. for (int i = 1; i <= 100 * line; i++) { int i = 2; // ERROR: overlapping scope i = 4; // ERROR: outside scope

44 Using a constant Constant allows many methods to refer to same value:
public static final int SIZE = 4; public static void main(String[] args) { topHalf(); printBottom(); } public static void topHalf() { for (int i = 1; i <= SIZE; i++) { // OK ... public static void bottomHalf() { for (int i = SIZE; i >= 1; i--) { // OK 44

45 Building Java Programs Chapter 3
Parameters and Objects Copyright (c) Pearson All rights reserved.

46 Stating that a method requires a parameter in order to run
Declaring a parameter Stating that a method requires a parameter in order to run public static void name ( type name ) { statement(s); } Example: public static void sayPassword(int code) { System.out.println("The password is: " + code); When sayPassword is called, the caller must specify the integer code to print.

47 Calling a method and specifying values for its parameters
Passing a parameter Calling a method and specifying values for its parameters name (expression); Example: public static void main(String[] args) { sayPassword(42); sayPassword(12345); } Output: The password is 42 The password is 12345 You have already called System.out.println and passed parameters to it.

48 How parameters are passed
When the method is called: The value is stored into the parameter variable. The method's code executes using that value. public static void main(String[] args) { chant(3); chant(7); } public static void chant(int times) { for (int i = 1; i <= times; i++) { System.out.println("Just a salad..."); 3 7

49 Common errors If a method accepts a parameter, it is illegal to call it without passing any value for that parameter. chant(); // ERROR: parameter value required The value passed to a method must be of the correct type. chant(3.7); // ERROR: must be of type int

50 Multiple parameters A method can accept multiple parameters. (separate by , ) When calling it, you must pass values for each parameter. Declaration: public static void name (type name, ..., type name) { statement(s); } Call: methodName (value, value, ..., value);

51 Multiple params example
public static void main(String[] args) { printNumber(4, 9); printNumber(17, 6); printNumber(8, 0); printNumber(0, 8); } public static void printNumber(int number, int count) { for (int i = 1; i <= count; i++) { System.out.print(number); System.out.println(); Output:

52 Value semantics value semantics: When primitive variables (int, double) are passed as parameters, their values are copied. Modifying the parameter will not affect the variable passed in. public static void strange(int x) { x = x + 1; System.out.println("1. x = " + x); } public static void main(String[] args) { int x = 23; strange(x); System.out.println("2. x = " + x); ... Output: 1. x = 24 2. x = 23 Note: This will not be the case for object variables!

53 Return values

54 Java's Math class Method name Description Math.abs(value)
absolute value Math.ceil(value) rounds up Math.floor(value) rounds down Math.log10(value) logarithm, base 10 Math.max(value1, value2) larger of two values Math.min(value1, value2) smaller of two values Math.pow(base, exp) base to the exp power Math.random() random double between 0 and 1 Math.round(value) nearest whole number Math.sqrt(value) square root Math.sin(value) Math.cos(value) Math.tan(value) sine/cosine/tangent of an angle in radians Math.toDegrees(value) Math.toRadians(value) convert degrees to radians and back Constant Description Math.E Math.PI

55 Calling Math methods Examples:
Math.methodName(parameters) Examples: double squareRoot = Math.sqrt(121.0); System.out.println(squareRoot); // 11.0 int absoluteValue = Math.abs(-50); System.out.println(absoluteValue); // 50 System.out.println(Math.min(3, 7) + 2); // 5 The Math methods do not print to the console. Each method produces ("returns") a numeric result. The results are used as expressions (printed, stored, etc.).

56 Return return: To send out a value as the result of a method.
The opposite of a parameter: Parameters send information in from the caller to the method. Return values send information out from a method to its caller. A call to the method can be used as part of an expression. main Math.abs(-42) -42 Math.round(2.71) 2.71 42 3

57 Type casting type cast: A conversion from one type to another. Syntax:
To promote an int into a double to get exact division from / To truncate a double from a real number to an integer Syntax: (type) expression Examples: double result = (double) 19 / 5; // 3.8 int result2 = (int) result; // 3 int x = (int) Math.pow(10, 3); // 1000

58 Returning a value Example: public static type name(parameters) {
statements; ... return expression; } Example: // Returns the slope of the line between the given points. public static double slope(int x1, int y1, int x2, int y2) { double dy = y2 - y1; double dx = x2 - x1; return dy / dx; slope(1, 3, 5, 11) returns 2.0

59 Return examples // Converts degrees Fahrenheit to Celsius. public static double fToC(double degreesF) { double degreesC = 5.0 / 9.0 * (degreesF - 32); return degreesC; } // Computes triangle hypotenuse length given its side lengths. public static double hypotenuse(int a, int b) { double c = Math.sqrt(a * a + b * b); return c; You can shorten the examples by returning an expression: return 5.0 / 9.0 * (degreesF - 32);

60 Common error: Not storing
Many students incorrectly think that a return statement sends a variable's name back to the calling method. public static void main(String[] args) { slope(0, 0, 6, 3); System.out.println("The slope is " + result); // ERROR: } // result not defined public static double slope(int x1, int x2, int y1, int y2) { double dy = y2 - y1; double dx = x2 - x1; double result = dy / dx; return result; }

61 Fixing the common error
Instead, returning sends the variable's value back. The returned value must be stored into a variable or used in an expression to be useful to the caller. public static void main(String[] args) { double s = slope(0, 0, 6, 3); System.out.println("The slope is " + s); } public static double slope(int x1, int x2, int y1, int y2) { double dy = y2 - y1; double dx = x2 - x1; double result = dy / dx; return result; How about dy=y-y1? y is not defined. How about defining y in the main()? How about defining y in as a class variable?

62 Objects and Classes; Strings

63 Classes and objects class: A program entity that represents either:
1. A program / module, or 2. A type of objects. A class is a blueprint or template for constructing objects. Example: The DrawingPanel class (type) is a template for creating many DrawingPanel objects (windows). Java has 1000s of classes. Later (Ch.8) we will write our own. object: An entity that combines data and behavior. object-oriented programming (OOP): Programs that perform their behavior as interactions between objects.

64 Objects object: An entity that contains data and behavior.
data: variables inside the object behavior: methods inside the object You interact with the methods; the data is hidden in the object. Constructing (creating) an object: Type objectName = new Type(parameters); Calling an object's method: objectName.methodName(parameters);

65 iPod blueprint/factory
Blueprint analogy iPod blueprint/factory state: current song volume battery life behavior: power on/off change station/song change volume choose random song creates iPod #1 state: song = "1,000,000 Miles" volume = 17 battery life = 2.5 hrs behavior: power on/off change station/song change volume choose random song iPod #2 state: song = "Letting You" volume = 9 battery life = 3.41 hrs iPod #3 state: song = "Discipline" volume = 24 battery life = 1.8 hrs

66 Strings string: An object storing a sequence of text characters.
Unlike most other objects, a String is not created with new. String name = "text"; String name = expression; Examples: String name = "Marla Singer"; int x = 3; int y = 5; String point = "(" + x + ", " + y + ")";

67 Indexes Characters of a string are numbered with 0-based indexes:
String name = "R. Kelly"; First character's index : 0 Last character's index : 1 less than the string's length The individual characters are values of type char (seen later) index 1 2 3 4 5 6 7 character R . K e l y

68 String methods These methods are called using the dot notation:
String gangsta = "Dr. Dre"; System.out.println(gangsta.length()); // 7 Method name Description indexOf(str) index where the start of the given string appears in this string (-1 if not found) length() number of characters in this string substring(index1, index2) or substring(index1) the characters in this string from index1 (inclusive) to index2 (exclusive); if index2 is omitted, grabs till end of string toLowerCase() a new string with all lowercase letters toUpperCase() a new string with all uppercase letters

69 Interactive Programs with Scanner

70 Input and System.in interactive program: Reads input from the console.
While the program runs, it asks the user to type input. The input typed by the user is stored in variables in the code. Can be tricky; users are unpredictable and misbehave. But interactive programs have more interesting behavior. Scanner: An object that can read input from many sources. Communicates with System.in (the opposite of System.out) Can also read from files (Ch. 6), web sites, databases, ...

71 Scanner syntax The Scanner class is found in the java.util package.
import java.util.*; // so you can use Scanner Constructing a Scanner object to read console input: Scanner name = new Scanner(System.in); Example: Scanner console = new Scanner(System.in);

72 Scanner methods prompt: A message telling the user what input to type.
Each method waits until the user presses Enter. The value typed by the user is returned. System.out.print("How old are you? "); // prompt int age = console.nextInt(); System.out.println("You typed " + age); prompt: A message telling the user what input to type. Method Description nextInt() reads an int from the user and returns it nextDouble() reads a double from the user next() reads a one-word String from the user nextLine() reads a one-line String from the user

73 Scanner example Console (user input underlined): age 29 years 36
import java.util.*; // so that I can use Scanner public class UserInputExample { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("How old are you? "); int age = console.nextInt(); int years = 65 - age; System.out.println(years + " years to retirement!"); } Console (user input underlined): How old are you? 36 years until retirement! age 29 years 36 It's also useful to write a program that prompts for multiple values, both on the same line or each on its own line. 29

74 Scanner example 2 Output (user input underlined):
import java.util.*; // so that I can use Scanner public class ScannerMultiply { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("Please type two numbers: "); int num1 = console.nextInt(); int num2 = console.nextInt(); int product = num1 * num2; System.out.println("The product is " + product); } Output (user input underlined): Please type two numbers: 8 6 The product is 48 The Scanner can read multiple values from one line. It's also useful to write a program that prompts for multiple values, both on the same line or each on its own line.

75 Input tokens token: A unit of user input, as read by the Scanner.
Tokens are separated by whitespace (spaces, tabs, new lines). How many tokens appear on the following line of input? 23 John Smith "Hello world" $2.50 " 19" When a token is not the type you ask for, it crashes. System.out.print("What is your age? "); int age = console.nextInt(); Output: What is your age? Timmy java.util.InputMismatchException at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) ...

76 Strings as user input Scanner's next method reads a word of input as a String. Scanner console = new Scanner(System.in); System.out.print("What is your name? "); String name = console.next(); name = name.toUpperCase(); System.out.println(name + " has " + name.length() + " letters and starts with " + name.substring(0, 1)); Output: What is your name? Chamillionaire CHAMILLIONAIRE has 14 letters and starts with C The nextLine method reads a line of input as a String. System.out.print("What is your address? "); String address = console.nextLine();

77 Strings question Write a program that outputs a person's "gangsta name." first initial Diddy last name (all caps) first name -izzle Example Output: Type your name, playa: Marge Simpson Your gangsta name is "M. Diddy SIMPSON Marge-izzle"

78 Strings answer // This program prints your "gangsta" name.
import java.util.*; public class GangstaName { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("Type your name, playa: "); String name = console.nextLine(); // split name into first/last name and initials String first = name.substring(0, name.indexOf(" ")); String last = name.substring(name.indexOf(" ") + 1); last = last.toUpperCase(); String fInitial = first.substring(0, 1); System.out.println("Your gangsta name is \"" + fInitial + ". Diddy " + last + " " + first + "-izzle\""); }


Download ppt "Building Java Programs Chapter 1"

Similar presentations


Ads by Google