CSC 2014 Java Bootcamp. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Similar documents
Arrays - Review. Two-Dimensional Arrays. Arrays, Part 2. Dr. Papalaskari 1. CSC 1051 Data Structures and Algorithms I

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Arrays - Review. Initializer Lists. The for-each Loop. Arrays, Part 2. Dr. Papalaskari 1. CSC 1051 Data Structures and Algorithms I

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Arrays - Review. Arrays as Parameters. Arrays as Parameters. Arrays and File Input. Dr. Papalaskari 1. CSC 1051 Data Structures and Algorithms I

Arrays and File Input

Arrays and File Input

Chapter 8 Arrays. Java Software Solutions. Foundations of Program Design Seventh Edition. John Lewis William Loftus

7.3 Arrays of Strings (Objects) 7.3 Arrays of Strings (Objects) 7.3 Tunes.java. 7.3 Arrays of Objects 9/11/13. ! A UML diagram for the Tunes program

CSC 1051 Data Structures and Algorithms I

Chapter 7. Arrays are objects that help us organize large amounts of information

b[0] = 1 b[1] = 2 b[2] = 3 b[3] = 4 b[4] = 5 c[0] = 2 c[1] = 3 c[2] = 4 c[3] = 5 c[4] = 6

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

CSC 1051 Algorithms and Data Structures I. Final Examination December 20, Name: KEY. Question Value Score

Arrays, Part 3: Multidimensional arrays

Arrays Chapter 7. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Selection Statements and operators

Selection Statements and operators

Introduction to Arrays

class declaration Designing Classes part 2 Getting to know classes so far Review Next: 3/18/13 Driver classes:

CSC 1051 Algorithms and Data Structures I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

Basics of Java Programming variables, assignment, and input

Java Bootcamp - Villanova University. CSC 2014 Java Bootcamp. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Algorithms and Conditionals

CSC 1051 Data Structures and Algorithms I

ECE 122. Engineering Problem Solving with Java

CSC 1051 Algorithms and Data Structures I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Designing Classes. Where do objects come from? Where do objects come from? Example: Account datatype. Dr. Papalaskari 1

Where do objects come from? Good question!

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Iterators and File Input

Designing Classes part 2

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

CSC 1051 Data Structures and Algorithms I

Review: Array Initializer Lists

Algorithms in everyday life. Algorithms. Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Algorithms in everyday life. Algorithms. Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Selection and Repetition Revisited

Lecture 3: Variables and assignment

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Conditional Statements

Selection and Repetition Revisited

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

Java I/O and Control Structures

Improved algorithm. Java code. Control flow and conditionals CSC 1051 Villanova University. Dr Papalaskari 1. Java Programè Algorithm

Java code. Updated program. Control flow and conditionals CSC 1051 Villanova University. Dr Papalaskari 1. Java Programè Algorithm. Improved algorithm

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name: Question Value Score

Java I/O and Control Structures Algorithms in everyday life

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

CSC 1051 Algorithms and Data Structures I. Final Examination May 12, Name

Control flow, conditionals, boolean expressions, block statements, nested statements. Course website:

File I/O and Exceptions

CSC 1051 Algorithms and Data Structures I. Final Examination May 12, Name: KEY. Question Value Score

Designing Classes. Where do objects come from? Where do objects come from? Example: Account datatype. Dr. Papalaskari 1

Using Classes and Objects

2: Basics of Java Programming

Selection and Repetition

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

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 7, Name:

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name:

COE 211 Computer Programming. Welcome to Exam I Tuesday March 13, 2018

Graphical User Interfaces

Programming - 2. Common Errors

CSC 1051 Algorithms and Data Structures I. Final Examination December 17, Name:

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Using Classes and Objects. Chapter

Selection and Repetition

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Graphical User Interfaces

Applets and the Graphics class

CS159. Nathan Sprague. September 30, 2015

Graphical User Interfaces

CS 101 Fall 2006 Midterm 3 Name: ID:

ECE 122 Engineering Problem Solving with Java

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

System.out.print("Enter sales for salesperson " + i + ": "); sales[i] = scan.nextint();

CS 101 Spring 2006 Final Exam Name: ID:

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Array indexing. Before we use an array, we have to declare it. For example, int[] height=new int[11];

Designing Classes Part 2

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

JAVA Ch. 4. Variables and Constants Lawrenceville Press

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

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

Data Representation and Applets

Practice Questions for Chapter 9

Data Representation Classes, and the Java API

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

CS 302 Week 9. Jim Williams

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

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

Introduction to Computer Science II (CSI 1101)

Data Representation and Applets

CSC 1214: Object-Oriented Programming

Transcription:

Arrays of objects CSC 2014 Java Bootcamp Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Some slides in this presentation are adapted from the slides accompanying Java Software Solutions by Lewis & Loftus

Example: An array of Strings String[] words = new String[5]; At this point, the following line of code would throw a NullPointerException: words - - System.out.println(words[0]); - - -

Example: An array of Strings String[] words = new String[5]; Now, store some String objects in the array: words[0]= "friendship"; words[1] = "loyalty"; words[2] = "honor ; words - - "friendship" "loyalty" "honor"

Arrays of Objects The following declaration creates an array object called verbs and fills it with four String objects created using string literals String[] verbs = "play", "work", "eat", "sleep", "run";

Arrays of Objects Example: managing a collection of DVD objects Movies.java DVDCollection.java DVD.java

