Computer Science, asked by pradu94, 5 months ago

8. The values of data members or member variables at any given point of time determine an object's​

Answers

Answered by Anonymous
1

Answer:

THIS PAGE CONTAINS sample answers to the quiz on Chapter 5 of Introduction to Programming Using Java. Note that generally, there are lots of correct answers to a given question.

Question 1:

Object-oriented programming uses classes and objects. What are classes and what are objects? What is the relationship between classes and objects?

Answer:

When used in object-oriented programming, a class is a factory for creating objects. (We are talking here about the non-static part of the class.) An object is a collection of data and behaviors that represent some entity (real or abstract). A class defines the structure and behaviors of all entities of a given type. An object is one particular of that type of entity. For example, if Dog is a class, then a particular dog named Lassie would be an object of type Dog.

Question 2:

Explain carefully what null means in Java, and why this special value is necessary.

Answer:

When a variable is of object type (that is, declared with a class or interface as its type rather than one of Java's primitive types), the value stored in the variable is not an object. Objects exist in a part of memory called the heap, and the variable holds a pointer or reference to the object. Null is a special value that can be stored in a variable to indicate that it does not actually point to any object.

Question 3:

What is a constructor? What is the purpose of a constructor in a class?

Answer:

A constructor is a special kind of subroutine in a class. It has the same name as the name of the class, and it has no return type, not even void. A constructor is called with the new operator in order to create a new object. Its main purpose is to initialize the newly created object, but in fact, it can do anything that the programmer wants it to do.

Question 4:

Suppose that Kumquat is the name of a class and that fruit is a variable of type Kumquat. What is the meaning of the statement That is, what does the computer do when it executes this statement? (Try to give a complete answer. The computer does several things.)

Answer:

This statement creates a new object belonging to the class Kumquat, and it stores a reference to that object in the variable fruit. More specifically, when the computer executes this statement, it allocates memory to hold a new object of type Kumquat. It calls a constructor, which can initialize the instance variables of the object as well as perform other tasks. A reference to the new object is returned as the value of the expression . Finally, the assignment statement stores the reference in the variable, fruit. So, fruit can now be used to access the new object.

Question 5:

What is meant by the terms instance variable and instance method?

Answer:

Instance variables and instance methods are non-static variables and methods in a class; that is, their definitions in the class are not marked with the modifier. This means that they do not belong to the class itself. Instead, they specify what variables and methods are in an object that belongs to that class. That is, the class contains the source code that defines instance variables and instance methods, but actual instance variables and instance methods are contained in objects, at least logically. (Such objects are called of the class.) Thus, instance variables and instance methods are the data and the behaviors of objects.

Question 6:

Explain what is meant by the terms subclass and superclass.

Answer:

In object oriented programming, one class can inherit all the properties and behaviors from another class. It can then add to and modify what it inherits. The class that inherits is called a subclass, and the class that it inherits from is said to be its superclass. In Java, the fact that ClassA is a subclass of ClassB is indicated in the definition of ClassA as follows:

class ClassA extends ClassB {...}

Question 7:

Modify the following class so that the two instance variables are private and there is a getter method and a setter method for each instance variable:

public class Player {

String name;

int score;

}

Answer:

To make a variable private, just add the word in front of each declaration. We need two methods for each variable. One of them returns the value of the variable. The other provides a new value for the variable. The names for these methods should follow the usual naming convention for getter and setter methods. (Note that my setter methods use the special variable this so that I can use the same name for the parameter of the method as is used for the instance variable.

Similar questions
Math, 9 months ago