Methods (Functions) CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Size: px
Start display at page:

Download "Methods (Functions) CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington"

Transcription

1 Methods (Functions) CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1

2 What Is a Method? A method is a mechanism for allowing a piece of code to be executed more than once, in a safe way. Examples: s.length(), s.indexof(s2), Math.floor(3.4) Syntax Communication with the calling line. Signature of the method Parameters Return value (when appropriate) 2

3 import java.util.scanner; public static double square(double number) double result = number * number; return result; Scanner in = new Scanner(System.in); System.out.printf("Please enter a number: "); double N = in.nextdouble(); double N_square = square(n); System.out.printf("%.2f squared = %.2f\n", N, N_square); 3

4 Terminology: Methods/Functions In Java, the term methods is the one mainly used. In some other languages, the term functions is more common. In some languages (e.g., C++) methods and functions have minor differences in meaning. In this course, I will use the two terms (methods and functions) as synonyms. 4

5 A First Example Here we see our first example of a function. It is called square. Its job is to compute the square of a number. import java.util.scanner; public static double square(double number) double result = number * number; return result; Scanner in = new Scanner(System.in); System.out.printf("Please enter a number: "); double N = in.nextdouble(); double N_square = square(n); System.out.printf("%.2f squared = %.2f\n", N, N_square); 5

6 What Is a Function A function is a piece of code that does a specific job. Usually (but not always) the job is to compute something. Examples: Computing the square of a number. Determining if an number is prime. Computing the number of digits of a number. import java.util.scanner; public static double square(double number) double result = number * number; return result; Scanner in = new Scanner(System.in); System.out.printf("Please enter a number: "); double N = in.nextdouble(); double N_square = square(n); System.out.printf("%.2f squared = %.2f\n", N, N_square); Counting the number of vowels in a string. 6

7 How to Write a Function Specify a name, like square. import java.util.scanner; public static double square(double number) double result = number * number; return result; function name Scanner in = new Scanner(System.in); System.out.printf("Please enter a number: "); double N = in.nextdouble(); double N_square = square(n); System.out.printf("%.2f squared = %.2f\n", N, N_square); 7

8 How to Write a Function Specify a name, like square. Specify the inputs (called parameters). The parameters are the input that the function needs in order to compute an output. For example, what does the square function take as input? import java.util.scanner; public static double square(double number) double result = number * number; return result; Scanner in = new Scanner(System.in); System.out.printf("Please enter a number: "); double N = in.nextdouble(); double N_square = square(n); System.out.printf("%.2f squared = %.2f\n", N, N_square); 8

9 How to Write a Function Specify a name, like square. Specify the inputs (called parameters). The parameters are the input that the function needs in order to compute an output. For example, what does the square function take as input? A number. import java.util.scanner; public static double square(double number) double result = number * number; return result; Scanner in = new Scanner(System.in); System.out.printf("Please enter a number: "); double N = in.nextdouble(); parameter(s) double N_square = square(n); System.out.printf("%.2f squared = %.2f\n", N, N_square); We can have functions with 0, 1, or more arguments. We will see examples later. 9

10 How to Write a Function Specify a name, like square. Specify the inputs (called parameters). Specify the return type of the function. What is the type of the value that the function computes? In other words, what values are legal and illegal for the output of the function? import java.util.scanner; public static double square(double number) double result = number * number; return result; Scanner in = new Scanner(System.in); System.out.printf("Please enter a number: "); double N = in.nextdouble(); return type double N_square = square(n); System.out.printf("%.2f squared = %.2f\n", N, N_square); 10

11 How to Write a Function Specify a name, like square. Specify the inputs (called parameters). Specify the return type of the function. Specify what the function does. This is what we call the body of the function. import java.util.scanner; public static double square(double number) double result = number * number; return result; Scanner in = new Scanner(System.in); System.out.printf("Please enter a number: "); double N = in.nextdouble(); body double N_square = square(n); System.out.printf("%.2f squared = %.2f\n", N, N_square); 11

12 How to Write a Function Specify a name, like square. Specify the inputs (called parameters). Specify the return type of the function. Specify what the function does. This is what we call the body of the function. import java.util.scanner; public static double square(double number) double result = number * number; return result; Scanner in = new Scanner(System.in); System.out.printf("Please enter a number: "); double N = in.nextdouble(); return statement Specify what value the function returns, using one or more return statements. 12 double N_square = square(n); System.out.printf("%.2f squared = %.2f\n", N, N_square);

13 Using a Function (Function Calls) Once you have written a function, you can use it anywhere in your code, by doing a function call. import java.util.scanner; public static double square(double number) double result = number * number; return result; Scanner in = new Scanner(System.in); System.out.printf("Please enter a number: "); double N = in.nextdouble(); function call double N_square = square(n); System.out.printf("%.2f squared = %.2f\n", N, N_square); 13

14 Using a Function (Function Calls) Once you have written a function, you can use it anywhere in your code, by doing a function call. When you call a function, you must: Provide the appropriate argument(s). import java.util.scanner; public static double square(double number) double result = number * number; return result; Scanner in = new Scanner(System.in); System.out.printf("Please enter a number: "); double N = in.nextdouble(); argument(s) double N_square = square(n); System.out.printf("%.2f squared = %.2f\n", N, N_square); 14

15 Using a Function (Function Calls) Once you have written a function, you can use it anywhere in your code, by doing a function call. When you call a function, you must: Provide the appropriate argument(s). Use the return value appropriately. import java.util.scanner; public static double square(double number) double result = number * number; return result; Scanner in = new Scanner(System.in); System.out.printf("Please enter a number: "); double N = in.nextdouble(); storing the return value double N_square = square(n); System.out.printf("%.2f squared = %.2f\n", N, N_square); 15

16 Using the Return Value In the square function, the return value is a double. You can use this value in any way that you can use any double number. Examples: You can store the return value in a variable. import java.util.scanner; public static double square(double number) double result = number * number; return result; Scanner in = new Scanner(System.in); System.out.printf("Please enter a number: "); double N = in.nextdouble(); storing the return value in a variable double N_square = square(n); System.out.printf("%.2f squared = %.2f\n", N, N_square); 16

