Final Gaddis Chap 3 Flashcards

1
Q

package

A

a group of related classes

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

the classes in the Java API are organized into

A

packages

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

the Scanner class is in…

A

the java.util package

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

Before they can be used, many API classes must be

A

imported

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

import the Scanner class

A

import java.util.Scanner;

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

explicit import statements

A
specify a single class
ex: 
import java.util.Scanner;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

wildcard import statements

A

imports all of the classes in a package
ex:
import java.util.*;

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

the java.lang package

A
  • automatically imported into every Java program
  • contains general classes such as String and System
  • does not need an import statement (automatically imported)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Write a call to get an integer from the keyboard and store it in an integer variable num

A

Scanner scan=new Scanner (System.in);

num=scan.nextint();

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

finding the classes

A

1) get written description of the problem domain
2) identify all nouns, each is a potential class
3) redefine list to include only classes relevant to the problem

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

identify the responsibilities

A

1) things a class is responsible for knowing
2) things a class is responsible for doing
3) refine list to include only classes relevant to the problem

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

encapsulation

A

enclosing the proper attributes and methods inside a single class. All that is necessary for the class to operate

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

encapsulation ensures that

A

the class is self-contained

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

data hiding

A

an aspect of encapsulation that uses the private access modifier on fields to hide them from other classes with the idea that classes should not only be self-contained but also self-governing. Methods are needed to allow access and modification of the class’ data

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

Decisions that must be made when designing a class: (5)

A

1) what data must be accounted for
2) what actions need to be performed
3) what data can be modified
4) what data needs to be accessible
5) any rules as to how data should be modified

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

Class design is typically done with the aid of

A

a Unified Modeling Language (UML) diagram

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

UML class diagram

A

a graphical tool that can aid in the design of a class

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

3 main sections for UML diagrams

A

class name, attributes, methods

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

the data elements of a class

A

define the object to be instantiated from the class

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

Attributes must be

A
specific to the class and define it completely
ex:
a rectangle is defined by: length and width
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Attributes are then

A

accessed by methods in the class

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

instance

A

kind of like each use. For example, each instance of the Rectangle class shares the same design, but each instance contains different data.

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

Each instance has

A

all of the attributes and methods that were defined in the class

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

Classes are defined to represent

A

a single concept or service

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
A class can be thought of as
a blueprint that objects are created from
26
Constructors
special methods that are used to perform operations at the time an object is created. Constructors typically initialize instance fields and perform other object initialization tasks.
27
If a constructor is not defined
Java provides a default constructor with no parameters
28
Java's default constructor
sets all the class' numeric fields to 0, boolean fields to false, reference variables to null
29
default constructors are used to
initialize an object in a default configuration
30
Objects and classes
``` many objects can be created from a class. Each object is independent of the others ex: Rectangle r1=new Rectangle(); Rectangle yellowRectangle=new Rectangle(); Rectangle hersheyBar=new Rectangle(); ```
31
each object is
an address
32
The class' methods define
the actions that an instance of the class can perform
33
The attributes of a class might need to be
changed, accessed, and calculated
34
the methods that change and access attributes are called
accessors and mutators
35
Because of the concept of data hiding,
fields in a class are private
36
accessors
the methods that retrieve the data of fields
37
mutators
the methods that modify the data of fields
38
An accessor is needed when
the programmer wishes for a field to be viewed by other classes
39
A mutator is needed when
the programmer wishes for a field to be modified
40
accessors and mutators are also called
getters and setters
41
an example of getters and setters
public void setLength (double len) | puble double getLength ()
42
access modifier
a java key word that indicates how a field or method can be accessed
43
3 Java access modifiers
public, private, protected
44
public
states that any other class can access the resource
45
private
indicates that only data within this class can access the resource
46
protected
indicates that only classes in the current package or a class lower in the class hierarchy can access this resource
47
classes that need to be used by other classes are typically made
public
48
if there is more than one class in a file,
only one can be public and it must match the file name
49
typically, the layout of a source code file is
1) attributes 2) methods - the main method is sometimes 1st, sometimes last - accessors and mutators are typically grouped
50
exmple of layout
``` public class Rectangle { // instance variables //constructor(s) – optional** // getters & setters // toString – optional** ``` ``` //** but HIGHLY suggested } ```
51
method header format:
accessModifier ReturnType MethodName (Parameters)
52
methods that need to be used by other classes should be made
public
53
example
``` public class Rectangle { private double width; private double length; ``` ``` public void setWidth (double w) { width = w; } public void setLength (double len) { length = len; } public double getWidth () { return width; } public double getLength () { return length; } ``` }
54
application
a collection of classes that interact
55
the class that starts the application must have
a main method
56
The class that starts the application can be used as
a driver to test the capabilities of other classes
57
So, you only need a main method when
a program has multiple classes (is an application)
58
Demos
classes that use other classes
59
stale data
data that is the result of calculation of various factors. If you change one of the factors, the overall calculation does not change, making the number stale. ex: length * width=area If length changes, area doesn't. So it is inaccurate and stale
60
to avoid stale data
calculate the value of that data within a method rather than storing it in a variable
61
Application: how to avoid stale data:
public double getArea() { return length*width; } This dynamically calculates the value of the rectangle's area when the method is called, so any change to the length or width variables will not leave the area of the rectangle stale
62
Write a program that sets width and length and returns width length and area
``` public class Rectangle { private double width; private double length; ``` ``` public void setWidth(double w) { width = w; } public void setLength(double len) { length = len; } public double getWidth() { return width; } public double getLength() { return length; } public double getArea() { return length * width; } ``` }