Chapter 8

Exercise

1. Design an application that accepts 10 numbers and displays them in descending order.

4. The village of Ringwood conducted a census and created records that contain household data, including income. Ringwood has exactly 75 households. Write a program into which a village statistician can enter each of the 75 household income values, and determine the mean and median house-hold income.

13. Your student disk contains fi les named DEBUG08- 01. txt, DEBUG08- 02. txt, and DEBUG08- 03. txt. Each fi le starts with some comments that describe the problem. Comments are lines that begin with two slashes (//). Following the comments, each fi le contains pseudocode that has one or more bugs you must fi nd and correct.

08-01

// This application reads 10 numbers

// and sorts them, and displays the three highest numbers

start

Declarations

num SIZE = 10

num number

num numbers[SIZE]

num count

getReady()

while count < SIZE

detailLoop()

endwhile

finish()

stop

getReady()

output “Enter a number “

input number

return

detailLoop()

numbers[SIZE] = number

count = count + 1

output “Enter a number “

input number

return

finish()

sort()

output “The highest three are “, numbers[0], numbers[0], numbers[0]

return

sort()

num x = 0

num y = 0

num COMPS = count – 1

while y < COMPS

x = 0

while x < COMPS

if numbers[x] < numbers[x + 1] then

swap()

endif

x = x + 1

endwhile

y = y + 1

endwhile

return

swap()

num temp

temp = numbers[x + 1]

numbers[x + 1] = numbers[x]

numbers[x] = temp

return

08-02

// This application reads student typing test data

// including number of errors on the test, and the number

// of words typed per minute. Grades are assigned based

// on the following table:

// Errors

// Speed 0 1 2 or more

// 0–30 C D F

// 31–50 C C F

// 51–80 B C D

// 81–100 A B C

// 101 and up A A B

start

Declarations

num MAX_ERRORS = 2

num errors

num wordsPerMinute

num grades[5][3] = {“C”, “D”, “F”},

{“C”, “C”, “F”},

{“B”, “C”, “D”},

{“A”, “B”, “C”},

{“A”, “A”, “B”}

num LIMITS = 5

num speedLimits[LIMITS] = 0, 31, 51, 81, 101

num row

output “Enter number of errors on the test “

input errors

if errors = MAX_ERRORS then

errors > MAX_ERRORS

endif

output “Enter the speed in words per minute “

input wordsPerMinute

while row < LIMITS AND wordsPerMinute >= speedLimits[row]

row = row + 1

endwhile

row = row – 1

output “Your grade is “, grades[errors][row]

stop

08-03

This application reads sales data for an automobile dealership.

// Up to 100 sale amounts can be entered. The entered sale amounts

// are sorted so the median sale can be displayed.

start

Declarations

num SIZE = 100

num QUIT = 99999

num saleAmount

num sales[SIZE]

num count = 0

num middlePosition

num x

num y

num temp

num comps

getReady()

while saleAmount count < SIZE

detailLoop()

endwhile

finish()

stop

getReady()

output “Enter sale amount “

input saleAmount

return

detailLoop()

sales[x] = saleAmount

count = count + 10

output “Enter sale amount “

input saleAmount

return

finish()

sort()

middlePosition = count * 2

output “The median sale amount is “, sales[middlePosition]

return

sort()

comps = count – 1

while y < comps

x = 0

while x < y

if sales[x] > sales[x + 1] then

swap()

endif

x = x + 1

endwhile

y = y + 1

endwhile

return

void swap()

temp = sales[x]

sales[x + 1] = sales[x]

sales[x] = temp

return

Complete Chapter 10: Exercise 1, 2, 3, 6, 11

Exercises 1,2,3,6

1. Identify three objects that might belong to each of the following classes: a. Automobile b. Novel Author c. College Course

2. Identify three different classes that might contain each of these objects: a. Wolfgang Amadeus Mozart b. My pet cat named Socks c. Apartment 14 at 101 Main Street

3. Design a class named Customer Record that holds a customer number, name, and address. Include methods to set the values for each data field and display the values for each data field. Create the class diagram and write the pseudocode that defines the class.

6. Complete the following tasks: a. Design a class named Book that holds a stock number, author, title, price, and number of pages for a book. Include methods to set and get the values for each data fi eld. Create the class diagram and write the pseudocode that defi nes the class. b. Design an application that declares two Book objects and sets and displays their values. c. Design an application that declares an array of 10 Books. Prompt the user for data for each of the Books, then display all the values.

Chapter 9

Exercises 2, 5, 6

2. Create the logic for a program that continuously prompts the user for a number of dollars until the user enters 0. Pass each entered amount to a conversion method that displays a breakdown of the passed amount into the fewest bills; in other words, the method calculates the number of 20s, 10s, 5s, and 1s needed.

5. a. Create the logic for a program that performs arithmetic functions. Design the program to contain two numeric variables, and prompt the user for values for the variables. Pass both variables to methods named sum() and difference(). Create the logic for the methods sum() and difference(); they compute the sum of and diff erence between the values of two arguments, respectively. Each method should perform the appropriate computation and display the results. b. Add a method named product() to the program in Exercise 5a. Th e product() method should compute the result when multiplying two numbers, but not display the answer. Instead, it should return the answer to the calling program, which displays the answer.

6. Create the logic for a program that continuously prompts a user for a numeric value until the user enters 0. Th e application passes the value in turn to a method that squares the number and to a method that cubes the number. The program displays the results before reprompting the user. Create the two methods that square and cube a number passed to them, and then return the calculated value.

Exercise 8, 12,

8. Create the logic for a program that continuously prompts the user for two numeric values that represent the sides of a rectangle. Include two overloaded methods that compute a rectangle’s area. One method takes two numeric parameters and calculates the area by multiplying the parameters. Th e other takes a single numeric parameter, which is squared to • If both numbers entered are greater than 0, call the method version that accepts two parameters and pass it both values. • If one of the values entered is 0 but the other is not, call the version of the method that accepts just one parameter and pass it the nonzero value. • If both the entered values are 0, display an error message. • If both numbers entered are greater than 0, call the method version that accepts two parameters and pass it both values. • If one of the values entered is 0 but the other is not, call the version of the method that accepts just one parameter and pass it the nonzero value. • If both the entered values are 0, display an error message.

12. Create the logic for a program that prompts a user for three numbers and stores them in an array. Pass the array to a method that reverses the order of the numbers. Display the reversed numbers in the main program.

Exercise 6,

6. Complete the following tasks: a. Design a class named Book that holds a stock number, author, title, price, and number of pages for a book. Include methods to set and get the values for each data field. Also include a displayInfo() method that displays each of the Book’s data fields with explanations. b. Design a class named Text Book that is a child class of Book. Include a new data field for the grade level of the book. Override the Book class displayInfo() method so that you accommodate the new grade- level fi eld. c. Design an application that instantiates an object of each type and demonstrates all the methods.

Ch 11 Exercise 1, 2a, b Exercise 6, 17

1. Complete the following tasks: a. Design a class named Circle with fields named radius, area, and diameter. Include a constructor that sets the radius to 1. Include get methods for each field, but include a set method only for the radius. When the radius is set, do not allow it to be zero or a negative number. When the radius is set, calculate the diameter ( twice the radius) and the area ( the radius squared times pi, which is approximately 3.14). Create the class diagram and write the pseudocode that defines the class.

2. 2. Complete the following tasks: a. Design a class named Square with fields that hold the length of a side, the length of the perimeter, and the area. Include a constructor that sets the length of a side to 1. Include get methods for each field, but include a set method only for the length of a side, and do not allow a side to be zero or negative. When the side is set, calculate the perimeter length ( four times the side length) and the area ( a side squared). Create the class diagram and write the pseudocode that defines the class.

b. Design an application that declares two Squares. Set the side length of one manually, but allow the other to use the default value supplied by the constructor. Then, display each Square’s values.

6. Complete the following tasks: a. Design a class named Book that holds a stock number, author, title, price, and number of pages for a book. Include methods to set and get the values for each data field. Also include a displayInfo() method that displays each of the Book’s data fields with explanations.

b. Design a class named TextBook that is a child class of Book. Include a new data field for the grade level of the book. Override the Book class displayInfo() method so that you accommodate the new grade- level field.

c. Design an application that instantiates an object of each type and demonstrates all the methods

Ch 12 Exercises 1, 2, 4, 10

1. Take a critical look at three GUI applications with which you are familiar— for example, a spreadsheet, a word- processing program, and a game. Describe how well each conforms to the GUI design guidelines listed in this chapter.

2.Select one element of poor GUI design in a program with which you are familiar. Describe how you would improve the design.

4. Design the storyboards, interactivity diagram, object dictionary, and any necessary scripts for an interactive program for customers of Sunflower Floral Designs. Allow customers the option of choosing a fl oral arrangement ($ 25 base price), cut fl owners ($ 15 base price), or a corsage ($ 10 base price). Let the customer choose roses, daisies, chry-santhemums, or irises as the dominant flower. If the customer chooses roses, add $5 to the base price. After the customer clicks an Order Now button, display the price of the order.