17 Using the Return Value In the square function, the return value is a double. You can use this value in any way that you can use any double number. Examples: You can store the return value in a variable. You can print the return value directly. import java.util.scanner; public static double square(double number) double result = number * number; return result; Scanner in = new Scanner(System.in); System.out.printf("Please enter a number: "); double N = in.nextdouble(); printing the return value directly System.out.printf("%.2f squared = %.2f\n", N, square(n)); 17

18 Using the Return Value In the square function, the return value is a double. You can use this value in any way that you can use any double number. Examples: You can store the return value in a variable. You can print the return value directly. You can use it as part of a more complicated expression. import java.util.scanner; public static double square(double number) double result = number * number; return result; Scanner in = new Scanner(System.in); System.out.printf("Please enter a number: "); double N = in.nextdouble(); double my_var = 18 + square(n); using the return value in an expression System.out.printf("result = %.2f\n", my_var); 18

19 Using the Return Value In the square function, the return value is a double. You can use this value in any way that you can use any double number. Examples: You can store the return value in a variable. You can print the return value directly. You can use it as part of a more complicated expression. You can use it as an argument for another function call. import java.util.scanner; public static double square(double number) double result = number * number; return result; Scanner in = new Scanner(System.in); System.out.printf("Please enter a number: "); double N = in.nextdouble(); the return value is used as argument for another function call double my_var = 18 + square(square(n)); System.out.printf("result = %.2f\n", my_var); 19

20 Function Calls You can call a function as many times as you like. On the next slide, we call the square function three times. 20

21 import java.util.scanner; Example: Calling the square function three times. public static double square(double number) double result = number * number; return result; Scanner in = new Scanner(System.in); System.out.printf("Please enter a number M: "); double M = in.nextdouble(); System.out.printf("Please enter a number N: "); double N = in.nextdouble(); double M_square = square(m); System.out.printf("%.2f squared = %.2f\n", M, M_square); double N_square = square(n); System.out.printf("%.2f squared = %.2f\n", N, N_square); double MN_square = square(m*n); System.out.printf("%.2f squared = %.2f\n", M*N, M*N_square); 21

22 Example: Printing Prime Numbers Write a program that: Asks the user to enter an integer N. Prints all prime integers from 2 up to (and including) N. 22

23 Example: Printing Prime Numbers Write a program that: Asks the user to enter an integer N. Prints all prime integers from 2 up to (and including) N. First step: Write the basic structure of the program. Put placeholders where more detail is needed. import java.util.scanner; Scanner in = new Scanner(System.in); System.out.printf("Enter an integer: "); int N = in.nextint(); for (int i = 2; i <= N; i++) if i is prime System.out.printf("%d\n", i); 23

24 Example: Printing Prime Numbers Write a program that: Asks the user to enter an integer N. Prints all prime integers from 2 up to (and including) N. Second step: Identify functions that can be used to complete the program. import java.util.scanner; Scanner in = new Scanner(System.in); System.out.printf("Enter an integer: "); int N = in.nextint(); for (int i = 2; i <= N; i++) if i is prime System.out.printf("%d\n", i); 24

25 Example: Printing Prime Numbers Write a program that: Asks the user to enter an integer N. Prints all prime integers from 2 up to (and including) N. We can use a function is_prime to check if i is prime. Arguments? import java.util.scanner; Scanner in = new Scanner(System.in); System.out.printf("Enter an integer: "); int N = in.nextint(); for (int i = 2; i <= N; i++) if i is prime System.out.printf("%d\n", i); Return type? 25

26 Example: Printing Prime Numbers Write a program that: Asks the user to enter an integer N. Prints all prime integers from 2 up to (and including) N. We can use a function is_prime to check if i is prime. Arguments? an integer import java.util.scanner; Scanner in = new Scanner(System.in); System.out.printf("Enter an integer: "); int N = in.nextint(); for (int i = 2; i <= N; i++) if i is prime System.out.printf("%d\n", i); Return type? boolean 26

27 Example: Printing Prime Numbers Write a program that: Asks the user to enter an integer N. Prints all prime integers from 2 up to (and including) N. Step 3: use the function. We have not written the function yet, but that is OK. Obviously, the program will not run until we write the function. import java.util.scanner; Scanner in = new Scanner(System.in); System.out.printf("Enter an integer: "); int N = in.nextint(); for (int i = 2; i <= N; i++) if (is_prime(i)) System.out.printf("%d\n", i); 27

28 import java.util.scanner; Write a program that: Asks the user to enter an integer N. Prints all prime integers from 2 up to (and including) N. Step 4: write the function. public static boolean is_prime(int N) for (int i = 2; i < N; i++) if (N % i == 0) return false; return true; Scanner in = new Scanner(System.in); System.out.printf("Enter an integer: "); int N = in.nextint(); for (int i = 2; i <= N; i++) if (is_prime(i)) System.out.printf("%d\n", i); 28

29 Output: Enter an integer: import java.util.scanner; public static boolean is_prime(int N) for (int i = 2; i < N; i++) if (N % i == 0) return false; return true; Scanner in = new Scanner(System.in); System.out.printf("Enter an integer: "); int N = in.nextint(); for (int i = 2; i <= N; i++) if (is_prime(i)) System.out.printf("%d\n", i); 29

30 Why Do We Need Functions To write better code: More correct, easier to read/write/change. To write complicated code. Functions help us organize code. YOU CANNOT WRITE NON-TRIVIAL PROGRAMS IF YOU DO NOT USE FUNCTIONS 30

31 Example: Prime Numbers, Again Write a program that: Asks the user to enter an integer N. Prints out the smallest prime number greater than or equal to N. (problem solving: if lost, think of a simpler property) 31

32 Example: Prime Numbers, Again Write a program that: Asks the user to enter an integer N. Prints out the smallest prime number greater than or equal to N. First step: Write the basic structure of the program. Put placeholders where more detail is needed. import java.util.scanner; Scanner in = new Scanner(System.in); System.out.printf("Enter an integer: "); int N = in.nextint(); int i = N; while (i is not prime) i++; System.out.printf("%d\n", i); 32

33 Example: Prime Numbers, Again Write a program that: Asks the user to enter an integer N. Prints out the smallest prime number greater than or equal to N. Second step: Identify functions that can be used to complete the program. We can use the is_prime function again!!! import java.util.scanner; Scanner in = new Scanner(System.in); System.out.printf("Enter an integer: "); int N = in.nextint(); int i = N; while (is_prime(i) == false) i++; System.out.printf("%d\n", i); 33

