Be Prepared Ch 3-4 - Java Features Flashcards
arguments and parameters
the values passed in a method call (in the parentheses) The words Argument and parameters are used interchangeably
The concept of “privacy” applies to the ____________ and not to individual objects. Different objects of the same class have full access to each other’s fields and methods.
class as a whole
• Requierement on ap exam to make all instance variables _____
private
• Instance vs static variables
object vs class
static method rules
§ Static methods cannot access or modify any instanec variables and cannot refer to this (a reference to a particular object) because this is undefined when a static method is running. Static variables can be initialized in a class ocnstructor and can be accessed and modified in instance methods.
Should you print the return value using System.out from inside the method (when it is not requested)?
NO. • It is a minor error (-1/2) to print the return value to a System.out from inside the method (when it is not requested)
Parameters of primitive data types are passed to methods ___________
“by value”
• Given § Public void fun(int a, int b){ □ a+=b; □ b+=a; § } • The output from the following code: § Int x = 3, y = 5; § Fun(x,y); § System.out.println(x + “ “ + y); • Will print ____________
“3 5” • This is beause x and y are ints, so they are passed to fun by value.
§ All objects are passed to methods as _____. A method receives a _______ to (the address of ) the object
references a copy of a reference
immutable objects
objects without modifier methods String, Integer, and Double classes For example, there is no way in Java to write a method □ Public void toUpperCase(String s) { . . . } § Because the method has no way to change the string passed to it. For immutable objects, the method has to create and return a new object with the desired properties: □ Public String toUpperCase(String s) { . . .}
§ A method whose return type is a class can also return
a null (a reference with a zero value that indicates that it does not refer to any valid object.
§ If a method returns an ArrayList, write the
full ArrayList type, including its elements’ type in angle brackets, as the method’s return type.
• The compiler treats overloaded methods as ________.
different methods It figures out whichh one to call depending on the number and types of the parameters.
Random Numbers [1,n]
• Int r = (int) (n * Math.random()) + 1;
Input and Output
• Ap subset does not include any classes or methods for data input. The Scanner class is not in the AP subset. • If a question ivolves user input it may be stated as follows: § Double x = < call to a method that reads a floating-point number> § Or § Int x = IO.readInt(); //reads user input
• An exception
is a run-time event that signals an abnormal condition in the program.
Checked Exceptions
• Some run-time errors, such as invalid user input or an attempt to read past the end of a file, are considered fixable,. The try-catch-finally syntax allows the programmer to catch and process the exception and have the program recover. This type of exception is called a checked exception § Checked exceptions and the try-catch statements are not in the AP subset.
Unchecked exceptions
• Other errors, such as an array index out of bounds or an attempt to call a method of a non-existing objet (null reference) are considere fataL: the program displays an error message with information about where the error occurred, then quits. This type of exception is called an unchecked exception.
• In Java, an exception is ____________
an object. The Jaba library impletments many types of exceptions, and if necessary you can derive your own exception class from one of the library classes.