Data Abstraction Software Development. Review of Java Constructs. - - PowerPoint PPT Presentation

data abstraction software development review of java
SMART_READER_LITE
LIVE PREVIEW

Data Abstraction Software Development. Review of Java Constructs. - - PowerPoint PPT Presentation

Data Abstraction Software Development. Review of Java Constructs. Janyl Jumadinova September 1416, 2020 Janyl Jumadinova Data Abstraction Software Development. Review of Java Constructs. September 1416, 2020 1 / 17 Collections


slide-1
SLIDE 1

Data Abstraction Software Development. Review of Java Constructs.

Janyl Jumadinova September 14–16, 2020

Janyl Jumadinova Data Abstraction Software Development. Review of Java Constructs. September 14–16, 2020 1 / 17

slide-2
SLIDE 2

Collections

Collection: an object that stores data; a.k.a. “data structure” The objects stored are called elements Some collections maintain an ordering; some allow duplicates Typical operations: add, remove, clear, contains (search), size

Janyl Jumadinova Data Abstraction Software Development. Review of Java Constructs. September 14–16, 2020 2 / 17

slide-3
SLIDE 3

Collections

Collection: an object that stores data; a.k.a. “data structure” The objects stored are called elements Some collections maintain an ordering; some allow duplicates Typical operations: add, remove, clear, contains (search), size Examples found in the Java class libraries: ArrayList, LinkedList, HashMap, TreeSet, PriorityQueue all collections are in the java.util package

Janyl Jumadinova Data Abstraction Software Development. Review of Java Constructs. September 14–16, 2020 2 / 17

slide-4
SLIDE 4

Collections

Janyl Jumadinova Data Abstraction Software Development. Review of Java Constructs. September 14–16, 2020 3 / 17

slide-5
SLIDE 5

Lists

List: a collection storing an ordered sequence of elements Each element is accessible by a 0-based index A list has a size (number of elements that have been added) Elements can be added to the front, back, or elsewhere

Janyl Jumadinova Data Abstraction Software Development. Review of Java Constructs. September 14–16, 2020 4 / 17

slide-6
SLIDE 6

Lists

List: a collection storing an ordered sequence of elements Each element is accessible by a 0-based index A list has a size (number of elements that have been added) Elements can be added to the front, back, or elsewhere In Java, a list can be represented as an ArrayList object

Janyl Jumadinova Data Abstraction Software Development. Review of Java Constructs. September 14–16, 2020 4 / 17

slide-7
SLIDE 7

Lists

List: a collection storing an ordered sequence of elements Each element is accessible by a 0-based index A list has a size (number of elements that have been added) Elements can be added to the front, back, or elsewhere In Java, a list can be represented as an ArrayList object

Janyl Jumadinova Data Abstraction Software Development. Review of Java Constructs. September 14–16, 2020 4 / 17

slide-8
SLIDE 8

A Few ArrayList Methods

Janyl Jumadinova Data Abstraction Software Development. Review of Java Constructs. September 14–16, 2020 5 / 17

slide-9
SLIDE 9

Type Parameters (Generics)

ArrayList<Type> name = new ArrayList<Type>(); When constructing an ArrayList, you must specify the type of elements it will contain between < and >. This is called a type parameter or a generic class. Allows the same ArrayList class to store lists of different types.

Janyl Jumadinova Data Abstraction Software Development. Review of Java Constructs. September 14–16, 2020 6 / 17

slide-10
SLIDE 10

Type Parameters (Generics)

ArrayList<Type> name = new ArrayList<Type>(); When constructing an ArrayList, you must specify the type of elements it will contain between < and >. This is called a type parameter or a generic class. Allows the same ArrayList class to store lists of different types. ArrayList<String> names = new ArrayList<String>(); names.add("Marty Stepp"); names.add("Stuart Reges");

Janyl Jumadinova Data Abstraction Software Development. Review of Java Constructs. September 14–16, 2020 6 / 17

slide-11
SLIDE 11

