Week 2 - Review Arrays
Week 2 - Review Arrays
John Lewis
William Loftus
0 1 2 3 4 5 6 7 8 9
scores 79 87 94 82 67 98 87 81 74 91
scores[2] = 89;
scores[first] = scores[first] + 2;
mean = (scores[0] + scores[1])/2;
System.out.println ("Top = " + scores[5]);
pick = scores[rand.nextInt(11)];
scores 79
87
94
82
The name of the array 67
is an object reference 98
variable
87
81
74
91
Copyright © 2012 Pearson Education, Inc.
Declaring Arrays
• The scores array could be declared as follows:
int[] scores = new int[10];
• Note that the array type does not specify its size,
but each object of that type has a specific size
• See BasicArray.java
import java.util.Scanner;
continue
continue
System.out.println ();
System.out.println ("Non-alphabetic characters: " + other);
}
}
import java.util.Scanner;
continue
words -
-
-
-
-
words "friendship"
"loyalty"
"honor"
-
-
//-----------------------------------------------------------------
// Constructor: Sets up this Grade object with the specified
// grade name and numeric lower bound.
//-----------------------------------------------------------------
public Grade (String grade, int cutoff)
{
name = grade;
lowerBound = cutoff;
}
//-----------------------------------------------------------------
// Returns a string representation of this grade.
//-----------------------------------------------------------------
public String toString()
{
return name + "\t" + lowerBound;
}
continue
Copyright © 2012 Pearson Education, Inc.
continue
//-----------------------------------------------------------------
// Name mutator.
//-----------------------------------------------------------------
public void setName (String grade)
{
name = grade;
}
//-----------------------------------------------------------------
// Lower bound mutator.
//-----------------------------------------------------------------
public void setLowerBound (int cutoff)
{
lowerBound = cutoff;
}
continue
//-----------------------------------------------------------------
// Name accessor.
//-----------------------------------------------------------------
public String getName()
{
return name;
}
//-----------------------------------------------------------------
// Lower bound accessor.
//-----------------------------------------------------------------
public int getLowerBound()
{
return lowerBound;
}
}
import java.text.NumberFormat;
//-----------------------------------------------------------------
// Creates a new DVD with the specified information.
//-----------------------------------------------------------------
public DVD (String title, String director, int year, double cost,
boolean bluRay)
{
this.title = title;
this.director = director;
this.year = year;
this.cost = cost;
this.bluRay = bluRay;
}
continue
Copyright © 2012 Pearson Education, Inc.
continue
//-----------------------------------------------------------------
// Returns a string description of this DVD.
//-----------------------------------------------------------------
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
String description;
if (bluRay)
description += "\t" + "Blu-Ray";
return description;
}
}
import java.text.NumberFormat;
//-----------------------------------------------------------------
// Constructor: Creates an initially empty collection.
//-----------------------------------------------------------------
public DVDCollection ()
{
collection = new DVD[100];
count = 0;
totalCost = 0.0;
}
continue
//-----------------------------------------------------------------
// Adds a DVD to the collection, increasing the size of the
// collection array if necessary.
//-----------------------------------------------------------------
public void addDVD (String title, String director, int year,
double cost, boolean bluRay)
{
if (count == collection.length)
increaseSize();
continue
//-----------------------------------------------------------------
// Returns a report describing the DVD collection.
//-----------------------------------------------------------------
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
return report;
}
continue
//-----------------------------------------------------------------
// Increases the capacity of the collection by creating a
// larger array and copying the existing collection into it.
//-----------------------------------------------------------------
private void increaseSize ()
{
DVD[] temp = new DVD[collection.length * 2];
collection = temp;
}
}
System.out.println (movies);
System.out.println (movies);
}
}
Copyright © 2012 Pearson Education, Inc.
//********************************************************************
//
Output
// Movies.java Author: Lewis/Loftus
System.out.println (movies);
}
}
Copyright © 2012 Pearson Education, Inc.
//********************************************************************
//
Output
// Movies.java Author: Lewis/Loftus
System.out.println (movies);
}
}
Copyright © 2012 Pearson Education, Inc.
Arrays of Objects
• A UML diagram for the Movies program:
• See NameTag.java
if (list.length != 0)
{
int sum = 0;
for (int num : list)
sum += num;
result = (double)num / list.length;
}
return result;
}
//-----------------------------------------------------------------
// Constructor: Sets up this family by storing the (possibly
// multiple) names that are passed in as parameters.
//-----------------------------------------------------------------
public Family (String ... names)
{
members = names;
}
continue
//-----------------------------------------------------------------
// Returns a string representation of this family.
//-----------------------------------------------------------------
public String toString()
{
String result = "";
return result;
}
}
System.out.println(lewis);
System.out.println();
System.out.println(camden);
}
}
one two
dimension dimensions
• See TwoDArray.java
• See SodaSurvey.java
import java.text.DecimalFormat;
continue
System.out.println ();
for (int person=0; person < PEOPLE; person++)
System.out.println ("Person #" + (person+1) + ": " +
fmt.format ((float)personSum[person]/SODAS));
}
}