Introduction

# Introduction

Télécharger la présentation

## Introduction

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. Section 13.1 Introduction

2. Algorithm Definition An algorithm is a step-by-step solution to a problem.

3. Niklaus Wirth’s Programming Language Definition Niklaus Wirth, the creator of the programming language Pascal, made the following equation about data structures and algorithms. Data Structures + Algorithms = Programs

4. Algorithm Example:Find the average of 5 numbers 1. Enter 5 numbers. 2. Add them and store the total. 3. Divide the total by 5. The result is the average. 4. Display the average.

5. Section 13.2 Using Java’s Random Class

6. When is a Random# not a Random#? This section will seem somewhat strange. You are going to learn how to generate random numbers that are not random. It probably sounds quite weird, but later in the chapters you will see good reasons for controlling the random numbers. Right now for starters let us review generating true random integers with the Expo.random(min,max) method.

7. // Java1301.java // This program reviews generating random numbers with the <Expo.random> method. public class Java1301 { public static void main(String args[]) { System.out.println("\nGenerating numbers in the [10..99] range\n"); for (int k = 0; k < 100; k++) { int randInt1 = Expo.random(10,99); System.out.print(randInt1 + " "); } System.out.println("\n\n"); System.out.println("\nGenerating numbers in the [100..999] range\n"); for (int k = 0; k < 100; k++) { int randInt2 = Expo.random(100,999); System.out.print(randInt2 + " "); } System.out.println("\n\n"); System.out.println("\nGenerating numbers in the [1000..9999] range\n"); for (int k = 0; k < 100; k++) { int randInt3 = Expo.random(1000,9999); System.out.print(randInt3 + " "); } System.out.println("\n\n"); } }

8. Execution #1

9. Execution #2

10. // Java1302.java // This program introduces the <Random> class and the <nextInt> method. The <nextInt> method // generates a random integer in the [0..n-1] range, if n is the integer parameter passed to <nextInt>. import java.util.Random; public class Java1302 { public static void main(String args[]) { Random rand = new Random(); System.out.println("\nGenerating numbers in the [0..99] range\n"); for (int k = 0; k < 100; k++) { int randInt1 = rand.nextInt(100); System.out.print(randInt1 + " "); } System.out.println("\n\n"); System.out.println("\nGenerating numbers in the [0..999] range\n"); for (int k = 0; k < 100; k++) { int randInt1 = rand.nextInt(1000); System.out.print(randInt1 + " "); } System.out.println("\n\n"); System.out.println("\nGenerating numbers in the [0..9999] range\n"); for (int k = 0; k < 100; k++) { int randInt1 = rand.nextInt(10000); System.out.print(randInt1 + " "); } System.out.println("\n\n"); } }

11. Execution #1

12. Execution #2

13. Random Class and nextInt Method Java has a Random class to generate random integers. An object must be constructed first like: Random rand = new Random(); Random integers are then generated with method nextInt, like int n1 = rand.nextInt(100); // random int in [0..99] range int n2 = rand.nextInt(500); // random int in [0..499] range int n3 = rand.nextInt(n); // random int in [0..n-1] range

14. // Java1303.java // This program shows that the <Random> object, like all objects, can be any name, and frequently // the lower-case class name is used. It also shows how to control the random integer range. import java.util.Random; public class Java1303 { public static void main(String args[]) { Random random = new Random(); System.out.println("\nGenerating numbers in the [10..99] range \n"); for (int k = 0; k < 100; k++) { int randInt1 = random.nextInt(90) + 10; System.out.print(randInt1 + " "); } System.out.println("\n\n"); System.out.println("\nGenerating numbers in the [100..999] range \n"); for (int k = 0; k < 100; k++) { int randInt1 = random.nextInt(900) + 100; System.out.print(randInt1 + " "); } System.out.println("\n\n"); System.out.println("\nGenerating numbers in the [1000..9999] range \n"); for (int k = 0; k < 100; k++) { int randInt1 = random.nextInt(9000) + 1000; System.out.print(randInt1 + " "); } System.out.println("\n\n"); } }

15. Execution #1

16. Execution #2

17. Random Integer Algorithm • 1. Determine how many different integers will be generated, called the range, which is done using: • int range = max - min + 1; • Use the range value in the parameter of nextInt, like: • intrandomInt = rand.nextInt(range); • Add the minimum value to step 2, like: • intrandomInt = rand.nextInt(range) + min;

18. // Java1304.java // In this program the program user is able to enter the minimum // and the maximum integer value of the random number range. // Essentially, the program now behaves like the <Expo.random> method. import java.util.Random; public class Java1304 { public static void main(String args[]) { Random random = new Random(); System.out.print("Enter minimum random integer ===>> "); int min = Expo.enterInt(); System.out.print("Enter maximum random integer ===>> "); int max = Expo.enterInt(); intrange = max - min + 1; System.out.println("\nGenerating numbers in the ["+min+".."+max+"] range\n"); for (int k = 0; k < 100; k++) { intrandomInt = random.nextInt(range) + min; System.out.print(randomInt + " "); } System.out.println("\n\n"); } }

