AP Computer Science A - Using Objects (Unit 2) Test Review

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Null

-a special value that means "no reference" -Java holds this value automatically for a string variable when the variable has not been set to any value

Keyword "new"

-a word that tells Java that we need to reserve some space in memory for the new object that we are creating -class/reference data types (except strings) need this word in order to tell the computer to create a new spot in memory, but primitive data types don't need this

Static methods/class methods

-methods that can be used using the name of the class without having to create an instance of the class (an object) first -all math class methods are of this type

Non-void methods

-methods that do not change the state of an object, but are used to find some information about the object -these methods return a value to the console, which is usually captured in some variable -ex: double area = c.getArea();

Void methods

-methods that only change the state of an object, and perform an action, but don't return any information -for example, c.setRadius(7.5)

Non-static methods

-methods that require for an object to have been previously instantiated before they can be used -example: a.toUpperCase; //in this code you can see that in order to use the .toUpperCase method we have to have previously created the object a

object-oriented programming

-programming that uses objects in code to represent real world entities -this type of programming allows us to map our program to the real situations that out programs represent, and so it becomes easier to solve problems -Java is an example of an _________-__________ _______________ language

API

-the application programing interface -it defines all the ways in which we can interact with classes, inside a library -basically, all the accessible methods for a class can be found in the API documentation for the relevant library -for example, the documentation for the edhesive shape API's

Dot operator

-the following operator is used to call methods: .

Autoboxing

-when Java is given a primitive data type value that is to be stored in a wrapper class variable, it will automatically _____________ the primitive data type value into the wrapper class value -this happens because it is expected that when you set a wrapper class variable equal to something, that it is stored as a wrapper class type value -________________ can be done with any wrapper class

Unboxing

-when Java is given a wrapper class type value that is to be stored in a primitive data type variable, it will automatically _____________ the wrapper class type value into the primitive data type value -this happens because it is expected that when you set primitive data type variable equal to something, that it is stored as a primitive data type value -____________________ can be done with any primitive data type

String concatenation

-when we concatenate a string onto another string variable's value -we do this by using the + operator

void methods cannot return values but they can print them if they have a built in print statement

Can void methods return values? Can they print them?

yeah

Can you use methods inside of other methods?

Escape characters

quick tricks that allow additional items to be placed inside print statements

an exception that you get when you don't take into account that the index counting starts at 0 and not 1

What is an index out of bound exception?

an "instance" of the class

What is another word for an object that is created from a class?

state

What is another word for the attribute of an object?

they are not methods because they are not followed by a pair of parentheses

Are .MAX_VALUE and .MIN_VALUE, methods or not? Why?

ye

Are constructors special types of methods that allow the programmer to create a new object?

Some constructors take in parameters, while others don't

Do all constructors take in parameters, or do only some?

nope

Do any two constructors have BOTH the same name and the same parameters (order of parameters and type of parameters)?

they hold only one piece of data at a time

Do primitive data types hold multiple pieces of data at once, or only one piece of data?

no they don't overwrite the previous value stored inside them, instead they reference a whole separate memory location for the new value

Do reference data type variables overwrite the previous value stored inside them? If not, then what do they do?

yes

Do spaces count as indexes?

yessir

Does Java have built in methods for strings?

each class defines its own constructors

Does every class define its own constructors or does each class in Java have the same constructors?

the order of the data types matters more since you could have two constructors with the same datatypes in the same order but with different variables, and this would not be allowed

Does the order of the data types or the order of the constructors have to be different in overloaded constructors? Why?

no, this is a situation where autoboxing/unoboxing never occurs, and Java does this automatically without autoboxing/unboxing

Does unboxing or autoboxing occur any time when a wrapper class type variable or primitive data type variable is inputted as the parameter for a constructor, where the value of the wrapper class type variable is going to be set as the value of the primitive data type variable or the value of the primitive data type variable is going to be set as the value of the wrapper class type variable?

