Computer Science CSC 162-02

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What is the difference between makeCopy and getCopy?

- To use makeCopy, BOTH objects (the object whose data is being copied and the object that is copying the data) must be instantiated before invoking this method. - To use getCopy, the object whose data is being copied must be instantiated before invoking this method, while the object of the reference variable receiving a copy of the data need not be instantiated.

Client

A program that manipulates the objects of a class.

Constructor

A special type of method that automatically executes when an object is created or instantiated.

A ________ is a collection of a fixed number of components.

Class

The method toString() is a public member of the class ____.

Object

Each class is associated with how many packages.

One package.

Members of a class are accessed by ___________.

Their name

A member of a class can be a _________________ or a _________________.

Variable, method

Once a class is defined, what do we do?

We can declare reference variables of that class type.

What are the largest structures in Java? a) Packages b) Objects c) Strings d) Classes

a) Packages

The ____________ members of a class are NOT accessible outside the class.

Private

The ____________ members of a class are declared using the modifier private.

Private

The ____________ members of a class are accessible outside the class.

Public

The ____________ members of a class are declared using the modifier public.

Public

Members of a class are classified into 1 of 4 categories: ___________________, ___________________, ___________________, ___________________.

Public, Private, Protected, and Default

What are the four modifiers in Java?

Public, private, default, and protected.

What can you do to remove a reference?

Set the reference to null.

Protected scope

Similar to default scope, but child classes (subclasses) can access protected variables from any package.

Dot operator / member access operator

The built-in operation that is valid for classes.

If a class has more than one constructor, what happens?

The constructors have a different number of formal parameters.

Encapsulation

The first principle of Object-Oriented programming, where it combines the data and the operations on that data into a single unit.

What do the plus sign (+), the minus sign (-), and the hashtag (#) represent in a UML diagram.

The plus sign represents that it's a public member. The minus sign represents that it's a private member. The hashtag represents that it's protected member.

What does the top box, middle box, and bottom box in a UML (Unified Modelling Language) represent.

The top box in a UML diagram represents the class name. The middle box represents the data members and their data types. And the bottom box represents the method names, parameter lists, and return types.

To run a program: a) Create a directory folder under your current working ___________________ & name it ______________________. b) Compile the source file & put _____________ file into the directory you created. c) Execute the program from the _______________ directory, which is the directory where the _____________ is installed.

To run a program: a) Development directory, same name as the package statement. b) .class c) development, JDK

True or False? If a member of a class is a method, it can directly access any member of a class.

True

True or False? If the object is created in the definition of a method of the class, then the object can access both the public and private members of the class.

True

True or False? ADTs hide the implementation details of operations from users.

True.

How do you create a class object?

Using the "new" operator.

How do you declare a package?

Using the package keyword, and then using the directory name, the dot operator, the subdirectory name, the dot operator, and the package name. Example: package directory.subdirectory.packageName;

Valid or Invalid? class ExampleClass {...}

Valid.

Valid or Invalid? package TestDemo; class TestBookDemo{...} // In another file: package testDemo classBook{...}

Valid.

Deep copy

Where each reference variable refers to its own object.

Shallow copy

Where two or more reference variables point to the same object.

Suppose that we have created the class Mystery. Write the Java statement that would put this class into the package strangeClasses in the subdirectory CSC162 of the directory computerScience.

package computerScience.CSC162.strangeClasses;

Given the constructor: public CC(int a, int b, double d) {...} Write the definition of the constructor so that the instance variables u, v, and w are initialized according the values of parameters a, b, and d, respectively.

public CC(int a, int b, double d) { a = u; b = v; d = w; }

What two user-defined methods are used to avoid shallow copying?

makeCopy and getCopy.

The phrase "create an object of a class type" means:

* Declare a reference variable of the class type. * Instantiate the class object. * Store the address of the object into the reference variable declared.

Packages are used in Java to avoid:

1. Name conflicts. 2. Control access of classes and interfaces.

Static Class

A class that cannot be instantiated.

Package

A collection of related classes.

Abstract Data Type (ADT)

A data type that specifies the logical properties without implementation details.

Non-Static Data Field

A field that is accessed inside an object.

Static Data Field

A field that is accessed outside a class.

Accessor method

A method that accesses the value of an instance variable.

Finalize( )

A method that automatically executes when a class object goes out of scope.

Mutator method

A method that changes the value of an instance variable.

toString()

A method that converts an object into a string object.

Static Method

A method that doesn't need an object to be invoked.

Garbage Collection

A process that deallocates the memory when an object doesn't have a reference to it.

This

A reference that is used to refer the currently running object.

Copy constructor

A special constructor that executes when an object is instantiated and initialized using an existing object.

What is the difference between a standard data type and an abstract data type.

A standard data type has a set of values and a set of operations, and represents the data. An abstract data type has a set of values and operations but doesn't do data representation.

Precondition statement

A statement specifying the conditions that must be true before the method is created.

