Object Oriented Programming Uses Classes And Objects 633977
Object Oriented Programming Usesclassesandobjects What Are
Question 1: Object-oriented programming uses classes and objects. What are classes and what are objects? What is the relationship between classes and objects?
Question 2: Explain carefully what null means in Java, and why this special value is necessary.
Question 3: What is a constructor? What is the purpose of a constructor in a class?
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 "fruit = new Kumquat();"? What does the computer do when it executes this statement?
Question 5: What is meant by the terms instance variable and instance method?
Question 6: Explain what is meant by the terms subclass and superclass.
Question 7: Modify the following class so that the two instance variables are private and there are getter and setter methods for each:
public class Player {
String name;
int score;
}
Question 8: Explain why the class Player has an instance method named toString(), even though no definition of this method appears in the class definition.
Question 9: Explain the term polymorphism.
Question 10: Java uses "garbage collection" for memory management. Explain what is meant by garbage collection and what the alternative to garbage collection is.
Question 11: What is an abstract class, and how can you recognize an abstract class in Java?
Question 12: What is this?
Question 13: Write a complete class named Counter that counts from 0 upwards. It has a private instance variable for the count, and two methods: increment() to add one, and getValue() to return the current count.
public class Counter {
private int count;
public Counter() {
this.count = 0;
}
public void increment() {
this.count++;
}
public int getValue() {
return this.count;
}
}
Question 14: Using the Counter class, fill in the blanks to simulate tossing a coin 100 times, counting heads and tails:
Counter headCount, tailCount;
tailCount = new Counter();
headCount = new Counter();
for (int flip = 0; flip
if (Math.random()
// Count a "head"
______________________;
} else {
// Count a "tail"
______________________;
}
}
System.out.println("There were " + ______________________ + " heads.");
System.out.println("There were " + ______________________ + " tails.");
Question 15: Explain why it can never make sense to test "if (obj.equals(null))."