Chap08 Exercise Solutions - Weebly

3y ago
35 Views
2 Downloads
354.14 KB
5 Pages
Last View : 1m ago
Last Download : 3m ago
Upload by : Javier Atchley
Transcription

thJava Software Solutions, 7 EditionExercise Solutions, Ch. 8Chapter 8 Exercise SolutionsEX 8.1. Which of the following are valid declarations? Which instantiate an array object?Explain your answers.int primes {2, 3, 4, 5, 7, 11};Invalid; an int cannot be declared and initialized using an intializer list. The brackets aremissing.float elapsedTimes[] {11.47, 12.04, 11.72, 13.88};Valid; the brackets can be placed either after the element type or after the referencevariable. However, this is not the preferred technique. This declaration creates an arrayobject.int[] scores int[30];Invalid; the right hand side of the assignment operator must contain either an initializerlist or a new operation.int[] primes new {2,3,5,7,11};Invalid; “new” on the right hand side of the assignment operator is neither necessarynor acceptable.int[] scores new int[30];Valid; the assignment is correct Java syntax. This declaration creates an array object.char grades[] {'a', 'b', 'c', 'd', 'f'};Valid; the brackets can be placed either after the element type or after the referencevariable. However, this is not the preferred technique. This declaration creates an arrayobject.char[] grades new char[];Invalid; the size of the array must be indicated when the array is instantiated.EX 8.2. Describe five programs that would be difficult to implement without using arrays.A program to find the average midterm score of 600 students enrolled in anintroductory computer science course.A program to record and compute the sum of the snowfalls, recorded on a daily basisfor the 40 days preceding the Winter Olympics.A program to determine the relative frequency of each character in the Cyrillic alphabetin the original version of The Brothers Karamasov.A program to compute the mean and standard deviation of the Dow Jones IndustrialAverage closings since September 11, 2001.A program to store the coordinates of the vertices of polygons approximating thesurface of a beating heart.EX 8.3. Describe how an element in an array is accessed in memory. For example, where ismyArray[25] stored in memory?The elements of an array are stored contiguously in memory. The name of an array,such as myArray, is a reference to an object that stores the beginning of that data inmemory. To compute the address of a particular element, the address of the first dataelement is multiplied by the index (25) and the size of the array element. That is whyarray indexes begin at zero. 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