34 import java.util.scanner; Write a program that: Asks the user to enter an integer N. Prints out the smallest prime number greater than or equal to N. To complete the program, we can just use the is_prime function we already have. Functions make it really easy to re-use code!!! public static boolean is_prime(int N) for (int i = 2; i < N; i++) if (N % i == 0) return false; return true; Scanner in = new Scanner(System.in); System.out.printf("Enter an integer: "); int N = in.nextint(); while (is_prime(n) == false) N++; System.out.printf("%d\n", N); 34

35 Example Output: Enter an integer: Example Output: Enter an integer: Example Output: Enter an integer: import java.util.scanner; public static boolean is_prime(int N) for (int i = 2; i < N; i++) if (N % i == 0) return false; return true; Scanner in = new Scanner(System.in); System.out.printf("Enter an integer: "); int N = in.nextint(); while (is_prime(n) == false) N++; System.out.printf("%d\n", N); 35

36 Making Code Easier to Read In lots of programs, we need to perform a task many times. Copying and pasting code can work, but has disadvantages: The resulting code can look long and ugly. If we make a mistake, we end up copying and pasting the mistake many times. Fixing such mistakes, that have been copied and pasted many times, can be a pain. 36

37 Example: Repeated Printing Write a program that: Asks the user to enter a string S. Asks the user to enter a number N. Prints N times string S. Example Output: Enter string S: hello Enter integer N: 5 hello hello hello hello hello 37

38 Example: Repeated Printing Write a program that: Asks the user to enter a string S. Asks the user to enter a number N. Prints N times string S. Let's write a function, that: takes as input a string S and an integer N. prints the string N times. import java.util.scanner; Scanner in = new Scanner(System.in); System.out.printf("Enter string S: "); String S = in.next(); System.out.printf("Enter integer N: "); int N = in.nextint(); print String S repeatedly, N times. 38

39 Example: Repeated Printing The complete program is shown on the right. Function repeat_print has two features we have not seen before: It takes two arguments. It returns nothing. import java.util.scanner; public static void repeat_print(string message, int times) for (int i = 0; i < times; i++) System.out.printf("%s\n", message); Scanner in = new Scanner(System.in); System.out.printf("Enter string S: "); String S = in.next(); System.out.printf("Enter integer N: "); int N = in.nextint(); repeat_print(s, N); 39

40 A Function That Returns Nothing Function repeat_print does something useful. However, we do not want any value returned from the function. In that case, we specify the return type of the function as void. import java.util.scanner; public static void repeat_print(string message, int times) for (int i = 0; i < times; i++) System.out.printf("%s\n", message); Scanner in = new Scanner(System.in); System.out.printf("Enter string S: "); String S = in.next(); System.out.printf("Enter integer N: "); int N = in.nextint(); repeat_print(s, N); 40

41 The main Function When Java executes a program, how does Java know where to start? Every program must have a function called main, such that: It takes one argument, of type String []. We will understand this type in our next topic, when we do arrays. the return type is void. Until we saw how to write functions, all our code used to go to main. Now that we have started using functions, the main code will be a relatively small part of the program. Functions will do the bulk of the work. 41

42 Function Arguments Functions have arguments. To call a function XYZ, you write something like : XYZ(argument_1,, argument_n) How would you know how many arguments to use? 42

43 Function Arguments Functions have arguments. To call a function XYZ, you write something like: XYZ(argument_1,, argument_n) How would you know how many arguments to use? From the function definition, which (among other things) defines EXACTLY: how many arguments to use. the type of each argument. the order of the arguments. public static type XYZ(type 1 arg_1,, type_n arg_n) 43

44 Executing a Function Call When the body of the function starts executing, the only variables visible to the function are: the arguments. variables defined in the body of the function. 44

45 What Will This Program Do? public static void foo(string var1, String var2) System.out.printf("var1 = %s\n", var1); String var1 = "hello"; String var2 = "goodbye"; String var3 = "earth"; String var4 = "moon"; foo(var3, var4); 45

46 Step-by-step Execution public static void foo(string var1, String var2) System.out.printf("var1 = %s\n", var1); variables in main: String var1 = "hello"; String var2 = "goodbye"; String var3 = "earth"; String var4 = "moon"; foo(var3, var4); Current line 46

47 Step-by-step Execution public static void foo(string var1, String var2) System.out.printf("var1 = %s\n", var1); variables in main: String var1 = "hello"; String var2 = "goodbye"; String var3 = "earth"; String var4 = "moon"; foo(var3, var4); First line to execute: 47

48 Step-by-step Execution public static void foo(string var1, String var2) System.out.printf("var1 = %s\n", var1); variables in main: var1 = "hello" String var1 = "hello"; String var2 = "goodbye"; String var3 = "earth"; String var4 = "moon"; foo(var3, var4); First line creates variable var1 48

49 Step-by-step Execution public static void foo(string var1, String var2) System.out.printf("var1 = %s\n", var1); variables in main: var1 = "hello" String var1 = "hello"; String var2 = "goodbye"; String var3 = "earth"; String var4 = "moon"; foo(var3, var4); Next line to execute: 49

50 Step-by-step Execution public static void foo(string var1, String var2) System.out.printf("var1 = %s\n", var1); variables in main: var1 = "hello" var2 = "goodbye" String var1 = "hello"; String var2 = "goodbye"; String var3 = "earth"; String var4 = "moon"; foo(var3, var4); Next line to execute: 50

51 Step-by-step Execution public static void foo(string var1, String var2) System.out.printf("var1 = %s\n", var1); variables in main: var1 = "hello" var2 = "goodbye" var3 = "earth" String var1 = "hello"; String var2 = "goodbye"; String var3 = "earth"; String var4 = "moon"; foo(var3, var4); Next line to execute: 51

52 Step-by-step Execution public static void foo(string var1, String var2) System.out.printf("var1 = %s\n", var1); variables in main: var1 = "hello" var2 = "goodbye" var3 = "earth" var4 = "moon" String var1 = "hello"; String var2 = "goodbye"; String var3 = "earth"; String var4 = "moon"; foo(var3, var4); Next line to execute: function call 52

