Java Programming: Program Design Including Data Structures 1 Vectors The class Vector can be used to implement a list Unlike an array, the size of a Vector.

Slides:



Advertisements
Similar presentations
Chapter 21 Implementing lists: array implementation.
Advertisements

Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
Static Members, Structures, Enumerations, Generic Classes, Namespaces Learning & Development Team Telerik Software Academy.
More methods and classes, 4 of 4 Math 130 Introduction to Programming Lecture # 18 Monday, October 8, 2007.
OO Programming Objectives for today: Casting Objects Introduction to Vectors The instanceof keyword.
Chapter 3 - Java Programming With Supplied Classes1 Chapter 3 Java Programming With Supplied Classes.
Objectives Learn about objects and reference variables Explore how to use predefined methods in a program.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
Alice in Action with Java
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Java Implementation: Part 3S CompSci 230 Software Construction.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
Applications of Arrays (Searching and Sorting) and Strings
Comp 248 Introduction to Programming Chapter 6 Arrays Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Vectors, Strings, and Enumeration Data Types.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Java Programming: From Problem Analysis to Program Design, 4e
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 9: Arrays J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second Edition.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration Lecture 23.
Chapter 14: Searching and Sorting
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
Java 1.5 The New Java Mike Orsega Central Carolina CC.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
CONTENTS Wrapper Class Enumeration Garbage Collection Import static.
Recitation 5 Enums and The Java Collections classes/interfaces 1.
Array Declarations Arrays contain a fixed number of variables of identical type Array declaration and allocation are separate operations Declaration examples:
Introduction to C# By: Abir Ghattas Michel Barakat.
Chapter 10: Class Vector and String, and Enumeration Types Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
1 Enums (Chapter 4) To enumerate is: to name things one after another in a list Java has a type, called an enum, where a programmer specifies a finite.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays days until the AP Computer Science test.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
Java Programming: Chapter 9: Arrays
Week 6 - Friday.  What did we talk about last time?  Loop examples.
1 Chapter 9 Arrays Java Programming from Thomson Course Tech, adopted by kcluk.
Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
Array contiguous memory locations that have the same name and type. Note: an array may contain primitive data BUT an array is a data structure a collection.
1 1  Lecture 11 – Structured Data FTMK, UTeM – Sem /2014.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
Chapter Structured Data 11. Combining Data into Structures 11.2.
ENUMERATED TYPES Java Basics. Enumerated Type Topics Why use them? Defining an enumerated type Using an enumerated type Advanced features.
Introduction to Enumerations CMSC 202. Enumerated Values Enumerated values are used to represent a set of named values. Historically in Java (and other.
1 Arrays and Variable Length Parameter List  The syntax to declare a variable length formal parameter (list) is: dataType... identifier.
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
Chapter VII: Arrays.
Sixth Lecture ArrayList Abstract Class and Interface
Primitive Types Vs. Reference Types, Strings, Enumerations
Method Mark and Lyubo.
An Introduction to Java – Part II
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Lecture 9 Objectives Learn about arrays.
Grouped Data Arrays, and Array Lists.
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Presentation transcript:

Java Programming: Program Design Including Data Structures 1 Vectors The class Vector can be used to implement a list Unlike an array, the size of a Vector object can grow/shrink during program execution You do not need to worry about the number of data elements in a vector

Java Programming: Program Design Including Data Structures 2 Members of the class Vector

Java Programming: Program Design Including Data Structures 3 Members of the class Vector (continued)

Java Programming: Program Design Including Data Structures 4 Members of the class Vector (continued)

Java Programming: Program Design Including Data Structures5 Members of the class Vector (continued)

Java Programming: Program Design Including Data Structures 6 Vectors (continued) Every element of a Vector object is a reference variable of the type Object To add an element into a Vector object: – Create appropriate object – Store data into object – Store address of object holding data into Vector object element

Declaring a vector object import java.util.*; or import java.util.Vector; Vector Vector stringList = new Vector (); Every component is an object Must use the wrapper classes Integer, Double, etc Vector list = new Vector ();

Java Programming: Program Design Including Data Structures8 Vector stringList = new Vector (); stringList.addElement("Spring"); stringList.addElement("Summer"); stringList.addElement("Fall"); stringList.addElement("Winter"); Vectors (continued)

Arrays of Objects Remember: int arr[] = new int[100]; Same with Objects

Java Programming: Program Design Including Data Structures 10 Additional String Methods

Java Programming: Program Design Including Data Structures11 Additional String Methods (continued)

Java Programming: Program Design Including Data Structures12 Additional String Methods (continued)

Java Programming: Program Design Including Data Structures 13 Additional String Methods (continued)

Arrays of String Objects String[] nameList = new String[10]; Declares and instantiates an array of 10 reference variables nameList[0] = “Sue Jones”; nameList[1] = “Tom Smith”; Etc. Creates String object, Allocates space

Arrays of String Objects output all names for (int index = 0; index < namesList.length;index++) System.out.println(namesList[index] + “ “); Access objects in the list if (nameList[0].equals (“Ray Rodgers”))

Arrays of Objects of Other Classes Clock[] arrivalTimeEmp[] = new Clock[100]; Declares and instantiates an array of 100 reference variables Instantiate Clock objects for (int ix = 0; ix < arrivalTimeEmp.length;ix++) arrivalTimeEmp [ix] = new Clock();//calls constructor Access objects in the list arrivalTimeEmp [40].setTime(7,30,5); for (int ix = 0; ix < arrivalTimeEmp.length;ix++) System.out.println(arrivalTimeEmp [ix] );

Arrays and Variable Length Parameter List To declare a variable length parameter list datatype … identifier dataType – primitive type or Java class Identifier is a variable length formal parameter A method can have both a variable length formal parameter and other formal parameters public static void myMethod (String name, double num, int … intList) A method can have at most one variable length formal parameter Variable length formal parameter must be last in the formal parameter list

example of variable length parameter list //method with formal argument public static double largest(double … numList) { double max; int ix; if (numList.length != 0) { max = list[0]; for (ix = 1; ix < numList.length;ix++) { if (max < numList[ix]) max = numList[ix]; } return max; } return 0.0; } //examples of actual arguments num1 = largest(34,56); num2 = largest(12.3,17.8, 56.8); num3 = largest(1.0,2.34,55,67.8); double [] list = {18.3, 56.7, 88.2, 37.8, 99.1, 26.4, 14.9}; num4 = largest(list);

Enumerated Values Enumerated values are used to represent a set of named values. Historically in Java (and other languages), these were often stored as constants. For example, in Java... public static final int SUIT_CLUBS = 0; public static final int SUIT_DIAMONDS = 1; public static final int SUIT_HEARTS = 2; public static final int SUIT_SPADES = 3;

Issues with this Approach There are, however, a number of issues with this approach. –Acceptable values are not obvious –No type safety –No name-spacing –Not printable

enum An enum type is a type whose fields consist of a fixed set of constants. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week. Because they are constants, the names of an enum type's fields are in uppercase letters. Use the enum keyword. For example, you would specify a days-of-the-week enum type as: public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }

enum Grades {A, B, C, D, F}; enum Status {Freshman, Sophomore, Junior, Senior} enum LabColor {Chocolate, Yellow, Black}; Grades myGrade; myGrade = Grade.B; Can be used with switch