CS1101X: Programming Methodology Recitation 7 Arrays II.

22
CS1101X: Programming Methodology Recitation 7 Arrays II

Transcript of CS1101X: Programming Methodology Recitation 7 Arrays II.

CS1101X: Programming Methodology

Recitation 7 Arrays II

CS1101X Recitation #7 2

Class: Point2D.Double (1/2)

java.awt.geom Class Point2D.Double

public static class Point2D.Double extends Point2DThe Double class defines a point specified in double precision.

Field Summary double x           The X coordinate of this Point2D. double y           The Y coordinate of this Point2D.

Constructor SummaryPoint2D.Double()           Constructs and initializes a Point2D with coordinates (0, 0).Point2D.Double(double x, double y)           Constructs and initializes a Point2D with the specified coordinates.

CS1101X Recitation #7 3

Class: Point2D.Double (2/2)

Method Summary 

double getX()           Returns the X coordinate of this Point2D in double precision. 

double getY()           Returns the Y coordinate of this Point2D in double precision. 

void setLocation(double x, double y)           Sets the location of this Point2D to the specified double coordinates. 

String toString()           Returns a String that represents the value of this Point2D.

CS1101X Recitation #7 4

Task 5: Array of Objects (1/5)

Given a list of 2D points (use Point2D.Double class), find out which point is closest to the origin.

For example, given(2.5, 3.1), (-4.2, 1.5), (7.9, -0.23), (0.3, 1.4), (-1.02, -2.6)

The point closest to the origin is

(0.3, 1.4)

CS1101X Recitation #7 5

Task 5: Array of Objects (2/5)import java.awt.geom.*;import java.util.*;

public class ArrayOfPoints {

public static void main (String [] args) {

Point2D.Double[] points = createArray(); // printArray(points);

int index = findClosestPoint(points); System.out.print("Closest point to origin is "); printPoint(points[index]);

}

CS1101X Recitation #7 6

Task 5: Array of Objects (3/5) // Returns a new array of points from user's inputs public static Point2D.Double[] createArray () { Scanner scanner = new Scanner(System.in); int size = scanner.nextInt(); Point2D.Double[] arr = new Point2D.Double[size];

for (int i = 0; i < size; ++i) { double x = scanner.nextDouble(); double y = scanner.nextDouble(); arr[i] = new Point2D.Double(x, y); } return arr; }

CS1101X Recitation #7 7

Task 5: Array of Objects (4/5)

CS1101X Recitation #7 8

Task 5: Array of Objects (5/5)

CS1101X Recitation #7 9

Task 6: Sort Array of Points (1/6)

Sort the array of points created in task 5 in ascending order of the x-coordinates of the points.

If two points have the same x-coordinate, then they should be arranged in ascending order of their y-coordinates.

CS1101X Recitation #7 10

Task 6: Sort Array of Points (2/6)

For example, given

2.5 3.1

-4.2 1.5

7.9 -0.23

0.3 1.4

2.5 1.9

-1.02 -2.6

-4.2 -2.4

0.1 1.2

The sorted array is:

-4.2 -2.4

-4.2 1.5

-1.02 -2.6

0.1 1.2

0.3 1.4

2.5 1.9

2.5 3.1

7.9 -0.23

CS1101X Recitation #7 11

Task 6: Sort Array of Points (3/6)How do you adopt the following bubbleSort program? (Textbook page 625.)

public static void bubbleSort (int[] arr) { int temp; int bottom = arr.length - 2; boolean exchanged = true;

while (exchanged) { exchanged = false; for (int i = 0; i <= bottom; i++) { if (arr[i] > arr[i+1]) { temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; exchanged = true; } } bottom--; } }

CS1101X Recitation #7 12

Task 6: Sort Array of Points (4/6)import java.awt.geom.*;import java.util.*;

public class ArrayOfPoints { public static void main (String [] args) { Point2D.Double[] points = createArray();

int index = findClosestPoint(points); System.out.print("Closest point to origin is "); printPoint(points[index]);

// sort the array of points bubbleSort(points); System.out.println("Array after sorting: "); printArray(points);

}

CS1101X Recitation #7 13

Task 6: Sort Array of Points (5/6)

CS1101X Recitation #7 14

Task 6: Sort Array of Points (6/6)

CS1101X Recitation #7 15

Task 7: Best Route (Maximal Sum) (1/7)

Write a program that calculates the highest sum of numbers passed on a route that starts at the top and ends somewhere on the base.

Each step can go either diagonally down to the left or diagonally down to the right.

The number of rows in the triangle is > 1 but <= 100. The numbers in the triangle are integers in [0, 99].

7

3 8

1 087

5

2 44

4 2 6 5

CS1101X Recitation #7 16

Task 7: Best Route (Maximal Sum) (2/7)

Input:573 88 1 02 7 4 44 5 2 6 5

2-dimensional array

Quick analysis:

CS1101X Recitation #7 17

Task 7: Best Route (Maximal Sum) (3/7)

Solution 1: Fill from top to bottom

7

3 8

1 08

7

5

2 44

4 2 6 5

CS1101X Recitation #7 18

Task 7: Best Route (Maximal Sum) (4/7)

import java.util.*;public class MaximalSum { public static void main (String [] args) { int[][] table = createArray(); // printArray(table);

System.out.println("Answer = " + maximalSum(table)); }

// Returns a new 2D array of integers from user's inputs public static int[][] createArray () { Scanner scanner = new Scanner(System.in); int size = scanner.nextInt(); int[][] arr = new int[size][size];

for (int r = 0; r < size; ++r) { for (int c = 0; c <= r; ++c) { arr[r][c] = scanner.nextInt(); return arr; }

CS1101X Recitation #7 19

Task 7: Best Route (Maximal Sum) (5/7)

CS1101X Recitation #7 20

Task 7: Best Route (Maximal Sum) (6/7)

CS1101X Recitation #7 21

Task 7: Best Route (Maximal Sum) (7/7)

CS1101X Recitation #7 22

End of Recitation #7