Lecture 17. For Array Class Shenanigans

Similar documents
Lecture 14. 'for' loops and Arrays

Lecture 15. Arrays (and For Loops)

Chapter 7: Arrays and the ArrayList Class

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

Combo Lecture Lecture 14/15. Instructor: Craig Duckett

static String usersname; public static int numberofplayers; private static double velocity, time;

Last Class. While loops Infinite loops Loop counters Iterations

Arrays. Weather Problem Array Declaration Accessing Elements Arrays and for Loops Array length field Quick Array Initialization Array Traversals

Loops. CSE 114, Computer Science 1 Stony Brook University

Linked Lists. private int num; // payload for the node private Node next; // pointer to the next node in the list }

Lecture (07) Arrays. By: Dr. Ahmed ElShafee. Dr. Ahmed ElShafee, ACU : Fall 2015, Programming I

Lecture 02, Fall 2018 Friday September 7

Last Class. Introduction to arrays Array indices Initializer lists Making an array when you don't know how many values are in it

Lecture 17. Instructor: Craig Duckett. Passing & Returning Arrays

Introduction to Programming Using Java (98-388)

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

CS 231 Data Structures and Algorithms, Fall 2016

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Computer Science is...

Example: Computing prime numbers

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

FORM 2 (Please put your name and form # on the scantron!!!!)

Class C{ int a; } what variables below are objects : a. C c; b. String str; c. Scanner scanner; d. int num; e. float f;

Object Oriented Programming. Java-Lecture 6 - Arrays

Lab Assignment Three

Last Class. More on loops break continue A bit on arrays

About this exam review

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters

Example Program. public class ComputeArea {

H212 Introduction to Software Systems Honors

1.00 Introduction to Computers and Engineering Problem Solving Quiz 1 March 4, 2005

Arrays. Outline 1/7/2011. Arrays. Arrays are objects that help us organize large amounts of information. Chapter 7 focuses on:

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Chapter 4: Loops and Files

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2014

Declarations and Access Control SCJP tips

Declaring and ini,alizing 2D arrays

Guessing Game with Objects

Java is an objet-oriented programming language providing features that support

Programming II (CS300)

Birkbeck (University of London) Software and Programming 1 In-class Test Mar Answer ALL Questions

ECE 122. Engineering Problem Solving with Java

Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming

Arrays. Lecture 11 CGS 3416 Fall October 26, 2015

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

Programming Languages and Techniques (CIS120)

GROUP - A <10 questions x 4 points each = 40 points >

Lecture 3. Lecture

Pointers. Memory. void foo() { }//return

Basics of Java: Expressions & Statements. Nathaniel Osgood CMPT 858 February 15, 2011

Pointers in C/C++ 1 Memory Addresses 2

Arrays. Lecture 11 CGS 3416 Spring March 6, Lecture 11CGS 3416 Spring 2017 Arrays March 6, / 19

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Learning objec-ves. Declaring and ini-alizing 2D arrays. Prin-ng 2D arrays. Using 2D arrays Decomposi-on of a solu-on into objects and methods

Module 7: Arrays (Single Dimensional)

CSC Java Programming, Fall Java Data Types and Control Constructs

Java Coding 3. Over & over again!

CS 1044 Program 6 Summer I dimension ??????

Chapter 4: Loops and Files

AP Computer Science Java Mr. Clausen Program 6A, 6B

ICOM 4015-Advanced Programming. Spring Instructor: Dr. Amir H. Chinaei. TAs: Hector Franqui, Jose Garcia, and Antonio Tapia. Reference: Big Java

Arrays and the TOPICS CHAPTER 8. CONCEPT: An array can hold multiple values of the same data type simultaneously.

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

Arrays. COMS W1007 Introduction to Computer Science. Christopher Conway 10 June 2003

CSE143 Summer 2008 Final Exam Part B KEY August 22, 2008

1B1b Classes in Java Part I

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Appendix: Common Errors

CMPSCI 187: Programming With Data Structures. Lecture 12: Implementing Stacks With Linked Lists 5 October 2011

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

Odds and Ends. Think about doing research Programming grading. Assignment 3 due Tuesday

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency

Assignment 2.4: Loops

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2013

Java Bytecode (binary file)

Topics. Java arrays. Definition. Data Structures and Information Systems Part 1: Data Structures. Lecture 3: Arrays (1)

Wrapper Classes double pi = new Double(3.14); 3 double pi = new Double("3.14"); 4... Zheng-Liang Lu Java Programming 290 / 321

Getting started with Java

TOPICS TO COVER:-- Array declaration and use.

Guessing Game with Objects

CSE143 Summer 2008 Final Exam Part A KEY August 21, 2008

Absolute C++ Walter Savitch

Arrays Classes & Methods, Inheritance

Exercise 4: Loops, Arrays and Files

Programming Languages and Techniques (CIS120)

Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue

Java Review. Fundamentals of Computer Science

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Lecture 13 & 14. Single Dimensional Arrays. Dr. Martin O Connor CA166

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Building a Java First-Person Shooter

Principles of Computer Science I

CS1150 Principles of Computer Science Loops (Part II)

array Indexed same type

Slide 1 CS 170 Java Programming 1 Multidimensional Arrays Duration: 00:00:39 Advance mode: Auto

Timing for Interfaces and Abstract Classes

CMSC 341 Lecture 7 Lists

Outline. Why Java? (1/2) Why Java? (2/2) Java Compiler and Virtual Machine. Classes. COMP9024: Data Structures and Algorithms

Arrays. Lecture 9 COP 3014 Fall October 16, 2017

Constants are named in ALL_CAPS, using upper case letters and underscores in their names.

Transcription:

Lecture 17 For Array Class Shenanigans

For or While? class WhileDemo { public static void main(string[] args){ int count = 1; while (count < 11) { System.out.println("Count is: " + count); count++; Note: They both evaluate the predicate expression (boolean check) before proceeding to the enclosed code block. class ForLoopDemo { public static void main(string[] args { for(int count = 1; count < 11; count++){ System.out.println("Count is: " + count);

For or While? class WhileDemo { public static void main(string[] args){ int count = 1; while (count!= 0) { System.out.println("Count is: " + count); count = keyboard.nextint(); Note: These are both valid pieces of code. class ForLoopDemo { public static void main(string[] args { for(int count = 1; count!= 0; ){ System.out.println("Count is: " + count); count = keyboard.nextint();

Array Reminder So far, you have been working with variables that hold only one value. The integer variables you have set up have held only one number (and later in the quarter we will see how string variables will hold one long string of text). An array is a collection to hold more than one value at a time. It's like a list of items. Think of an array as like the columns in a spreadsheet. You can have a spreadsheet with only one column, or several columns. grades The data held in a single-list (one-dimensional) array might look like this: 0 100 1 89 2 96 3 100 4 98

grades = new int[5]; grades is a named reserved space set aside to hold exactly five [5] 32-bit elements all initializing to a value of zero 0. As we have learned about programming languages, the index always starts at 0, not 1, and procedes until the size of the array is reached. In our example, since we declared [5] the array element index starts with 0 and ends at 4. array element index space reserved for data value initialized in element 0 1 2 3 4 32-bits 32-bits 32-bit 32-bits 32-bits 0 0 0 0 0 index grades 0 100 1 89 2 96 3 100 4 98 5 grades grades = new int[5]; // <-- Steps 1 & 2 grades[0] = 100; // Steps 3 grades[1] = 89; grades[2] = 96; grades[3] = 100; grades[4] = 98; STEP 1: Declare Variable STEP 2: Allocate Memory STEP 3: Initialize Elements 0 1 2 3 4 100 89 96 100 98

About Array Sizes The array size must be a non-negative number. It may be a literal value, a constant, or variable. final int ARRAY_SIZE = 6; int[] numbers = new int[array_size]; Once created, an array size is fixed and cannot be changed.

Off-by-one Errors It is very easy to be off-by-one when accessing arrays // This code has an off-by-one error. int[] numbers = new int[100]; for (int i = 1; i <= 100; i++) // Would work with < only { numbers[i] = 99; Here, the equal sign allows the loop to continue on to index 100, but 99 is the last index in the array This code would throw an ArrayIndexOutOfBoundsException

More Array Declarations Previously we showed arrays being declared: int[] numbers; However, the brackets can also go here: int numbers[]; These are equivalent but the first style is typical (and preferred by most developers/coders). Multiple arrays can be declared on the same line. int[] numbers, codes, scores; With the alternate notation each variable must have brackets. int numbers[], codes[], scores; The scores variable in this instance is simply an int variable.

Array of Objects Declaration Card[] deck = new Card [52]; // declares an array of type Card In this case, each element is initialized to null pointer. SO, we need to initialize each element and give it a value int index = 0; for (int suit = 0; suit < 4; suit++) { for (int rank = 1; rank <= 13; rank++) { deck[index] = new Card (suit, rank); // call the constructor index++;

Enhanced For-loop Simplified array processing (read only) Always goes through all elements General: for(datatype elementvariable : array) { statement; int[] numbers = {3, 6, 9; for(int val : numbers) { // <-- Only two parts. You can read the line as // "iterate on elements from the collection named numbers. The current // element will be referenced by the int val." System.out.println("The next value is " + val);

Encapsulation Encapsulate: to show or express the main idea or quality of (something) in a brief way, to completely cover (something) especially so that it will not touch anything else In programming, encapsulation refers to the bundling of data with the methods that operate on that data. Groups related data and methods Suggests protecting (making private) object attributes Reduces collisions of like-named variables Allows for refactoring by making code segments independent

Class Separation GuessingGame Class Attributes: RandomNumber MaxGuesses NumberGuesses GuessArray Methods: ResetGame PlayGame SetDifficulty Game Program: Instantiates GuessingGame Text entry interface Game App: Instantiates GuessingGame Cell Phone graphical app Quest with Mini-games: Instantiates GuessingGame Guessing game used as conflict resolution