Chapter 3 - In person Class Flashcards

1
Q

What does scan.nextline(); do? (differently than scan.nextInt).

A

It gobbles up the enter key you used for the inputted numbers.

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

How do you make sure your scan can take a string as an integer.

A

val1 = Integer.parseInt(scan.nextLine());

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

What is a null pointer?

A

It’s when the String title; has not been initialized yet. It points to nothing and we call it “null pointer”.

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

How do you create an object?

A

title = new String (“Java Software Solutions”);

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

What is the constructor?

A

The constructor is a special method that sets up the object (the name must match).

It is a special method that has the same name as the class.

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

How do you invoke a method?

A

count = title.length();

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

How do you access the methods of the scanner object scan?

A

You type a period after the name i.e.
scan.

String s; -> declares a variable s.
s = new String (“Hello World”); - -> assigns the string value.

We can do both by combining step 1 and 2.

String s = new String (“Hello World”);

s. (—–methods…
- —–method 2
- —–method 3
- —–method 4…)

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

What happens to the memory space and object that doesn’t have an ARROW pointing to it?

A

That object becomes an ORPHAN, and it needs to be taken out like GARBAGE.

Have controls/documentation in place to stop this from happening. Garbage will not modify your code.

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

String is the only object that allows you to create WITHOUT using NEW. (primitive types are not objects).

A

title = “Star Wars”;

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

What does it mean when we say a string is immutable?

A

A string is immutable because you cannot change it once it’s created. BUT you can create new string from it.

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

How do we replace the object with a new string?

A
String s = new String("Hello World");
s = s.replace( 'l' , 'p' );
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What do we assume about the following initialization?

String phrase = “Change is Inevitable”;

A

String phrase = new String(“Change is Inevitable”);

For Strings Java allows us to exclude new String();

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

Where is the method in this code?

String phrase = “Change is Inevitable”;
String mutation 1, mutation2, mutation3, mutation4;
mutation1 = phrase.concat (“,except from vending machines.”);

A

Right after the phase, where we say .concat();

.Concat is one of the many methods of the String Class.

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

Do the methods belong to the String Class or String Object?

A

The methods come from the String Object.

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

How can we compare two strings to see if they are equal?

Can we use if ( a1 == a2); ?

A

we use if (a1.equal(a2));

The == sign can only be used to compare two PRIMITIVE types. Everything else needs a method.

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

Is it better to use

import java.util.*; or the specific package class name?
import.java.util.Scanner;
A

It depends on if you’re going to use more than 3 classes. If it’s 3 or less then you should name each for documentation purposes. This will be helpful in case java makes an update to the util package and you need to know if you need to change any classes.

For GUI packages we often use * because we are often using more than 3 classes.

17
Q

What does it mean when the Math class has static methods?

A

Static methods can be invoked through the class name without using an object.

18
Q

There is no way to exponentiate in java, then how do we calculate 3^2?

A

We use the Math class that is a static class. So we would say Math.pow(3,2);

.pow stands for power.

19
Q

How can you create your own type?

A

You can create your own type by using ENUM.

enum Season (winter, spring, summer, fall);

Each Value has an ordinal value in addition to the actual value. There are methods for going back and forth between them.

time = Season.fall;

20
Q

Enumarated variables are a list of data types created outside the main method.

A

enum Flavor {vanilla. chocolate}

Flavor cone1, cone2, cone3;

cone1 = Flavor.rockyRoad
cone2 = Flavor.butterPecan

Flavor cone1, cone2, cone3;

21
Q

What are the methods of enum class?

A

It is cone1.ordinal() and cone2.name();

We are creating an enumarated data type called Flavor.

22
Q

What does the DecimalFormat df = new DecimalFormat (“0.0#”) do? Why can’t we have 0s after #s?

A

the second zero requires a zero to be displayed even if there is not number. The # sign requires a display only if the is a non-zero number.

23
Q

What are wrapper classes?

A

A wrapper class is not primitive data BUT AN OBJECT. It wraps an object around a primitive data so it’s STORED using a REFERENCE.

Its so you can treat everything can b treated in the same way IF THEY ARE ALL OBJECTS (consistency and continuinity).

z = x;
wrapper and int.
z = new Integer(x);
24
Q

HW help

A

public static void main(String[]args)

Integer.parseInt(null); => extracts an integer value from a string.

25
Q

Where is the integer methods for chapter 3.

A

It may be used in quiz.

26
Q

What is a conditional operator?

A

It’s an operator and not a statement. It has three operands.

? :
(t/f test ? true value : false value);
(pounds>1 ? “pounds” : “pound”);