Date post: | 13-Dec-2015 |
Category: |
Documents |
Upload: | natalie-nichols |
View: | 221 times |
Download: | 0 times |
ArrayLists While loops String Methods
Methods Errors
100200300400
500
100200300400
500
100200300400
500
100200300400
500
100200300400
500
While loops - 300While loops - 300
How many times would you loop here?int x = 10;while (true){ if ( x > 15) break; else x -= 1;}
While loops - 400While loops - 400
What would I code in the following loop header to break out if the score was not between 0 and 100?
while ( _____________ ){ …….}
While loops - 500While loops - 500
Using a while loop, continue printing out the elements in an integer array until the integer is greater than 10 and its an even number.
Strings - Strings - 300300
Allows the programmer to take pieces of strings and save them into new strings
Methods - Methods -
400400
What are the variables called that track key information about an object?
ArrayLists – 100 (solution)ArrayLists – 100 (solution)
Refer to an element in an array list.
ArrayList<Integer> aList;aList = new ArrayList<Integer>();aList.get(0) refers to first elementaList.get(i) refers to the ith element
ArrayLists – 200 (solution)ArrayLists – 200 (solution)
Create an element at the beginning of an array list
ArrayList<Integer> aList;aList = new ArrayList<Integer>();aList.add(0, 25); //creates an element with value 25 and shifts all other elements up
ArrayLists – 300 (solution)ArrayLists – 300 (solution)
Remove the last element in an array list
If (aList.size() > 0) aList.remove(aList.size() – 1);
ArrayLists – 400 (solution)ArrayLists – 400 (solution)
Sum up all elements in an array list using an enhanced for loop
int sum = 0;for ( int e : aList ){ sum += e;}
ArrayLists – 500 (solution)ArrayLists – 500 (solution)
Using a for loop, print out all the elements in reverse order
for(int i = aList.size()-1; i >= 0; i--){ System.out.println(aList.get(i));}
While loops – 100 (solution)While loops – 100 (solution)
What is the first step in creating a while loop?
Determine what is true when the loop ends. This is usually easier than determine what needs to be true to keep looping.
While loops – 200 (solution)While loops – 200 (solution)
When should you use a while loop instead of a for loop?
Use a while loop instead of a for loop when you don’t know how many times you need to loop (indefinite loop).
While loops – 300 (solution)While loops – 300 (solution)
How many times would you loop here?int x = 10;while (true){ if ( x > 15) break; else x -= 1;}
Infinitely or until the computer ran out of memory.
While loops – 400 (solution)While loops – 400 (solution)
What would I code in the following loop header to break out if the score was not between 0 and 100?
while ( score >= 0 && score <= 100 ){ …….}
While loops – 500 (solution)While loops – 500 (solution)
Using a while loop, continue printing out the elements in an integer array until the integer is greater than 10 and its an even number.int i = 0;while ( a[i] <= 10 || a[i]%2 == 1 ){
System.out.println( a[i] ); i++;}
Strings – 100 (solution)Strings – 100 (solution)
Returns how many characters are in the string
s.length();
Strings – 200 (solution)Strings – 200 (solution)
Returns true or false depending if the Strings contain the same word
s.equals(word)
if (s.equals(“cat”){ ……}
Strings – 300 (solution)Strings – 300 (solution)
Allows the programmer to take pieces of strings and save them into new strings
String part = s.substring(2,5);
Remember that subtracting 5-2 = 3 tells you that the substring is 3 characters.
Strings – 400 (solution)Strings – 400 (solution)
This method uses lexicographical analysis
s.compareTo(“cat”);
if it returns -1 then s < “cat”If it returns 0 then s = “cat”If it returns 1 then s > “cat”
Strings – 500 (solution)Strings – 500 (solution)
Allows the programmer to combine strings to form a single new string.
Concatenation:String fullName = firstName + “ “ + lastName;
Methods – 100 (solution)Methods – 100 (solution)
A specialized method that constructs new objects
The Constructor method has the same name as the class and is used to construct the object. One of its main functions is to initialize the instance variables.
Methods – 200 (solution)Methods – 200 (solution)
A method that returns a value to the caller
A Return method returns a value to the calling class. The return statement is used to return the value and its data type must match that specified in the method’s signature line.
Methods – 300 (solution)Methods – 300 (solution)
The information that is passed to the method
Parameters are passed to the method. There can be multiple parameters separated by commas.
Methods – 400 (solution)Methods – 400 (solution)
What are the variables called that track key information about an object?Instance variables store the important information about an object. They are normally private variables and are declared outside of your methods at the top of the class.
Methods – 500 (solution)Methods – 500 (solution)
A method in a class that is not used for an individual object
Static methods are methods that don’t operate against individual objects. In fact, you don’t have to instantiate the object in order to use them.
Errors – 100 (solution)Errors – 100 (solution)
These extremely common errors will prevent the program from compiling
Syntax errors
Errors – 200 (solution)Errors – 200 (solution)
These can cause the program to crash while running
Runtime errors
Errors – 300 (solution)Errors – 300 (solution)
This occurs when the programmer misunderstands the program
Logic errors
Errors – 400 (solution)Errors – 400 (solution)
This occurs when a call to an array or a String has exceeded its length
Index out of bounds errors