thJava Software Solutions, 7 EditionExercise Solutions, Ch. 8EX 8.4.Describe what problem occurs in the following code. What modificationsshould be made to it to eliminate the problem?int[] numbers {3, 2, 3, 6, 9, 10, 12, 32, 3, 12, 6};for (int count 1; count numbers.length; count )System.out.println (numbers[count]);thThe for loop fails to print the 0 element of the array, and attempts to print ,anArrayIndexOutOfBoundsException is thrown. The problem can be eliminated byproviding a for loop which initializes count to 0 (rather than 1) and tests if count is lessthan (rather than less than or equal to) numbers.length.EX 8.5. Write an array declaration and any necessary supporting classes to represent thefollowing statements:a. students’ names for a class of 25 studentsString[] students new String[25];b. students’ test grades for a class of 40 studentsint[] grades new int[40];or, for simple letter grades:char[] grades new char[40];or, for letter grades that include pluses and minusesString[] grades new String[40];c. credit-card transactions that contain a transaction number, a merchant name, anda chargeTransactions[] charges new Transactions[MAX];public class Transactions{private int transactionNumber;private String merchantName;private double charge;// etc.}d. students’ names for a class and homework grades for each studentStudent[] myClass new Student[MAX];public class Student{private String name;private int[] grades;// etc.}e. for each employee of the L&L International Corporation: the employee number,hire date, and the amount of the last five raises 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

thJava Software Solutions, 7 EditionExercise Solutions, Ch. 8Employee[]employees new Employee[MAX];public class Employee{private int employeeNumber;private String hireDate;private double raise[] new double[5];// etc.}EX 8.6. Write code that sets each element of an array called nums to the value of thecontstant INITIAL.for (int index 0; index nums.length; index )nums[index] INITIAL;EX 8.7. Write code that prints the values stored in an array called names backwards.for (int index names.length-1; index 0; index--)System.out.println(names[index]);EX 8.8. Write code that sets each element of a boolean array called flags to alternatingvalues (true at index 0, false at index 1, etc.).for (int index 0; index flags.length; index )flags[index] (index%2 0);EX 8.9. Write a method called sumArray that accepts an array of floating point values andreturns the sum of the values stored in the array.public float sumArray (float[] values){float sum 0;for (int index 0; index values.length; index )sum values[index];return sum;}EX 8.10.Write a method called switchThem that accepts two integer arrays asparameters and switches the contents of the arrays. Take into account that thearrays may be of different sizes.public void switchThem (int[] first, int[] second){if (first.length second.length){// copy contents of first into tempint [] temp new int[first.length];for (int i 0; I first.length; i )temp[i] first[i]; 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

thJava Software Solutions, 7 EditionExercise Solutions, Ch. 8//copy contents of second into firstfor (int i 0; I first.length; i )first[i] second[i];//copy contents of temp into secondfor (int i 0; I first.length; i )second[i] temp[i];}elseSystem.out.println("Arrays are of different sizes");}EX 8.11.Describe a program for which you would use the ArrayList class insteadof arrays to implement choices. Describe a program for which you would usearrays instead of the ArrayList class. Explain your choices.A program associated with a mail order Website for backpacks would use an object ofthe ArrayList class to implement the choices of colors of the backpacks because thecolors and number of colors change with the seasons and as colors gain and losepopularity. An object of the ArrayList class can grow and shrink dynamically toaccommodate these changes.A program associated with a personal day planner, with entries possible for each hourof the day, would use an array object to implement the choices for each hour of the daybecause the number of hours in a day, and hence the number of hours for whichchoices can be made for any given day, never changes. There is no need for the arrayobject to grow or shrink to accommodate a larger or smaller number of hours.EX 8.12.What would happen if, in the Dots program, we did not provide emptydefinitions for one or more of the unused mouse events?The compiler would complain because class DotsListener, which implementsMouseListener, must provide definitions for all methods specified in the interface. If itdoesn't, the class must be declared as abstract.EX 8.13.The Dots program listens for a mouse pressed event to draw a dot. Howwould the program behave differently if it listened for a mouse released eventinstead? A mouse clicked event?If the program listened instead for a mouse released event, the position andappearance of the dot would be determined at the time of the release, and wouldn’tappear until the mouse button was released. Similarly, if the program listened for amouse clicked event, the position and appearance of the dot would be determined atthe time of a click (which requires a press followed by a release).EX 8.14.What would happen if the call to super.paintComponent were removedfrom the paintComponent method of the DotsPanel class? Remove it and run theprogram to test your answer.The paintComponent method is a member of class DotsPanel which extendsJPanel which means that the call to super.paintComponent is a call to thepaintComponent method of the JPanel class. Without this call, thepaintComponent method of the JPanel class is not called. As a consequence, thebackground is not cleared each time. This means that there is no need to store andredraw all of the dots each time, but it causes other problems, such as the background 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

thJava Software Solutions, 7 EditionExercise Solutions, Ch. 8color not being drawn and the count value overwriting itself so that it becomesunreadable.EX 8.15.What would happen if the call to super.paintComponent were removedfrom the paintComponent method of the RubberLinesPanel class? Remove itand run the program to test your answer. In what ways is the answer different fromthe answer to Exercise 8.13?Without this call, the paintComponent method of the JPanel class is not called, andthe background is not cleared and redrawn each time. As a consequence, previouslydrawn lines remain on the screen. Furthermore, since the lines are drawn over andover again as the mouse is dragged, moving the mouse in a curve causes severaldifferent lines to be drawn during one drag.EX 8.16.Create a UML class diagram for the Direction program. 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

Java Software Solutions, 7th Edition Exercise Solutions, Ch. 8 color not being drawn and the count value overwriting itself so that it becomes unreadable. EX 8.15. What would happen if the call to super.paintComponent were removed

Related Documents:

INDEX PRESENTATION 5 THE THUMB 7 MECHANICAL EXERCISES 8 SECTION 1 THUMB Exercise 1 12 Exercise 2 13 Exercise 3 - 4 14 Exercise 5 15 Estudio 1 16 SECTION 2 THUMB WITH JUMPS Exercise 6 17 Exercise 7 - 8 18 Exercise 9 19 Exercise 10 20 Exercise 11 - 12 21 Estudio 6 22 SECTION 3 GOLPE Exercise 13 23 Exercise 14 24 Exercise 15 25 Exercise 16 - 17 26 Exercise 18 27 .

Chapter 1 Exercise Solutions Exercise 1.1 Exercise 1.2 Exercise 1.3 Exercise 1.4 Exercise 1.5 Exercise 1.6 Exercise 1.7 Exercise 1.8 Exercise 1.9 Exercise 1.10 Exercise 1.11 Exercise 1.12 Fawwaz T. Ulaby and Umberto Ravaioli, Fundamentals of Applied Electromagnetics c 2019 Prentice Hall

6 CHAPTER EIGHT Carbohydrate Metabolism The Reactions of the Glycolytic Pathway Glycolysis is summarized in Figures 8.3. The 10 reactions of

CLIMATE-SMART AGRICULTURE TRAINING MANUAL iv Exercises Exercise A.1 Introduction to the training course 18 Exercise A.2 Weather and climate 18 Exercise A.3 Global Warming 18 Exercise A.4 Changes in rainfall 18 Exercise A.5 The greenhouse effect 19 Exercise A.6 Climate change in your area 19 Exercise B.1 Understanding the effects of future climate change 43

TRX Power Stretch. Round 4, Exercise 1 Round 4, Exercise 2 Round 4, Exercise 3 Round 4, Exercise 4 Round 4, Exercise 5 Round 4, Exercise 6. Block 5 – Hamstring/Folds (Adjustment: mid length) EXERCISE SETS REPS / TIME SET REST TRAN

2. Selecting an exercise 4 2.1 Scoping the exercise 4 2.2 Setting the aims and objectives 4 2.3 Types of exercise 5 2.4 Choosing the type of exercise 6 2.4.1 What is being tested? 6 2.4.2 What resources are available? 7 3. Planning the exercise 9 3.1 Exercise management team 9 3.2 Exercise plan 9 3.3 Target audience 10

EXERCISE 17 Spinal Cord Structure and Function 277 EXERCISE 18 Spinal Nerves 287 EXERCISE 19 Somatic Reflexes 299 EXERCISE 20 Brain Structure and Function 309 EXERCISE 21 Cranial Nerves 333 EXERCISE 22 Autonomic Nervous System Structure and Function APPENDIX C: 343 EXERCISE 23 General Senses 355 E

enFakultätaufAntragvon Prof. Dr. ChristophBruder Prof. Dr. DieterJaksch Basel,den16. Oktober2012, Prof. Dr .