ArrayList of Primitives?

The type you specify when creating an ArrayList must be an object type; it cannot be a primitive type.

Janyl Jumadinova Data Abstraction Software Development. Review of Java Constructs. September 14–16, 2020 7 / 17

slide-12
SLIDE 12

ArrayList of Primitives?

The type you specify when creating an ArrayList must be an object type; it cannot be a primitive type. // illegal -- int cannot be a type parameter ArrayList<int> list = new ArrayList<int>();

Janyl Jumadinova Data Abstraction Software Development. Review of Java Constructs. September 14–16, 2020 7 / 17

slide-13
SLIDE 13

ArrayList of Primitives?

The type you specify when creating an ArrayList must be an object type; it cannot be a primitive type. // illegal -- int cannot be a type parameter ArrayList<int> list = new ArrayList<int>(); But we can still use ArrayList with primitive types by using special classes called wrapper classes in their place. // creates a list of ints ArrayList<Integer> list = new ArrayList<Integer>();

Janyl Jumadinova Data Abstraction Software Development. Review of Java Constructs. September 14–16, 2020 7 / 17

slide-14
SLIDE 14

Wrapper Classes

A wrapper is an object whose sole purpose is to hold a primitive value. Once you construct the list, use it with primitives as normal: ArrayList<Double> grades = new ArrayList<Double>(); grades.add(3.2); grades.add(2.7); ... double myGrade = grades.get(0);

Janyl Jumadinova Data Abstraction Software Development. Review of Java Constructs. September 14–16, 2020 8 / 17

slide-15
SLIDE 15

Random Class

The Random class is part of the java.util package It provides methods that generate pseudo-random numbers

Janyl Jumadinova Data Abstraction Software Development. Review of Java Constructs. September 14–16, 2020 9 / 17

slide-16
SLIDE 16

Random Class

The Random class is part of the java.util package It provides methods that generate pseudo-random numbers Before you can use its methods, you must create an instance of the Random class Random rand = new Random( );

Janyl Jumadinova Data Abstraction Software Development. Review of Java Constructs. September 14–16, 2020 9 / 17

slide-17
SLIDE 17

Random Class

float nextFloat( ) float nextDouble( )

  • Returns a random number between 0.0 inclusive and 1.0 exclusive.

Janyl Jumadinova Data Abstraction Software Development. Review of Java Constructs. September 14–16, 2020 10 / 17

slide-18
SLIDE 18

Random Class

float nextFloat( ) float nextDouble( )

  • Returns a random number between 0.0 inclusive and 1.0 exclusive.

Random rand = new Random( ); float f; f = rand.nextFloat( );

Janyl Jumadinova Data Abstraction Software Development. Review of Java Constructs. September 14–16, 2020 10 / 17

slide-19
SLIDE 19

Random Class

int nextInt( ) - Returns a random number that ranges over all possible int values positive and negative.

Janyl Jumadinova Data Abstraction Software Development. Review of Java Constructs. September 14–16, 2020 11 / 17

slide-20
SLIDE 20

Random Class

int nextInt( ) - Returns a random number that ranges over all possible int values positive and negative. Random rand = new Random( ); int num; num = rand.nextInt( );

Janyl Jumadinova Data Abstraction Software Development. Review of Java Constructs. September 14–16, 2020 11 / 17

slide-21
SLIDE 21

Random Class

int nextInt( ) - Returns a random number that ranges over all possible int values positive and negative. Random rand = new Random( ); int num; num = rand.nextInt( ); int nextInt( int num ) - Returns a random number between 0 (inclusive) and num (exclusive).

Janyl Jumadinova Data Abstraction Software Development. Review of Java Constructs. September 14–16, 2020 11 / 17

slide-22
SLIDE 22

Random Class

int nextInt( ) - Returns a random number that ranges over all possible int values positive and negative. Random rand = new Random( ); int num; num = rand.nextInt( ); int nextInt( int num ) - Returns a random number between 0 (inclusive) and num (exclusive). Random rand = new Random( ); int num; num = rand.nextInt(5 );

