Joyce Farrell\'s Java Programming 9e: Ch.1-15 Tough Questions Solutions PDF

Title Joyce Farrell\'s Java Programming 9e: Ch.1-15 Tough Questions Solutions
Course ObjectOriented Programming
Institution University of Arkansas at Little Rock
Pages 52
File Size 654.4 KB
File Type PDF
Total Downloads 40
Total Views 120

Summary

Joyce Farrell's Java Programming 9e: Ch.1-15 Tough Questions Solutions...


Description

Tough Questions and Up for Discussion Questions There is more to being a successful programmer than writing programs that execute without generating errors. To truly be a professional, you need more than a knowledge of Java syntax. For example: 

Your programs might work, but be inefficient. Throughout this book, you have been encouraged to understand that there are often multiple ways to tackle the same problem, and that some ways are more efficient or more elegant. For example, storing related values in an array is almost always more elegant that storing them in a series of separate variables.



Your programs might work, but not adhere to your organization’s coding standards. For example, throughout this book you have been encouraged to use reasonable names for variables rather than shorter, but cryptic, ones. Similarly, you have learned to use named constants rather than to use unnamed constant values in your programs. Your employer might have many more such standards.



Achieving success at a job interview involves being able to think on your feet, but you can still be prepared. The Tough Questions presented here are organized by chapter. They are similar to questions that an interviewer might ask at a technical job interview. Some require you to write a program that is more difficult than those presented in the end-of-chapter exercises in the book. After completing each chapter, you have the knowledge needed to write the programs suggested here, but the thought process required might be a little deeper or trickier.



Job interview success also sometimes depends on how articulate you are in expressing opinions on a variety of topics. You can read many books on career advice that describe typical generic interview questions such as “Where do you want to be five years from now?” and “Tell me about a problem you had with a former boss and how you were able to solve it”. The Up for Discussion questions presented here, and organized by chapter, are meant to probe your thoughts on topics that are more

specifically relevant to computer programmers and other business professionals. A particular question might have several good answers. If you can’t think of an answer right away, try doing some research on the Web or in other Java books.

Chapter 1 Tough Questions 1. Describe Java and how it differs from other programming languages. For a job interview, you might start my memorizing this sentence: Java is a simple, object-oriented, network-savvy, interpreted, robust, secure, architecture neutral, portable, high-performance, multithreaded, dynamic language. Although the meaning of all these terms won’t become clear until you have studied the language further, you should at least appreciate that unlike older languages, it is objectoriented, that it has a built-in library of routines that facilitate creating network connections, and that it is interpreted meaning Java bytecodes are translated into machine instructions while a program is running, giving Java its “write once; run anywhere” capability. You might also mention that Java is a compiled language. 2. What is the difference between a compiler and an interpreter? Which does Java use? Why? Compilers and interpreters both translate source code. A compiler translates the entire program at once, then executes it. An interpreter, translates one statement, executes it, translates the next statement, executes it, and so on. Interpreted programs can start running faster – as soon as one statement is translated. However, to run the program a second time, or to repeat a group of instructions, the whole interpretation must take place again. Compiled programs run faster, after the initial time is taken translate the whole program. When a compiled program is run the second time, or a group of instructions is repeated, it is very fast, because it is already compiled. When a language is interpreted, the interpreter must be loaded into memory while the

program runs, so there is less space available during execution. A compiler must only be in memory when the program is being compiled. Unlike other compiled languages that produce machine code for a specific computer, the Java compiler produces bytecode that is not machine dependent. Any computer that has the Java interpreter(The Java Virtual Machine) installed can read and understand the bytecode. 3. What are the conventions for naming classes and variables in Java? What are the advantages to using these conventions? Class and variable names can have no embedded spaces and cannot be reserved words. By convention, classes start with an uppercase letter and variables start with a lowercase one. Following coding conventions helps the programmer stay organized. Additionally, those who read the program have a clearer sense of the programmer’s intentions. 4. Can you store two public Java classes in the same file? How do you know? No, because a filename must match the name of the class it contains.

Up for Discussion 1.

Have you written programs in any programming language before starting this book?

If so, what do you think the advantages and disadvantages of using Java will be? If not, how difficult do you think writing programs will be compared to other new skills you have mastered? Student answers will vary. Advantages of Java include being object-oriented and useful for Web applications. Disadvantages include being less English-like than some other languages. 2.

Using the Web, try to discover which computer game is the most popular one ever

sold. Have you played this game? Would you like to? What makes this game so appealing? Many Web sites provide different answers to what is the best selling or most popular game, partially because games run on so many different platforms. Certainly, the families of games featuring the Sims, Mario Brothers, and Halo are among the biggest

sellers. In simulation games like the Sims, the player manages a group of people in everyday situations. In particular women and older game players like them because there is little violence and the games resemble everyday life; also unlike many other games, you do not win or lose. (Because of the nature of book publishing, you might be reading this several years after it was written. If other games have become more popular, student answers will vary.) 3.

Obviously, learning Java is useful to a Computer Information Systems major. List

