data structures and
play

Data Structures and What is a data structure? Algorithms Way of - PDF document

Introduction: Data Structures Data Structures and What is a data structure? Algorithms Way of storing data in computer so can be used efficiently Set of operations that access the data in prescribed ways Prof. Nadeem Abdul Hamid


  1. Introduction: Data Structures Data Structures and • What is a data structure? Algorithms – Way of storing data in computer so can be used efficiently – Set of operations that access the data in prescribed ways Prof. Nadeem Abdul Hamid CSC 220 - Fall 2005 Lecture Unit 1 - Java Review 1 2 Choosing Data Structures What is the Study of Data Structures? • Choice of data structures affects • ‘Program’ : computer language algorithm for solving a problem – Performance – Simplicity • To solve larger problems, need to build on solutions to smaller problems - reuse – Readability solutions, etc. of computer programs • Data structures : study of how to abstract solutions to problems of storing data so • Proper use of data structures necessary that we can easily reuse them for large programs 3 4 Course Mechanics Assignments • Syllabus, lectures notes, assignments, etc. on web page • Weekly lab/homeworks – http://fsweb.berry.edu/academic/mans/nhamid/classes/cs220/05fall – Due on Mondays • Class meetings • Programming Projects – Lectures: Mon/Wed/Fri, 9-9:50AM, SCI 233 – Labs: Tues, 12:30–2:30PM, SCI 233 • DON’T WAIT UNTIL DAY/NIGHT BEFORE TO • Contact START WORKING ON ASSIGNMENTS – Office: SCI 354B — Phone: 368-5632 – Email: nadeem@acm.org – No late work accepted, without formal excuse/prior arrangement • Office Hours – You will NOT be able to complete the programming – Mon — 11AM–12:30PM assignments in one night – Tue — 11AM–12:30PM – Wed — 11AM–12:30PM and 2–4PM • Send email if you have a problem (attached relevant files and – Thu — 10AM–12:30PM and 2-3PM say where you’re stuck) 5 6 – (or by appointment…) 1

  2. Programming Assignments Materials and Resources • Textbook: • Completed programs must ‘work’!!! – Objects, Abstraction, Data Structures and Design Using Java , Elliot B. Koffman and Paul A.T. Wolfgang – Compile and run • Online course website: Check regularly • If you leave programming • Software (in computer lab SCI 228/233) assignments to the last minute, you – Java 5.0 (JDK): http://java.sun.com/j2se/1.5.0/download.jsp • Compiler; runtime system will run a major risk of having – Eclipse: http://www.eclipse.org incomplete work • integrated development environment • Using computers during class 7 8 Grading and Evaluation What is Java? • Class participation and attendance (10%) • JVM - virtual bytecodes, platform • Lab participation and attendance (10%) independence, security, Java vs. • Assignments/Projects (50%) • Exams (30%) Tentative dates: JVML – Midterm exam: Friday, October 7, 2005 – javac -- compiler – Final exam: Thursday, December 8, 2005 (8 - 10AM) – java -- virtual machine • Policies (see syllabus) – Attendance – Academic integrity – Late work – Disabilities 9 10 Java Web Pages Object-Oriented Programming • Download from: • is …? – http://java.sun.com/j2se/1.5.0/download.jsp • API documentation • Classes vs. objects – http://java.sun.com/j2se/1.5.0/docs/api/index.html • Fields (attributes) • The Java Tutorial – http://java.sun.com/docs/books/tutorial/ • Methods (operations) 11 12 2

  3. Hello World! Java Data Types import javax.swing.*; • Java distinguishes between primitive public class HelloWorld { public static void main( String[] args ) { types (numbers, characters) and objects String name = JOptionPane.showInputDialog( "Enter your name" ); JOptionPane.showMessageDialog( null, "Hello " + name • Values of primitive types stored directly + ", welcome to Java!" ); System.exit( 0 ); in variables } } • Objects are manipulated through • Java API - Swing, AWT, util, … reference variables , which ‘point to’ (store address in memory of) an object • import statement • main method 13 14 Primitive Data Types Unicode Character Set 15 16 Primitive Variables and Constants Operators • Declaring and initializing variables • Constants • Identifier conventions 17 18 3

  4. Operators (cont.) Object Variables • Store references to objects String greeting = "hello"; String welcome = greeting; String hello = new String( welcome ); 19 20 Control Statements If/else and Switch Statements switch ( operator ) { case '+': • Determine flow of execution through if ( operator == '+' ) { result = x + y; result = x + y; addOp++; a program break; addOp++; case '-': } – Sequence else if ( operator == '-' ) { result = x - y; result = x - y; subtractOp++; subtractOp++; break; – Selection -- if…else / switch } case '*': else if ( operator == '*' ) { result = x * y; – Repetition -- while / for / do…while multiplyOp++; result = x * y; break; multiplyOp++; case '/': } else if ( operator == '/' ) { result = x / y; result = x / y; divideOp++; divideOp++; break; } default: // do nothing... 21 } 22 Defining Classes Person Object Operations • A Java program is a collection of • Calculate age interacting objects, defined by their • Determine if old enough to vote classes • Example: Person class • Determine if senior citizen – Objects of class Person store data: • Get values of Person object data fields • Given name • Set values of Person object data fields • Family name • ID number • DOB 23 24 4

  5. UML Diagrams Person Class Implementation • Private data fields (‘instance variables’) • UML = Unified Modeling Language ™ – Industry standard for documenting class relationships • Public methods • Constants • Constructors • Accessor and mutator (modifier) methods • Use of this keyword • Methods toString , equals 25 26 Comparing Objects TestPerson Program /** TestPerson is an application that tests class Person. */ String myName = "Elliot Koffman"; public class TestPerson { myName = myName.substring(7) + ", " public static void main( String[] args ) { + myName.substring(0,6); Person p1 = new Person( "Sam", "Jones", "1234", 1930 ); Person p2 = new Person( "Jane", "Jones", "5678", 1990 ); String anyName = new String( myName ); System.out.println( "Age of " + p1.getGivenName() + " is " + p1.age( 2005 ) ); if ( p1.isSenior( 2005 ) ) String otherName = anyName; System.out.println( p1.getGivenName() + " can ride the subway for free" ); else System.out.println( p1.getGivenName() + " must pay to ride the subway" ); System.out.println( "Age of " + p2.getGivenName() + " is " + p2.age( 2005 ) ); if ( p2.canVote( 2005 ) ) System.out.println( p2.getGivenName() + " can vote"); else System.out.println( p2.getGivenName() + " can't vote"); } 27 28 } Class Components (UML) Javadoc • String objects are components of a • Uses standard form of writing Person object comments to generate HTML • Focuses on text within /** and */ • Javadoc tags: 29 30 5

  6. String Class Math Class 31 32 StringBuffer Class StringTokenizer Class 33 34 Using StringTokenizer Arrays import java.util.StringTokenizer; • Java arrays are also objects (with public class TestTokenizer { public static void main( String[] args ) { some special syntax provided) String personData = "Doe, John 5/15/65"; StringTokenizer sT = new StringTokenizer( personData, " ,/" ); – Indexed using subscript notation String familyName = sT.nextToken(); // stores "Doe" String givenName = sT.nextToken(); // stores "John" • arrayName[subscript] String month = sT.nextToken(); // stores "5" String day = sT.nextToken(); // stores "15" String year = sT.nextToken(); // stores "65" String sentence = "This is a set of seven tokens"; StringTokenizer getWords = new StringTokenizer( sentence ); while ( getWords.hasMoreTokens() ) System.out.println( getWords.nextToken() ); } } 35 36 6

  7. Array Example A.14 Array Example A.15 String[] names = { "Sally", "Jill", "Hal", "Rick" }; int[] scores = new int[5]; // an array with 5 type int values 37 38 Array Example A.16 Array Example A.18 // Declare people as type Person[] Person[] people; double[][] matrix = new double[5][10]; // Define n in some way. int n = ... // Allocate storage for the array people = new Person[n]; ... people[0] = new Person("Elliot", "Koffman", "010-055-0123", 1942); people[1] = new Person("Paul", "Wolfgang", "015-023-4567", 1945); 39 40 Array Example A.19 Array Example A.20 char[][] letters = new char[5][]; letters[0] = new char[4]; letters[1] = new char[10]; ... int[][] pascal = { {1}, // row 0 {1, 1}, // row 1 {1, 2, 1}, {1, 3, 3, 1}, {1, 4, 6, 4, 1} }; 41 42 7

Recommend


More recommend