the creation of an object that the method can be used on

Except certain methods such as those from the math class, what do most methods require before using them?

methods define behaviors and allow us to execute the behaviors of an object (basically, methods are the same as behaviors)

How are methods related to the behavior of an object?

using constructors

How are non-primitive data type objects created?

using call by value (meaning that the constructors gets a copy of that value, not the actual value itself)

How are parameters passed to the constructor?

by looking at the data type of each parameter in the constructor and the order of the parameters in each constructor

How can Java differentiate between constructors of the same name (overloaded constructors)?

Integer.MAX_VALUE

How can we access an int's max value?

Integer.MIN_VALUE

How can we access and int's minimum value?

String planet = new String("earth");

How can we create a string object called planet, and make it so that it stores the value "earth", using a constructor?

by writing the following code: import Java.lang

How do we import the Java.lang package?

by writing the following code: import edhesive.shapes.*

How do we import the edhesive shapes package?

import java.lang.math

How do we import the math class?

-to put a double quote inside a string, we put "\ before the first double quote of the string, and we put \" right before the second double quote of the string -following the instructions from above will output your string inside double quotes

How do we put a literal double quote inside a string?

-to put a single quote inside a string, we put '\ before the first double quote of the string, and we put \' right before the second double quote of the string -following the instructions from above will output your string inside single quotes

How do we put a literal single quote inside a string?

it's really important because primitive data types like ints and doubles will usually involve number calculations, so you have to make sure that if you are doing a number calculation inside your print statement, then either you put the number calculation at the beginning of the print statement so that Java recognizes it first and does it first, or you can put the calculation anywhere inside the print statement as long as it is in its own set of parentheses so that Java will recognize the order of operations

How important is it to order things carefully like primitive data type variables and string variables when concatenating?

a class is the blueprint/template for creating an object inside of it

How is a class related to an object?

wrapper classes have the same name as their respective primitive data type, but the only exception is that they start with a capital letter instead of a lower case one, and for the int primitive data type specifically, its whole name (Integer) is written out instead of just int

How is the name of each wrapper class related to its corresponding primitive data type?

there is only a single wrapper class for each primitive data type

How many wrapper classes are there fore each primitive data type?

we can store it in the appropriate type of variable

If a string method such as .indexOf() outputs an integer value, then what can we do with this value?

the header

In a method, does the header or body come first (reading from left to right)?

java.lang

In which package are standard Java packages for things like strings, part of?

inside the java.lang package

In which package do wrapper classes live in?

no they do not

Just like constructors, do all methods require parameters?

they're usually variables

Objects are generally what thing in code?

False

True or False: A method can be called on a string variable that has not been declared

true

True or False: Each letter in a string is stored separately

True

True or False: Primitive data type variables over write the previous value stored

they are the representation of an object's state, and they are the values that the object stores or characteristics of the object itself

What are attributes/states?

into packages

What are classes in Java organized into?

a set of two parantheses, like so: ()

What are do methods always have after them?

tools that we can use to help us change the data

What are methods?

constructors with the same name, but different parameters, so that they create different objects from one another with different characteristics

What are overloaded constructors?

class data types

What are reference data types also known as?

.nextLine();, .nextInt();, .nextDouble();, etc.

What are some of the behaviors/methods of the scanner class?

int, double, and boolean

What are the 3 main primitive data types that we use in this class?

-to include a literal double quote inside a string, use: "\ and \" -to include a new line inside a string, use: \n -to include a tab inside a string, use: \t -to include a literal single quote inside a string, use: '\ and \' -to include a literal backslash inside a string, use: \\

What are the 5 main strings that we need to know for this class?

classes

What are the blueprints from which objects are created?

-they hold more than one piece of data at a time -they can hold data of different types -they have built in methods (tools) -user can create their own classes

What are the characteristics of class/reference data types?

