Introduction to Programming with Java, for Beginners Arrays of Objects.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 5 Array and Collections.
Advertisements

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Arrays.
1 SSD3 - Unit 2 Java toString & Equals Presentation Class Website:
Introduction to Programming with Java, for Beginners “Has a” Relationship.
Introduction to Application Programming IST 256 Application Programming for Information Systems Xiaozhong Liu
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Programming with Collections Collections in Java Using Arrays Week 9.
Java Syntax Primitive data types Operators Control statements.
Loops – While, Do, For Repetition Statements Introduction to Arrays
CS 106 Introduction to Computer Science I 02 / 20 / 2008 Instructor: Michael Eckmann.
Repetition Structures: For Loop Constants CSC 1401: Introduction to Programming with Java Week 5 Wanda M. Kunkle.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Arrays, Conditionals & Loops in Java. Arrays in Java Arrays in Java, are a way to store collections of items into a single unit. The array has some number.
An Introduction to C Programming Geb Thomas. Learning Objectives Learn how to write and compile a C program Learn what C libraries are Understand the.
Question Hot Seat public MyConstructor (int a, int b) { index = a+b; } A.Parameters B.Class Name C.Body D.Modifier Please put match these choices to these.
CISC474 - JavaScript 03/02/2011. Some Background… Great JavaScript Guides: –
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
Java Arrays [Lists] in 10 Minutes. Declaring an array (and its types) int[] myIntArray; double[] myDoubleArray; String[] myStrings; //What’s the pattern?
Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: Arrays.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Chapter 8: Arrays.
Introducing Objects. Structure  Objects have two parts: Instance Variables (attributes, adjectives) Instance Variables (attributes, adjectives) private.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
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.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Other Variable Types Dim lab as String makes a box that can store a label tag Dim ColHead As String ColHead = “function” ColHead function Dim lab as Boolean.
CMSC 341 Java Packages, Classes, Variables, Expressions, Flow Control, and Exceptions.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
2016 N5 Prelim Revision. HTML Absolute/Relative addressing in HTML.
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
A: A: double “4” A: “34” 4.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Classes and Objects - Part I. What is an Object? An Object has two primary components: state – properties of the object behavior – operations the object.
11 PART 2 ARRAYS. 22 PROCESSING ARRAY ELEMENTS Reassigning Array Reference Variables The third statement in the segment below copies the address stored.
Arrays Chapter 7. 2 Declaring and Creating Arrays Recall that an array is a collection of elements all of the _____________ Array objects in Java must.
Arrays, cont MONDAY – APRIL 6, Review from lab Deep vs Shallow copy array1array array2 = array1; What happens?
Arrays What is an array… –A data structure that holds a set of homogenous elements (of the same type) –Associate a set of numbers with a single variable.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
02/09/2005 Introduction to Programming with Java, for Beginners Arrays (of primitives)
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
Asserting Java © Rick Mercer Chapter 7 The Java Array Object.
© Rick Mercer Chapter 7 The Java Array Object.  Some variables store precisely one value: a double stores one floating-point number a double stores one.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Introduction to programming in java Lecture 21 Arrays – Part 1.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
Review for Test2. Scope 8 problems, 60 points. 1 Bonus problem (5 points) Coverage: – Test 1 coverage – Exception Handling, Switch Statement – Array of.
Topics introduced today (these topics would be covered in more detail in later classes) – Primitive Data types Variables Methods “for” loop “if-else” statement.
Relational Operator and Operations
Arrays Chapter 7.
Agenda Warmup Finish 2.4 Assignments
Java for Android is specific
Objects First with Java CITS1001
Review Operation Bingo
Creating Objects in a Few Simple Steps
Chapter 7 The Java Array Object © Rick Mercer.
Arrays Chapter 7.
Recap Week 2 and 3.
CISC181 Introduction to Computer Science Dr
Review for Test1.
Fundamental Programming
Barb Ericson Georgia Institute of Technology Oct 2005
Presentation transcript:

Introduction to Programming with Java, for Beginners Arrays of Objects

1 Array of Primitives int[] data; data = new int[3]; data[0] = 5; data[1] = 10;

2 Array of Objects counters[0]=new Counter(); counters[0].inc(); counters[1]=new Counter(); Counter[] counters; counters = new Counter[3];

3 “Traversing” Arrays of Objects We’ve used loops to traverse arrays of primitives We can do the same with arrays of objects Write a method that Takes one argument: an array of counters Returns the sum of numbers contained in the counters (Assume that each array element points to a valid counter) Then adjust the method so it will handle arrays for which some or all of its elements are null

4 Declaring & Initializing Object Arrays  Person[] people = {new Person("jo"),new Person("flo")}; Person[] people = new Person[] {new Person("jo"), new Person("flo")};  Point p1 = new Point(0,0);  Point[] points1 = {p1, new Point(0, 10)};  Point[] points2 = new Point[] {p1, new Point(0, 10)}; Note: The advantage of using the “new type[ ]” syntax is that it can be used in an assignment statement that is not a variable declaration statement.

5 public class Person{ private String name; private int age; Person(String name, int age){ this.name = name; this.age = age; } public int getAge() { return age; } public String getName() { return name; } } E.g. Person Database

6 E.g. Person Database (contd..) public class PersonDB{ private Person[] people; public PersonDB(){ people = new Person [] {new Person("jo",25), new Person("flo",18), new Person("mo", 19)}; } /** Calculates and returns the average age. */ public double getAverageAge(){ return 0; // complete this method } /** Returns true if name is in database, otherwise false */ public boolean isInDatabase(String searchName){ return false; // complete, using String.equals() }

7 E.g. Person Database (contd..) Person[] people; people = new Person[3]; people[0]=new Person(“jo”); people[1]=new Person(“flo”);