Download presentation

Presentation is loading. Please wait.

Published byMichelle Barnes Modified over 3 years ago

1
2.1 Program Construction In Java Joey Militti Brendan Tackney Period 1

2
Mistakes The IB examination is not to picky on small code errors such as a missing semi-colon here and there. However a mistake that leads to incorrect logic will result in a penalty An example of how a missing SemiColon wont hurt is : If(x==3) { Y = y+4 //there is an error here Z=y-x; } X=x +1

3
Comments If you want to add information that describes your code you should use comments // Some comments may be graded in the IB exam. Your program does not execute comments.

4
Basic Variables Byte – used for small integers or whole numbers in the range -128 to +127 Int is used for whole numbers negative or positive in the range to Long used for whole numbers negative or positive that are much bigger then numbers used for ints Double used for numbers with fractional parts in the approximate range ex – Char represents a signal symbol a letter other symbols or a number. Can also represent integer values in the range Boolean – represents one of two states only.

5
Assignment Assignment always takes the expression on the right side of the equals sign and places the results in the variable on the left side of the equals sign. number = number + 23; //now number is greater than it was When it is executed, the right side is evaluated first. So 23 is added to the value in memory (4) and then 27 is placed in the memory. The 4 that was there is now overwritten and can not be retrieved at any time.

6
Beyond Primitives Classes, like String, begin with a capital letter. Its important to remember that where objects are concerned the identifier is a reference as to where the object lies in the memory. String name = Binh Nguyen Le; int len = name.length(); Primitives have no such extra information therefore int len = name.length(); is not possible. Primitives really consist just of a value whereas objects have a whole lot of associated methods.

7
Combining String Combining Strings is a simple easily done task in most cases. To combine a string we use a + sign. We do not include the + sign in the instead we put it as follows Brendan + in a non gay way Joey Backslashes also have a big use in strings. \n – begins a new line in the string \t – does a tab in the string \ – puts a quote in a string.

8
Arithmetic Operators In the IB test the main arithmetic operators we will use are + for addition, - for subtraction, * for multiplication, / for division, % for modulus The order in which you place arithmetic operators can effect the final value. For Example: int x = 23 % Int x = (23 % 5) +2 Parenthesis change the result because the first one equals 5 while the second question equals 2.

9
Mathematical Functions double, int abs(x) – Returns the absolute value of its argument (x may be double or int). double pow(x, y) – Returns x raised to the power y. double sin(x) – Returns the sine of x, where x is in radians. double cos(x) – Returns the cosine of x, where x is in radians. Long round (x) – Rounds x to the nearest integer. double floor(x) – Rounds x to the nearest integer not greater than x.

10
Sequence Sequence of instructions are statements or actions in an algorithm simply flow from one to the next. public Add(){ double number1 = inputDouble(Input the first number: ); double number2 = inputDouble(Input the next number: ); double total = 0; output(The total is: + total); double total = number1 + number2; }

11
Constructing A Modular Program IB Computer Science program requires students to analyze and solve problems rather than to write code solutions to simple programming exercises. As the programming exercises increase in complexity, other stages can also be brought in.

12
Parameters One way of passing a variable to another method is to use a parameter. The identifier or expression where the function is called is often referred to as the argument. The parameter acts like a local identifier whose scope is the method body.

13
Method Signatures The line where the function parameters and return type are defined is known as the method signature.

14
Selection This is the use of different sets of instructions depending on a given condition. Selection is also often referred to as branching.

15
Nesting Nesting occurs when one control structure is placed inside another. if and if else statements are control structures. int x = 9; int y = 0; int z = 3; char c = x; if (x ==9) if (y < 3) if (c != x) output (flip); else if (z >= 3) output (flop); else output (fly);

16
Multiple Selections When making multiple selections, if statements can become quite complicated. Instead of: if else You can use: if else if This is very helpful when multiple choices need to be made.

17
Repetition Repeating things again and again until some condition is met or while the condition is true or false is the basic structure of a loop. A While loop keeps executing while the condition is true EX: While(brendanownz= true) { Own(); } A different while loop can check the condition at the end so the loop executes once before checking the condition EX: Int x = 1; { Output(X); X *= 2; } While(x < 10);

18
Tracing Loops In order to sum values a list of numbers must be input ending with a 0 or -1 Tracing an algorithm means to work out what the values of variables will be in each step as a means of understanding how it works.

19
Classes, User-Defined methods, and Objects Java has built-in functions like the one that returns the length of a string. Sometimes the term function is used to distinguish a method that returns a single value and thus has a return type.

20
Exercise Devise an algorithm which finds the largest value in a given array of numbers. To help you here is an algorithm which finds the smallest in a given array of numbers listed called listA: // initialise an int array with values int[] listA = new int[] {3, 67, -9, 304, -56, 2}; Int pos = 1; Int smallest = listA[ 0]; Whiile( pos < 6) { if(listA[ pos] < smallest) { smallest = listA[pos]; } pos = pos +1; } Output(the smallest number in the list is: + smallest);

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google