19. Execution #1 Execution #2

20. // Java1305.java // A small, but significant change is made in this program. In line-13 the <Random> // constructor now has a parameter value. The result is that each time the random // number set will be identical. This is called a "RANDOM SEED". import java.util.Random; public class Java1305 { public static void main(String args[]) { Randomrandom=new Random(12345);// Note the different constructor System.out.print("Enter minimum random integer ===>> "); int min = Expo.enterInt(); System.out.print("Enter maximum random integer ===>> "); int max = Expo.enterInt(); System.out.println("\nGenerating numbers in the ["+min+".."+max+"] range\n"); for (int k = 0; k < 100; k++) { int range = max - min + 1; intrandomInt = random.nextInt(range) + min; System.out.print(randomInt + " "); } System.out.println("\n\n"); } }

21. Execution #1 Execution #2 Execution #3

22. How Is This Useful? Example. At Rubix Cube competitions, every contestant’s cube is scrambled the exact same way in order to be completely fair. If the contest was done on computer with a Rubix Cube Simulator, it would make sense that the computer randomly scrambles the cube, but to be fair, every cube needs to be scrambled the exact same way!

23. // MyRandom.java // This class is an example of a user-defined class that // simplifies operating with some Java class, like <Random>. import java.util.Random; public class MyRandom { private Random random; public MyRandom() { random = new Random(); } public MyRandom(int seed) { random = new Random(seed); } public int nextRandom(int min, int max) { int range = max - min + 1; int randomInt = random.nextInt(range) + min; return randomInt; } }

24. // Java1306.java // This program tests the user-defined <MyRandom> class. // The first group of numbers will be different in both executions. // The second group of numbers will be identical in both executions. public class Java1306 { public static void main(String args[]) { System.out.print("Enter minimum random integer ===>> "); int min = Expo.enterInt(); System.out.print("Enter maximum random integer ===>> "); int max = Expo.enterInt(); MyRandom rand1 = new MyRandom(); System.out.println("\nGenerating true random numbers in the ["+min+".."+max+"] range\n"); for (int k = 0; k < 100; k++) { int randomInt = rand1.nextRandom(min,max); System.out.print(randomInt + " "); } System.out.println("\n\n"); MyRandom rand2 = new MyRandom(1234); System.out.println("\nGenerating pseudo random numbers in the ["+min+".."+max+"] range\n"); for (int k = 0; k < 100; k++) { int randomInt = rand2.nextRandom(min,max); System.out.print(randomInt + " "); } System.out.println("\n\n"); } }

25. Execution #1 Execution #2

26. Truly Random & Pseudo Random Truly Random Values Random rand1 = new Random(); Pseudo Random Values Random rand2 = new Random(12345); The parameter value used in the Random constructor is the starting seed of the random number generation. The same sequence of integers will be generated every time the same starting seed value is used.

27. Section 13.3 The List class Case Study

28. The List Class Case Study Back in Chapter 11, Arrays were introduced. However, we had our arrays separate from the methods. This is not in the true flavor of OOP Encapsulation. Before we get too involved with Algorithms, we will create and build a List class. The creation of a List class allows storage of array elements along with the actions or methods that process the array. In this chapter you will learn some of the common algorithms used in computer science. These algorithms are independent of a programming language. The syntax implementation in sample programs may be specific to Java, but the algorithmic considerations carry across all program languages. The List case study will grow with each new algorithm.

29. // Java1307.java // List case study #1 // The first stage of the List case study public class Java1307 { public static void main(String args[]) { List1 array1 = new List1(15); array1.display(); List1 array2 = new List1(15,999); array2.display(); array2.assign(); array2.display(); System.out.println(); } }