the # of parameters that the constructor can take in, the data types of the parameters, and the variable names of the parameters inputted

What are the formal parameters of a constructor?

indexes

What are the locations for storing each letter in a string, called?

the commands/functions that use on the attributes/states of the object

What are the methods/behaviors of an object?

either a true value or a false value

What are the only two values that the .equals() method can output when it is printed?

-being open (allowing scans) -bein close (not allowing scans/inputs from the user)

What are the state/attributes of the scanner class?

-String bruh = "bruh"; -String bruh; bruh = "bruh"; -String bruh = new String("bruh");

What are the three ways to create the string: "bruh"?

-one way: Integer num1 = new Integer(5); -second way: Integer num1 = 5; -third way: Integer num1; num1 = 5;

What are the three ways to declare a wrapper class variable and set its value (for example, try to create an Integer type variable called num1 and then set its value to 5)?

a header and a body

What are the two parts of a method?

-String empty = null; //this is just an in-line comment and you don't need it in your program, but it's just to tell you that you DO NOT put the null in double quotes or single quotes -String empty; //this is just an in-line comment and you don't need it in your program, but it's just to tell you that empty was automatically set the value of null, because we never set a value originally

What are the two ways that we set the value of a string variable to null?

class/reference data type

What datatype is a string?

they have the same behaviors/methods because they're all following the same template (they're all in the same class)

What do all objects of certain class have the same of? Why?

Math.

What do all the math class methods start with?

-we get an empty string -for example, if you start at index 1 but the inputted stopping value is also 1, then remember that when using substring, Java prints everything from the starting index to the stopping index's value minus 1, so when we input both the starting and stopping index as 1, Java tries to print from index 1 to 0, and nothing exists here, so we get an empty string

What do we get if both parameters in the substring method are at the same index?

-we need to escape it, inside the string literal using another backslash before the backslash that we want to print -for example, the following print statement will print a SINGLE backslash: System.out.print("\\");

What do we have to do in order to print a backslash (\) in a print statement?

they wrap a primitive data type into its own class type, and then bring in methods that can be used

What do wrapper classes do?

-it basically mathematically compares if the value stored in the variable that comes before the dot operator to the value that is either directly placed as a parameter or the variable that holds the value which has been put in as a parameter -For example, in the following line of code: System.out.print(n1.compareTo(n2));, if n1 > n2 then we get outputted a 1, if n1 < n2 then we get outputted a -1, and if n1 = n2, then we get outputted a 0

What does .compareTo(other) wrapper class type method do?

then we import all the classes in that package

What does it mean if add an asterisk * after the name of a package while importing it?

when we have methods of the same name, but Java determines which method to use based on the parameter that is passed in, the number of parameters passed in, and the parameter data types

What is method overloading?

when something is returned, it can be stored/modified, but this is different from when something is printed since it can't be modified in the console

What is the difference between returning a value to the console and printing it?

-it compares two one string stored in a string variable with a string literal, alphabetically -the string variable that holds a string is placed before the dot operator and the string literal is placed as a parameter inside the parentheses in double quotes -if the string literal parameter, alphabetically, comes after the string value stored in the string variable that is being compared, then a negative int is outputted whose magnitude is determined depending on how far the strings are alphabetically apart -if the string literal parameter, alphabetically, comes before the string value stored in the string variable that is being compared, then a positive int is outputted, whose magnitude is determined depending on how far/close the strings are alphabetically

What does the .compareTo(otherString) string type method do?