several other major courses of study and discuss how programming skills might be useful to them. Many Web sites provide different answers to what is the best selling or most popular game, partially because games run on so many different platforms. Certainly, the families of games featuring the Sims, Mario Brothers, and Halo are among the biggest sellers. In simulation games like the Sims, the player manages a group of people in everyday situations. In particular women and older game players like them because there is little violence and the games resemble everyday life; also unlike many other games, you do not win or lose. (Because of the nature of book publishing, you might be reading this several years after it was written. If other games have become more popular, student answers will vary.) 4.

Most programming texts encourage students to use many comments in their

programs. Many students feel that writing program comments is a waste of time. What is your opinion? Why? Are there circumstances under which you would take the opposite stance? Student answers will vary. Writing comments is time-consuming and seems to detract from the “real” task of programming. However, good comments can help to clarify a programmer’s intentions for readers and may help in debugging programs. If an application must be completed quickly, many programmers would forego comments in order to complete the project on schedule.

Chapter 2 Tough Questions 1. In a single sentence, describe the relationship between associativity and precedence. Associativity describes the order of operations with the same precedence. 2. Write Java statements that would round a numeric value to the nearest whole number. You cannot use a prewritten round() method. You can add 0.5 to a floating-point number, then cast it to an integer, and the result is the rounded number. For example, if the starting value is 8.3, add 0.5 giving 8.8, then cast to an integer, leaving 8. As another example, if the starting value is 8.5, then add 0.5, giving 9.0. When you cast to an integer, the result is 9. So the java statements are similar to: double tempResult = startingFloat + 0.05; int finalRoundedResult = (int)tempResult; 3. Suppose you need to display the last digit of a four-digit number. You can use the remainder operator (%) to extract the remainder when the number is divided by 10. Suppose the “%” key is broken on your computer, but the program must be written immediately. What would you do? An interviewer will appreciate clever answers such as “Go find another computer,” and “Copy a % operator out of another document and paste it into my program,” as long as you follow up with a programming solution that shows you understand how the remainder operator works. To get a remainder without using the remainder operator you can perform integer division, and then subtract that answer times the divisor from the original number. For example, the value of 32 % 5 is the same as 32 – (32 / 5) * . The value of 32 % 5 is 2. The value of 32 – (32 / 5) * 5 is the same as 32 - 6 * 5, which is the same as 32 – 30, which is 2. The code is: remainder = dividend – (dividend / divisor) * divisor;

4. Write a series of Java statements that interchange the values of two numeric variables without using a third variable. This is very tricky! To reverse the values of num1 and num2, you can use the following code num1 = num1 + num2; num2 = num1 – num2; num1 = num1 – num2;

For example, assume num1 is 10 and num2 is 3. After the first statement, num1 is 13 and num2 is still 3. After the second statement, num2 is 10 and num1 is 13. After the last statement, num1 is 3 and num2 is 10. 5. In many programming languages, a named constant must be assigned a value when it is declared. This isn’t true in Java. Provide some examples of how this restriction might be advantageous. You might want a value to be constant throughout a program, yet not know the value when the program starts. For example, in a biological simulation you might want POPULATION_GROWTH_RATE to be a randomly-generated number, yet be constant after it is generated. As another example, you might want a EURO_TO_DOLLAR conversion rate figure to be constant throughout a program, but, because it is a figure that changes constantly, allow a user to enter today’s conversion rate at the start of the program.

Up for Discussion

1. What advantages are there to requiring variables to have a data type? It is easier for a person to distinguish between different types of data than it is for a computer. For example, if a friend says, “The book cost twelve thirty-four” you recognize the value as dollars and cents; if the friend says “Let’s meet for lunch at twelve thirty-four,” you might think the friend is a little too precise, but you recognize the value as a time of day. Computer data is stored without such clues, so declaring a data type provides meaning as to how the values should be used. Major functions that type systems provide include the following: 

Error detection. Use of data types allows a compiler to detect some meaningless or

invalid code. For example, “cat” * 3is invalid -- a string cannot be multiplied 

Optimization. Type-checking provides useful information to a compiler, allowing it

to use more efficient instructions to handle data. 

Documentation. Types can make the programmer’s intent clearer.

2. Some programmers use a system called Hungarian notation when naming their variables. What is Hungarian notation, and why do many object-oriented programmers feel it is not a valuable style to use? What do you think? Hungarian notation is a naming convention in computer programmer in which part of the name of an object indicates its data type intended use. For example, an integer that holds can account number might be named iAccountNum. Hungarian notation makes many object names less readable and less pronounceable. Object-oriented programmers usually create short methods in which there are few variables in a module. With code in clearly named, short, concise modules, many object-oriented programmers feel there is no need for the complexity that Hungarian notation adds. 3. Some languages do not require explicit type casting when you want to perform an unlike assignment, such as assigning a double to an int. Instead, the type casting is performed automatically, the fractional part of the double is lost, and the whole-number portion is simply stored in the int result. Are there any reasons this approach is superior or inferior to the way Java works? Which do you prefer? An automatic type cast saves the programmer the keystrokes needed to create the cast explicitly. However, without explicit casting, it is easier to make a mistake in which the programmer does not realize a cast has occurred and significant data is lost.

