Defining Your Own Classes—Part 2 Solutions to Quick Check ...
Recommend Documents
1. Java Programming Basics. 2. Solutions to. Quick Check Questions. 2.1. The
First Java Program. 1. Which of the following are invalid identifiers? a. one b.
Check your grammar: multiple choice – conjunctions. Circle the best word to
complete these sentences. 1. I'd love to stay so / and / but I have to catch my bus.
2.
[3] European Space Agency, GlobCover Project, available online: http://due.esrin.esa.int/page_globcover.php (accessed on 18 April 2016). [3] European Space ...
WRITE YOUR OWN EROTIC SCRIPT. Use this diagram to read your sexy story
into our system. Begin reading your message into the phone after each beep.
1: (cl) Royalty-Free/CORBIS; (cl) John Kaprielian/Photo. Researchers, Inc. 2: (c)
Ruth A. Adams/Index Stock. 3: (tl). Royalty Free/Corbis; (tr) PhotoLink/Getty ...
WORK-RELATED MUSCULOSKELETAL RISKS - QUICK EXPOSURE CHECK (QEC). Guangyan Li* and Peter Buckle. Robens Centre for Health Ergonomics, ...
eBooks How to Own Your Own Mind (Mental Dynamite) Free Download, How ... to Own Your Own Mind (Mental Dynamite) Free Onl
40 Spemng 88 Weather and natural phenomena. 41 Verbs to adjectives 90 work.
42 Verbs to nouns 93 Vocabulary record sheet. 45 Topics 95 Practice essays 1 ...
p 60. Easy English for Busy People p 60. American. English. A1. A2. B1. B2 .... A1
. A2. Destination B1. Student's Book + Key. 9780230035362. Student's Book - ...
Page 1 ... These children are involved in a project to build their own country. ... How to build your own country was one of dozens of class projects that were ...
Design Your Own Database. Concept to Implementation or How to Design a
Database Without Touching a Computer. The following is an aggregation of
several ...
How to Plan Your own. EvEnt. Feed the PeoPle. It's not a party without party food,
and there's no better way to make your guests remember your. GeekDad Day ...
Page 1. How to Make Your Own Rain Barrel. Tips for using your rain ... â Elevate your rain barrel slightly to make acc
Feb 18, 2010 - SILVER BOWL reflectors with ECA bulbs are cheap and popular but do ... are difficult to judge in the view
Feb 18, 2010 - I use Lowell Tota ($120) and DP ($325) lights that use quart halogen bulbs ($15-$30 each). All these bulb
Selecting Your Own Approach to P2. Robert B. ... remove the need to make the particular modification you ..... on Dr. Pojasek's web site for your personal use.
difficult to manage and the cost of supplies becomes ... iPhone or Android app. You might also check out ...... step 4:
iQ, a fellow xB driver waving at a red light, or the packed house at the last ....
Smoke's boasts three locations, a mobile poutine truck, and a .... road raced by
under our wheels while we told .... Display it proudly in your home, at your des
www.vda.virginia.gov. 2. Narrator: There are, however, an increasing number of
private payment options that help to cover the costs of long- term care services.
Your Very Own Robot (Choose Your Own Adventure - Dragonlark) Free Download, Your Very ... Your Own Adventure - Dragonlar
YOU CAN build a quality metal detector on your own home workbench ... large
compo- nents. Schematic diagram of beat frequency metal described in the text.
order to avoid or minimise breast engorgement and nipple trauma (see Chapter 5). ...... I had very sore nipples during week two of breastfeeding my baby.
Feb 18, 2010 - It is up to you to center the artwork and make the sides of viewfinder parallel to the sides of flat art.
Defining Your Own Classes—Part 2 Solutions to Quick Check ...
1. Defining Your Own Classes—Part 2. 7. Solutions to. Quick Check Questions.
7.1. Returning an Object from a Method. 1. What's wrong with the following ...
Solutions to Quick Check Questions
7
Defining Your Own Classes—Part 2
7.1
Returning an Object from a Method
1.
What’s wrong with the following declaation? class Question { Person student; public void getStudent(
) {
return student; } ... }
The return type for the getStudent method must be Person as in public Person getStudent( ) { return student; }
2. Define a Vehicle class. It has a data member owner of type Person. Include an accessor to retrieve the owner person and a mutator to set the owner. 1
2
Solutions to Chapter 7 Quick Check Questions
Answer: class Vehicle { private Person owner; public Person getOwner( ) { return owner; } public void setOwner(Person person) { owner = person; } }
7.2
The Reserved Word this
1.
Write a single statement to express the following operations on fractions using the methods from the Fraction class: f5 = (f1 + f2) / (f3 - f4)
Answer: //assume f1, ..., f5 are declared and //assigned values f5 = f1.add(f2).divide(f3.minus(f4));
2. If the add method is defined thus public void add(Fraction frac) { int a, b, c, d; a = this.getNumerator(); //get this fraction’s b = this.getDenominator(); //num and denom c = frac.getNumerator(); //get frac’s num d = frac.getDenominator(); //and denom setNumerator(a*b + c*b); //updates this setDenominator(b*d); //fraction’s num and denom
Solutions to Chapter 7 Quick Check Questions
3
}
why is it wrong to use the method as f3 = f1.add(f2);
Because this add method is a void method. The method does not return a Fraction object. Rather, it adds the argument Fraction object to the receiving Fraction object. The correct would be f1.add(f2);
3. Write statements to assign the sum of fractions f1 and f2 to fraction f3 using the add method defined in Quick Check question 2 above. f1.add(f2); f3 = f1;
The Fraction object f3 becomes the sum of f1 and f2, but the value of f1 will change also. If we do not want f1 to change (which probably is the case for most situations), then we write f3 = new Fraction(f1); f3.add(f2);
7.3
Overloaded Methods and Constructors
1.
Are there any conflicts in the following three constructors for ClassX to be valid? public ClassX( int X ) { ... } public ClassX( float X ) { ... }
4
Solutions to Chapter 7 Quick Check Questions public ClassX( int Y ) { ... }
Yes, the first and the third constructors have the same signature so they cannot be overloaded.
2. Define a Student class. A Student has a name. Define two constructors, one with no argument and another with the name as its argument. Initialize the name to a default value Unknown for the zero-argument constructor. Answer: class Student { private String name; public Student( ) { name = "Unknown"; // or this("Unknown"); } public Student(String name) { this.name = name; } ... }
3. Rewrite the following constructors, so the first one calls the second one: public ClassOne(int alpha) { this.alpha = alpha; this.beta = 0; } public ClassOne(int alpha, int beta) { this.alpha = alpha; this.beta = beta; }
Solutions to Chapter 7 Quick Check Questions
Answer: public ClassOne(int alpha) { this(alpha, 0); }
7.4
Class Variables and Methods
No Quick Check Questions. 7.5
Call-by-Value Parameter Passing
1.
What is the name of the scheme used in Java to pass arguments to a method? Call-by-value (also called as Pass-by-value).
2. What is the output from the following code? class Question { private int one; public void myMethod( int one ) { this.one = one; one = 12; } } class Test { public static void main(String[] arg) { int one = 30; Question q = new Question(); q.myMethod(one); System.out.println(one); } }
Answer: 30
5
6
Solutions to Chapter 7 Quick Check Questions
7.6
Organizing Classes into a Package
No Quick Check Questions. 7.7
Using Javadoc Comments for Class Documentation
1.
Add javadoc comments to the following class: class Instructor { private String name; public void setName(Sring name) { this.name = name; } public String getName( ) { return name; } }
Answer: /** * The class to model an instructor * * @author Dr. Caffeine */ class Instructor { /** the name of this instuctor */ private String name; /** * Assigns the name to this instructor * * @param name the name to assign */ public void setName(Sring name) { this.name = name; } /** * Returns this instructor’s name * * @return the name of this instructor */ public String getName( ) {