knowt logo

Chapter 6 Study Guide

Chapter 6

Complete Study

Study Guide

What is a counter? A variable used to count the number of times an event occurs.

What does this counter store? int count = 15;count = count + 1;System.out.println(count); 16

What is counting in Java? Process of incrementing(increasing a variable by a particular amount)

What are two ways we can write counters? Count = Count +1; Count +=1;

T/F: We can use other math operators with counters. True

Understand simple counter outputs if given. For example, relook at question 2. No answer needed. Add your own notes here if needed.

What is an accumulator? A variable used to keep a running sum

What is the difference between counters and accumulators? Counters = count Accumulators = sum

What are Loops? Process used to repeat a set of statements

What are the difference between definite loops and indefinite loops? Definite Loop

A loop that repeats a certain number of times

Indefinite Loop

A loop that repeats based on a condition (Boolean Expressions)

What is a for loop? A definite loop structure that executes a set of statements a fixed number of times.

What is the syntax and example for a for loop? for (<initialization> ; <condition> ; <increment>)

{

<statements>

}

What is an increment? Increments - increases the number by one.

T/F: For loops cannot work backwards. False. They can be done backwards

T/F: For loops can be incremented by a counter.

True

Out of the two loops, what kind of loop is a while loop? Indefinite Loop

What is a while loop? Repeats while a condition is true Will a while loop execute if initially false? May never execute if the condition is initially false

What is the syntax and example for a while loop? while (<condition>)

{

<statements>

}

What is a do-while loop? Alternate While Loop

Evaluated at the end of the loop

Executes once before the condition is evaluated.

What is the syntax and example for a do-while loop? do

{

<statements>

}

while (<condition>);

What is the difference between a while loop and do while loop? Do/while loop will always execute at least once

What is an infinite loop? A loop where the condition never evaluates to false.

Loop continues to run and will cause a run time error or cause the computer to appear to freeze.

Understand Syntax errors in general, loops, and infinite loops? No answer needed. Add your own notes here if needed.

What is a nested loop? Loops where one loop is completely inside (nested) another loop.

What is the syntax and example for a nested loop? for (outer = 1 ; outer <= 3 ; outer ++)

{

for (inner = 1 ; inner <= 4 ; inner ++)

{

System.out.println( outer + “ “ + inner);

}

}

What are the three debugging techniques and give details about each one?

  1. Variable Tree

  2. Additional println()

  3. Commenting out code

What is a String? variable contains a collection of characters surrounded by double quotes

What is the location package and what do you need to import?

Package: java.lang Import java.lang.String

Understand what is in the String Class. No answer needed. Add your own notes here if needed.

What is an index? Definition: The position of a character in a string.

What is the positive and negative indexing?

Pos = 0,1,2,3,4,….

Negative: …., -4,-3,-2,-1.0

What are the three methods associated with indexes? Three Methods (In Chart) indexOf – Finds the index of the parameter charAt - Finds the character at the parameter substring – returns a String based on index value parameter or parmeters.

What is the output?

public class Exercise1 {

public static void main(String[] args)

{

String str = "Java Exercises!";

System.out.println("Original String = " + str);

// Get the character at positions 0 and 10.

int index1 = str.charAt(0);

int index2 = str.charAt(10);

// Print out the results.

System.out.println("The character at position 0 is " + (char)index1);

System.out.println("The character at position 10 is " + (char)index2);

} } RESULTS: Original String = Java Exercises! The character at position 0 is J The character at position 10 is i

Give an example of a String. String word = “Dog”;

What package is the Math class in and what do you need to import? Located Package: java.lang package

Import: java.lang.Math

What are the methods of the math class and what does each one do? abs(num) returns the absolute value of num pow(num1,num2) returns num1 raised to the num2 power as a double sqrt(num) returns the square root of num as a double sin(double angle)returns the sine of angle, where angle is in radians cos(double angle) returns the cosine of angle, where angle is in radians tan(double angle) returns the tangent of angle, where angle is in radians toRadians(double deg) converts degrees to radians

Evaluate output if I give you code from math class. Like abs, pow, and sqrt. You can see the code written in different ways.

int posNum = 3, negNum = -8; int num1 = 12 num2 = 4, square = 25; System.out.println(Math.abs(posNum)); System.out.println(Math.abs(negNum)); System.out.println(Math.pow(num1,num2)); System.out.println(Math.sqrt(square));,

System.out.println(Math.abs(5)); System.out.println(Math.abs(-65)); System.out.println(Math.pow(5,10)); System.out.println(Math.sqrt(100));

a.

3 -8 20736 5

b.

5 65 9765625 10