Postcondition statement

A statement specifying what is true after the method is completed.

Public scope

Accessible from anywhere

Default scope

Accessible from the class or any class in the same package.

Private scope

Accessible only from within the class

Consider the following two classes: public class Cat { private String name; private Breed breed; private double weight; public Cat(String name, Breed breed, double weight) { this.name = name; this.breed = breed; this.weight = weight; } public Breed getBreed( ) { return breed; } public double getWeight( ) { return weight; } } public class Breed { private String name; private double averageWgt; public Breed(String name, double averageWgt) { this.name = name; this.averageWgt = averageWgt; } public double getWeight( ) { return averageWgt; } } Identify each line as being either a valid or invalid statement and TELL WHY it is invalid in the following main( ). public class Q1Main { public static void main(String[] args) { Breed persian = new Breed("Persian", 10.0); Cat chacha = new Cat("Cha Cha", persian, 12.0); Cat bombom = new Cat("Bom Bom", "mix", 10.0); Cat puffpuff = new Cat("Puff Puff", chacha, 9.0); double diff = chacha.getWeight( ) - persian. getWeight( ); System.out.println(puffpuff.getBreed( ).getWeight( )); } }

Breed persian = new Breed("Persian", 10.0); - Valid Cat chacha = new Cat("Cha Cha", persian, 12.0); - Valid Cat bombom = new Cat("Bom Bom", "mix", 10.0); - Invalid, "mix" is a String, not a breed. Cat puffpuff = new Cat("Puff Puff", chacha, 9.0); - Invalid, chacha is a cat, not a breed. double diff = chacha.getWeight( ) - persian. getWeight( ); - Valid System.out.println(puffpuff.getBreed( ).getWeight( )); - Valid

Inner classes

Classes that are defined within other classes.

Top-level classes

Classes that belong to a package.

Package is a group of related _________________, _________________, _________________, _________________.

Classes, enumerations, subpackages, interfaces.

Write the Java statement that creates the object mysteryClock of the Clock type, and initializes the instance variables hr, min, and sec of mysteryClock to 7, 18, and 39, respectively.

Clock mysteryClock = new Clock(7, 18, 39);

In Java, class is a reserved word, and it defines only a _____________; NO memory is allocated.

Data type.

What are the two kinds of constructors?

Default (without parameters), and one with parameters.

What is the first principle of Object-Oriented programming where it combines the data with the operations on that data?

Encapsulation.

True or False? Constructors have a type.

False, constructors have no type. They are neither value-returning nor void.

True or False? The default definition of the method toString is it creates a string that is the name of the package, followed by the name of the class, followed by the hash code of the object.

False. The default definition of the method toString is it creates a string that is the name of the class, followed by the hash code of the object.

True or False? In Java, the reference this is used to refer to only to the instance variables, not the methods of a class.

False. The reference this is used to refer to the instance variables and instance methods of a class.

True or False? We can use the relational operator == to determine whether two different objects of the same class contain the same data.

False. We use the equals( ) method.

True or False? If a member of a class is a method, it can directly access some members of the class without passing the data member as a parameter to the method.

False. If a member of a class is a method, it can directly access ALL MEMBERS of the class.

Given the following: package myPack; public class Employee{...} - Java uses a _________________ to store the package. - For all classes defined to be part of myPack package, the ________ files must be stored in a directory called ____________.

File system directory, .class, myPack

If any member of a class is a(n) _________________________, it is declared like any other variable.

Instance variable

Valid or Invalid? class ExampleClass{...} package example;

Invalid. Package should be in the first line.

Valid or Invalid? import java.util.Scanner; package example; class ExampleClass {...}

Invalid. The package must be at the first line.

Valid or Invalid? package example; import java.util.Date; class ExampleClass {...} import java.util.Collections;

Invalid. The second import must be before the class.

Valid or Invalid? package abc; package def; class ExampleClass{...}

Invalid. Two packages.

Valid or Invalid? package myPack; class Book{...} package testDemo; class TestBookDemo{...} // all saved in TestBookDemo.java

Invalid. Two separate packages in one file.

What happens if we do not include any constructor in a class?

Java will automatically provide the default constructor.

What happens if we provide at least one constructor and do not include the default constructor?

Java will not automatically provide the default constructor.

Components of a class are called the ________ of the class.

Members

Instance variables

Non-static data members of a class.

Instance method

Non-static method of a class.


Set pelajaran terkait

Business Strategy Chapter 2: Charting a Company's Direction: Its vision, mission, objectives, and strategy

View Set

Chapter 37: Nursing Management: Patients With Immunodeficiency, HIV Infection, and AIDS

View Set

The 3-1: Obstructive Pulmonary Diseases

View Set

Network+ Questions that i got wrong

View Set

Maria Sharapova Unstoppable: My Life So Far

View Set

United States history Exam 1 review

View Set

econ ch 17 markets for labor and other factors

View Set