Chapter 7 Exercises - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Chapter 7 Exercises

Description:

Credit-card transactions that contain a transaction number, a merchant name, and a charge ... Take into account that the arrays may be of different sizes. ... – PowerPoint PPT presentation

Number of Views:58
Avg rating:3.0/5.0
Slides: 17
Provided by: csee8
Category:

less

Transcript and Presenter's Notes

Title: Chapter 7 Exercises


1
Chapter 7 Exercises
  • 7.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 is
    missing
  • float elapsedTimes 11.47, 12.04, 11.72,
    13.88
  • valid the can be placed either after the
    element type (float) or after the reference
    variable (elapsedTimes)
  • int scores int30
  • invalid the right hand side of the assignment
    operator must contain either an initializer list
    or an expression of the form, new int30
  • int primes new 2,3,5,7,11
  • invalid new on the right hand side of the
    assignment operator is neither necessary nor
    acceptable

2
Chapter 7 Exercises
  • 7.1 Which of the following are valid
    declarations? Which instantiate an array object?
    Explain your answers.
  • int scores new int30
  • valid the assignment is correct Java syntax
  • char grades 'a', 'b', 'c', 'd', 'f'
  • valid the assignment is correct Java syntax
  • char grades new char
  • invalid the size of the array must be indicated
    when the array is instantiated. Changing the
    right half of the statement to "new charSIZE",
    for example, would make the statement valid.

3
Chapter 7 Exercises
  • 7.3 Describe what problem occurs in the following
    code. What modifications should 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 lt numbers.length
    count)
  • System.out.println (numberscount)
  • The for loop fails to print the 0th element of
    the array, and attempts to print the nonexistent
    11th element of the array. As a consequence, an
    ArrayIndexOutOfBoundsException is thrown.
  • The problem can be eliminated by providing a for
    loop which initializes count to 0 (rather than 1)
    and tests if count is less than (rather than less
    than or equal to) numbers.length.

4
Chapter 7 Exercises
  • 7.4 Write an array declaration and any necessary
    supporting classes to represent the following
    statements
  • Students names for a class of 25 students
  • String students new String25
  • Students test grades for a class of 40 students
  • int grades new int40 // for integer
    percentages
  • or
  • char grades new char40 // for simple
    letter grades
  • or
  • String grades new String40 / for letter
    grades with
  • s and
    s. /

5
Chapter 7 Exercises
  • 7.4 Write an array declaration and any necessary
    supporting classes to represent the following
    statements
  • Credit-card transactions that contain a
    transaction number, a merchant name, and a
    charge
  • Transactions charges new Transactionsnumber
  • / assumes int number is assigned a value prior
    to the array declaration /
  • public class Transactions
  • private int transactionNumber
  • private String merchantName
  • private double charge
  • . . .

6
Chapter 7 Exercises
  • 7.4 Write an array declaration and any necessary
    supporting classes to represent the following
    statements
  • Students names for a class and homework grades
    for each student
  • NameAndGrades theClass new NameAndGrades
    enrollees
  • / assumes int enrollees is assigned a value
    prior to the array declaration /
  • public class NameAndGrades
  • private String name
  • private int grades
  • . . .

7
Chapter 7 Exercises
  • 7.4 Write an array declaration and any necessary
    supporting classes to represent the following
    statements
  • For each employee of the LL International
    Corporation the employee number, hire date, and
    the amount of the last five raises
  • Employee LAndL new EmployeestaffSize
  • / assumes int staffSize is assigned a value
    prior to the array declaration /
  • public class Employee
  • private int employeeNumber
  • private String hireDate
  • private double raise new double5
  • . . .

8
Chapter 7 Exercises
  • 7.5 Write a method called sumArray that accepts
    an array of floating point values and returns the
    sum of the values stored in the array.
  • public int sumArray (int values)
  • int sum 0
  • for (int count 0 count lt values.length
    count)
  • sum valuescount
  • return sum

9
Chapter 7 Exercises
  • 7.6 Write a method called switchThem that accepts
    two integer arrays as parameters and switches the
    contents of the arrays. Take into account that
    the arrays may be of different sizes.
  • public void switchThem (int first, int
    second)
  • if (first.length second.length)
  • // copy contents of first into temp
  • int temp new intfirst.length
  • for (int i0 iltfirst.length i)
  • tempi firsti
  • //copy contents of second into first
  • for (int i0 iltfirst.length i)
  • firsti secondi

10
Chapter 7 Exercises
  • 7.6 Write a method called switchThem that accepts
    two integer arrays as parameters and switches the
    contents of the arrays. Take into account that
    the arrays may be of different sizes.
  • //copy contents of temp into second
  • for (int i0 iltfirst.length i)
  • secondi tempi
  • else
  • System.out.println(Arrays are of different
  • sizes
    and cannot be switched.)

11
Programming Problem 7.5
  • public class Statistics
  • public static double mean(int numbers)
  • double result 0.0
  • for (int i0 ilt numbers.length i)
  • result numbersi
  • return (double)result /
    (numbers.length)

12
Programming Problem 7.5
  • public static double standardDeviation(int
    numbers)
  • double result 0.0
  • double mean mean(numbers)
  • // compute summation
  • for (int i0 iltnumbers.length i)
  • result ((double)numbersi - mean)
    ((double)numbersi - mean)
  • result result / numbers.length
  • return Math.sqrt(result)

13
Programming Problem 7.5
  • import java.util.Random
  • import java.text.DecimalFormat
  • public class StatisticsDriver
  • private static final int NUM 50,
  • MAX 20,
  • NUM_CYCLES 5

14
Programming Problem 7.5
  • // Demonstrates the mean and standard
    deviation methods
  • public static void main (String args)
  • DecimalFormat formatter new
    DecimalFormat("0.00")
  • Random gen new Random()
  • int numbers new intNUM
  • int j

15
Programming Problem 7.5
  • for (int i0 iltNUM_CYCLES i)
  • // create random numbers
  • for (j0 jltNUM j)
  • numbersj gen.nextInt(MAX)
    1
  • System.out.println("\n\nCycle " (i1)
    "\n-----------------------------")
  • System.out.println("The mean "
  • formatter.format(Statistics.mean(
    numbers)))
  • System.out.println("The standard
    deviation "
  • formatter.format(Statistics.stand
    ardDeviation(numbers)))

16
StatisticsDriver Output
Write a Comment
User Comments (0)
About PowerShow.com