APCSA Chapter Two

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Give Java code for a string with the contents "Hello, Dave"

"Hello, Dave!"

Name two accessor methods and two mutator methods of the Rectangle class.

Accessor: getWidth(), getHeight() Mutator: translate(int, int), add(int, int)

Explain the difference between an object and an object variable.

An object contains information. An object variable contains an object reference (the location of an object).

Explain the difference between an object and an object reference.

An object contains state information. An object reference is the location of that object in memory.

Explain the difference between an object and a class.

An object is an instance (an entity) that defined by a class. A class provides a definition which includes characteristics (data) and behavior (methods).

Explain the difference between the = symbol in Java and in mathematics.

In Java it means "gets" or "assigned", in math it means "equals".

Write a Java statement to initialize a variable square with a rectangle object whose top-left corner is (10, 20) and whose sides all have length 40. Then write a statement that replaces square with a rectangle of the same size and top-left corner (20, 20).

Rectangle square = new Rectangle(10, 20, 40, 40); square = new Rectangle(10, 20, 40, 40);

Declare a variable object for a rectangle with center (100, 100) and all side lengths equal to 50

Rectangle square = new Rectangle(75, 75, 50, 50);

Write Java statements that initialize two variables square1 and square2 to refer to the same square with center (20, 20) and side length 40.

Rectangle square1 = new Rectangle(20, 20, 40, 40); Rectangle square2 = square1;

Find the error: double width = Rectangle(5, 10, 15, 20).getWidth();

Rectangle(5, 10, 15, 20) does not refer to an object. The corrected version should be: double width = (new Rectangle(5, 10, 15, 20)).getWidth();

Declare a variable object for a string with the contents "Hello, Dave"

String greeting = "Hello, Dave!";

Write Java statements that initialize a string message with "Hello" and then change it to "hello". Use the replace method.

String message = "Hello"; message = message.replace("H", "h");

Write Java statements that initialize a string message with "Hello" and then change it to "HELLO". Use the toUpperCase method.

String message = "Hello"; message = message.toUpperCase();

Write Java statements that initialize a string message with a message such as "Hello, World" and then remove punctuation characters from the message, using repeated calls to the replace method.

String message = "Hello, World!"; message = message.replace(",", ""); message = message.replace("!", "");

Find the error: r = new Rectangle(); r.translate("far, far away!");

The method translate takes two integer arguments, not a string argument.

What is the public interface of a class? How does it differ from the implementation of a class?

The public interface contains all the methods we can apply to any of its objects. The implementation is how the methods accomplish their tasks.

What is the value of mystery after this sequence of statements? int mystery = 1; mystery = mystery + 1; int mystery = 1 - 2 * mystery;

The value of mystery is set to -3.

What is the value of mystery after this sequence of statements? int mystery = 1; mystery = 1 - 2 * mystery; mystery = mystery + 1;

The value of mystery is set to 0.

Declare and initialize variables for holding the price and the description of an article that is available for sale.

double price = 5.50; String description = "Dress Socks";

Give the Java code for constructing an object of class Rectangle, and for declaring an object variable of class Rectangle.

new Rectangle(5, 10, 20, 30); // Object Rectangle box; // Object variable

Give Java code for a rectangle with center (100, 100) and all side lengths equal to 50

new Rectangle(75, 75, 50, 50)

Find the error: Rectangle r = (5, 10, 15, 20);

newRectangle is missing

Find the error: Rectangle r; r.translate(15, 25);

r has not been initialized; it does not refer to any object.

Give an example of a method that has an argument of type int. Give an example of a method that has a return value of type int. Repeat for the type String.

title = "Big Java"; char letter = title.chatAt(0); // letter would be 'B' int titleLength = title.length(); // titleLength would be 8


Kaugnay na mga set ng pag-aaral

Lesson 8 - Chapter 11 (Part A: Nervous System and Tissues)

View Set

Humanities 1: Discovering the Humanities- Chapters 4, 5, 6, 7, 8, 11, & 12

View Set

Safety/Infection Control - Saunders Quiz 3

View Set

Mastering Astronomy, Astronomy 2, Chapter 2

View Set

Brain and Behavior Exam 1: Chapters 1-3

View Set

Chapter 4: Genes and Their Evolution

View Set

Titanic-Voices from the Disaster Boat Vocabulary

View Set