Docsity

Java Arrays: Properties, Declaration, Powerful Features, and Usage, Slides of Applications of Computer Sciences

Partial preview of the text

Download Java Arrays: Properties, Declaration, Powerful Features, and Usage and more Slides Applications of Computer Sciences in PDF only on Docsity!

Arrays

Chapter 8 Overview

Today’s Lesson Plan

  • Discuss labs, quizzes, and TDA topics (10 min)
    • Discussion posts: some EXCELLENT posts this week!
  • Class Activities – covers Chapter 8 (140 min)
    • Discuss how to declare, create, and initialize one dimensional arrays. (30 min)
    • Discuss the Arrays class (30 min)
    • Discuss the Collection Framework. (20 min)
    • Discuss the ArrayList class. (30 min)
    • Discuss Two-Dimensional Arrays. (30 min)
  • Coding Exercises & break (90 min)
    • Code Ex 1 – p 351 – #6.8.8a & 8b
    • Code Ex 2 – p 421 – #8.4.
    • Code Ex 3 – p 435 – #8.5.13a & 13b Docsity.com

UML Diagram Types

  • Class
  • Object
  • State
  • Sequence
  • Activity
  • Use-case
    • Component
    • Deployment
    • Collaboration

Defining Class

A CLASS is a template (specification, blueprint) for a collection of objects that share a common set of attributes and operations.

HealthClubMember Class

Objects

attributes operations

Relationships

  • The three basic relationships include:
    • Association (between objects) – relationships that are described as “has-a”
    • Aggregation (between objects) – “composed-of” (strong aggregation/composition) and “consists-of” (weak aggregation) - Aggregations (weak and strong/composition) are considered a more tightly coupled type of association and also have the same multiplicities/cardinalities
    • Generalization (between classes) – “is-a”

Road Map

  • What is an Array?
  • Parts of an Array
    • Elements, Indexes, Subscripts, etc.
  • Declaring Arrays
  • Initializing Arrays
  • Passing arrays to methods

recap - data types

 primitive types  int, long, short, byte, float, double, boolean, char  objects  String, CD, Gymnasts, …  arrays  define a linear grouping of primitive types or objects

What exactly are we talking about?!

  • Let’s say you have ten students and you want to save the grades for each of them for use throughout your program (i.e., we need to remember every grade not just loop and count them or average them, etc.)
  • Could do it the hard way:
    • Set up ten variables called studentOneGrade, studentTwoGrade, studentThreeGrade, etc.
  • Very difficult to use and manipulate
    • Instead, could use an array to do it the easy way…Docsity.com

Using an array variable

  • Create an array variable called

studentGrades

  • Declared as follows:
    • int studentGrades[] = new int[10];
  • This sets up a location in memory for 10 integers

which can be referenced using studentGrades[#] where # is the particular student you want to look at.

array example

public class Test() { private int x1, x2,x3;

public Test() { x1 = 10; x2 = 20; x3 = 30; } }

declare 3 integer variables

private class TestArray() { private int [] x;

public TestArray() { x = new int[3]; x[0] = 10; x[1] = 20; x[2] = 30; }

declare 1 array with 3 elements

Parts of the array

  • The array has some terminology we haven’t

seen yet:

  • Elements
  • Index
    • Position Number (or Subscript)
  • Zeroth element