53 Step-by-step Execution public static void foo(string var1, String var2) System.out.printf("var1 = %s\n", var1); String var1 = "hello"; String var2 = "goodbye"; String var3 = "earth"; String var4 = "moon"; foo(var3, var4); variables in main: var1 = "hello" var2 = "goodbye" var3 = "earth" var4 = "moon" variables in foo: var1 =??? var2 =??? Next line to execute: function call. First, assign values to arguments. 53

54 Step-by-step Execution public static void foo(string var1, String var2) System.out.printf("var1 = %s\n", var1); String var1 = "hello"; String var2 = "goodbye"; String var3 = "earth"; String var4 = "moon"; foo(var3, var4); variables in main: var1 = "hello" var2 = "goodbye" var3 = "earth" var4 = "moon" variables in foo: var1 = "earth" var2 = "moon" Next line to execute: function call. First, assign values to arguments. 54

55 Step-by-step Execution public static void foo(string var1, String var2) System.out.printf("var1 = %s\n", var1); String var1 = "hello"; String var2 = "goodbye"; String var3 = "earth"; String var4 = "moon"; foo(var3, var4); variables in main: var1 = "hello" var2 = "goodbye" var3 = "earth" var4 = "moon" variables in foo: var1 = "earth" var2 = "moon" Next line to execute: How does Java know which var1 to print? 55

56 Step-by-step Execution public static void foo(string var1, String var2) System.out.printf("var1 = %s\n", var1); String var1 = "hello"; String var2 = "goodbye"; String var3 = "earth"; String var4 = "moon"; foo(var3, var4); variables in main: var1 = "hello" var2 = "goodbye" var3 = "earth" var4 = "moon" variables in foo: var1 = "earth" var2 = "moon" Next line to execute: Currently executing the body of foo. Only the variables of foo are visible. earth" is printed. 56

57 Step-by-step Execution public static void foo(string var1, String var2) System.out.printf("var1 = %s\n", var1); variables in main: var1 = "hello" var2 = "goodbye" var3 = "earth" var4 = "moon" variables in foo: String var1 = "hello"; String var2 = "goodbye"; String var3 = "earth"; String var4 = "moon"; foo(var3, var4); var1 = "earth var2 = "moon" Next line to execute: "moon" is printed. 57

58 Step-by-step Execution public static void foo(string var1, String var2) System.out.printf("var1 = %s\n", var1); variables in main: var1 = "hello" var2 = "goodbye" var3 = "earth" var4 = "moon" variables in foo: String var1 = "hello"; String var2 = "goodbye"; String var3 = "earth"; String var4 = "moon"; foo(var3, var4); var1 = "earth" var2 = "moon" Next line to execute: Which line comes next? 58

59 Step-by-step Execution public static void foo(string var1, String var2) System.out.printf("var1 = %s\n", var1); variables in main: var1 = "hello" var2 = "goodbye" var3 = "earth" var4 = "moon" String var1 = "hello"; String var2 = "goodbye"; String var3 = "earth"; String var4 = "moon"; foo(var3, var4); Next line to execute: Done with the function call. The variables of foo disappear. 59

60 Step-by-step Execution public static void foo(string var1, String var2) System.out.printf("var1 = %s\n", var1); variables in main: var1 = "hello" var2 = "goodbye" var3 = "earth" var4 = "moon" String var1 = "hello"; String var2 = "goodbye"; String var3 = "earth"; String var4 = "moon"; foo(var3, var4); Next line to execute: "goodbye" is printed. 60

61 Summary of Program Output public static void foo(string var1, String var2) System.out.printf("var1 = %s\n", var1); String var1 = "hello"; String var2 = "goodbye"; String var3 = "earth"; String var4 = "moon"; foo(var3, var4); Output: var1 = earth var2 = moon var2 = goodbye 61

62 Practice Write functions Based on problem description: Write a function that takes [as argument] a string S and returns the sum of the ASCII code of the characters in it. Write a function that takes 2 strings, a piece size, p_sz, and merges the strings in chunks of p_sz. Write a function that takes [as argument] a list L and returns the sum of the elements in L. If L has strings, it should return the concatenation of all the strings. Write a fct that takes two arguments: a String, s, and a char,c, and removes all the occurrences of c from the string s. Test cases: s = aaxyaj, c = a // s = aaaaaaaa, c = a // s = aaa, c = a return a new string with the modifications. Based on your own choice. 62

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington First Programs CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1 Output System.out.println( ) prints out something. System.out.println is the first

More information

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington First Programs CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1 Output System.out.println( ) prints out something. System.out.println is the first

More information

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington First Programs CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1 Output System.out.println( ) prints out something. System.out.println is the first

More information

Formatted Output (printf) CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Formatted Output (printf) CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Formatted Output (printf) CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1 Formatted Printing - References Java API: https://docs.oracle.com/javase/7/docs/api/java/util/formatter.html#syntax

More information

Decisions (If Statements) And Boolean Expressions

Decisions (If Statements) And Boolean Expressions Decisions (If Statements) And Boolean Expressions CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Last updated: 2/15/16 1 Syntax if (boolean_expr)

More information

Common Mistakes with Functions. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Common Mistakes with Functions. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Common Mistakes with Functions CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1 Common Mistakes with Functions Printing instead of returning. Returning

More information

Arrays and Array Lists. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington

Arrays and Array Lists. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington Arrays and Array Lists CSE 1310 Introduction to Computers and Programming Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington 1 Motivation Current limitation: We cannot record multiple

More information

Variables, Types, Operations on Numbers

Variables, Types, Operations on Numbers Variables, Types, Operations on Numbers CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Updated 9/6/16 1 Summary Variable declaration, initialization,

More information

Maximization and Minimization Problems. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Maximization and Minimization Problems. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Maximization and Minimization Problems CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1 Maximization Problems Given a set of data, we want to find

More information

CS212 Midterm. 1. Read the following code fragments and answer the questions.

