Chapter 3 - Using Classes and Objects Flashcards

1
Q

What are the chapter objectives?

A

1- Discuss the creation of objects and the use of object reference variables.
2- Explore the services provided by the String class.
3- Explore the services provided by the Random and Math classes.
4- Discuss ways to format output.
5- Introduce enumerated types.
6- Discuss wrapper classes and the concept of autoboxing.
7- Introduce the JavaFX API.
8- Explore classes used to represent shapes.

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

What is a fundamental part of object-oriented software and sets the stage for writing classes of our own?

A

Knowing how to use classes (+predefined) and objects (we created from them) for the services they provide. Using classes and objects to manipulate character strings, produce random numbers, perform complex calculations, and format output.

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

What’s the difference between a primitive variable and an object variable?

A

An object variable does not hold an object itself but it holds the address of an object. The String variable holds a reference to a String object.

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

Why is it important to always initialize a variable (assign a value) before using it?

A

Because if you don’t then JAVA will assign a default variable.

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

What do you call it when two names (object variables) refer to the same object? Can multiple reference variables refer to the same object?

A

With object variables they reference the same object and are called aliases. Integer variables take on the object and not reference it so a num2 = num1 keeps two separate objects while name2 = name1 removes the reference and references to the object originally referenced by name 1. If we change the referenced object for name1, it will also change the output for name2. Yes.

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

What happens when an object loses all references to it by variables (perhaps by reassignment)?

A

That object can no longer contribute to the program and the program can no longer invoke its methods or use its variables. At this out the object is called GARBAGE b/c it serves no useful purpose.

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

What does automatic garbage collection mean?

A

After an object loses all references to it and becomes garbage, Java performs automatic garbage collection and removes those objects and returns their memory to the stream for future use.

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

What does it mean when we say a string object (once created) is immutable?

A

Once a String object is created it’s value cannot be lengthened or shortened, nor can any of its characters change. However there are several Methods in the string Class return a NEW string Object that are the result of modifying the original string’s value.

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

What is a Java API?

A

It’s a Java Application Programming Interfaces (API)s. It’s a standard class library, a set of classes (each contains methods) programmers like to use. Although programmers think classes are part of the language but are not technically part of the language.

ITS IMPORTANT TO KNOW HOW TO GET MORE INFORMATION about Java API classes (this is where you use the online documentation).

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

How are java files shared?
How do you create these .jar files?
We are the high priests of technology.

A

You have to create a java archive file, that zips multiple java CLASS files. Most programs will have multiple java files in a .jar file.

The files you share are compiler files javac.exe and interpreter files java.exe

Class is the executable file (the interpreter reads). The compiler takes the .java file and converts it to .class file using the compiler. .jar is a collection of .class files. The .jar file goes through the interpreter java.exe to program execution.

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

Why should you be focusing on the principles of JAVA?

What are procedural vs. object vs. declarative vs. functional languages?

A

Because you might use a different object oriented language.

procedural language is python (or C), object is Java, declarative are AIs, functional languages are math related.

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

What is a JVM (JRE)? What is the point of creating this virtual machine?

A

It is a Java Virtual Environment (JRE - Java Runtime Environment). You have a consistent sharable file but a little slower. The tradeoff is portability.

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

What do you call the groups of classes from the Java standard class library?

A

Packages (each class is a part of a particular package).

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

What package is the string class, system class, and Scanner class part of?

A

String and system class are part of the java.lang package. Scanner is part of the java.util package.

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

Why do we use an import declaration?

A

It’s to save you from having to define the package each time you use the scanner. Instead you can import it once in the beginning.

For example we could say java.util.Scanner each time we want to use the Scanner class. ON THE OTHER HAND if we just say import java.util.Scanner ; we can then use Scanner after that.

import java.util.Scanner ;

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

What do you do in the rare circumstances where you have two classes with the same name from different packages?

A

You will need to FULLY QUALIFIED NAME with each.

17
Q

How do you indicate any class from the whole package can be used in the program instead of just the one you specified?

A

The way to do is use a period followed by an asterisk (*) to indicate any class inside the package may be used in the program. Here is an example:

import java.util.* ;

18
Q

Why don’t we have to import the java.lang. class?

A

java.lang. class that houses System and String classes are fundamental to the Java RTE and therefore are imported automatically. It’s as if each program starts with an invisible import of:

import java.lang.* ;

19
Q

Where do you need to use random numbers? What class can you use in java, and what are some applications of it in the real world?

A

The need for random numbers occurs frequently when writing software. Games often use a random number to represent the roll of a die or the shuffle of a deck of cards. A flight simulator may use it to determine the weather and a study package may use it to select the questions a student receives on a randomized test.

Random class is part of the java.util class. It represents the pseudorandom number generator. It’s pseudorandom because the number generator performs a series of complicated calculations (figure 3.4 lists some of the methods).

20
Q

What does the MATH (math) class do?

A

The math class provides a large number of BASIC mathematical FUNCTIONS. All the methods of the Math class are STATIC METHODS (class methods) and RETURNS values.

21
Q

What is a static method (CLASS METHOD)?

A

Static methods can be invoked through the name of the class in which they are defined. They are invoked through the class name.

22
Q

What are some classes we can use to format information so it looks appropriate when printed or displayed?

A

The NumberFormat class and the DecimalFormat class are part of the Java standard class library defined in the java.text package.

23
Q

What does the printf method from the System class do?

A

It prints using a specific format (f). It’s used to transform C programming (legacy systems).

It’s not used in the book because it’s not considered clean object-oriented solution to the problem of formatting output, so we avoid its use in this book.

24
Q

What is a type-safe type?

A

Enumerated type is a type-safe that ensures only the values listed, when the type is created, are used. For example:

enum Season {winter, spring, summer, fall}

25
Q

What does a WRAPPER class do? Is there a wrapper class in the Java class library for each primitive type in Java? What does the AUTOboxing do?

A

A wrapper class allows a primitive value to be managed as an object. Yes, like MAX_VALUE and MIN_VALUE.

Autoboxing provides automatic conversions between primitive values and corresponding wrapper objects.