Final Gaddis Chap 3 Flashcards

You may prefer our related Brainscape-certified 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
Q

A class can be thought of as

A

a blueprint that objects are created from

26
Q

Constructors

A

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
Q

If a constructor is not defined

A

Java provides a default constructor with no parameters

28
Q

Java’s default constructor

A

sets all the class’ numeric fields to 0, boolean fields to false, reference variables to null

29
Q

default constructors are used to

A

initialize an object in a default configuration

30
Q

Objects and classes

A
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
Q

each object is

A

an address

32
Q

The class’ methods define

A

the actions that an instance of the class can perform

33
Q

The attributes of a class might need to be

A

changed, accessed, and calculated

34
Q

the methods that change and access attributes are called

A

accessors and mutators

35
Q

Because of the concept of data hiding,

A

fields in a class are private

36
Q

accessors

A

the methods that retrieve the data of fields

37
Q

mutators

A

the methods that modify the data of fields

38
Q

An accessor is needed when

A

the programmer wishes for a field to be viewed by other classes

39
Q

A mutator is needed when

A

the programmer wishes for a field to be modified

40
Q

accessors and mutators are also called

A

getters and setters

41
Q

an example of getters and setters

A

public void setLength (double len)

puble double getLength ()

42
Q

access modifier

A

a java key word that indicates how a field or method can be accessed

43
Q

3 Java access modifiers

A

public, private, protected

44
Q

public

A

states that any other class can access the resource

45
Q

private

A

indicates that only data within this class can access the resource

46
Q

protected

A

indicates that only classes in the current package or a class lower in the class hierarchy can access this resource

47
Q

classes that need to be used by other classes are typically made

A

public

48
Q

if there is more than one class in a file,

A

only one can be public and it must match the file name

49
Q

typically, the layout of a source code file is

A

1) attributes
2) methods
- the main method is sometimes 1st, sometimes last
- accessors and mutators are typically grouped

50
Q

exmple of layout

A
public class Rectangle
{
// instance variables
	//constructor(s) – optional**
 // getters & setters
 // toString – optional**
//** but HIGHLY suggested
}
51
Q

method header format:

A

accessModifier ReturnType MethodName (Parameters)

52
Q

methods that need to be used by other classes should be made

A

public

53
Q

example

A
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
Q

application

A

a collection of classes that interact

55
Q

the class that starts the application must have

A

a main method

56
Q

The class that starts the application can be used as

A

a driver to test the capabilities of other classes

57
Q

So, you only need a main method when

A

a program has multiple classes (is an application)

58
Q

Demos

A

classes that use other classes

59
Q

stale data

A

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
Q

to avoid stale data

A

calculate the value of that data within a method rather than storing it in a variable

61
Q

Application: how to avoid stale data:

A

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
Q

Write a program that sets width and length and returns width length and area

A
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;
	}
}