6.1 Objects and Classes & 6.2 Writing a Simple Class, Step by Step
instance
Each object that is created from a class
Declare a reference variable of type File named myFile.
File myFile;
Declare a variable named myMenu suitable for holding references to Menu objects.
Menu myMenu;
A string literal, such as "Janice", causes a(n) _____ object to be created.
String
methods
The operations that an object can perform
UML
Unified Modeling Language: It provides a set of standard diagrams for graphically depicting object-oriented systems.
private
When the private access specifier is applied to a class member, the member cannot be accessed by code outside the class. The member can be accessed only by methods that are members of the same class.
public
When the public access specifier is applied to a class member, the member can be accessed by code inside the class or outside.
accessor method
a method that accesses an object but does not change it, aka "getters"
mutator method
a method that changes the state of an object, aka "setters"
A reference variable holds _____.
an object's memory address
Study the UML diagram below and then answer the question: CAR -MAKE -YEARMODEL +SETMAKE() +SETYEARMODEL() +GETMAKE() +GETYEARMODEL() What is the name of the class in this UML diagram?
car
A housing construction consultant describes the process of building a new house as follows: "A blueprint is a design for the house. A carpenter can use the blueprint to build the house. If the carpenter wishes, he or she can build several identical houses from the same blueprint." If this is applied as a metaphor for object-oriented programming, then the blueprint represents a(n) _____.
class
What term refers to a collection of programming statements that specify the fields and methods that a particular type of object may have?
class
True or False: Any objects used in a Java program must be created from classes in the Java API.
false
Truth or Dare: When a variable is said to reference an object, a copy of the object is stored in the variable.
false
A class member that holds data is known as a(n) _____.
field
Study the UML diagram below and then answer the question: CAR -MAKE -YEARMODEL +SETMAKE() +SETYEARMODEL() +GETMAKE() +GETYEARMODEL() How many methods are in the class represented by this UML diagram?
four
Study the UML diagram below and then answer the question: CAR -MAKE -YEARMODEL +SETMAKE() +SETYEARMODEL() +GETMAKE() +GETYEARMODEL() How many public members are in the class represented by this UML diagram?
four
An object can best be described as a(n) _____.
instance of a class
object
is a software component that exists in memory and serves a specific purpose in a program.
Data hiding
is an important concept in object-oriented programming. An object hides its internal data from code that is outside the class that the object is an instance of. Only the class's methods may directly access and make changes to the object's internal data.
class
is code that describes a particular type of object.
The Java operator _____ creates an object in memory, and returns that object's memory address?
new
Assume there is a class AirConditioner that supports the following behaviors: turning the air conditioner on and off. The following methods provide these behaviors: turnOn and turnOff. Both methods accept no arguments and return no value. Assume there is a reference variable officeAC of type AirConditioner. Create a new object of type AirConditioner and save the reference in officeAC. After that, turn the air conditioner on using the reference to the new object.
officeAC = new AirConditioner(); officeAC.turnOn(); or officeAC = new AirConditioner(); officeAC.turnOff(); officeAC.turnOn();
An object's methods _____.
perform operations
Assume that pickup is a variable that references an instance of the class shown below. Write a statement that calls setMake and passes the argument "Ford". Car -make yearModel +setMake() +setYearModel() +getMake() +getYearModel()
pickup.setMake("Ford");
Write the definition of a class Clock. The class has no constructors and two instance variables. One is of type int called hours and the other is of type boolean called isTicking.
public class Clock { private int hours; private boolean isTicking; }
Write the definition of a class Clock. The class has no constructors and one instance variable of type int called hours.
public class Clock { private int hours; }
Write the definition of a class PlayListEntry containing: An instance variable title of type String, initialized to the empty String. An instance variable artist of type String, initialized to the empty String. An instance variable playCount of type int, initialized to 0. In addition, your PlayList class definition should provide an appropriately named "get" method and "set" method for each of these.No constructor need be defined.
public class PlayListEntry { private String title = ""; private String artist = ""; private int playCount = 0; public void setTitle(String title) { this.title = title; } public void setArtist(String artist) { this.artist = artist; } public void setPlayCount(int playCount) { this.playCount = playCount; } public String getTitle() { return title; } public String getArtist() { return artist; } public int getPlayCount() { return playCount; } }
Write the definition of a class Player containing: An instance variable name of type String, initialized to the empty String. An instance variable score of type int, initialized to zero. A method called setName that has one parameter, whose value it assigns to the instance variable name. A method called setScore that has one parameter, whose value it assigns to the instance variable score. A method called getName that has no parameters and that returns the value of the instance variable name. A method called getScore that has no parameters and that returns the value of the instance variable score. No constructor need be defined.
public class Player { private String name = ""; private int score = 0; public void setName(String name) { this.name = name; } public void setScore(int score) { this.score = score; } public String getName() { return name; } public int getScore() { return score; } }
Write the definition of a class Simple. The class has no constructors, methods or instance variables.
public class Simple {}
Suppose the value of an item is dependent on other data and that item is not updated when the other data is changed. In programming terminology, we would say that this item has become _____.
stale
Study the UML diagram below and then answer the question: CAR -MAKE -YEARMODEL +SETMAKE() +SETYEARMODEL() +GETMAKE() +GETYEARMODEL() How many fields are in the class represented in this UML diagram?
two
Study the UML diagram below and then answer the question: CAR -MAKE -YEARMODEL +SETMAKE() +SETYEARMODEL() +GETMAKE() +GETYEARMODEL() How many private members are in the class represented by this UML diagram?
two