Janyl Jumadinova Data Abstraction Software Development. Review of Java Constructs. September 14–16, 2020 11 / 17

slide-23
SLIDE 23

Math Class

Math class is part of the java.lang package (no need to import). Math class consists of: static methods, which are methods that don’t depend on the contents

  • f an object.

static fields, which are values that are usually defined to be public, final and static, meaning that anyone can access them outside the

  • package. Since their values are final, that means that they are

constant and can’t be changed.

Janyl Jumadinova Data Abstraction Software Development. Review of Java Constructs. September 14–16, 2020 12 / 17

slide-24
SLIDE 24

Software Development

1 Design. 2 Implementation. 3 Testing. 4 Debugging. Janyl Jumadinova Data Abstraction Software Development. Review of Java Constructs. September 14–16, 2020 13 / 17

slide-25
SLIDE 25

Design

Separation into classes, class interaction, data, actions Responsibilities: using action words. Independence: autonomy of each class. Behaviors: actions of each class.

Janyl Jumadinova Data Abstraction Software Development. Review of Java Constructs. September 14–16, 2020 14 / 17

slide-26
SLIDE 26

Design

Separation into classes, class interaction, data, actions Responsibilities: using action words. Independence: autonomy of each class. Behaviors: actions of each class. CRC (Class-Responsibility-Collaborator) cards → UML diagrams → Pseudocode → Coding

Janyl Jumadinova Data Abstraction Software Development. Review of Java Constructs. September 14–16, 2020 14 / 17

slide-27
SLIDE 27

UML Diagram

Top compartment:

  • Name of class, Bolded, Centered

Janyl Jumadinova Data Abstraction Software Development. Review of Java Constructs. September 14–16, 2020 15 / 17

slide-28
SLIDE 28

UML Diagram

Top compartment:

  • Name of class, Bolded, Centered

Middle compartment:

  • Attributes (data members)

Janyl Jumadinova Data Abstraction Software Development. Review of Java Constructs. September 14–16, 2020 15 / 17

slide-29
SLIDE 29

UML Diagram

Top compartment:

  • Name of class, Bolded, Centered

Middle compartment:

  • Attributes (data members)

Bottom compartment:

  • Behaviors (member methods), method name, followed by

parentheses

  • Plus (+) sign indicates public member method

Janyl Jumadinova Data Abstraction Software Development. Review of Java Constructs. September 14–16, 2020 15 / 17

slide-30
SLIDE 30

UML Diagram

Top compartment:

  • Name of class, Bolded, Centered

Middle compartment:

  • Attributes (data members)

Bottom compartment:

  • Behaviors (member methods), method name, followed by

parentheses

  • Plus (+) sign indicates public member method

CreditCard

  • customer :

String ... <<constructor>> CreditCard (String name) + getCustomer() : String + makePayment(amount : double) ...

Janyl Jumadinova Data Abstraction Software Development. Review of Java Constructs. September 14–16, 2020 15 / 17

slide-31
SLIDE 31

Testing and Debugging

Testing: Verify the correctness of a program on a representative subset of inputs. Handcrafted test suites. Randomly generated inputs.

Janyl Jumadinova Data Abstraction Software Development. Review of Java Constructs. September 14–16, 2020 16 / 17

slide-32
SLIDE 32

Testing and Debugging

Testing: Verify the correctness of a program on a representative subset of inputs. Handcrafted test suites. Randomly generated inputs. Debugging: print statements. debugger (jdb).

Janyl Jumadinova Data Abstraction Software Development. Review of Java Constructs. September 14–16, 2020 16 / 17

slide-33
SLIDE 33

DESIGN ACTIVITY: Sentence Reverser

A system that reverses sentences in a text. More details about the behavior of this program are provided in the description of Project P-1.26 in the textbook.

Janyl Jumadinova Data Abstraction Software Development. Review of Java Constructs. September 14–16, 2020 17 / 17