CS212 Midterm. 1. Read the following code fragments and answer the questions. CS1 Midterm 1. Read the following code fragments and answer the questions. (a) public void displayabsx(int x) { if (x > 0) { System.out.println(x); return; else { System.out.println(-x); return; System.out.println("Done");

More information

Java Coding 3. Over & over again!

Java Coding 3. Over & over again! Java Coding 3 Over & over again! Repetition Java repetition statements while (condition) statement; do statement; while (condition); where for ( init; condition; update) statement; statement is any Java

More information

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops COMP-202 Unit 4: Programming with Iterations Doing the same thing again and again and again and again and again and again and again and again and again... CONTENTS: While loops Class (static) variables

More information

Introduction to Software Development (ISD) Week 3

Introduction to Software Development (ISD) Week 3 Introduction to Software Development (ISD) Week 3 Autumn term 2012 Aims of Week 3 To learn about while, for, and do loops To understand and use nested loops To implement programs that read and process

More information

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15 Table of Contents 1 INTRODUCTION... 1 2 IF... 1 2.1 BOOLEAN EXPRESSIONS... 3 2.2 BLOCKS... 3 2.3 IF-ELSE... 4 2.4 NESTING... 5 3 SWITCH (SOMETIMES KNOWN AS CASE )... 6 3.1 A BIT ABOUT BREAK... 7 4 CONDITIONAL

More information

Strings. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Strings. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Strings CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1 Summary String type Concatenation: +, += Escape sequences Substring Substring vs char my_str.length()

More information

COMP-202 Unit 4: Programming with Iterations

COMP-202 Unit 4: Programming with Iterations COMP-202 Unit 4: Programming with Iterations Doing the same thing again and again and again and again and again and again and again and again and again... CONTENTS: While loops Class (static) variables

More information

Files. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Files. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Files CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1 Files and Text Files A file is a collection of data, that is saved on a hard drive. A text

More information

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014 M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014 Question One: Choose the correct answer and write it on the external answer booklet. 1. Java is. a. case

More information

Files. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Files. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Files CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1 Files and Text Files A file is a collection of data, that is saved on a hard drive. A text

More information

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods Islamic University of Gaza Faculty of Engineering Computer Engineering Dept. Computer Programming Lab (ECOM 2114) ABSTRACT In this Lab you will learn to define and invoke void and return java methods JAVA

More information

Files & Exception Handling. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Files & Exception Handling. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Files & Exception Handling CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1 Exception Handling (try catch) Suppose that a line of code may make your

More information

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements COMP-202 Unit 4: Programming With Iterations CONTENTS: The while and for statements Introduction (1) Suppose we want to write a program to be used in cash registers in stores to compute the amount of money

More information

CSE Module 1. A Programming Primer 1/23/19 CSE 1321 MODULE 1 1

CSE Module 1. A Programming Primer 1/23/19 CSE 1321 MODULE 1 1 CSE 1321 - Module 1 A Programming Primer 1/23/19 CSE 1321 MODULE 1 1 Motivation You re going to learn: The skeleton Printing Declaring variables Reading user input Doing basic calculations You ll have

More information

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7) Array Basics: Outline Arrays (Savitch, Chapter 7) TOPICS Array Basics Arrays in Classes and Methods Programming with Arrays Searching and Sorting Arrays Multi-Dimensional Arrays Static Variables and Constants

More information

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

CS 152: Data Structures with Java Hello World with the IntelliJ IDE CS 152: Data Structures with Java Hello World with the IntelliJ IDE Instructor: Joel Castellanos e-mail: joel.unm.edu Web: http://cs.unm.edu/~joel/ Office: Electrical and Computer Engineering building

More information

What did we talk about last time? Examples switch statements

What did we talk about last time? Examples switch statements Week 4 - Friday What did we talk about last time? Examples switch statements History of computers Hardware Software development Basic Java syntax Output with System.out.print() Mechanical Calculation

More information

CSE 1223: Introduction to Computer Programming in Java Chapter 3 Branching

CSE 1223: Introduction to Computer Programming in Java Chapter 3 Branching CSE 1223: Introduction to Computer Programming in Java Chapter 3 Branching 1 Flow of Control The order in which statements in a program are executed is called the flow of control So far we have only seen

More information

Arithmetic and IO. 25 August 2017

Arithmetic and IO. 25 August 2017 Arithmetic and IO 25 August 2017 Submissions you can submit multiple times to the homework dropbox file name: uppercase first letter, Yourlastname0829.java the system will use the last submission before

More information

Define a method vs. calling a method. Chapter Goals. Contents 1/21/13

Define a method vs. calling a method. Chapter Goals. Contents 1/21/13 CHAPTER 2 Define a method vs. calling a method Line 3 defines a method called main Line 5 calls a method called println, which is defined in the Java library You will learn later how to define your own

More information

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018 Object-oriented programming 1 and data-structures CS/ENGRD 2110 SUMMER 2018 Lecture 1: Types and Control Flow http://courses.cs.cornell.edu/cs2110/2018su Lecture 1 Outline 2 Languages Overview Imperative

More information

Arrays. Eng. Mohammed Abdualal

Arrays. Eng. Mohammed Abdualal Islamic University of Gaza Faculty of Engineering Computer Engineering Department Computer Programming Lab (ECOM 2114) Created by Eng: Mohammed Alokshiya Modified by Eng: Mohammed Abdualal Lab 9 Arrays

More information

Example Program. public class ComputeArea {

Example Program. public class ComputeArea { COMMENTS While most people think of computer programs as a tool for telling computers what to do, programs are actually much more than that. Computer programs are written in human readable language for

More information

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website: Repetition CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Some slides in this

More information

St. Edmund Preparatory High School Brooklyn, NY

St. Edmund Preparatory High School Brooklyn, NY AP Computer Science Mr. A. Pinnavaia Summer Assignment St. Edmund Preparatory High School Name: I know it has been about 7 months since you last thought about programming. It s ok. I wouldn t want to think

More information

CSC 1051 Data Structures and Algorithms I

CSC 1051 Data Structures and Algorithms I Repetition CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Some slides in this

More information

CS170 (005): Introduction to Computer Science Exam 2

CS170 (005): Introduction to Computer Science Exam 2 CS70 (005): Introduction to Computer Science Exam Name (print): Instructions Keep your eyes on your own paper and do your best to prevent anyone else from seeing your work Do NOT communicate with anyone

More information

Loops! Step- by- step. An Example while Loop. Flow of Control: Loops (Savitch, Chapter 4)

Loops! Step- by- step. An Example while Loop. Flow of Control: Loops (Savitch, Chapter 4) Loops! Flow of Control: Loops (Savitch, Chapter 4) TOPICS while Loops do while Loops for Loops break Statement continue Statement CS 160, Fall Semester 2014 2 An Example while Loop Step- by- step int count

More information

Problem Solving for Intro to Computer Science

Problem Solving for Intro to Computer Science Problem Solving for Intro to Computer Science The purpose of this document is to review some principles for problem solving that are relevant to Intro to Computer Science course. Introduction: A Sample

More information

Midterm Examination (MTA)

Midterm Examination (MTA) M105: Introduction to Programming with Java Midterm Examination (MTA) Spring 2013 / 2014 Question One: [6 marks] Choose the correct answer and write it on the external answer booklet. 1. Compilers and

More information

COMP 110 Project 1 Programming Project Warm-Up Exercise

COMP 110 Project 1 Programming Project Warm-Up Exercise COMP 110 Project 1 Programming Project Warm-Up Exercise Creating Java Source Files Over the semester, several text editors will be suggested for students to try out. Initially, I suggest you use JGrasp,

More information

Object Oriented Programming. Java-Lecture 6 - Arrays

Object Oriented Programming. Java-Lecture 6 - Arrays Object Oriented Programming Java-Lecture 6 - Arrays Arrays Arrays are data structures consisting of related data items of the same type In Java arrays are objects -> they are considered reference types

More information

Programming with Java

Programming with Java Programming with Java Data Types & Input Statement Lecture 04 First stage Software Engineering Dep. Saman M. Omer 2017-2018 Objectives q By the end of this lecture you should be able to : ü Know rules

More information

Place your name tag here

Place your name tag here CS 170 Exam 1 Section 001 Spring 2015 Name: Place your name tag here Instructions: Keep your eyes on your own paper and do your best to prevent anyone else from seeing your work. Do NOT communicate with

More information

CSE 1223: Exam II Autumn 2016

CSE 1223: Exam II Autumn 2016 CSE 1223: Exam II Autumn 2016 Name: Instructions: Do not open the exam before you are told to begin. This exam is closed book, closed notes. You may not use any calculators or any other kind of computing

More information

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Methods

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Methods WIT COMP1000 Methods Methods Programs can be logically broken down into a set of tasks Example from horoscope assignment:» Get input (month, day) from user» Determine astrological sign based on inputs

More information

Section 003 Fall CS 170 Exam 1. Name (print): Instructions:

Section 003 Fall CS 170 Exam 1. Name (print): Instructions: CS 170 Exam 1 Section 003 Fall 2012 Name (print): Instructions: Keep your eyes on your own paper and do your best to prevent anyone else from seeing your work. Do NOT communicate with anyone other than

More information

Array. Prepared By - Rifat Shahriyar

Array. Prepared By - Rifat Shahriyar Java More Details Array 2 Arrays A group of variables containing values that all have the same type Arrays are fixed length entities In Java, arrays are objects, so they are considered reference types

More information

Over and Over Again GEEN163

Over and Over Again GEEN163 Over and Over Again GEEN163 There is no harm in repeating a good thing. Plato Homework A programming assignment has been posted on Blackboard You have to convert three flowcharts into programs Upload the

More information

Loops. CSE 114, Computer Science 1 Stony Brook University

Loops. CSE 114, Computer Science 1 Stony Brook University Loops CSE 114, Computer Science 1 Stony Brook University http://www.cs.stonybrook.edu/~cse114 1 Motivation Suppose that you need to print a string (e.g., "Welcome to Java!") a user-defined times N: N?

More information

Hello World. n Variables store information. n You can think of them like boxes. n They hold values. n The value of a variable is its current contents

Hello World. n Variables store information. n You can think of them like boxes. n They hold values. n The value of a variable is its current contents Variables in a programming language Basic Computation (Savitch, Chapter 2) TOPICS Variables and Data Types Expressions and Operators Integers and Real Numbers Characters and Strings Input and Output Variables

More information

DM550 Introduction to Programming part 2. Jan Baumbach.

DM550 Introduction to Programming part 2. Jan Baumbach. DM550 Introduction to Programming part 2 Jan Baumbach jan.baumbach@imada.sdu.dk http://www.baumbachlab.net COURSE ORGANIZATION 2 Course Elements Lectures: 10 lectures Find schedule and class rooms in online

More information

Nested Loops ***** ***** ***** ***** ***** We know we can print out one line of this square as follows: System.out.

Nested Loops ***** ***** ***** ***** ***** We know we can print out one line of this square as follows: System.out. Nested Loops To investigate nested loops, we'll look at printing out some different star patterns. Let s consider that we want to print out a square as follows: We know we can print out one line of this

More information

Array Lists. CSE 1310 Introduction to Computers and Programming University of Texas at Arlington. Last modified: 4/17/18

Array Lists. CSE 1310 Introduction to Computers and Programming University of Texas at Arlington. Last modified: 4/17/18 Array Lists CSE 1310 Introduction to Computers and Programming University of Texas at Arlington Last modified: 4/17/18 1 DEPARTAMENTAL FINAL EXAM Monday, DEC 10, 5:30pm-8pm rooms will be determined 2 Fixed

More information

P 6.3) import java.util.scanner;

P 6.3) import java.util.scanner; P 6.3) import java.util.scanner; public class CurrencyConverter public static void main (String[] args) Scanner in = new Scanner(System.in); System.out.print("How many euros is one dollar: "); double rate

More information

Loops. Eng. Mohammed Abdualal. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Department

Loops. Eng. Mohammed Abdualal. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Department Islamic University of Gaza Faculty of Engineering Computer Engineering Department Computer Programming Lab (ECOM 2114) Created by Eng: Mohammed Alokshiya Modified by Eng: Mohammed Abdualal Lab 6 Loops

More information

1 class Lecture4 { 2 3 "Loops" / References 8 [1] Ch. 5 in YDL 9 / Zheng-Liang Lu Java Programming 125 / 207

1 class Lecture4 { 2 3 Loops / References 8 [1] Ch. 5 in YDL 9 / Zheng-Liang Lu Java Programming 125 / 207 1 class Lecture4 { 2 3 "Loops" 4 5 } 6 7 / References 8 [1] Ch. 5 in YDL 9 / Zheng-Liang Lu Java Programming 125 / 207 Loops A loop can be used to make a program execute statements repeatedly without having

More information

CSC 1051 Data Structures and Algorithms I

CSC 1051 Data Structures and Algorithms I Repetition CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Some slides in this

More information

double float char In a method: final typename variablename = expression ;

double float char In a method: final typename variablename = expression ; Chapter 4 Fundamental Data Types The Plan For Today Return Chapter 3 Assignment/Exam Corrections Chapter 4 4.4: Arithmetic Operations and Mathematical Functions 4.5: Calling Static Methods 4.6: Strings

More information

Scope. Chapter Ten Modern Programming Languages 1

Scope. Chapter Ten Modern Programming Languages 1 Scope Chapter Ten Modern Programming Languages 1 Reusing Names Scope is trivial if you have a unique name for everything: fun square a = a * a; fun double b = b + b; But in modern languages, we often use

More information

Programming Constructs Overview. Method Call System.out.print( hello ); Method Parameters

Programming Constructs Overview. Method Call System.out.print( hello ); Method Parameters Programming Constructs Overview Method calls More selection statements More assignment operators Conditional operator Unary increment and decrement operators Iteration statements Defining methods 27 October

More information

Controls Structure for Repetition

Controls Structure for Repetition Controls Structure for Repetition So far we have looked at the if statement, a control structure that allows us to execute different pieces of code based on certain conditions. However, the true power

More information

STUDENT LESSON A7 Simple I/O

STUDENT LESSON A7 Simple I/O STUDENT LESSON A7 Simple I/O Java Curriculum for AP Computer Science, Student Lesson A7 1 STUDENT LESSON A7 Simple I/O INTRODUCTION: The input and output of a program s data is usually referred to as I/O.

More information

CSE 114 Computer Science I

CSE 114 Computer Science I CSE 114 Computer Science I Iteration Cape Breton, Nova Scotia What is Iteration? Repeating a set of instructions a specified number of times or until a specific result is achieved How do we repeat steps?

More information

1 class Lecture5 { 2 3 "Methods" / References 8 [1] Ch. 5 in YDL 9 [1] Ch. 20 in YDL 0 / Zheng-Liang Lu Java Programming 176 / 199

1 class Lecture5 { 2 3 Methods / References 8 [1] Ch. 5 in YDL 9 [1] Ch. 20 in YDL 0 / Zheng-Liang Lu Java Programming 176 / 199 1 class Lecture5 { 2 3 "Methods" 4 5 } 6 7 / References 8 [1] Ch. 5 in YDL 9 [1] Ch. 20 in YDL 0 / Zheng-Liang Lu Java Programming 176 / 199 Methods 2 Methods can be used to define reusable code, and organize

More information

CONTENTS: Arrays Strings. COMP-202 Unit 5: Loops in Practice

CONTENTS: Arrays Strings. COMP-202 Unit 5: Loops in Practice CONTENTS: Arrays Strings COMP-202 Unit 5: Loops in Practice Computing the mean of several numbers Suppose we want to write a program which asks the user to enter several numbers and then computes the average

More information

Lists, loops and decisions

Lists, loops and decisions Caltech/LEAD Summer 2012 Computer Science Lecture 4: July 11, 2012 Lists, loops and decisions Lists Today Looping with the for statement Making decisions with the if statement Lists A list is a sequence

More information

Section 2.2 Your First Program in Java: Printing a Line of Text

Section 2.2 Your First Program in Java: Printing a Line of Text Chapter 2 Introduction to Java Applications Section 2.2 Your First Program in Java: Printing a Line of Text 2.2 Q1: End-of-line comments that should be ignored by the compiler are denoted using a. Two

More information

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341) CSE 413 Languages & Implementation Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341) 1 Goals Representing programs as data Racket structs as a better way to represent

More information

Software Practice 1 - Basic Grammar Basic Syntax Data Type Loop Control Making Decision

Software Practice 1 - Basic Grammar Basic Syntax Data Type Loop Control Making Decision Software Practice 1 - Basic Grammar Basic Syntax Data Type Loop Control Making Decision Prof. Hwansoo Han T.A. Minseop Jeong T.A. Wonseok Choi 1 Java Program //package details public class ClassName {

More information

printf( Please enter another number: ); scanf( %d, &num2);

printf( Please enter another number: ); scanf( %d, &num2); CIT 593 Intro to Computer Systems Lecture #13 (11/1/12) Now that we've looked at how an assembly language program runs on a computer, we're ready to move up a level and start working with more powerful

More information

Top-Down Program Development

Top-Down Program Development Top-Down Program Development Top-down development is a way of thinking when you try to solve a programming problem It involves starting with the entire problem, and breaking it down into more manageable

More information

Java Console Input/Output The Basics. CGS 3416 Spring 2018

Java Console Input/Output The Basics. CGS 3416 Spring 2018 Java Console Input/Output The Basics CGS 3416 Spring 2018 Console Output System.out out is a PrintStream object, a static data member of class System. This represents standard output Use this object to

More information

1 Short Answer (15 Points Each)

1 Short Answer (15 Points Each) COSC 7 Exam # Solutions Spring 08 Short Answer (5 Points Each). Write a method called RollCount that takes in two integer parameters rolls and target. The method should simulate the rolling of two die,

More information

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics WIT COMP1000 Java Basics Java Origins Java was developed by James Gosling at Sun Microsystems in the early 1990s It was derived largely from the C++ programming language with several enhancements Java

More information

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

More information

Loops (while and for)

Loops (while and for) Loops (while and for) CSE 1310 Introduction to Computers and Programming Alexandra Stefan 1 Motivation Was there any program we did (class or hw) where you wanted to repeat an action? 2 Motivation Name

More information

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/ CS61C Machine Structures Lecture 4 C Pointers and Arrays 1/25/2006 John Wawrzynek (www.cs.berkeley.edu/~johnw) www-inst.eecs.berkeley.edu/~cs61c/ CS 61C L04 C Pointers (1) Common C Error There is a difference

More information

Question: Total Points: Score:

Question: Total Points: Score: CS 170 Exam 1 Section 001 Fall 2014 Name (print): Instructions: Keep your eyes on your own paper and do your best to prevent anyone else from seeing your work. Do NOT communicate with anyone other than

More information

2/9/2012. Chapter Four: Fundamental Data Types. Chapter Goals

2/9/2012. Chapter Four: Fundamental Data Types. Chapter Goals Chapter Four: Fundamental Data Types Chapter Goals To understand integer and floating-point numbers To recognize the limitations of the numeric types To become aware of causes for overflow and roundoff

More information

CMPS 12A Winter 2006 Prof. Scott A. Brandt Final Exam, March 21, Name:

CMPS 12A Winter 2006 Prof. Scott A. Brandt Final Exam, March 21, Name: CMPS 12A Winter 2006 Prof. Scott A. Brandt Final Exam, March 21, 2006 Name: Email: This is a closed note, closed book exam. There are II sections worth a total of 200 points. Plan your time accordingly.

More information

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find CS1622 Lecture 15 Semantic Analysis CS 1622 Lecture 15 1 Semantic Analysis How to build symbol tables How to use them to find multiply-declared and undeclared variables. How to perform type checking CS

More information

(Refer Slide Time: 01.26)

(Refer Slide Time: 01.26) Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Lecture # 22 Why Sorting? Today we are going to be looking at sorting.

More information

COE 212 Engineering Programming. Welcome to Exam II Monday May 13, 2013

COE 212 Engineering Programming. Welcome to Exam II Monday May 13, 2013 1 COE 212 Engineering Programming Welcome to Exam II Monday May 13, 2013 Instructors: Dr. Randa Zakhour Dr. Maurice Khabbaz Dr. George Sakr Dr. Wissam F. Fawaz Name: Solution Key Student ID: Instructions:

More information

Data Structure and Programming Languages

Data Structure and Programming Languages 204700 Data Structure and Programming Languages Jakarin Chawachat From: http://ocw.mit.edu/courses/electrical-engineering-and-computerscience/6-092-introduction-to-programming-in-java-january-iap-2010/index.htm

More information

STUDENT LESSON A12 Iterations

STUDENT LESSON A12 Iterations STUDENT LESSON A12 Iterations Java Curriculum for AP Computer Science, Student Lesson A12 1 STUDENT LESSON A12 Iterations INTRODUCTION: Solving problems on a computer very often requires a repetition of

More information

Programming Exercise 7: Static Methods

Programming Exercise 7: Static Methods Programming Exercise 7: Static Methods Due date for section 001: Monday, February 29 by 10 am Due date for section 002: Wednesday, March 2 by 10 am Purpose: Introduction to writing methods and code re-use.

More information

Example: Computing prime numbers

Example: Computing prime numbers Example: Computing prime numbers -Write a program that lists all of the prime numbers from 1 to 10,000. Remember a prime number is a # that is divisible only by 1 and itself Suggestion: It probably will

More information

Software Testing Prof. Meenakshi D Souza Department of Computer Science and Engineering International Institute of Information Technology, Bangalore

Software Testing Prof. Meenakshi D Souza Department of Computer Science and Engineering International Institute of Information Technology, Bangalore Software Testing Prof. Meenakshi D Souza Department of Computer Science and Engineering International Institute of Information Technology, Bangalore Lecture 04 Software Test Automation: JUnit as an example

More information

Arrays. Chapter 7 (Done right after 4 arrays and loops go together, especially for loops)

Arrays. Chapter 7 (Done right after 4 arrays and loops go together, especially for loops) Arrays Chapter 7 (Done right after 4 arrays and loops go together, especially for loops) Object Quick Primer A large subset of Java s features are for OOP Object- Oriented Programming We ll get to that

More information

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

More information

Software Practice 1 Basic Grammar

Software Practice 1 Basic Grammar Software Practice 1 Basic Grammar Basic Syntax Data Type Loop Control Making Decision Prof. Joonwon Lee T.A. Jaehyun Song Jongseok Kim (42) T.A. Sujin Oh Junseong Lee (43) 1 2 Java Program //package details

More information

Final CSE 131B Spring 2004

Final CSE 131B Spring 2004 Login name Signature Name Student ID Final CSE 131B Spring 2004 Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 Page 7 Page 8 (25 points) (24 points) (32 points) (24 points) (28 points) (26 points) (22 points)

More information

Chapter 5 Methods. public class FirstMethod { public static void main(string[] args) { double x= -2.0, y; for (int i = 1; i <= 5; i++ ) { y = f( x );

Chapter 5 Methods. public class FirstMethod { public static void main(string[] args) { double x= -2.0, y; for (int i = 1; i <= 5; i++ ) { y = f( x ); Chapter 5 Methods Sections Pages Review Questions Programming Exercises 5.1 5.11 142 166 1 18 2 22 (evens), 30 Method Example 1. This is of a main() method using a another method, f. public class FirstMethod

More information

Object Oriented Programming and Design in Java. Session 2 Instructor: Bert Huang

Object Oriented Programming and Design in Java. Session 2 Instructor: Bert Huang Object Oriented Programming and Design in Java Session 2 Instructor: Bert Huang Announcements TA: Yipeng Huang, yh2315, Mon 4-6 OH on MICE clarification Next Monday's class canceled for Distinguished Lecture:

More information

4. Java Project Design, Input Methods

4. Java Project Design, Input Methods 4-1 4. Java Project Design, Input Methods Review and Preview You should now be fairly comfortable with creating, compiling and running simple Java projects. In this class, we continue learning new Java

More information

First Java Program - Output to the Screen

First Java Program - Output to the Screen First Java Program - Output to the Screen These notes are written assuming that the reader has never programmed in Java, but has programmed in another language in the past. In any language, one of the

More information

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C C: A High-Level Language Chapter 11 Introduction to Programming in C Original slides from Gregory Byrd, North Carolina State University Modified slides by Chris Wilcox, Colorado State University Gives

More information

Chapter Goals. Contents LOOPS

Chapter Goals. Contents LOOPS CHAPTER 4 LOOPS Slides by Donald W. Smith TechNeTrain.com Final Draft Oct 30, 2011 Chapter Goals To implement while, for, and do loops To hand-trace the execution of a program To become familiar with common

More information

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8 Today... Java basics S. Bowers 1 of 8 Java main method (cont.) In Java, main looks like this: public class HelloWorld { public static void main(string[] args) { System.out.println("Hello World!"); Q: How

More information

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software.

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software. Introduction to Netbeans This document is a brief introduction to writing and compiling a program using the NetBeans Integrated Development Environment (IDE). An IDE is a program that automates and makes

More information