4. Did you have a favorite computer game when you were growing up? Do you have one now? How are they similar and how are they different? Did you have a favorite board game? What does it have in common with your favorite computer game? Student answers will vary. Possibly students will see similarities and differences in games that appeal to them in “real life” and on a computer. For example, some will prefer games that emphasize cooperation whether playing a board game or a computer game. Others will prefer heated competition no matter which genre of game is played. Some game players prefer action, others prefer testing their recall or mental skills, and others prefer games of pure chance.

Chapter 3 Tough Questions 1. Describe some situations in which passing arguments to a method in the wrong order would not cause any noticeable errors. If a method accepts two parameters of the same type and returns their sum or product, you would not notice if arguments were passed to it in the wrong order. 2. Suppose you have created a class named Employee. Further suppose that you have an application that declares the following: final Employee myEmployee = new Employee(); Can myEmployee’s fields be altered? When final is used with an object, it refers to the reference to (address of) that object. This means that you cannot assign another object to that address. However, you can use methods such as setIdNumber() to alter the contents of the fields within the object. 3. Describe a chicken using a programming analogy. (This question is purposely vague and even silly. Be creative.) If a chicken is a method, you can describe its header as: egg chicken(feed x)

In other words, you put feed into a chicken and get an egg. If a chicken is a class, then it has fields such as weight and color, and contains methods to get and set those fields as well as methods such as layEgg(), walk(), and peck(). 4. Consider the real-life class named StateInTheUnitedStates. What are some reallife attributes of this class that are static attributes, and what attributes are instance attributes? Create another example of a real-life class and discuss what its static and instance members might be.

Each StateInTheUnitedStates, such as Alaska or Alabama, has its own name, population, governor, average educational level, area in square miles, capitol, and so on. Each of these is an instance variable. A field that has the same value for every StateInTheUnitedStates, such as numberOfSenators (which is always 2), could be

static. As another example, a class named Circlemight have instance variables for radius or color, but diameterFactor(2) or circumferenceFactor(3.14) can be static. A class named Bird can have instance variables for name and color, but a static field to hold numberOfLegs(2) or reproductionMethod(egg). 5. Can you think of any circumstances in which program comments would be a detriment? This might be a tricky question to answer in a job interview because the interviewer might have strong feelings about good commenting. However, to provide a thoughtful answer, after you assure the interviewer that you believe in documenting programs thoroughly, you could mention some drawbacks. For example, probably the biggest drawback to documentation is when it is wrong, either because it was originally wrong, or because program changes have made the existing, unchanged documentation obsolete. If users or other programmers rely on incorrect documentation, they might misinterpret output, make bad business decisions, and lose money. If no documentation exists, then at least future programmers will have to look more closely at the code and figure out exactly what is taking place. Additionally, at times, a program must be produced very quickly, and you must skimp on documentation to get the needed results. Also, if you are instructing someone on a fine point of programming, you might prefer to just show them the code so as not to confuse the issue with embedded comments.

Up for Discussion 1. One of the advantages to writing a program that is subdivided into methods is that such a structure allows different programmers to write separate methods, thus dividing the work. Would you prefer to write a large program by yourself, or to work on a team in which each programmer produces one or more modules? Why?

Student answers will vary. Some students will prefer the team approach whereas others will prefer the autonomy of developing a major system on their own. 2. Suppose you wrote a program that designed a building that collapsed. Would you rather have been the only programmer who worked on the program and be totally responsible for one death, or would you rather have worked with a team of 20 programmers, but the collapse caused 20 fatalities? Student s will be divided on whether fewer deaths or less responsibility is more important to them. 3. Hidden implementations are often said to exist in a black box. What are the advantages to this approach in both programming and real life? Are there any disadvantages? When implementation exists in a black box, you do not need to be concerned with the details. In real life, when you use a device such as a television set, you do not have to be concerned how the signals are transmitted from a far-off location to your living room. In programming, you can use a module written by someone else, get correct results, and not worry about how the results were achieved. However, when something goes wrong, either in programming or real life, if you do not understand the hidden details, you are less likely to know how to go about resolving problems.

Chapter 4 Tough Questions 1. Give an example of a situation in which you would want a class’s field to be final but not static. Give another example of when you would want a field to be static but not final. Consider an Automobile class. You might want a VIN (Vehicle Identification number) to be nonstatic because there is a different one for each instantiation. However, the field should be final once an object is constructed. You might want a field like countOfAutos to be static so that there is only one count that all objects share, but not final so that it can be incremented or decremented when you add or remove an Automobile. 2. Suppose class A contains a main() method that instantiates an object of class B. Can class B also have a main() method? Explain your answer. Yes. When you run a program using the java command and a class name, the JVM looks for a main() method only in the class you have named. There is no conflict if there is also a main() method (with the header public static void main(String[] args))in the other class. 3. Can a single class contain two main() methods? Explain your answer. Yes a...


Similar Free PDFs