Final Review - PowerPoint PPT Presentation

About This Presentation
Title:

Final Review

Description:

A Tertiary Search divides a sorted data structure into three equal parts and ... Catch the exception inside the average method, print out the message, and return zero. ... – PowerPoint PPT presentation

Number of Views:14
Avg rating:3.0/5.0
Slides: 18
Provided by: jennif113
Category:
Tags: final | printout | review

less

Transcript and Presenter's Notes

Title: Final Review


1
Final Review
2
Review
  • A Binary Search divides a sorted data structure
    into two equal parts and each time throws away
    the part that cannot contain the item of
    interest. A Tertiary Search divides a sorted
    data structure into three equal parts and each
    time throws away the two parts that cannot
    contain the item of interest. Write a recursive
    method with the signature
  • private int search (int target, int first, int
    last)
  • that performs a Tertiary Search on an integer
    array named stuff (which is a class variable).

3
  • private int search (int target, int first, int
    last)
  • int result -1
  • int mid1, mid2, interval
  • if (first gt last)
  • result -1
  • else
  • interval last first
  • mid1 first interval/3
  • mid2 first 2interval/3
  • if (target stuffmid1)
  • result mid1
  • else if (target stuffmid2)
  • result mid2
  • else if (target lt stuffmid1)
  • result search(target, first, mid1-1)
  • else if (target lt stuffmid2)
  • result search(target, mid11, mid2-1)

4
Review
  • Create a class that extends JFrame to display a
    window like this
  • Before the user clicks OK for the first time, the
    bottom third of the window contains no text (ie.
    it does not say Hello..)
  • After the user clicks OK, the bottom third
    displays Hello plus the name the user entered.

5
  • import javax.swing.
  • import java.awt.
  • import java.awt.event.
  • public class FinalQuestion extends JFrame
    implements ActionListener
  • public static final int WIDTH 400
  • public static final int HEIGHT 200
  • JPanel namePanel
  • JLabel nameLabel
  • JTextField nameText
  • JTextField helloText
  • JButton helloButton

6
  • FinalQuestion()
  • setSize(WIDTH,HEIGHT)
  • Container contentPane getContentPane()
  • contentPane.setLayout(new GridLayout(3,1))
  • namePanel new JPanel()
  • namePanel.setLayout(new FlowLayout())
  • nameText new JTextField(20)
  • nameLabel new JLabel("Enter your name")
  • namePanel.add(nameLabel)
  • namePanel.add(nameText)
  • helloText new JTextField(20)
  • helloText.setText("")
  • helloText.setEditable(false)
  • helloButton new JButton("OK")
  • helloButton.addActionListener(this)

7
  • public void actionPerformed (ActionEvent e)
  • if (e.getActionCommand().equals("OK"))
  • helloText.setText ("Hello "
  • nameText.getText() "!")
  • public static void main(String args)
  • FinalQuestion f new FinalQuestion()
  • f.setVisible(true)
  • // end class

8
Review
  • Write a method called average which takes as
    parameters an int array a and an int numValues.
  • If numValues is less than or equal to zero, throw
    a DivideByZeroException with a message that says
    Error. Catch the exception inside the average
    method, print out the message, and return zero.
  • If no exception is thrown, call a method called
    sumArray which takes as parameters an int array
    and an int representing the number of values to
    average. Divide this sum by numValues to get the
    average. Return the average as a double.

9
Review
  • public double average (int a, int numValues)
  • try
  • if (numValues lt 0)
  • throw new DivideByZeroException(Error)
  • int sum sumArray (a, numValues)
  • return sum / numValues
  • catch (DivideByZeroException e)
  • System.out.println(e.getMessage())
  • return 0

10
Review
  • Modify the method average so that the method
    throws a DivideByZeroException containing the
    message Error if numValues lt 0. The calling
    method must handle the exception.
  • If no exception is thrown, calculate and return
    the average as in the previous question.

11
Review
  • public double average (int a, int numValues)
    throws DivideByZeroException
  • if (numValues lt 0)
  • throw new DivideByZeroException(Error)
  • int sum sumArray (a, numValues)
  • return sum / numValues

12
Review
  • Given a ListNode class with the following
    methods
  • void setData(String x)
  • String getData()
  • void setLink (ListNode l)
  • ListNode getLink()
  • And a LinkedList class with the following class
    variables
  • private ListNode head
  • private ListNode current
  • Write a LinkedList method with the signature
  • public void insertNode(String newData)
  • that (1) creates a new ListNode, (2) puts
    newData into it, (3) inserts the new ListNode
    right after the node pointed to by current.
    Assume that the class variables in ListNode are
    private, so you will have to call setData,
    getData, etc, methods.

13
Review
  • public void insertNode (String newData)
  • ListNode newNode new ListNode()
  • newNode.setData(newData)
  • newNode.setLink(current.getLink())
  • current.setLink(newNode)

14
Review
  • Write a recursive method to display an integer
    number with embedded blanks. For example, if the
    number is 285, your method should print 2 8 5.

static void printDigits(int number) if
(number lt 10) System.out.print(number)
else printDigit(number/10)
System.out.print( number10)
15
Review
  • Write some Java code to compute 1 1/x 1/x2
    1/x3 ... 1/x10. Assume that x is already
    declared and initialized. Use a loop and do not
    use any function in the Math class.

double result 1.0 double value 1.0 for
(int i0 ilt10 i) value /
x result value
16
Review
  • Write a code fragment to ask the user to "Enter
    number of zoo animals". Store that in the integer
    variable numberOfAnimals. Use that variable to
    declare an array of ZooAnimal class objects.
    Write a for loop to iterate through each of those
    objects Declare each one, prompt the user to
    "Enter data for zoo animal", and then use the
    ZooAnimal method readInput() to read that data.
    You can assume a Scanner object called keyboard
    is already declared.

17
Review
  • System.out.println("Enter number of zoo
    animals")
  • numberOfAnimals keyboard.nextInt()
  • ZooAnimal animal new ZooAnimalnumberOfAnimals
  • int i
  • for (i 0 i lt numberOfAnimals i)
  • animali new ZooAnimal()
  • System.out.println("Enter data for zoo
    animal")
  • animali.readInput()
Write a Comment
User Comments (0)
About PowerShow.com