DOC PREVIEW
Purdue ECE 462 - Lecture notes

This preview shows page 1-2-14-15-30-31 out of 31 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

ECE 462Object-Oriented Programmingusing C++ and JavaLecture 8Yung-Hsiang [email protected] 5 2Java List//ListOps.javaimport java.util.*;class ListOps {public static void main( String[] args ){List <String> animals = new ArrayList <String>(); animals.add( "cheetah" ); animals.add( "lion" ); animals.add( "cat" ); animals.add( "fox" ); animals.add( "cat" ); //duplicate cat System.out.println( animals ); //cheetah, lion, cat, fox, catdifferent from textbookfor Java 1.5week 5 3animals.remove( "lion" ); System.out.println( animals ); //cheetah, cat, fox, catanimals.add( 0, "lion" ); // overload, two arguments System.out.println( animals ); //lion, cheetah, cat, fox, catanimals.add( 3, "racoon" ); System.out.println( animals ); //lion, cheetah, cat, racoon, fox, catanimals.remove(3); System.out.println( animals ); //lion, cheetah, cat, fox, catListIterator iter = animals.listIterator();while ( iter.hasNext() ) { System.out.println( iter.next() ); }}}week 5 4Java Set (no duplicate element)//SetOps.javaimport java.util.*;class SetOps {public static void main( String[] args ){Set <String> animals = new TreeSet<String> (); //(A)animals.add( "cheetah" ); //(B)animals.add( "lion" ); //(C)animals.add( "cat" ); //(D)animals.add( "elephant" ); //(E)animals.add( "cat" ); // duplicate cat //(F)week 5 5System.out.println( animals ); //(G)// cat cheetah elephant lionSystem.out.println( animals.size() ); // 4 //(H)animals.remove( "lion" ); //(I}System.out.println( animals ); // cat cheetah elephant //(J)Iterator iter = animals.iterator(); //(K)while ( iter.hasNext() ) //(L)System.out.println( iter.next() ); //(M)// cat cheetah elephant}}week 5 6Java Map (Hash Table)//MapHist.javaimport java.io.*;import java.util.*;class WordHistogram {public static void main (String args[]) throws IOException{Map <String, Integer> histogram = new TreeMap<String, Integer>(); String allChars = getAllChars( args[0] ); StringTokenizer st = new StringTokenizer( allChars ); • array: integer → element• map: key (object, integer, string ...) → valueexample: name → phone number,student ID → departmentcity name → zip codeKeys are not necessarily continuous.week 5 7while ( st.hasMoreTokens() ) { String word = st.nextToken(); Integer count = (Integer) histogram.get( word ); histogram.put( word, ( count==null ? new Integer(1) : new Integer( count.intValue() + 1 ) ) ); }System.out.println( "Total number of DISTINCT words: " + histogram.size() ); System.out.println( histogram ); }week 5 8static String getAllChars( String filename ) throws IOException {String str = "";int ch;Reader input = new FileReader( filename );while ( ( ch = input.read() ) != -1 )str += (char) ch;input.close();return str;} }week 5 9Container Class (code reuse)• Many programs need "containers" to store information. Examples of containers include vector, list, stack, queue, map, and set.• A container needs to be able to hold items of different types (i.e. classes). Examples– list of strings, integers, floating points, student objects– queues of customer objects, car objects– maps: name → address, student ID → name, course title → classroom• C++ standard template library (STL) and Java container classes provide such functionality.week 5 10Select Container Class• All container classes have internal memory management, automatic allocation or release.• random or sequential accesses• allow unique or duplicate items• O(1) or O(N) for array-like access (using [index])• efficient insert / delete–front– end–middle• Java containers cannot store primitive types (int, char, float ...)week 5 11Efficiency O(1)O(N)O(N)insert/delete in middleO(1)O(1)+O(1)+insert/delete at endO(1)O(1)+O(N)insert/delete at frontO(N)O(1)O(1)array-like accesslistdequevectoroperationN: current number of itemsweek 5 12import java.io.*;import java.util.*;class Student {private int s_ID;private String s_name;private String s_department;public Student(int id, String nam, String dept) {s_ID = id; s_name = nam; s_department = dept;}public String toString() {String rtv = "\nID = " + s_ID + "\n\tname = " + s_name +"\n\tdepartment = " + s_department + "\n";return rtv;}}week 5 13class VectorClass {public static void main( String[] args ){Vector<Student> stdVec = new Vector<Student>();Student s1 = new Student(109032, "James", "Math");Student s2 = new Student(100075, "Amy", "ECE");stdVec.addElement(s1);stdVec.addElement(s2);System.out.println(stdVec);}}week 5 14class MapClass {public static void main( String[] args ){Map<Student, String> stdMap =new HashMap<Student, String>();Student s1 = new Student(109032, "James", "Math");Student s2 = new Student(100075, "Amy", "ECE");Student s3 = new Student(200069, "David", "CS");stdMap.put(s1, "Apartment 1, Lafayette");stdMap.put(s2, "Apartment 2, West Lafayette");System.out.println(stdMap.get(s1));System.out.println(stdMap.get(s3));}}week 5 15#include <iostream>#include <vector>#include <algorithm>#include <string>using namespace std;class Student{private:int s_ID;string s_name;string s_department;public:Student(int id, string nam, string dept):s_ID(id), s_name(nam), s_department(dept) { }friend ostream & operator << (ostream & os, const Student & std);};week 5 16ostream & operator << (ostream & os, const Student & std){os << endl << "ID = " << std.s_ID << endl<< "\tname = " << std.s_name << endl<< "\tdepartment = " << std.s_department << endl;return os;}int main(){vector<Student> vec;Student s1(7009, "Mary", "ECE");Student s2(7184, "Tom", "Physics");Student s3(6553, "Jennifer", "Chemistry");week 5 17vec.push_back( s1 );vec.push_back( s3 );vec.push_back( s2 );vector<Student>::iterator p = vec.begin();while (p != vec.end()){cout << (*p);p++;}}week 5 18Copy Constructorclass NameOfClass{NameOfClass(const NameOfClass & origobj) {// a constructor with special syntax// If a user does not provide a copy constructor// C++ compiler automatically creates one}}ECE 462Object-Oriented


View Full Document

Purdue ECE 462 - Lecture notes

Download Lecture notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?