10/1/2018 programming data structures · 2019-10-30 · characters of “a” for an ant, “d”...

23
10/1/2018 Programming Data Structures 1 Quiz Exception Handling

Upload: others

Post on 26-Jul-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 10/1/2018 Programming Data Structures · 2019-10-30 · characters of “A” for an ant, “D” for a doodlebug, “.” for an empty cell. Create a class named Critter that encapsulates

10/1/2018

Programming Data Structures

1

QuizException Handling

Page 2: 10/1/2018 Programming Data Structures · 2019-10-30 · characters of “A” for an ant, “D” for a doodlebug, “.” for an empty cell. Create a class named Critter that encapsulates

Quiz – 30 min

2

Page 3: 10/1/2018 Programming Data Structures · 2019-10-30 · characters of “A” for an ant, “D” for a doodlebug, “.” for an empty cell. Create a class named Critter that encapsulates

Quiz Answers

3

Page 4: 10/1/2018 Programming Data Structures · 2019-10-30 · characters of “A” for an ant, “D” for a doodlebug, “.” for an empty cell. Create a class named Critter that encapsulates

What we have learned ...

Array, 2-D arrays

Base classes and derived classes

Private, protected, default, and public access

this, super, this(), super()

Overriding methods

Upcasting and downcasting

Debugger

4

Page 5: 10/1/2018 Programming Data Structures · 2019-10-30 · characters of “A” for an ant, “D” for a doodlebug, “.” for an empty cell. Create a class named Critter that encapsulates

This Week: Exception Handling

5

https://hocvien.haravan.com/blogs/chia-se-thong-tin/6-loi-trinh-duyet-internet-thuong-gap-p2

Page 6: 10/1/2018 Programming Data Structures · 2019-10-30 · characters of “A” for an ant, “D” for a doodlebug, “.” for an empty cell. Create a class named Critter that encapsulates

Exception Handling: Exceptions

Throwing exceptions (class demo 1)

6

https://design.tutsplus.com/tutorials/how-to-animate-a-character-throwing-a-ball--cms-26207

public static void main(String[] argv){int[] arr = new int[]{1, 2, 3, 4, 5};for(int i = 0; i <= arr.length; i++){

System.out.println("index: " + i + " element: " + arr[i]);

}}

Page 7: 10/1/2018 Programming Data Structures · 2019-10-30 · characters of “A” for an ant, “D” for a doodlebug, “.” for an empty cell. Create a class named Critter that encapsulates

Exception Handling: Catching the exception

Try and Catch exceptions (class demo 2)

7

https://www.videoblocks.com/video/golden-retriver-catching-ball-slow-motion-oucukj8

int[] arr = new int[]{1, 2, 3, 4, 5};try {

for (int i = 0; i <= arr.length; i++) {System.out.println("index: " + i

+ " element: " + arr[i]);}

} catch (Exception e){System.out.println("Caught an exception!\n"

+ e.getMessage());}

Page 8: 10/1/2018 Programming Data Structures · 2019-10-30 · characters of “A” for an ant, “D” for a doodlebug, “.” for an empty cell. Create a class named Critter that encapsulates

Exception Handling: Catching the exception

Finally: clean-up if needed (class demo 3)

8

http://www.omnissl.com/finalclean.html

int[] arr = new int[]{1, 2, 3, 4, 5};try {

for (int i = 0; i <= arr.length; i++) {System.out.println("index: " + i

+ " element: " + arr[i]);}

} catch (Exception e){System.out.println("Caught an exception!\n"

+ e.getMessage());return;

} finally {System.out.println("Exiting the main method!\n");

}

Page 9: 10/1/2018 Programming Data Structures · 2019-10-30 · characters of “A” for an ant, “D” for a doodlebug, “.” for an empty cell. Create a class named Critter that encapsulates

catch block

9

Catch is like a method call, is the parameter. is the parameter type. is the parameter name.

Throwing an exception

try {

for (int i = 0; i <= arr.length; i++) {

System.out.println("index: " + i + " element: " + arr[i]);

}

} catch (Exception e){

System.out.println("Caught an exception!\n" + e.getMessage());

}

Page 10: 10/1/2018 Programming Data Structures · 2019-10-30 · characters of “A” for an ant, “D” for a doodlebug, “.” for an empty cell. Create a class named Critter that encapsulates

throw an exception

10

Customized exception message

Print the message of the exception

