Presentation is loading. Please wait.

Presentation is loading. Please wait.

A table is a useful way to capture a discrete function. Days Per Month MonthLength 1) Jan 31 2) Feb 28 3) Mar 31... 12) Dec 31 Cost Per Burger QuantityItem.

Similar presentations


Presentation on theme: "A table is a useful way to capture a discrete function. Days Per Month MonthLength 1) Jan 31 2) Feb 28 3) Mar 31... 12) Dec 31 Cost Per Burger QuantityItem."— Presentation transcript:

1 A table is a useful way to capture a discrete function. Days Per Month MonthLength 1) Jan 31 2) Feb 28 3) Mar 31... 12) Dec 31 Cost Per Burger QuantityItem Cost 0 $0.00 1 $1.50 2 $1.40 3 $1.35 4 or more $1.30 Mileage Between Cities AlbanyAtlanta BoiseMiami Albany 0 1025 2527 1441 Atlanta 1025 0 2214 653 Boise 2527 2214 0 2861 Miami 1441 653 2861 0 Decoding Table Letter A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Cypher X C B H T Z I D G Y Q P N M U L K W V E O S R A J F

2 Often an array is an efficient way to store tabular information. Cost Per Burger QuantityItem Cost 0 $0.00 1 $1.50 2 $1.40 3 $1.35 4 or more $1.30 double[] burgerCost = {0.0, 1.5, 1.4, 1.35, 1.3}; The following code displays the cost for quantity burgers (assuming a non-negative quantity). if (quantity < burgerCost.length) { System.out.println( quantity * burgerCost[quantity] ); } else { System.out.println( quantity * burgerCost[burgerCost.length-1] ); } This is an example of table-driven code. The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

3 Table-driven code is generally a good alternative to multi-way selection. Cost Per Burger QuantityItem Cost 0 $0.00 1 $1.50 2 $1.40 3 $1.35 4 or more $1.30 Table-Driven Version if (quantity < burgerCost.length) { System.out.println( quantity * burgerCost[quantity] ); } else { System.out.println( quantity * burgerCost[burgerCost.length-1] ); } What are the advantages to table-driven code? Selection Version if (quantity == 0) { System.out.println( 0 ); } else if (quantity == 1) { System.out.println( 1.5 ); } else if (quantity == 2) { System.out.println( 2 * 1.4 ); } else if (quantity == 3) { System.out.println( 3 * 1.35 ); } else if (quantity >= 4) { System.out.println( quantity * 1.3 ); } The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

4 Sometimes the index numbering is different than the table. Days Per Month MonthLength 1) Jan 31 2) Feb 28 3) Mar 31... 12) Dec 31 Decoding Table Letter A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Cypher X C B H T Z I D G Y Q P N M U L K W V E O S R A J F int[] monthLength = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; Sometimes the the table is indexed with non-numerics. char[] cypher = {‘X’, ‘C’, ‘B’, ‘H’, ‘T’, ‘Z’, ‘I’, ‘D’, ‘G’, ‘Y’, ‘Q’, ‘P’, ‘N’, ‘M’, ‘U’, ‘L’, ‘K’, ‘W’, ‘V’, ‘E’, ‘O’, ‘S’, ‘R’, ‘A’, ‘J’, ‘F’ };

5 Using constants ( final variables) can be helpful for table-driven code. int[][] distance = { {0, 1025 2527 1441}, {1025, 0, 2214, 653}, {2527, 2214, 0, 2861}, {1441, 653, 2861, 0} }; int final albany = 0; int final atlanta = 1; int final boise = 2; int final miami = 3; Mileage Between Cities AlbanyAtlanta BoiseMiami Albany 0 1025 2527 1441 Atlanta 1025 0 2214 653 Boise 2527 2214 0 2861 Miami 1441 653 2861 0 The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.


Download ppt "A table is a useful way to capture a discrete function. Days Per Month MonthLength 1) Jan 31 2) Feb 28 3) Mar 31... 12) Dec 31 Cost Per Burger QuantityItem."

Similar presentations


Ads by Google