JK

Chapter 6 Study Guide

Chapter 6

Complete Study

Study Guide

What is a counter? A variable used to count the number of times an event occurs.

What does this counter store? int count = 15;count = count + 1;System.out.println(count); 16

What is counting in Java? Process of incrementing(increasing a variable by a particular amount)

What are two ways we can write counters? Count = Count +1; Count +=1;

T/F: We can use other math operators with counters. True

Understand simple counter outputs if given. For example, relook at question 2. No answer needed. Add your own notes here if needed.

What is an accumulator? A variable used to keep a running sum

What is the difference between counters and accumulators? Counters = count Accumulators = sum

What are Loops? Process used to repeat a set of statements

What are the difference between definite loops and indefinite loops? Definite Loop

A loop that repeats a certain number of times

Indefinite Loop

A loop that repeats based on a condition (Boolean Expressions)

What is a for loop? A definite loop structure that executes a set of statements a fixed number of times.

What is the syntax and example for a for loop? for (<initialization> ; <condition> ; <increment>)

{

<statements>

}

What is an increment? Increments - increases the number by one.

T/F: For loops cannot work backwards. False. They can be done backwards

T/F: For loops can be incremented by a counter.

True

Out of the two loops, what kind of loop is a while loop? Indefinite Loop

What is a while loop? Repeats while a condition is true Will a while loop execute if initially false? May never execute if the condition is initially false

What is the syntax and example for a while loop? while (<condition>)

{

<statements>

}

What is a do-while loop? Alternate While Loop

Evaluated at the end of the loop

Executes once before the condition is evaluated.

What is the syntax and example for a do-while loop? do

{

<statements>

}

while (<condition>);

What is the difference between a while loop and do while loop? Do/while loop will always execute at least once

What is an infinite loop? A loop where the condition never evaluates to false.

Loop continues to run and will cause a run time error or cause the computer to appear to freeze.

Understand Syntax errors in general, loops, and infinite loops? No answer needed. Add your own notes here if needed.

What is a nested loop? Loops where one loop is completely inside (nested) another loop.

What is the syntax and example for a nested loop? for (outer = 1 ; outer <= 3 ; outer ++)

{

for (inner = 1 ; inner <= 4 ; inner ++)

{

System.out.println( outer + “ “ + inner);

}

}

What are the three debugging techniques and give details about each one?

  1. Variable Tree

  2. Additional println()

  3. Commenting out code

What is a String? variable contains a collection of characters surrounded by double quotes

What is the location package and what do you need to import?

Package: java.lang Import java.lang.String

Understand what is in the String Class. No answer needed. Add your own notes here if needed.

What is an index? Definition: The position of a character in a string.

What is the positive and negative indexing?

Pos = 0,1,2,3,4,….

Negative: …., -4,-3,-2,-1.0

What are the three methods associated with indexes? Three Methods (In Chart) indexOf – Finds the index of the parameter charAt - Finds the character at the parameter substring – returns a String based on index value parameter or parmeters.

What is the output?

public class Exercise1 {

public static void main(String[] args)

{

String str = "Java Exercises!";

System.out.println("Original String = " + str);

// Get the character at positions 0 and 10.

int index1 = str.charAt(0);

int index2 = str.charAt(10);

// Print out the results.

System.out.println("The character at position 0 is " + (char)index1);

System.out.println("The character at position 10 is " + (char)index2);

} } RESULTS: Original String = Java Exercises! The character at position 0 is J The character at position 10 is i

Give an example of a String. String word = “Dog”;

What package is the Math class in and what do you need to import? Located Package: java.lang package

Import: java.lang.Math

What are the methods of the math class and what does each one do? abs(num) returns the absolute value of num pow(num1,num2) returns num1 raised to the num2 power as a double sqrt(num) returns the square root of num as a double sin(double angle)returns the sine of angle, where angle is in radians cos(double angle) returns the cosine of angle, where angle is in radians tan(double angle) returns the tangent of angle, where angle is in radians toRadians(double deg) converts degrees to radians

Evaluate output if I give you code from math class. Like abs, pow, and sqrt. You can see the code written in different ways.

int posNum = 3, negNum = -8; int num1 = 12 num2 = 4, square = 25; System.out.println(Math.abs(posNum)); System.out.println(Math.abs(negNum)); System.out.println(Math.pow(num1,num2)); System.out.println(Math.sqrt(square));,

System.out.println(Math.abs(5)); System.out.println(Math.abs(-65)); System.out.println(Math.pow(5,10)); System.out.println(Math.sqrt(100));

a.

3 -8 20736 5

b.

5 65 9765625 10