Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1101X: Programming Methodology Recitation 7 Arrays II.

Similar presentations


Presentation on theme: "CS1101X: Programming Methodology Recitation 7 Arrays II."— Presentation transcript:

1 CS1101X: Programming Methodology Recitation 7 Arrays II

2 CS1101X Recitation #72 Class: Point2D.Double (1/2) java.awt.geom Class Point2D.Double public static class Point2D.Double extends Point2DPoint2D The Double class defines a point specified in double precision. Field Summary double x The X coordinate of this Point2D. x double y The Y coordinate of this Point2D.y Constructor Summary Point2D.DoublePoint2D.Double() Constructs and initializes a Point2D with coordinates (0, 0). Point2D.DoublePoint2D.Double(double x, double y) Constructs and initializes a Point2D with the specified coordinates.

3 CS1101X Recitation #73 Class: Point2D.Double (2/2) Method Summary double getX() Returns the X coordinate of this Point2D in double precision. getX double getY() Returns the Y coordinate of this Point2D in double precision. getY void setLocation(double x, double y) Sets the location of this Point2D to the specified double coordinates. setLocation String toString() Returns a String that represents the value of this Point2D.toString

4 CS1101X Recitation #74 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)

5 CS1101X Recitation #75 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]); }

6 CS1101X Recitation #76 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; }

7 CS1101X Recitation #77 Task 5: Array of Objects (4/5)

8 CS1101X Recitation #78 Task 5: Array of Objects (5/5)

9 CS1101X Recitation #79 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.

10 CS1101X Recitation #710 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

11 CS1101X Recitation #711 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--; }

12 CS1101X Recitation #712 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); }

13 CS1101X Recitation #713 Task 6: Sort Array of Points (5/6)

14 CS1101X Recitation #714 Task 6: Sort Array of Points (6/6)

15 CS1101X Recitation #715 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 38 108 7 5 244 4265

16 CS1101X Recitation #716 Task 7: Best Route (Maximal Sum) (2/7) Input: 5 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 2-dimensional array Quick analysis:

17 CS1101X Recitation #717 Task 7: Best Route (Maximal Sum) (3/7) Solution 1: Fill from top to bottom 7 38 108 7 5 244 4265

18 CS1101X Recitation #718 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; }

19 CS1101X Recitation #719 Task 7: Best Route (Maximal Sum) (5/7)

20 CS1101X Recitation #720 Task 7: Best Route (Maximal Sum) (6/7)

21 CS1101X Recitation #721 Task 7: Best Route (Maximal Sum) (7/7)

22 CS1101X Recitation #722 End of Recitation #7


Download ppt "CS1101X: Programming Methodology Recitation 7 Arrays II."

Similar presentations


Ads by Google