Level 90 Level 92
Level 91

[Quiz] Methods IV


116 words 0 ignored

Ready to learn       Ready to review

Ignore words

Check the boxes below to ignore/unignore words, then click save at the bottom. Ignored words will never appear in any learning session.

All None

Ignore?
What are overloaded methods?
methods within the same class that have the same name but different numbers or types of parameters
any number
How many overloaded versions can a method have?
based on the types of the parameters passed to it
How does the compiler know which one of the overloaded methods to call?
true, only the types of the parameters matter
T/F: Names of the formal parameters in the method definition do not distinguish overloaded methods.
may get confusing and cause bugs
Problems with having too many overloaded versions of a method?
they are overloaded by definition
All constructors in a class have the same name, so...
non-static and static
What are the two kinds of fields a Java class may define?
Instance Variables
Variables declared outside of any method definitions and are not modified by the "static" keyword
What is another name for static fields?
class variables (shared by all objects of the class)
Why are static fields called "static"?
their memory is not dynamically allocated: is reserved before any objects of the class are created
Reasons for static fields (3)
to define "universal" public constant (ex. Color.BLUE), to have all objects of the class share same constants or settings (ex. craps table), to collect stats or accumulate totals for all objects of the class …
What are static methods?
do not access/ manipulate any instance variables: only work with static fields or do not access any fields at all (attributed to the whole class)
class methods
methods that cant use the "this" keyword :
Reasons for defining static methods (2)
may provide "public service" that has nothing to do with any particular object (ex. find GCF), may work with static fields of the class (ex. accessor or modifier for a static field)
such a call implies this-dot
Why is a static method is not allowed to access/ modify instance fields or call instance methods without an object-dot prefix?
main is always static
Why can't main call a non-static method without a reference to a particular object?
in constructors
Where are instance variables initialized?
instance (non-static) methods
Where are instance variables used?
Why doesn't it make sense to have a constructor set static fields?
normally you don't want to affect all class objects while constructing just one of them
they would all be identical
Why does Math never create objects?
static
Indicates that a variable or method belongs to a class, rather than to any object created from the class.
java.lang (built into Java)
String class belongs to what package?
+ and += (for concatenation)
What are 2 operators in the String class?
literal strings
objects of the String class; text in double quotes
false (they are "just there")
T/F: Literal strings have to be constructed.
newline
\n stands for:
immutability
What is the term for: once created, a string can not be changed (even by its methods)
Why are immutable objects convenient?
several references can point to the same object safely (no danger of changing one without the others being aware)
empty strings
have no characters; length zero
int length ()
string length
char charAt(k)
returns the k-th character (positions numbered from zero)
String s2 = s.substring(i,k);
returns the substring of chars in positions from i to k-1
String s2 = s.substring(i)
returns the substring from the i-th char to the end
concatenates s1 and s2
String result = s1 + s2
result += s3
concatenates s3 to result
result += num
converts num to string and concatenates it to result
zero
A value that is a number.
11 (starts searching at position 9)
String date = "July 5, 2012 1:28:19 PM";
-1 (not found)
String date = "July 5, 2012 1:28:19 PM"
if s1 is equal to s2
boolean b = s1.equals(s2) returns true when?
if s1 matches s2, case blind
boolean b = s1.equalsIgnoreCase(s2) returns true when?
returns the "difference": s1-s2
int diff = s1.compareTo(s2)
int diff = s1.compareToIgnoreCase(s2)
returns the "difference": s1-s2, case blind
String s2 = s1.trim()
returns a new string formed from s1 by removing white space at both ends
String s2 = s1.replace (oldCh, newCh)
returns a new string formed from s1 by replacing all occurrences of oldCh with newCh
String s2 = s1.toUpperCase(); or String s2 = s1.toLowerCase();
returns a new string formed from s1 by converting its characters to upper or lower case
s1 = s1.toUpperCase();
How do you convert s1 to uppercase?
converts numbers to string
String s = "" + num
converts integer to string
String s = Integer.toString(i)
converts double to string
String s = Double.toString(d)
56.74
import java.text.DecimalFormat;
returns number from a string
String s1 = "-123"; s2 = "123.45";
Number Format Exception
Which exception is thrown if s does not represent a valid number when trying to get a number from a string?
if(Character.isDigit(ch)).../ .isLetter.../ .isWhiteSpace...
return true if ch belongs to the corresponding category
char ch2 = Character.toUpperCase(ch1);
if ch1 is a letter, returns its upper case, otherwise returns ch1
int d = Character.digit (ch, radix);
returns the int value of the digit ch in the given int radix
char ch = Character.forDigit (d, radix);
returns a char that represents int d in a given int radix
StringBuffer class
represents a string of characters as a mutable object
StringBuffer()
empty StringBuffer of the default capacity
StringBuffer(n)
empty StringBuffer of a given capacity
StringBuffer(str)
converts str into a StringBuffer
toString
Which method converts StringBuffer into a string?
Math.pow(x, y)
How is the pow method used in Java?
a method
In Java a function is known as _______
Math.random();
How is the random method used in Java?
modifier returnType name(parameters)
What is the structure of a method?
Math.ceil() and Math.floor() do?
Takes a double argument and returns the rounded up/down value respectively to the nearest integer.
Math.max() and Math.min() do?
Takes 2 int, long, float, or double arguments and returns the max/min between the 2
Math.abs() does?
Takes an int, long, float, or double argument and returns the absolute value of it.
int [] series;
How is an array variable created? How is it given a size? How about initializing it in one line?
array.length
What is the method to get the size of the array?
public static void doIt(int[] array)
How is an array passed to a method?
public static double[] doStuff(double[] series)
How is an array returned from a method?
must have: import java.lang.System
How is the arraycopy method used?
for(double x: arrayName)
What is the enhanced for-loop for arrays?
Variable length argument?
public static void (int...numbers);
import java.util.Arrays
What is the sort method from the Arrays class?
import java.util.Arrays;
What is the binary search method from the Arrays class?
builder
responsible for declaring & defining how some module works
caller
somebody that makes use of an existing module (user)
classes & interfaces
can contain fields (data variables) & methods (member functions)
wildcards
if you are going to be using many classes from the same package, you can tell the compiler to import all classes from a single package with *
*
multiplication
API description
gives all the information you need to be able to syntactically use it correctly.
field summary
section that lists data fields that you might want to use (often constants)
constructor summary
section that lists the constructor methods that are available for this class
method summary
lists methods that are available for this class
className.fieldName
static variable format
className.methodName (arguments)
static method format
java.lang.Math
has two variables- e & PI
objectName.fieldName
the format to call upon fields/methods with dot-operators for instance members, once you've declared 1 > objects
random library
a pseudo-random generator with a seed used as the input to an algorithm for a number
method
A software object's behavior is exposed through a
function
The types that are part of the Java platform are members of various packages that bundle classes by _______
continue
when used in a loop, this statement causes the current loop iteration to end, but the loop then moves on to the next step
while loop process
the rest of the loop body is skipped, and execution moves on to the test condition
for loop process
the rest of the loop body is skipped, and execution moves on to the iterative statement
for (initialCondition; booleanExpression; iterativeStatement)
format for FOR loop
do/while loop
the loop body is executed first
while (booleanexpression)
while loop format
x++
incrementing is done after the value of x is used in the rest of the expression
v+=e
v = v+e
charAt(index)
Returns the character at Index in this string. Index numbers begin at 0.
compareTo(A_String)
Compares this string with A_String to see which string comes first in the lexicographic ordering. Returns negative integer if this string is first, zero if the two strings are equal, and a positive integer if A_String is first.
concat(A_String)
Returns a new string having the same characters as this string concatenated with the characters in A_String. You can use the + operator instead of concat.
equals(Other_String)
Returns true if this string and Other_String are equal. Otherwise, returns false.
equalsIgnoreCase(Other_String)
Behaves like method equals, but considers uppercase and lowercase versions of a letter to be the same.
indexOf(A_String)
Returns the index of the first occurrence of the substring A_String within this string. Returns a -1 if A_String is not found. Index numbers begin with 0.
lastIndexOf(A_String)
Returns the index of the last occurrence of the substring A_String within this string. Returns a -1 if A_String is not found. Index numbers begin with 0.
length( )
returns how long a string is
toLowerCase( )
returns string in all lowercase letters
toUpperCase( )
returns string in all uppercase letters
replace(OldChar , NewChar)
Returns a new string having the same characters as this string, but with each occurrence of OldChar replaced by NewChar.
substring(Start)
Returns a new string having the same characters as the substring that begins at the index Start of this string through the end of the string. Index numbers begin at 0.
substring(Start , End)
Returns a new string having the same characters as the substring that begins at the index Start of this string through, but not including, index End of the string. Index numbers begin at 0.
trim( )
returns a copy of the string without spaces