try{

if (men == 0 && women == 0)

throw new Exception("Lesson is canceled. No students.");

else if (men == 0)

throw new Exception("Lesson is canceled. No men.");

else if (women == 0)

throw new Exception("Lesson is canceled. No women.");

// women >= 0 && men >= 0

if (women >= men)

System.out.println("Each man must dance with " +

women/(double)men + " women.");

else

System.out.println("Each woman must dance with " +

men/(double)women + " men.");

}

catch(Exception e){

String message = e.getMessage( );

System.out.println(message);}

Page 11: 10/1/2018 Programming Data Structures · 2019-10-30 · characters of “A” for an ant, “D” for a doodlebug, “.” for an empty cell. Create a class named Critter that encapsulates

Example (demo 4)

11

public static void main(String[] argv){

int waitTime = 46;

try{

System.out.println("Try block entered.");

if(waitTime > 30){

throw new Exception("Over 30");

} else if (waitTime < 30){

throw new Exception("Under 30");

} else{

System.out.println("No exception!");

}

System.out.println("Leaving the try block");

} catch (Exception e) {

System.out.println(e.getMessage());

}

System.out.println("After the catch block");

}

Page 12: 10/1/2018 Programming Data Structures · 2019-10-30 · characters of “A” for an ant, “D” for a doodlebug, “.” for an empty cell. Create a class named Critter that encapsulates

Throwing an exception and not catch it...

12

Throwing the exception out of the method

public static double safeDivide(int top, int bottom) throws Exception

{

if (bottom == 0)

throw new Exception("Division by Zero!");

return top/(double)bottom;

}

Page 13: 10/1/2018 Programming Data Structures · 2019-10-30 · characters of “A” for an ant, “D” for a doodlebug, “.” for an empty cell. Create a class named Critter that encapsulates

Catching the exception outside the method

13

try{System.out.println("Enter numerator:"); int numerator = keyboard.nextInt();System.out.println("Enter denominator:"); int denominator = keyboard.nextInt();

double quotient = safeDivide(numerator, denominator);System.out.println(numerator + "/"

+ denominator+ " = " + quotient);

}catch(Exception e){

System.out.println(e.getMessage( ));secondChance( );

}

Page 14: 10/1/2018 Programming Data Structures · 2019-10-30 · characters of “A” for an ant, “D” for a doodlebug, “.” for an empty cell. Create a class named Critter that encapsulates

14

https://medium.com/mindorks/try-catch-v-s-null-check-in-android-41ba3eba3b65

Page 15: 10/1/2018 Programming Data Structures · 2019-10-30 · characters of “A” for an ant, “D” for a doodlebug, “.” for an empty cell. Create a class named Critter that encapsulates

Assignment 07 (in-class) 2 pointsdue Tomorrow Oct. 2, noonAdding a throw-catch-finally scheme to your code of assignment 06. (or you can download the solution of assignment 06, and add your throw-catch-finally in the solution) so that, for the method, AssignCritters, in Class World:if the number of ants and doodlebugs is larger than the number of the grid, AssignCritters throws an exception with a message: “Too many critters in this small world!” and returns to the main method. Inside finally block, use System.out.println to print a message: “I’m in the finally block!”

Assignment 06 solution: https://sjiang1.github.io/classes/java2/18fall/assignment06-solution.zip

Upload your updated code (with the throw-catch-finally) to the Github repos: assignment07

15

Page 16: 10/1/2018 Programming Data Structures · 2019-10-30 · characters of “A” for an ant, “D” for a doodlebug, “.” for an empty cell. Create a class named Critter that encapsulates

Grading rules – Assignment 07Github: https://classroom.github.com/a/8e7LJSYx

• 1 point: if you submitted the assignment 06 code, and there are throw, catch, finally statements in the code, and it’s compilable

• 1 point: if the main method throws the exception at the throw statement, and the catch block catches it, and the finally block is executed after throw and catch. And the message of the exception is correct, i.e., “Too many critters in this small world!”

16

Page 17: 10/1/2018 Programming Data Structures · 2019-10-30 · characters of “A” for an ant, “D” for a doodlebug, “.” for an empty cell. Create a class named Critter that encapsulates

Assignment 08 (take-home) 3 points (1/5)due Friday Oct. 5, 10:00 pmP. 519, (partial) Programming Project 4