-it checks if both strings have the same characters, spaces, etc., and then it returns either a true boolean value or a false boolean value depending on if the strings are equal or not (true shows that the strings were equal and false shows that they weren't) -in the parentheses after the method name, a parameter must be passed in -for example: String a = "cool" String b = "kool" System.out.println(a.equals(b)); -the code from above would print out the value: false, because the string "cool" is not EXACTLY the same as the string "kool" (note that the variable b was the parameter that we checked variable a against)

What does the .equals() string type method do?

-it tells us which index a specific character from some string, belongs to -we place the variable (that contains a string value) before the dot operator and then inside the parentheses, we put in a character from the string stored in the variable, in double-quotes, as a parameter -for example: String a = "epic" System.out.print(w.indexOf("i"))' -from the code above, we are outputted 2 because "i" is stored in index 2 in the string "epic"

What does the .indexOf(string) string type method tell us?

-it tells us the length of the string that is stored in the variable that was attached before the dot operator, as an int -for example: String name = "Bob" System.out.println(name.length()); -the code from above would print out 3 since the string stored in the variable name, "Bob", only had 3 characters

What does the .length() string type method do?

-a method that pulls off a section of a string from the starting index parameter to the stopping index parameter minus 1, both of which need to be specified -for example: String w = "bruh" System.out.println(w.substring(0,2)); -the code from above would print out the characters from the starting index 0 to the stopping index, which was specified as 2, minus 1. Hence, the method will print out characters from the string that was stored in variable w, from index 0 to 1, so br would be printed out -another thing to notice from the code above is how the starting and stopping indexes were specified as parameters inside the parantheses

What does the .substring(start, stop) string type method do?

-this method changes all letters in the string variables (that comes before the dot operator) to lower case letters, while keeping the rest of the string the same -for example: String a = "jAvA51" System.out.println(w.toLowerCase()); -in the code above, java51 would be printed out since all the letters were converted to lower case letters while keeping everything else the same

What does the .toLowerCase() string type method do?

-this method changes all letters in the string variable (that comes before the dot operator) to upper case letters, while keeping the rest of the string the same -for example: String w = "br5uh" System.out.println(w.toUpperCase()); -in the code above, BR5UH would be printed out since all the letters were converted to upper case letters while keeping everything else the same

What does the .toUpperCase() string type method do?

-it finds the absolute value of the double parameter that has been inputted in the parentheses -for example: double a = -5.5; System.out.print(Math.abs(a)); -the code above would output 5.5

What does the Math.abs(double) math class method do?

-it raises the inputted base to the inputted exponent -for example: System.out.print(Math.pow(3, 2)); -the code above would print 9 since 3^2 is 9

What does the Math.pow(base, exponent) math class method do?

it returns a random double value between 0.0 (inclusive) and 1.0 (exclusive)

What does the Math.random() math class method do?

what states will be possible for the object (what types of data it will store, how many, and what restrictions there will be on values, etc.)

What does the class of an object determine about the object itself?

a null pointer exception/error

What exception/error do you get when you try to call a method on a string that has a value of null?

Java allocates a space in memory called a reference, and you can imagine this a a box that is named the variable's name, but the box only holds the memory location of the actual data/value (it does this because the data is most likely large, since we are dealing with a class/reference data type)

What happens we declare and set the value of a class/reference data type variable?

-Java will force the primitive data type to a string in order to concatenate with the other string -for example: int x = 3; int y = 8; System.out.print("I have " + x + y + " dollars"); -in the above example, we would actually be printed out, I have 38 dollars, and this is because Java reads from left to right so it sensed that we have a string first inside the print statement, so when it detected the ints, it forced them to strings (so 3 became "3" and 8 became "8") -after Java had everything inside the print statement in terms of strings, it concatenated everything together -we could have fixed this problem by either writing the print statement like this: System.out.print(x + y + " is how much I have");, and in this situation, Java read the mathematical operation between the ints first, so it did that first, then forced the sum of x and y to a string to concatenate it with the other string -we could have also done this to fix it: System.out.print("I have " + (x + y) + " dollars);, and in this situation, by order of operations, Java first read the mathematical operation and then performed it first, and then it forced the sum of x and y onto a string and then concatenated it with the other two strings

What happens when you concatenate a class data type such as a string with a primitive data such as an int?

the rectangle class

What is a common class that has overloaded constructors?

-the method's name -the number of parameters -the data types of the parameters -the order of the parameters inputted into the method

What is a method signature composed of?

(int)(Math.random x range + minVal) or (int)(Math.random() x range) + minVal -either formula works and they do the exact same thing -the range is calculated by doing the maxmimum value minus the minimum value -minVal is just the minimum value

What is the formula for the Math.random() method that allows us to find a random int between a given minimum value and a given maximum value?

variableName.method(); -basically, put the variable's name then the dot operator then the name of the method with the two parentheses after it (sometimes parameters are inputted into the parantheses and other times they are not)

What is the general format to use string methods?

-the visibility modifier comes first, then the return data type, then the method name, and then the formal parameter list -for example: public int getFaceValue(); -note that not every method needs to have a parameter inputted, and so the formal parameter list can just be represented by the two parentheses sometimes

What is the general structure of a method?

-primitive data type variables hold their actual value that they have been set to -class/reference data type variables hold a reference to the memory location of the actual data

What is the main difference between primitive data types and class/reference data types?

approximately -2 billion to 2 billion

What is the range of values for an int?

Boolean

What is the wrapper class data type that corresponds to the boolean primitive data type?

Double

What is the wrapper class data type that corresponds to the double primitive data type?

Integer

What is the wrapper class data type that corresponds to the int primitive data type?

we have to import it

What must we do to a package before we can use the classes inside that package?

we will be printed a -1 to the console

What output does .indexOf(str) string method give us when we print it, whenever the method cannot find the index to which its parameter corresponds to in the string stored inside the variable? Basically an example like this: String m = "math" System.out.print(m.indexOf("y"));

-the name of the method -the formal parameter list, which is basically the two parentheses with the parameters inside them (the formal parameter list is written after the name of the method)

What parts make up the body of a method?

-the visibility modifier: public or private (the visibility always comes first) -the return data type: int, double, etc. (this comes right after the visibility modifier)

What parts make up the header in a method?

-state and behavior -for example, a string is an object and its state are things like its character length, etc., and its behavior would be methods like .getLength(), etc. that we can use on it

What two characteristics do objects in the real world and in code, share?

-the name of the constructor, which is ALWAYS THE SAME AS THE NAME OF THE CLASS THAT THE CONSTRUCTOR IS IN -the constructor's parameter list, which is the value of the variables passed into the constructor when creating it

What two things make up the signature of a constructor?

each and every math class method is a static method

What type of methods are all the math class methods?

an object oriented programming (OOP) language

What type of programming language is Java?

-they usually print string literals (a string that we put in double quotes but don't set to a variable) -they can also print the values stored inside string variables

What type of strings do print statements print?

-they start at 0 and they end at the 1 before the length of the string (length of the string minus 1) -for example, in the string "bruh", the length of this string is 4 and "b" is stored at index 0, "r" is at index 1, "u" is at index 2, and "h" is at index 3

What value do indexes start at? Where do they end?

the variable holds the value: null, since we never gave it a value

What value do reference/class variables get set if they were only declared and not actually set a value, in the past?

the output would be 1 even though the letter "a" is repeated twice in the string "variable", and this is because the .indexOf(str) method only looks at where its parameter(s) first appeared in the string

What would be the output of the following code, and why?: String b = "variable" System.out.println(b.indexOf("a"));

the output would be 3 and this is because when finding where mini-strings such as "cu" are in the larger string such as "calculator", Java looks for the mini string inside the larger string and it reports the FIRST INDEX that the mini string starts at inside the larger string

What would be the output of the following code, and why?: String c = "calculator" System.out.println(c.indexOf("cu");

new

When creating a new object using a constructor, what keyword must be used?

-the first parameter is an int that specifies the number of side lengths -the second parameter is a double that specifies the side length of each side -note that if the second parameter isn't inputted then some shape with the specified amount of sides will have a default of side length of 1.0 on each side, and if either parameter aren't specified then an equilateral triangle with a side length of 1.0 on each side is created

When suing the RegularPolygon constructors, what do the first and second parameters specify?

the side length/radius is automatically set to the default value of 1.0

When using a constructor from the shapes package, what happens when a side length/radius has not been specified?

1.0

When using constructors from the shapes package, what is the default side length/radius length?

-Java knows which constructor to use, and when, by looking at the signature of the constructor, which is made up of the name of the constructor and the constructor's parameter list

When using overloaded constructors, how does Java know which constructor to use, and when?

-that the string literal parameter, alphabetically, comes after the string value stored in the string variable that is being compared -the magnitude of the negative int is determined depending on how far/close the strings are alphabetically

When using the .compareTo(otherString) string type method, what does it mean if a negative int is outputted?

-that the the string literal parameter, alphabetically, comes before the string value stored in the string variable that is being compared -the magnitude of the positive int is determined depending on how far/close the strings are alphabetically

When using the .compareTo(otherString) string type method, what does it mean if a positive int is outputted?

-Java prints all the characters, spaces, etc. from the starting index minus 1 and then all the way to the end of the string -for example: String w = "bruh" System.out.println(w.substring(2)); -in the code above, ruh would be printed out since u is what the second index stores, and then Java just prints everything after the second index

When using the substring.(start, stop) string type method, what happens if you only specify the starting index and not the stopping index?

-the first "String" tells us the data type of the variable -alpha is the name of the string variable -new String helps initialize the creation of a new string variable -"alpha particles" is the value that the string variable alpha was set to (notice how we put the values in double-quotes inside the parentheses)

When we create the following string, what do the different parts of the line tell us?: String alpha = new String("alpha particles");

-all three of the codes are example of autoboxing -in the first line of code, the double 20.0 is compared with the Double 17.5, so in order for that comparison to occur, 20.0 must be autoboxed into a wrapper class Double value -in the second line of code, we are storing an int: 15, as a Double, so it must be autboxed into a wrapper class Double value -in the third line of code, we find the length of String s as an int, then we try to store that inside of an Integer, so the int length value must be autoboxed into a wrapper class Integer value

Which of the following lines of code are examples of autoboxing?: I. Double a = new Doouble(17.5); int compareValue = a.compareTo(20.0); II. Double d = 15 III. String s = "a string"; Integer len = s.length();

-D is the correct answer, since if we look at the print statement, we can see that we print j, then we go to a new line, we then tab once and then print a, then we force a new line, tab twice, and then print v, and then finally, we force another new line, tab three times, then print a (all of this should match the output from the code) -C is not correct since we are tabbing on each line before adding a new line, and tabbing one one line doesn't mean that the number of tabs will be transferred to the next line as well (instead, you start the beginning of the line on each new line)

Which of the following print statements will print the following code? Why?: j a v a A. System.out.println("j\ta\t\tv\t\t\ta"); B. System.out.println("j\na\n\nv\n\n\na"); C. System.out.println("j\t\na\t\t\nv\t\t\t\na"); D. System.out.println("j\n\ta\n\t\tv\n\t\t\ta");

the java.lang package

Which package is auto-imported when using Java?

the String data type has built in methods

Which reference/class data type has built in methods?

-because when you change a string variable's value, you are creating a new and separate reference for this new value as compared to the old value -basically, a new memory location is referenced without overwriting the original value -hence, the original value still exists, and therefore the string's original value wasn't changed, so strings are immutable

Why are strings immutable?

Encapsulation

the technique of controlling and protecting data of an object, and controlled access of data can be provided through public or private methods of an object

Immutable

unchanging over time/unable to be changed


संबंधित स्टडी सेट्स

[M1S1-POWERPOINT] Introduction to the Computing Industry

View Set

Med-Surg Chapters 56,57, & 58 GI

View Set

Chapter 5- Design of Goods and Services

View Set