30. class List1 { private int intArray[]; // stores array elements private int size; // number of elements in the array public List1(int s) { System.out.println("\nCONSTRUCTING NEW LIST OBJECT WITH DEFAULT VALUES"); size = s; intArray = new int[size]; } public List1(int s, int n) { System.out.println("\nCONSTRUCTING NEW LIST OBJECT WITH SPECIFIED VALUES"); size = s; intArray = new int[size]; for (int k = 0; k < size; k++) intArray[k] = n; } public void assign() { System.out.println("\nASSIGNING RANDOM VALUES TO LIST OBJECT"); MyRandom rand = new MyRandom(12345); for (int k = 0; k < size; k++) intArray[k] = rand.nextRandom(1000,9999); } public void display() { System.out.println("\nDISPLAYING ARRAY ELEMENTS"); for (int k = 0; k < size; k++) System.out.print(intArray[k] + " "); System.out.println(); } }

31. Java1307.java Output

32. Section 13.4 Improving Input & Output

33. // Java1308.java List case study #2 // This stage adds a third constructor, which instantiates an array object with // a specified set of random numbers. Old methods, like the first two constructors, // which are not tested are removed for better program brevity and clarity. public class Java1308 { public static void main(String args[]) { List2 array1 = new List2(15,0,100); array1.display(); List2 array2 = new List2(15,100,999); array2.display(); List2 array3 = new List2(15,0,1); array3.display(); List2 array4 = new List2(15,500,505); array4.display(); System.out.println(); } }

34. class List2 { private int intArray[ ]; // stores array elements private int size; // number of elements in the array private int minInt; // smallest random integer private int maxInt; // largest random integer public List2(int s, int min, int max) { size = s; System.out.println("\nCONSTRUCTING LIST WITH VALUES in [" + min + ".." + max + "] range"); intArray = new int[size]; MyRandom rand = new MyRandom(12345); for (int k = 0; k < size; k++) intArray[k] = rand.nextRandom(min,max); } public void display() { System.out.println("\nDISPLAYING ARRAY ELEMENTS"); for (int k = 0; k < size; k++) System.out.print(intArray[k] + " "); System.out.println(); } }

35. Java1308.java Output

36. // Java1309.java List case study #3 // This program uses <Expo.pause>, which freezes output display until // the Enter key is pressed. This method allows output viewing on the // monitor when the display becomes too large. public class Java1309 { public static void main(String args[]) { List3 array1 = new List3(60,100,200); array1.display(); Expo.pause(); List3 array2 = new List3(100,100,999); array2.display(); Expo.pause(); List3 array3 = new List3(200,10,19); array3.display(); Expo.pause(); List3 array4 = new List3(40,500,505); array4.display(); Expo.pause(); System.out.println(); } }

37. Java1309.java Output

38. Java1309.java Output

39. Java1309.java Output

40. Java1309.java Output

41. // Java1310.java List case study #4 // This program allows all list information to be entered at the keyboard // before a list object is constructed. public class Java1310 { public static void main(String args[]) { System.out.print("\nEnter list size ===>> "); int listSize = Expo.enterInt(); System.out.print("Enter minimum value ===>> "); int listMin = Expo.enterInt(); System.out.print("Enter maximum value ===>> "); int listMax = Expo.enterInt(); List4 array = new List4(listSize,listMin,listMax); array.display(); Expo.pause(); System.out.println(); } }

42. Java1310.java Output

43. Section 13.5 The Linear Search

44. // Java1311.java List case study #5 // This program introduces the "inefficient" Linear Search algorithm. public class Java1311 { public static void main(String args[]) { System.out.print("\nEnter list size ===>> "); int listSize = Expo.enterInt(); System.out.print("Enter minimum value ===>> "); int listMin = Expo.enterInt(); System.out.print("Enter maximum value ===>> "); int listMax = Expo.enterInt(); List5 array = new List5(listSize,listMin,listMax); array.display(); Expo.pause(); System.out.print("\nEnter search number ===>> "); int searchNumber = Expo.enterInt(); if (array.linearSearch(searchNumber)) System.out.println(searchNumber + " is in the list"); else System.out.println(searchNumber + " is not in the list"); System.out.println(); } }

45. The Inefficient Linear Search public boolean linearSearch(int sn) { boolean found = false; for (int k = 0; k < size; k++) if (intArray[k] == sn) found = true; return found; } • There are 2 problems with this algorithm: • It will keep searching to the end even if it has already • found the desired item. • 2) When an item is found, it does not tell you where it is.

46. Java1311.java Output #1

47. Java1311.java Output #2

48. // Java1312.java List case study #6 // The inefficient linear search is replaced with a conditional loop, which stops // the repetition once the searchNumber is found. public class Java1312 { public static void main(String args[]) { System.out.print("\nEnter list size ===>> "); int listSize = Expo.enterInt(); System.out.print("Enter minimum value ===>> "); int listMin = Expo.enterInt(); System.out.print("Enter maximum value ===>> "); int listMax = Expo.enterInt(); List6 array = new List6(listSize,listMin,listMax); array.display(); Expo.pause(); System.out.print("\nEnter search number ===>> "); int searchNumber = Expo.enterInt(); if (array.linearSearch(searchNumber)) System.out.println(searchNumber + " is in the list"); else System.out.println(searchNumber + " is not in the list"); System.out.println(); } }

49. An Efficient Linear Search public boolean linearSearch(int sn) { boolean found = false; int k = 0; while (k < size && !found) { if (intArray[k] == sn) found = true; else k++; } return found; } This algorithm does stop when the desired element is found, but it still does not tell us where it is.

50. Java1312.java Output #1