(You can use your code from assignment 06 or you can download the solution of assignment 06 at: https://sjiang1.github.io/classes/java2/18fall/assignment06-solution.zip)

The goal for this programming project is to create a simple 2D predator-prey simulation. In this simulation, the prey is ants, and the predators are doodlebugs. These critters live in a world composed of a 20x20 grid of cells. Only one critter may occupy a cell at a time. The grid is enclosed, so a critter is not allowed to move off the edges of the grid. Time is simulated in time steps. Each critter performs some action every time step.

17

Page 18: 10/1/2018 Programming Data Structures · 2019-10-30 · characters of “A” for an ant, “D” for a doodlebug, “.” for an empty cell. Create a class named Critter that encapsulates

Assignment 08 (take-home) 3 points (2/5)due Friday Oct. 5, 10:00 pmP. 519, (partial) Programming Project 4

The ants behave according to the following model:

• Move. Every time step, randomly try to move up, down, left, or right. If the cell in the selected direction is occupied or would move the ant off the grid, then the ant stays in the current cell.

• Breed. If an ant survives for three time steps, then at the end of the third time step (i.e., after moving), the ant will breed. This is simulated by creating a new ant in an adjacent (up, down, left, or right) cell that is empty. If there is no empty cell available, no breeding occurs. Once an offspring is produced, the ant cannot produce an offspring until three more time steps have elapsed.

18

Page 19: 10/1/2018 Programming Data Structures · 2019-10-30 · characters of “A” for an ant, “D” for a doodlebug, “.” for an empty cell. Create a class named Critter that encapsulates

Assignment 08 (take-home) 3 points (3/5)due Friday Oct. 5, 10:00 pmP. 519, (partial) Programming Project 4

The doodlebugs behave according to the following model:

• Move. Every time step, if there is an adjacent cell (up, down, left, or right) occupied by an ant, then the doodlebug will move to that cell and eat the ant. Otherwise, the doodlebug moves according to the same rules as the ant. Note that a doodlebug cannot eat other doodlebugs.

• (to be continued in the next week.)

19

Page 20: 10/1/2018 Programming Data Structures · 2019-10-30 · characters of “A” for an ant, “D” for a doodlebug, “.” for an empty cell. Create a class named Critter that encapsulates

Assignment 08 (take-home) 3 points (4/5)due Friday Oct. 5, 10:00 pmP. 519, (partial) Programming Project 4

During one turn, all the doodlebugs should move before the ants. Write a program to implement this simulation and draw the world using ASCII characters of “A” for an ant, “D” for a doodlebug, “.” for an empty cell. Create a class named Critter that encapsulates basic data common to both ants and doodlebugs.

This class should have an overridden method named move that is defined in the derived classes of Ant and Doodlebug. You may need additional data structures to keep track of which critters have moved.

20

Page 21: 10/1/2018 Programming Data Structures · 2019-10-30 · characters of “A” for an ant, “D” for a doodlebug, “.” for an empty cell. Create a class named Critter that encapsulates

Assignment 08 (take-home) 3 points (5/5)due Friday Oct. 5, 10:00 pmP. 519, (partial) Programming Project 4

Initialize the world with 5 doodlebugs and 100 ants. After each time step, prompt the user to press Enter to move to the next time step. You should see a cyclical pattern between the population of predators and prey, although random perturbations may lead to the elimination of one or both species.

21

Scanner scan = new Scanner(System.in);

while (true)

{

System.out.println("Press enter for next step");

scan.nextLine();

// do one step simulation

}

import java.util.Scanner;

Page 22: 10/1/2018 Programming Data Structures · 2019-10-30 · characters of “A” for an ant, “D” for a doodlebug, “.” for an empty cell. Create a class named Critter that encapsulates

Assignment 08 (take-home) 3 points (Extra)due Friday Oct. 5, 10:00 pmAn existing bug: critters move multiple steps at one "oneStepSimulation".

For example, an ant may move from grid[0][0] to grid[2][2] in "oneStepSimulation".

Why? How to fix it?

22

abstract public class Critter {

// mark whether the critter has been moved in a simulation step

private boolean moved;

public boolean moved(){return moved;}

public void setMoved(boolean m){moved = m;}

...

}

Page 23: 10/1/2018 Programming Data Structures · 2019-10-30 · characters of “A” for an ant, “D” for a doodlebug, “.” for an empty cell. Create a class named Critter that encapsulates

Grading rules – Assignment 08Github: https://classroom.github.com/a/O6qvP6we

• 1 point: fix the bug in the previous slide

• 1 point: Ants breed as described in slide Assignment 08 (2/5)

• 1 point: Doodlebugs move as described in slide Assignment 08 (3/5)

• Each bug based on its severity will cost 0.1 – 0.3 point.

23