CS1101X: Programming Methodology Recitation 7 Arrays II.

Slides:



Advertisements
Similar presentations
EXAMPLES (Arrays). Example Many engineering and scientific applications represent data as a 2-dimensional grid of values; say brightness of pixels in.
Advertisements

1 Arrays An array is a special kind of object that is used to store a collection of data. The data stored in an array must all be of the same type, whether.
Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
Exercise 1 Suppose C is a class that implements interfaces I and J. Which of the following Requires a type cast? C c = ……? I i = …..? J j = …..? c =
1 Various Methods of Populating Arrays Randomly generated integers.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
CS102--Object Oriented Programming Discussion 2: (programming strategy in java) – Two types of tasks – The use of arrays Copyright © 2008 Xiaoyan Li.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
15 Sorting1June Sorting CE : Fundamental Programming Techniques.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Scanner class for input Instantiate a new scanner object Scanner in = new Scanner(System.in); Getting input using scanner – int i = scanner.nextInt() –
Sorting Example Insertion Sort. Insertion Sort Sorting problem: n Given an array of N integers, rearrange them so that they are in increasing order. Insertion.
CS1101X: Programming Methodology Recitation 9 Recursion II.
Chapter 4: Control Structures II
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
CS1101: Programming Methodology Aaron Tan.
Part 2. Searching Arrays Looking for a specific element in an array E.g., whether a certain score (85) is in a list of scores Linear search Binary search.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Arrays Arrays are objects that help us organize large amounts of information.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Programming Fundamentals I (COSC-1336), Lecture 8 (prepared after Chapter 7 of Liang’s 2011 textbook) Stefan Andrei 4/23/2017 COSC-1336, Lecture 8.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
2D-Arrays Quratulain. Learning Objectives Two-dimensional arrays Declaration Initialization Applications.
Arrays Pepper. What is an Array A box that holds many of the exact same type in mini-boxes A number points to the mini-box The number starts at 0 String.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism.
Information and Computer Sciences University of Hawaii, Manoa
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
CS 11 java track: lecture 2 This week: more on object-oriented programming (OOP) objects vs. primitive types creating new objects with new calling methods.
CS1101X: Programming Methodology Recitation 6 Arrays I.
2.3 The Java API Documentation. What follows are the contents of the Java Application Programming Interface (API) Documentation for the system supplied.
CS1101X: Programming Methodology Recitation 4 Design Issues and Problem Solving.
Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/19/20151.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 7.
A: A: double “4” A: “34” 4.
CS1101: Programming Methodology Preparing for Practical Exam (PE)
14 BirthMonth1February BirthMonth CE : Fundamental Programming Techniques.
Java Scanner Class Keyboard Class. User Interaction So far when we created a program there was no human interaction Our programs just simply showed one.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism.
Midterm 2 Review Notes on the CS 5 midterm Take-home exam due by 5:00 pm Sunday evening (11/14) Hand in your solutions under the door of my office, Olin.
CS1101X: Programming Methodology Recitation 6 Arrays I.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
2 Arrays Array is a data structure that represents a collection of the same type of data. A "fixed length list".
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
A 2-D Array is a structure that storage space both vertically and horizontally. Thus, the array has both rows and columns. 2-D Arrays are used to create.
Topic 21 arrays - part 1 Copyright Pearson Education, 2010 Based on slides by Marty Stepp and Stuart Reges from "Should.
Chapter 8 – Arrays and Array Lists
[ 4.00 ] [ Today’s Date ] [ Instructor Name ]
Engineering Problem Solving with C++, Etter
Computer Programming Methodology Input and While Loop
Building Java Programs
Building Java Programs
CSC 142 Computer Science II
CSC141 Computer Science I Zhen Jiang Dept. of Computer Science
Building Java Programs Chapter 7
תרגול Introduction to C - Fall Amir Menczel.
מיונים וחיפושים קרן כליף.
Building Java Programs
Building Java Programs
Building Java Programs
Example. Sort {5, 1, 12, -5, 16, 2, 12, 14} using selection sort. Complexity analysis.
Building Java Programs
Podcast Ch22a Title: Array-based Binary Trees
Building Java Programs
Computer Science Club 1st November 2019.
Presentation transcript:

CS1101X: Programming Methodology Recitation 7 Arrays II

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.

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

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)

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]); }

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; }

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

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

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.

CS1101X Recitation #710 Task 6: Sort Array of Points (2/6) For example, given The sorted array is:

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--; }

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); }

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

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

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]

CS1101X Recitation #716 Task 7: Best Route (Maximal Sum) (2/7) Input: dimensional array Quick analysis:

CS1101X Recitation #717 Task 7: Best Route (Maximal Sum) (3/7) Solution 1: Fill from top to bottom

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; }

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

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

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

CS1101X Recitation #722 End of Recitation #7