Unit 2: Using Objects Flashcards

1
Q

What is the keyword “new” ?

A

The “new” keyword is used to instantiate a new object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Proper String instantiation

A

String myString = new String(“Hello, World!”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Overloading

A
In Java, two or more methods may have the same name if they differ in parameters (different number of parameters, different types of parameters, or both). This is called overloading.
Ex. 
    public Rectangle(int rectWidth, int rectHeight)
    {
        width = rectWidth;
        height = rectHeight;
    }
    public Rectangle(int sidelength)
    {
        width = sidelength;
        height = sidelength;
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are parameters?

A

Data/arguments that are passed to a method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Wrapper Classes

A
Wrapper classes provide a way to use primitive data types (int, boolean, etc..) as objects.
ex.
int myInt = 10;
Integer myInteger = new Integer(myInt);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Integer.parse

A

This method is used to get the primitive data type of a certain String.
Ex. Integer.parseInt(“11”); // Returns the int value 11

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Generating random numbers

A

The Math.random() method returns a random double from 0-1 excluding 1;
int randInt = (int) (Math.random() * (max - min) + min;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is an instance method?

A

A piece of code called on a specific instance (an object) of the class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a void method?

A

A void method is a method that does not have a return value. Usually, these contain a print statement or modify the variables within the object in some way.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What defines a non-void method?

A

Non-void methods have a return statement. The method returns a certain data type and can be used as a variable of that data type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is an Integer? What is its purpose?

A

A wrapper class that helps turn a primitive into an object and object into a primitive data type.

It can be used in lists and such because they only store data as objects, so the conversion from primitive to object is needed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Static Methods

A

Belong to the class rather than an instance of a class, therefore can be called without using an object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Instantiation

A

To create a new instance of an object.
ex.
Student Ryan = new Student();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the different string methods?

A

name. concat(“value”) - returns a new string with “value” appended at the end of name
name. replace(“a”, “p”) - returns a new string with all instances of “a” in name replaced with “p”
name. toUpperCase()- Returns name as Upper Case Letters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Can you change a string once it is created?

A

No! Strings are immutable, so they cannot be changed once they are created.
(If you wanted to change the value of a string, you would have to re-assign it!)

However, you can concatenate strings with one another to create a new string value!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the different types of escape sequences?

A

" - allows for quotations in a string
\ - includes a backslash
\n - creates a line break
\t - adds a tab space

17
Q

.substring() String method.

A
Returns a piece of a String to a specified index. ex.
String myString = new String("Hello World");
System.out.println(myString.substring(6); // Returns "World"
System.out.println(myString.substring(0,3); // Returns "He". The second index is exclusive.