//******************************************************************** // Movies.java Author: Lewis/Loftus // // Demonstrates the use of an array of objects. //******************************************************************** public class Movies // Creates a DVDCollection object and adds some DVDs to it. Prints // reports on the status of the collection. public static void main (String[] args) DVDCollection movies = new DVDCollection(); movies.adddvd ("The Godfather", "Francis Ford Coppala", 1972, 24.95, true); movies.adddvd ("District 9", "Neill Blomkamp", 2009, 19.95, false); movies.adddvd ("Iron Man", "Jon Favreau", 2008, 15.95, false); movies.adddvd ("All About Eve", "Joseph Mankiewicz", 1950, 17.50, false); movies.adddvd ("The Matrix", "Andy & Lana Wachowski", 1999, 19.95, true); System.out.println (movies); Movies.java movies.adddvd ("Iron Man 2", "Jon Favreau", 2010, 22.99, false); movies.adddvd ("Casablanca", "Michael Curtiz", 1942, 19.95, false); System.out.println (movies);

//******************************************************************** // Movies.java Output Author: Lewis/Loftus // // Demonstrates ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ the use of an array of objects. //******************************************************************** My DVD Collection public class Movies Number of DVDs: 5 Total cost: $98.30 // Average Creates cost: a DVDCollection $19.66 object and adds some DVDs to it. Prints // reports on the status of the collection. DVD List: public static void main (String[] args) $24.95 1972 The Godfather Francis Ford Coppala Blu-Ray DVDCollection movies = new DVDCollection(); $19.95 2009 District 9 Neill Blomkamp $15.95 movies.adddvd 2008 ("The Iron Godfather", Man "Francis Jon Favreau Ford Coppala", 1972, 24.95, true); $17.50 movies.adddvd 1950 ("District All About 9", Eve "Neill Joseph Blomkamp", Mankiewicz 2009, 19.95, false); $19.95 movies.adddvd 1999 ("Iron The Matrix Man", "Jon Favreau", Andy & Lana 2008, Wachowski 15.95, false); Blu-Ray movies.adddvd ("All About Eve", "Joseph Mankiewicz", 1950, 17.50, false); continue movies.adddvd ("The Matrix", "Andy & Lana Wachowski", 1999, 19.95, true); System.out.println (movies); movies.adddvd ("Iron Man 2", "Jon Favreau", 2010, 22.99, false); movies.adddvd ("Casablanca", "Michael Curtiz", 1942, 19.95, false); System.out.println (movies);

//******************************************************************** // Movies.java Output Author: Lewis/Loftus // // Demonstrates ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ the use of an array of objects. //******************************************************************** My DVD Collection Output (continued) public class Movies Number of DVDs: 5 Total ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ cost: $98.30 // Average Creates My DVD cost: a DVDCollection $19.66 object and adds some DVDs to it. Prints // reports on the status of the collection. DVD List: Number of DVDs: 7 public static Total void cost: main $141.24 (String[] args) $24.95 Average 1972 cost: The Godfather $20.18 Francis Ford Coppala Blu-Ray DVDCollection movies = new DVDCollection(); $19.95 2009 District 9 Neill Blomkamp $15.95 movies.adddvd 2008 List: ("The Iron Godfather", Man "Francis Jon Favreau Ford Coppala", 1972, 24.95, true); $17.50 movies.adddvd 1950 ("District All About 9", Eve "Neill Joseph Blomkamp", Mankiewicz 2009, 19.95, false); $19.95 movies.adddvd $24.95 1999 1972 ("Iron The Matrix Man", The Godfather "Jon Favreau", Andy Francis & Lana 2008, Wachowski 15.95, Ford Coppala false); Blu-Ray Blu-Ray movies.adddvd $19.95 2009 ("All About District Eve", "Joseph 9 Neill Mankiewicz", Blomkamp 1950, 17.50, false); continue movies.adddvd $15.95 2008 ("The Matrix", Iron Man "Andy & Lana Jon Wachowski", Favreau 1999, 19.95, true); $17.50 1950 All About Eve Joseph Mankiewicz System.out.println (movies); $19.95 1999 The Matrix Andy & Lana Wachowski Blu-Ray movies.adddvd $22.99 2010 ("Iron Man Iron 2", Man "Jon 2 Favreau", Jon Favreau 2010, 22.99, false); movies.adddvd $19.95 1942 ("Casablanca", Casablanca "Michael Curtiz", Michael 1942, Curtiz 19.95, false); System.out.println (movies);

//******************************************************************** // DVDCollection.java Author: Lewis/Loftus // // Represents a collection of DVD movies. //******************************************************************** import java.text.numberformat; public class DVDCollection private DVD[] collection; private int count; private double totalcost; DVDCollection.java // Constructor: Creates an initially empty collection. public DVDCollection () collection = new DVD[100]; count = 0; totalcost = 0.0; continue

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(); collection[count] = new DVD (title, director, year, cost, bluray); totalcost += cost; count++; continue

continue // Returns a report describing the DVD collection. public String tostring() NumberFormat fmt = NumberFormat.getCurrencyInstance(); String report = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"; report += "My DVD Collection\n\n"; report += "Number of DVDs: " + count + "\n"; report += "Total cost: " + fmt.format(totalcost) + "\n"; report += "Average cost: " + fmt.format(totalcost/count); report += "\n\ndvd List:\n\n"; for (int dvd = 0; dvd < count; dvd++) report += collection[dvd].tostring() + "\n"; return report; continue

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]; for (int dvd = 0; dvd < collection.length; dvd++) temp[dvd] = collection[dvd]; collection = temp;

//******************************************************************** // DVD.java Author: Lewis/Loftus // // Represents a DVD video disc. //******************************************************************** import java.text.numberformat; public class DVD private String title, director; private int year; private double cost; private boolean bluray; DVD.java // 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

continue // Returns a string description of this DVD. public String tostring() NumberFormat fmt = NumberFormat.getCurrencyInstance(); String description; description = fmt.format(cost) + "\t" + year + "\t"; description += title + "\t" + director; if (bluray) description += "\t" + "Blu-Ray"; return description;