Download presentation
Presentation is loading. Please wait.
Published byἌρτεμις Πανταζής Modified over 6 years ago
1
COMPUTER 2430 Object Oriented Programming and Data Structures I
Day1 on Thursday, before lab1
2
Lab1 Due 10 pm, Friday, September 7 -1 by 10 pm, Saturday, September 8
-2 by 10 pm, Monday, September 10
3
Features of OOP Abstraction Data Encapsulation Data Hiding
(Inheritance) (Polymorphism)
4
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 ) }
5
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); . . . }
6
// 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.”); . . . }
7
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).
8
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.
9
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
10
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 ++;
11
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 ++;
12
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!
13
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
14
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?
15
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?
16
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 ? 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!
17
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 ? 10 20 50 33 40 55 60 39 ? Moving intArray[i+1] to intArray[i] for i = 1, (or 8?) valCount --; Need a for loop Start and Stop?
18
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 --;
19
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];
20
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; }
21
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 ) // ??? }
22
Casting Integer to Float
int numValues, sum = 0; // Loop through and sum up the scores in the array ave = (float)sum / (float)numValues;
23
++, --, +=, -= 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?
24
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!
25
Program1 Due Time: 10 pm, Friday, September 14 Class ScoresList
Class Prog1
26
Software Development Grand Rules for CS 2430
27
Lab Assistant Lab 206 Monday - Thursday 3:00 – 5:00
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.