Java Software Solutions for AP* Computer Science Test 3.0-3.15

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Loop/ Repetition Statement

There are two types of loops that we will study: the "while" loop and the "for" loop. Loops allow us to execute the same set of statements repeatedly as long as the condition that is evaluated before the statement is true. Example: A set of statements is executed when a specified value is less than or equal to 50 and will at the end of the set of statements add 1 to the value. This set of statements will be repeatedly executed until the value reaches 51.

The while Statement

A while statement , like an if statement, evaluates a boolean condition and based on the result of that condition executes a series of statements known as the body of the loop. The condition is then re-evaluated and depending upon the result is either executed again or moves on to the next statement. Used when the programmer does not know how many times the loop body must be executed. Example: final int LIMIT = 5; int count = 1; while (count<=LIMIT) { System.out.println(count); count++; } System.out.println("Done."); output: 1 2 3 4 5 Done

Comparing Characters

Characters are evaluated the same way integer values are evaluated and use the same equality and relational operators. This is based on the unicode character set. Example: if (ch1 > ch2) { System.out.println (ch1 + " is not greater than " + ch2); } else System.out.println(ch1 + "is NOT greater than " + ch2);

Comparing Characters and Strings

Characters can be evaluated the same way integer values are evaluated based on the unicode character set, but strings require a whole other syntax. Strings are evaluated on similarity based on character structure.

<=

Rational operator that means "less than or equal to."

<

Rational operator that means "less than."

>=

Rational operators that means "more than or equal to."

>

Rational operators that means "more than."

-=

The assignment operator "-=" subtracts the value placed after it from the variable placed before it and assigns the original variable this new value. Example: int money; money +-5 --> money = money -5;

/=

The assignment operator "/=" divides the variable placed before it by the variable after it and assigns the original variable this new value. Example: int money; money /=5; ---> money = money / 5

=

The assignment operator "=" just assigns the value placed after it to the integer. Example: int money; money = 5;

%=

The assignment operator that takes the remainder of the variable and the value and assigns the original variable this new value. Example: int money; money %=5; ---> money = money%5;

Software Design

The blueprint for the structure of the program. Written in pseudocode, the software design is the general idea for how the problem will be solved.

Nested Loops

The body of a loop can contain a second loop that is evaluated only when the condition of the first loop is true. When the outer loop executes once the inner loop executes repeatedly and completely. The output is usually the number of times the inner loop is executed in one cycle multiplied by the number of cycles.

The for each Statement

