COMPUTER 2430 Object Oriented Programming and Data Structures I

Slides:



Advertisements
Similar presentations
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.
Advertisements

COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
LAB 10.
MSc IT Programming Methodology (2). MODULE TEAM Dr Aaron Kans Dr Sin Wee Lee.
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
1 Review of Java Higher Level Language Concepts –Names and Reserved Words –Expressions and Precedence of Operators –Flow of Control – Selection –Flow of.
Chapter 6 Iteration.  Executes a block of code repeatedly  A condition controls how often the loop is executed while (condition) statement  Most commonly,
CSC172 Intro Pepper. Goals Reminder of what a class is How to create and use classes How to design classes How to think in terms of objects How to comment.
Recitation 1 CS0445 Data Structures Mehmud Abliz.
CS 2430 Day 1. Agenda Load NetBeans Introduction Syllabus Review some array operations.
Chapter 2: Java Fundamentals
Textbook: Data Structures and the Java Collections Framework 3rd Edition by William Collins William Collins.
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.
Review Binary Numbers Bit : 0 or 1 Byte: 8 bites 256 different values 2 8 KB : 1024 bytes 2 10 bytes MB : 1024 * 1024 bytes 2 10 * 2 10 (2 20 ) bytes GB.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
Sit-In Lab 2 - OOP Restaurant.  Manage a restaurant and perform these types of queries: Assign a favorite table to a specific group Assign the lexicographically-smallest.
CS 1430: Programming in C++ 1. Class StudentList class StudentList { private: int numStudents; Student students[MAX_SIZE]; int find(const Student& s)
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
A High Flying Overview CS139 – Fall 2006 How far we have come.
Take-Home Lab #02 CS1020 – DATA STRUCTURES AND ALGORITHMS 1 AY SEMESTER 2 1.
CS 1430: Programming in C++ 1. Test 2 Friday Functions Arrays For Loops Understand Concepts and Rules Memorize Concepts and Rules Apply Concepts and Rules.
Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]
Chapter 9 Introduction to Arrays Fundamentals of Java.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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.
Variable Scope & Lifetime
COMPUTER 2430 Object Oriented Programming and Data Structures I
CSC111 Quick Revision.
CS 160 Final Review.
Chapter 8 – Arrays and Array Lists
[ 4.00 ] [ Today’s Date ] [ Instructor Name ]
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
Computer Programming Methodology Input and While Loop
CS 1430: Programming in C++.
TK1114 Computer Programming
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS 1430: Programming in C++.
CS 1430: Programming in C++.
What/how do we care about a program?
CS 177 Week 15 Recitation Slides
CSS161: Fundamentals of Computing
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS 1430: Programming in C++ No time to cover HiC.
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Week 6 CS 302 Jim Williams, PhD.
CS 1430: Programming in C++ No time to cover HiC.
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS 1430: Programming in C++.
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS 200 Primitives and Expressions
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS 2430 Object Oriented Programming and Data Structures I
CSC1401 Input and Output (with Files)
COMPUTER 2430 Object Oriented Programming and Data Structures I
Arrays.
Class StudentList class StudentList { private: int numStudents;
Tree.
Arrays Wellesley College CS230 Lecture 02 Thursday, February 1
CSS161: Fundamentals of Computing
A type is a collection of values
The Queue ADT Definition A queue is a restricted list, where all additions occur at one end, the rear, and all removals occur at the other end, the front.
Presentation transcript:

COMPUTER 2430 Object Oriented Programming and Data Structures I Day1 on Thursday, before lab1

Lab1 Due 10 pm, Friday, September 7 -1 by 10 pm, Saturday, September 8 -2 by 10 pm, Monday, September 10

Features of OOP Abstraction Data Encapsulation Data Hiding (Inheritance) (Polymorphism)

public class ScoresList { // Private data members private final int MAX_SIZE = 10; private int scores[] = new int[MAX_SIZE]; private int numValues = 0; // Public methods (operations) public boolean appendScore ( int score ) ... public boolean delete ( int score ) public float average() public void print() // Private methods (helper methods) private int find ( int score ) }

public class ScoresList { // Private data members // Public methods (operations) // Private methods // Test Bed main for the class public static void main( String [] args ) ScoresList scores; scores = new ScoresList(); scores.print(); scores.appendScore(5); . . . }

// Application using class ScoresList // In the same project with class ScoresList public class Prog1 { public static void main( String [] args ) ScoresList scores = new ScoresList(); scores.print(); scores.appendScore(5); if (scores.appendScore(7)) System.out.println( “Value 7 is added.”); else System.out.println( “Value 7 is not added.”); . . . }

Input from key board (System.in). import java.util.Scanner; public class Prog1 { public static void main( String [] args ) ScoresList scores = new ScoresList(); int score; Scanner sc = new Scanner( System.in ); score = sc.nextInt(); scores.appendScore(score); if (scores.appendScore(score)) System.out.println(“Value ” + score + “ is added.”); else System.out.println( “Value ” + score + “ is not added.”); . . . } Input from key board (System.in).

import java.util.Scanner; import java.io.File; public class Prog1 { public static void main( String [] args ) ScoresList scores = new ScoresList(); int score; Scanner sc; try // Input from a file if the file exists sc = new Scanner( new File("Lab1_1.in") ); } catch (Exception ex) // Input from System.in (keyboard) if no input file sc = new Scanner( System.in ); . . . Input from an file. Input file Prog1_1.in in the project folder, not inside src.

Input File Inside NetBeans Other Text editors such as Notepad New File Category: Other Type: Empty Name: Prog1_1.in Folder: Prog1 Copy and paste from the instruction Other Text editors such as Notepad Not Word documents

Adding Array Elements private final int MAX_SIZE = 10; private int intArray[] = new int[MAX_SIZE]; private valCount = 0; int xVal, index = 0; Scanner sc; // set up the scanner xVal = sc.nextInt(); // Insert xVal at the beginning of intArray intArray[0] = xVal; valCount ++;

Adding Array Elements private final int MAX_SIZE = 10; private int intArray[] = new int[MAX_SIZE]; private valCount = 0; /** intArray[] has 5 values: valCount is 5 New value xVal Store xVal at the end (as the 6th element) What’s the index value for the new xVal? */ intArray[5] = xVal; // what’s the value of valCount after adding? valCount ++;

Adding Array Elements Correct? private final int MAX_SIZE = 10; private int intArray[] = new int[MAX_SIZE]; private valCount = 0; /** intArray[] has valCount values New value xVal Store xVal at the end */ intArray[valCount] = xVal; ++ valCount; Correct? The array could be full!

Adding Array Elements private final int MAX_SIZE = 10; // const private int intArray[] = new int[MAX_SIZE]; private valCount = 0; /** intArray[] has valCount values New value xVal Store xVal at the end */ if (valCount < MAX_SIZE ) { intArray[valCount] = xVal; ++ valCount; } else //Cannot insert

Using ++ in Array Index if (valCount < MAX_SIZE ) { intArray[valCount] = xVal; valCount ++; } else //Cannot insert // Yes! The same result: increasing after using it if ( valCount < MAX_SIZE ) intArray[valCount ++] = xVal; // Correct?

Using ++ in Array Index if (valCount < MAX_SIZE ) { intArray[valCount] = xVal; valCount ++; } else //Cannot insert // No! Increasing before using it. if ( valCount < MAX_SIZE ) intArray[++ valCount] = xVal; // Correct?

Deleting Array Elements /** intArray[] has 9 values: intArray[0] to intArray[8] Removing the element at index 6 Keeping all elements together in the same order */ 10 15 20 50 33 40 55 60 39 ? . . . . 0 1 6 7 8 10 15 20 50 33 40 60 39 ? . . . . intArray[6] = intArray[7]; intArray[7] = intArray[8]; valCount --; Don’t worry about intArray[8] as long as valCount is updated!

Deleting Array Elements /** intArray[] has 9 values: intArray[0] to intArray[8] Removing the element at index 1 Keeping all elements together in the same order */ 10 15 20 50 33 40 55 60 39 ? . . . . 0 1 2 6 7 8 10 20 50 33 40 55 60 39 ? . . . . Moving intArray[i+1] to intArray[i] for i = 1, 2...7 (or 8?) valCount --; Need a for loop Start and Stop?

Deleting Array Elements /** intArray[] has valCount values (valCount <= MAX_SIZE) Removing the element at index (between 0 and valCount – 1) Keeping the order of other elements */ 10 . . . . . 50 33 . . . . . 60 39 44 ? . . . . index valCount-2 valCount-1 10 . . . . . 33 .. . . . . . 39 44 ? . . . . valCount-1 // Where to start? // Where to stop? for (int i = index; i < valCount – 1; i ++) intArray[i] = intArray[i + 1]; valCount --;

Deleting Array Elements /** intArray[] has valCount values (valCount <= MAX_SIZE) Removing the element at index (between 0 and valCount – 1) Keeping the order of other elements */ 10 . . . . . 50 33 . . . . . 60 39 44 ? . . . . index valCount-1 10 . . . . . 33 .. . . . . . 39 44 ? . . . . index valCount-1 // This might be better valCount --; for (int i = index; i < valCount; i ++) intArray[i] = intArray[i + 1];

Linear Search public class IntegerList { private final int MAX_SIZE = 50; private int intArray[] = new int[MAX_SIZE]; private int valCount = 0; /** Tries to find the FIRST occurrence of intValue in the list. @param intValue to find @return index of the FIRST occurrence of intValue, -1 if not found */ public int find( int target ) for (int i = 0; i < valCount; i++) if (intArray[i] == target) return i; return -1; }

Linear Search public class IntegerList { private final int MAX_SIZE = 50; private int intArray[] = new int[MAX_SIZE]; private int valCount = 0; /** Tries to find the LAST occurrence of intValue in the list. @param intValue to find @return index of the LAST occurrence of intValue, -1 if not found */ public int find( int target ) // ??? }

Casting Integer to Float int numValues, sum = 0; // Loop through and sum up the scores in the array ave = (float)sum / (float)numValues;

++, --, +=, -= count ++; count = count + 1; index --; index = index - 1; // 0.5 points off for Prog1 xVal += someVal; xVal = xVal + someVal; // 1 point off for Prog2 yVal -= someVal; yVal = yVal – someVal; // How many off for Prog3?

Don’t Over Do it! Never use ++ or -- on an expression that appears more than one in the statement. intArray[++ index] = index + 10; intArray[index --] = index + 10; x = x++ + ++x; // Very bad! // Lose Points!

Program1 Due Time: 10 pm, Friday, September 14 Class ScoresList Class Prog1

Software Development Grand Rules for CS 2430

Lab Assistant Lab 206 Monday - Thursday 3:00 – 5:00