Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Building Java Programs Chapter 1 Introduction to Java Programming Copyright (c) Pearson 2013. All rights reserved."— Presentation transcript:

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

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

3 3 A Java program 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 4 Structure of a Java program public class name { public static void main(String[] args) { ; statement ;...... ; statement ; } Every executable Java program consists of a class, –that contains a method named main, that contains the statements (commands) to be executed. class: a program statement: a command to be executedmethod: a named group of statements

5 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 49ers side-swipe Ph.D's

6 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 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 statement method C statement

9 9 Why Static Methods?

10 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 11 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. Class Methods (from King’s book)

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

13 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 14 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."); } Declaring a method

15 15 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 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 Now this is the story all about how My life got flipped turned upside-down

17 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). 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. System.out.println("Mix ingredients for frosting."); System.out.println("Spread frosting and sprinkles."); }

18 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 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."); message1(); System.out.println("Done with message2."); } Output: This is message1. This is message2. This is message1. Done with message2. Done with main. What will be the output of this program?

20 20 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."); } Control flow

21 21 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. Class Methods (from King’s book)

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

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

24 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  01101000 "hi"  01101000110101

25 25 Java's primitive types primitive types: simple types for numbers, text, etc. –Java also has object types, which we'll talk about later NameDescriptionExamples –int integers (up to 2 31 - 1) 42, -3, 0, 926394 –double real numbers (up to 10 308 ) 3.1, -0.25, 9.4e3 –char single text characters 'a', 'X', '?', '\n' –boolean logical values true, false

26 26 Variables

27 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 28 Assignment assignment: Stores a value into a variable. –The value can be an expression; the variable stores its result. Syntax: name = expression ; –int x; x = 3; –double myGPA; myGPA = 1.0 + 2.25; x3 myGPA3.25

29 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 x3 x11

30 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 31 The for loop

32 32 for loop syntax for ( initialization ; test ; update ) { statement ;... 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 33 Initialization 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 34 Test 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 35 Loop walkthrough 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! 1 1 2 2 3 3 4 4 5 5

36 36 Nested for loops

37 37 Nested loops nested loop: A loop placed inside another loop. 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

38 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 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 i = 1; i <= 5; i++) { for (int j = 1; j <= 10; i++) { System.out.print("*"); } System.out.println(); } Both of them produce infinite loops

40 40 Variable’s scope

41 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... }

42 42 Scope scope: The part of a program where a variable exists. –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 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 System.out.print("/"); } i = 4; // ERROR: outside scope

44 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... }

45 Interactive Programs with Scanner

46 46 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,...

47 47 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);

48 48 Scanner methods –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. MethodDescription 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

49 49 Scanner example 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! 29 age29 years36

50 50 Scanner example 2 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.

51 51 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 42.0 "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)...

52 52 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(); Strings as user input

53 53 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"

54 54 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\""); }

55 55 Exercise Write a program to ask user to enter a number. If the number is less than 10, reject the input and print a message “the number is too small!”. Otherwise, print out the number.

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

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

58 58 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

59 59 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

60 60 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

61 61 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 );

62 62 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: 444444444 171717171717 00000000

63 63 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!

64 Return values

65 65 Java's Math class Method nameDescription 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 ConstantDescription Math.E 2.7182818... Math.PI 3.1415926...

66 66 Calling Math methods 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.).

67 67 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

68 68 Type casting type cast: A conversion from one type to another. –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

69 69 Returning a value 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

70 70 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: public static double fToC(double degreesF) { return 5.0 / 9.0 * (degreesF - 32); }

71 71 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; }

72 72 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 as a class variable?

73 Objects and Classes; Strings

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

75 75 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 ); Example: The Scanner Class

76 76 Blueprint analogy iPod blueprint/factory state: current song volume battery life behavior: power on/off change station/song change volume choose random song 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 behavior: power on/off change station/song change volume choose random song iPod #3 state: song = "Discipline" volume = 24 battery life = 1.8 hrs behavior: power on/off change station/song change volume choose random song creates

77 77 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 + ")";

78 78 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) index01234567 character R. Kelly

79 79 String methods These methods are called using the dot notation: String gangsta = "Dr. Dre"; System.out.println(gangsta.length()); // 7 Method nameDescription 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

80 80 Exercise Write a method called getUserInput() that asks user to enter a number. If the number is less than 10, reject the input and print a message “the number is too small!”. Otherwise, return the number. Write a program that use the getUserInput() method to get a number and print out the number. Write a method called getUserInput() that asks user to enter a number. The method takes one parameter that represents the lowBoundary of the number to be entered. If the number is less than lowBoundary, reject the input and print a message “the number is too small!”. Otherwise, return the number. Write a program that use the previous getUserInput() method to get a number no less than 25 and print out the number.


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

Similar presentations


Ads by Google