The for each statement is used to print lists such as enums. Example: enum iceCream { for ( String iceCream :

The for Statement

The for statement is a repetition statement that is used when the programmer knows how many times they would like to execute a statement. The header of a for statement has three parts: Initialization, Condition, Incrementation. Example: for (int val = 10; val>=15; val++) System.out.println("Money");

Development Activities

The four steps to designing, writing, and testing a program.

The if Statement

The if statement consists of the reserved word if followed by a boolean expression (condition), followed by a conditions inside brackets. The condition is enclosed in parentheses and is either true or false. If the condition is true, the next statement will be executed. Basically the if statement lets a program choose whether to execute a particular statement. Example: if (total > amount) total ++;

The if-else Statement

The if-else statement essentially does the same thing as an if statement but with the addition of the else clause which is the group of statements that is executed if the condition in the if statement is false. The else clause is essentially the escape valve. Example: if (height <= MAX) adjustment = 0; else adjustment = MAX - height;

Software Requirements

The things that the program must accomplish. The tasks that the program must complete and the goals it must achieve. Clearly stated in the phrasing of the problem or question.

Rational Operators

There are four types of rational operators that we will study: " > ", " < ", " >= ", and " <= ". Rational operators are used to determine the relationship between two values.

Logical Operators

There are three types of logical operators we will study: " ! a ", " a && b ", and " a || b " Logical operators can be included in the boolean expression of if, if else, while, and for conditions and do not have operational precedence over arithmetics. Logical NOT has the highest precedence, followed by logical AND, and lastly logical OR.

Conditional Statement/ Selection Statement

There are two types of conditional statements that we will study: " if " statements and " if else " statements. Conditional statements allow for us to decide which statement will be executed next in the program, a decision based on the boolean expression (true or false condition). Example: Evaluating whether a person smokes or not to determine insurance costs.

Equality Operators

There are two types of equality operators that we will study: " == " and " != " Equality operators are used to determine whether two values are equal or not.

Increment and Decrement Operators

There are two types of increment/decrement operators: The increment operator(variable++) and the decrement operator(variable--) They are unary operators which means that they operate on only one operand.

Increment Operator

count = count +1; ---> count++; The value of count is increased by one when placed at the end of a while loop or at the increment/decrement spot of a for loop.

Decrement Operator

count = count - 1; ---> count--; The value of count is decreased by one when placed at the end of a while loop or at the increment/decrement spot of a for loop.

Algorithm

The step-by-step process for solving the problem. Not written in actual code, an algorithm is a "plan of attack."

Validate The Input

A measure that ensures that the input is valid and if it is not valid issues a error message and prompts the user to enter a valid value. This repeats until the user enters a valid message. Example: while (won < 0 || wom > NUM_GAMES) { System.out.print("Invalid input. Please reenter: "); won = scan.nextInt(); }

Pseudocode

A mixture of coding statements and English phrases that are essentially the "rough draft" of the program.

Robust Program

A robust program is able to handle user errors and prompt the user to correct their mistake.

Running Sum

A running sum would be the total of all the values entered and would be incrementally added during each loop. You can also keep a tally of how many times the loop has run by adding another incremented variable. Example: System.out.println("Enter Integer Value: "); int num1 = scan.nextInt(); int valuesAdded = 0; while(num1 != 0) { System.out.println("Happy"); num1 = scan.nextInt; valuesAdded +=num1; } //Values added would increase by num1 every time a value other than 0 is entered.

Short Circuits

A short circuit occurs when the first portion of a logical AND or logical OR operators answers the condition. In the case of the AND operator the first condition being false results in a short circuit while the case of the OR operator the

Iterators

An iterator is an object that has methods that can systematically process groups of items repeatedly. This object contains multiple methods including: hasNext, hasNextInt, hasNextDouble

Assignment Operators

Assignment operators combine a basic arithmetic operation with an assignment statement. We'll be studying seven different assignment operators including: " = ", " += ", " -= ", " *= ", " /= ", " %= "

Block Statements

Block statements replace individual statements when placed below the if statement. In the situation where there is an if statement with no brackets and two statements under it the first statement will be evaluated by the condition Example: if (netMoney>expense) { loss = 0; profit = netMoney - expense; System.out.println("You made a profit"); } else { profit = 0; loss = netMoney-expense; System.out.println("You lost money"); }

==

Equality operator that means "equal to."

!=

Equality operator that means "not equal to."

Comparing Floating Point Values

Floating point values are rarely compared the same way we compare integer values because the underlying representations may be different. Floating point values are instead compared by choosing a tolerance constant and setting that tolerance constant to more than the absolute value of the difference of the two floating point values. Example: if (Math.abs(f1 - f2) < TOLERANCE) System.out.println("Essentially equal.");

Reading Text Files

Given an input file that contains a list of some sort the Scanner object can process the input

Implementation

Implementation is the process of writing the source code that is outlined in the algorithms. Should basically be the translation of your software design into a program.

Infinite Loops

Infinite loops occur when there is no interruption to stop a loop. This occurs when there is no increment/decrement that will change the response to the conditions of the loop or the loop increments/decrements in such a way that the condition will always be true.

Nested if Statements

Nested if statements are when the statement being executed as a result of the condition of the if statement being true is actually an if statement itself. This is used to evaluate multiple boolean expressions (conditions) and execute different statements in different situations.

Comparing Strings (Part 2)

Strings can also be compare using the compareTo method which rather than returning a boolean true/false value returns a negative number if the first string is less than the second string, a zero if the strings are identical, and a positive number if the the first string is more than the second string. Example: int result = nam1.compareTo(name2); if (result == 0) System.out.println( name 1 + " is identical to " + name2); else if (result < 0 ) System.out.println(name1 + " is less than " + name2); else System.out.println(name1 + "is greater than" + name 2);

Comparing Strings (Part 1)

Strings can be evaluated based on whether they're identical or not using the str1.equals(str2) statement and are included in a boolean expression the same way that integer values are. Example: if (name1.equals(name2)) { System.out.println("These names are the same."); } else { System.out.println("These names are not the same."); }

Testing

Testing a program includes compiling and running the program with multiple inputs in order to ensure the accuracy of the results. Testing with the same inputs as in prior tests is called regression testing as it prevents the programmer from creating new errors.

*=

The assignment operator "*=" multiplies the value placed after it by the variable placed before it and assigns the original variable this new value. Example: int money; money *=5; ---> money = money * 5;

+=

The assignment operator "+=" adds the value placed after it to the variable placed before it and assigns the original variable this new value. Example: int money; money +=5; ---> money = money + 5;

&&

The logical AND operator. The result of the boolean expression (condition) is true if and only if both of the conditions stated before and after the " && " are true. If one of these are false then the program will not execute the following statement. This will short circuit if the first boolean expression in the condition is false as the AND operator requires that both of the inner conditions be true.

! a

The logical NOT operator. The opposite of a. When the condition of a is found true the condition of ! a would be false. boolean done = false; if (!done) System.out.println("not done");

||

The logical OR operator. The result of the boolean expression (condition) is true if one of the two conditions before and after the " || " is true. This will short circuit if the first boolean expression is true as the OR operator requires only one of the inner conditions to be true.

Flow of Control

The order in which the statements are executed in a program. Java applications begin with the first line of the main method. The programmer can change the flow of control in a programming using certain types of programming statements.

Sentinel Value

The sentinel value is a value that is outside the range of the normal range of input values and can be used to quit the loop. Example: System.out.println("Enter Integer Value: "); int num1 = scan.nextInt(); while(num1 != 0) { System.out.println("Happy"); num1 = scan.nextInt; } //0 would be the sentinel value


Set pelajaran terkait

Finance Chapter 13, Investing in Bonds

View Set

Week 5: Pharmaceutical Benefits Scheme

View Set

Final exam semester 2 Junior English terms

View Set

Personal Health -- Ch. 6: Manage Stress

View Set