COMPUTER 2430 Object Oriented Programming and Data Structures I

Slides:



Advertisements
Similar presentations
1 Todays Objectives Announcements Homework #1 is due next week Return Quiz 1 – answers are posted on the Yahoo discussion page site Basic Java Programming.
Advertisements

1. List Static List: no adding or deleting Dynamic List: can add or delete items from the list Both static and dynamic lists: linear search, update item.
Public class ABC { private int information = 0; private char moreInformation = ‘ ‘; public ABC ( int newInfo, char moreNewInfo) { } public ABC () {} public.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
LAB 10.
Computer Programming Lab(5).
DT249-Information Systems Research Practice Programming Revision Lecture 2 Lecturer: Patrick Browne.
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
CS 1430: Programming in C++ 1. Class StudentList class StudentList { private: int numStudents; Student students[MAX_SIZE]; int find(const Student& s)
A: A: double “4” A: “34” 4.
Computer Science 320 A First Program in Parallel Java.
Announcements Final Exam: TBD. Static Variables and Methods static means “in class” methods and variables static variable: one per class (not one per.
Class Method Read class Student { private: string id; string firstName, lastName; float gpa; public: // Will the method change any data members? // Yes!
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
CSE 143 Lecture 3 Implementing ArrayIntList reading: slides created by Marty Stepp and Hélène Martin
A 2-D Array is a structure that storage space both vertically and horizontally. Thus, the array has both rows and columns. 2-D Arrays are used to create.
COMPUTER 2430 Object Oriented Programming and Data Structures I
The need for Programming Languages
CS1020 – Data Structures And Algorithms 1 AY Semester 2
COMPUTER 2430 Object Oriented Programming and Data Structures I
Chapter 8 – Arrays and Array Lists
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS305J Introduction to Computing
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS360 Windows Programming
CS Week 14 Jim Williams, PhD.
An Introduction to Java – Part I
CS 200 Arrays, Loops and Methods
CS 1430: Programming in C++.
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS 1430: Programming in C++.
CS Week 13 Jim Williams, PhD.
CS 302 Week 10 Jim Williams.
CS Week 8 Jim Williams, PhD.
CS139 – Fall 2010 How far we have come
Review Operation Bingo
CS Week 7 Jim Williams, PhD.
Starting JavaProgramming
null, true, and false are also reserved.
COMPUTER 2430 Object Oriented Programming and Data Structures I
An Introduction to Java – Part I, language basics
CS 302 Week 8 Jim Williams, PhD.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Week 6 CS 302 Jim Williams, PhD.
CS 200 Objects and ArrayList
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS 1430: Programming in C++.
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS 200 Loops Jim Williams, PhD.
JavaScript Reserved Words
COMPUTER 2430 Object Oriented Programming and Data Structures I
class PrintOnetoTen { public static void main(String args[]) {
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
OBJECT ORIENTED PROGRAMMING II LECTURE 4 GEORGE KOUTSOGIANNAKIS
CS 2430 Object Oriented Programming and Data Structures I
CSE 143 Lecture 3 Implementing ArrayIntList reading:
CSC1401 Input and Output (with Files)
COMPUTER 2430 Object Oriented Programming and Data Structures I
Creating and Modifying Text part 3
Class StudentList class StudentList { private: int numStudents;
CSE 143 Lecture 4 Implementing ArrayIntList reading:
CS 200 Objects and ArrayList
Computer Science Club 1st November 2019.
Presentation transcript:

COMPUTER 2430 Object Oriented Programming and Data Structures I

Container Classes Class IntegersList of Lab1 Class ScoresList of Prog1 Class GolfLeague of Prog2 How to store multiple values? Arrays Linked Lists (CS 2630 – OOPS II)

Container Methods Adding elements Deleting elements Searching targets Updating elements Displaying elements . . .

public class StudentList { private final int MAX_SIZE = 450; private Student[] studentList = new Student[MAX_SIZE]; private int numStudents = 0; public boolean addStudent( Student student ) . . . public boolean removeStudent( Student student ) private int find ( Student student ) @Override public String toString () }

public class StudentList { private final int MAX_SIZE = 450; private Student[] studentList = new Student[MAX_SIZE]; private int numStudents = 0; public boolean addStudent( Student student ) if (num < MAX_SIZE) studentList[numStudents ++] = student; return true; } return false; . . .

What if we want to store more than 450 students? public class StudentList { private final int MAX_SIZE = 450; private Student[] studentList = new Student[MAX_SIZE]; private int numStudents = 0; public boolean addStudent( Student student ) if (num < MAX_SIZE) studentList[numStudents ++] = student; return true; } return false; . . .

How to Grow an Array? Make a New Larger Array!

public class StudentList { private final int MAX_SIZE = 450; private Student[] studentList = new Student[MAX_SIZE]; private int numStudents = 0; public void addStudent( Student student ) if (numStudents == MAX_SIZE) grow(); studentList[numStudents ++] = student; } private void grow() . . .

public class StudentList { private final int MAX_SIZE = 450; private Student[] studentList = new Student[MAX_SIZE]; private int numStudents = 0; private final int GROWBY = 50; private void grow () // Make a larger array int size = MAX_SIZE + GROWBY; Student[] list = new Student[size]; // Copy all existing elements for (int i = 0; i < numStudents; i ++) list[i] = studentList[i]; // Point to the new array, since studentList is // the class field and all methods use it. studentList = list; }

Only works the first time! Correct? public void addStudent( Student student ) { if (numStudents == MAX_SIZE) grow(); studentList[numStudents ++] = student; } Only works the first time!

Correct! public void addStudent( Student student ) { // if (numStudents == MAX_SIZE) if (numStudents == studentList.length) grow(); studentList[numStudents ++] = student; }

Correct? private void grow () { int size = MAX_SIZE + GROWBY; Student[] list = new Student[size]; for (int i = 0; i < numStudents; i ++) list[i] = studentList[i]; studentList = list; }

Correct! private void grow () { int size = studentList.length + GROWBY; Student[] list = new Student[size]; for (int i = 0; i < numStudents; i ++) list[i] = studentList[i]; studentList = list; }

Method arraycopy private void grow () { int size = studentList.length + GROWBY; Student[] list = new Student[size]; for (int i = 0; i < numStudents; i ++) list[i] = studentList[i]; // System.arraycopy(studentList, 0, // list, 0, numStudents); // DO NOT USE arraycopy! studentList = list; }

Prog2

/** Uses a scanner to get input from a file and from System.in if the file unavailable. It reads in one command at a time until end of input. */ public static void main(String [] args) { try sc = new Scanner( new File("Prog2_1.in") ); } catch (Exception ex) sc = new Scanner( System.in ); . . .

public static void main(String [] args) { try . . . String command; while ( sc.hasNext() ) command = sc.next().toLowerCase(); switch (command) case "add": addMember(); break; }

Quiz 2 10 points 25 minutes