Creating and Modifying Text part 3

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Unit 261 Introduction to Searching and Sorting Comparable Interface Comparator Interface Algorithm Complexity Classes Exercises.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Georgia Institute of Technology Making Text for the Web part 4 Barb Ericson Georgia Institute of Technology March 2006.
Georgia Institute of Technology Creating and Modifying Text part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
Georgia Institute of Technology Creating and Modifying Text part 4 Barb Ericson Georgia Institute of Technology Oct 2005.
CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann.
Recitation 4 Abstract classes, Interfaces. A Little More Geometry! Abstract Classes Shape x ____ y ____ Triangle area() base____ height ____ Circle area()
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005.
10-Nov-15 Java Object Oriented Programming What is it?
Georgia Institute of Technology More on Creating Classes part 2 Barb Ericson Georgia Institute of Technology Oct 2005.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Georgia Institute of Technology More on Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
CS2 Module 26 Category: OO Concepts Topic: Interfaces Objectives –Interfaces.
Today’s lecture Review of chapter 8 Go over examples.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
04-ManipulatingPictures-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology June 2008.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Comp1004: Environments The Java Library. Coming up Recap – Encapsulation – Constructors – Loops – Arrays – ArrayList – Iterators The Java Library – Implementation.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
The AP Java Subset Topics. A Topics Primitive Types int double boolean.
Lecture 5:Interfaces and Abstract Classes
Using the Java Collection Libraries COMP 103 # T2
Computer Organization and Design Pointers, Arrays and Strings in C
Sixth Lecture ArrayList Abstract Class and Interface
Java and OOP Part 5 – More.
Chapter 20 Generic Classes and Methods
USING ECLIPSE TO CREATE HELLO WORLD
COP 3503 FALL 2012 Shayan Javed Lecture 8
Interfaces I once attended a Java user group meeting where James Gosling (Java's inventor) was the featured speaker. During the memorable Q&A session,
Creating and Modifying Text part 2
CS Week 10 Jim Williams, PhD.
Interfaces and Inheritance
Topic 7 Interfaces I once attended a Java user group meeting where James Gosling (one of Java's creators) was the featured speaker. During the memorable.
Phil Tayco Slide version 1.0 Created Oct 16, 2017
Introduction to Processing Digital Sounds part 2
CMSC 202 Interfaces.
Can store many of the same kind of data together
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Writing Methods AP Computer Science A.
Introduction to Java part 2
Arrays versus ArrayList
Can store many of the same kind of data together
Object-Oriented Programming Paradigm
Object Oriented Programming in java
Grouped Data Arrays, and Array Lists.
Barb Ericson Georgia Institute of Technology Oct 2005
Workshop for Programming And Systems Management Teachers
Manipulating Pictures, Arrays, and Loops
slides created by Ethan Apter
CMSC 202 Interfaces.
Can store many of the same kind of data together
Barb Ericson Georgia Institute of Technology June 2005
Introduction to Java part 2
Making Text for the Web part 6
More on Creating Classes
Review: libraries and packages
TCSS 143, Autumn 2004 Lecture Notes
Barb Ericson Georgia Institute of Technology Oct 2005
Web Design & Development Lecture 6
ArrayLists Readings: 10.1 (pg. 559 – 571).
CMSC 202 Interfaces.
Arrays.
Intro to Programming (in JavaScript)
Presentation transcript:

Creating and Modifying Text part 3 Barb Ericson Georgia Institute of Technology Oct 2005 Georgia Institute of Technology

Georgia Institute of Technology Learning Goals Computing concepts What is a dynamic array (ArrayList)? Finding the methods of ArrayList and List What is an interface? Implementing an interface Using casting or generics with collections Creating students by reading delimited strings from a file Georgia Institute of Technology

Georgia Institute of Technology java.util.ArrayList An ArrayList object is an array that can grow or shrink as needed Do we always know how many students can be in a class period? If we create an array for more than we have, we waste space If we try to add a student past the end of the array We get an exception Use an ArrayList when you don’t know how many of something you need Georgia Institute of Technology

Georgia Institute of Technology ArrayList Methods Look in the Java API for ArrayList Open the class java.util Click on the class ArrayList What methods let you add an object to the ArrayList? What method lets you get an object from the ArrayList? What method tells you how many things are in the ArrayList? What method lets you remove an object from an index in the ArrayList? Georgia Institute of Technology

Georgia Institute of Technology An ArrayList is a List Look at the API for ArrayList It implements the List interface When a class implements an interface it can be said to be of the interface type. So since an ArrayList implements the List interface it is a kind of List. Georgia Institute of Technology

Georgia Institute of Technology Interfaces An interface is an abstract class that only has public abstract methods and/or constants An abstract method is a method without any code like the compareTo method in Comparable package java.lang; public interface Comparable { public int compareTo(Object o); } Useful for saying what methods a class needs to have The class that implements the interface must provide the code for the methods in the interface The String class implements the Comparable interface Georgia Institute of Technology

Georgia Institute of Technology Exercise Modify the Student class to implement the Comparable interface public class Student implements Comparable Try to compile it It won’t compile till you add the compareTo method And provide code for it Create a compareTo method to compare the current student to a passed in one You can compare the names Since the String class implements Comparable Georgia Institute of Technology

What the compareTo Method Returns The compareTo method returns an integer Negative if the current object is less than the passed object 0 if they are equal Positive if the current object is greater than the passed object You need to cast from Object to Student Before you can compare names Student testStudent = (Student) o; Georgia Institute of Technology

Final compareTo Method If o is null this will cause a NullPointerException If o isn’t a Student it will cause a ClassCastException This will say two students with the same name are equal Okay for sorting by name public int compareTo(Object o) { Student testStudent = (Student) o; return this.name.compareTo(testStudent.name); } Georgia Institute of Technology

Georgia Institute of Technology Decoupling Classes One of the goals of Object-Oriented Programming Is to decouple classes Make class A not dependant on class B so that you can change out B for C Interfaces let you do this Variables can be declared to be the interface type List studentList = null; Then any class that implements this interface can be used studentList = new ArrayList(); Georgia Institute of Technology

Georgia Institute of Technology Other Interfaces LEGO bricks have a common interface Makes it easy to connect two bricks It doesn’t matter what you connect as long as the interface is the same A USB interface Allows you to connect different devices to your computer USB drive, camera, etc As long as they use the USB interface Georgia Institute of Technology

Georgia Institute of Technology ArrayList Exercise In the ClassPeriod class Modify the studentArray to be a studentList List studentList = new ArrayList(); Change all the methods that use an array to use a list Cast back to Student when you pull the object out of the list public Student getStudent(int index) { return (Student) this.studentList.get(index); } Georgia Institute of Technology

Collections Store Objects Why do we need to cast the Student object back to Student when we pull it back out of a list? A list is a collection of objects We need to tell the compiler that it is really a Student object Or, if we are using Java 1.5 we can use generics List<Student> studentList = new ArrayList(); Then we wouldn’t need to cast to Student Casting from Object to Student is also called downcasting. Going from Student to Object is called upcasting and doesn’t require any casting. Think of this like casting from double to int. Georgia Institute of Technology

Add a Constructor that takes a File Name Let’s add a constructor to the ClassPeriod class that takes a file name to read the student information from public ClassPeriod(String name, int num, String fileName) { this.teacherName = name; this.periodNumber = num; loadStudentsFromFile(fileName); } Georgia Institute of Technology

Create Students from File Exercise Write the method loadStudentsFromFile(fileName); It will be similar to the readAndPrintFile method in SimpleReader It will loop reading from the specified file Until the line that is returned from the reader is null It will use each line that it reads to create a Student object Using the constructor that takes a delimited string It will add each new student to the studentList Georgia Institute of Technology

Georgia Institute of Technology Testing the Method public static void main(String[] args) { ClassPeriod period = new ClassPeriod("Ms. Clark",5,"student.txt"); // print info about the class period System.out.println(period); // print info for each student for (int i = 0; i < period.studentList.size(); i++) System.out.println("Student " + i + " is " + period.getStudent(i)); } Georgia Institute of Technology

Georgia Institute of Technology Summary An ArrayList is an array that can grow or shrink Use when you don’t know how many of something you will need An ArrayList is a List Implements the java.util.List interface An interface is an abstract class That only has public abstract methods And/or constants The java.lang.Comparable interface can be implemented by any class Used to sort objects in a collection By their natural order (defined by class) The class that implements an interface Must provide code for the interface methods Georgia Institute of Technology