Docsity

Testing and Java Coding: Developing Test Cases and Understanding Objects, Lecture notes of Object Oriented Programming

Toggle sidebar
bg1
๎˜ƒA๎˜ƒbit๎˜ƒabout๎˜ƒtesting๎˜ƒ
Test case: Set of input values, together with the expected output.
Develop test cases for a method from its specification --- even
before you write the methodโ€™s body.
/** = number of vowels in word w.
Precondition: w contains at least one letter and nothing but letters*/
public int numberOfVowels(String w) {
โ€ฆ
}
Developing test
cases first, in
โ€œcritiqueโ€ mode, can
prevent wasted work
and errors.
How๎˜ƒmany๎˜ƒvowels๎˜ƒin๎˜ƒeach๎˜ƒof๎˜ƒthese๎˜ƒwords?๎˜ƒ
๎˜ƒ๎˜ƒ๎˜ƒ๎˜ƒ๎˜ƒ๎˜ƒcreek๎˜ƒ
๎˜ƒ๎˜ƒ๎˜ƒ๎˜ƒ๎˜ƒ๎˜ƒsyzygy๎˜ƒ
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Testing and Java Coding: Developing Test Cases and Understanding Objects and more Lecture notes Object Oriented Programming in PDF only on Docsity!

A^ bit^

about

testing

Test case

: Set of input values, together with the expected output. Develop test cases for a method from its specification --- evenbefore you write the method

โ€™s body.

/** = number of vowels in word w.Precondition: w contains at least one letter and nothing but letters*/ public^ int

numberOfVowels(String w) {โ€ฆ }

Developing testcases first, in โ€œcritiqueโ€^ mode, canprevent wasted workand errors.

How^ many

vowels^

in^ each^ of

these^ words?

creek syzygy

Test^ cases

for^ number

of^ children

โ€œChild 2โ€ mom pop^ j0 children w0 0 name

BFly s0^ โ€œMumsie

โ€ null mom^

popnull children^1 name

BFly j

โ€œOpaโ€ mom pop null children

null 1 name

BFly โ€œPopsi b โ€ null mom^

b0pop children^2 name

BFly w

โ€œChild 1โ€ mom pop null children

w L0 name

BFly^1 If L0 gets a mom,say j0, the momโ€™snumber of childrenmust increase.You should test this.

Class^

Object:

the^ superest

class

of^ them

all

Java:^ Every

class^ that

does^ not extend^ another

extends

class

Object.^

That^ is, public class^ W^ {โ€ฆ} is^ equivalent

to public^ class

W^ extends

Object^ {โ€ฆ}

W@af

W

lname^

โ€œObama

ssn^123456789 boss^

null W(โ€ฆ)^ getLname() getSsn(),

getBoss()

Object setBoss(W) toString() equals(Object)

hashCode() Should^ draw

object^ like

this

We^ often

leave^ off

the^ top partition

to^ reduce

clutter; we

know^ that

it^ is^ always

there

Method

toString

Object W@af^ lname^

โ€œObama

ssn^123456789 boss^

W null getSsn()

toString()

toString()

in^ Object

returns the^ name

of^ the^ object:

W@af

Java Convention

: Define toString() in any class to return a representation ofan object, giving info about the valuesin its fields.New definition of toString()

overrides

the definition in partition Object

c^ W@af toString()^

c.toString()

calls^ this

method In appropriate places, the expressionc^ automatically does c.toString()

Another

example

of^ toString()

/** An instance represents a point (x, y) in the plane */ public class

Point { private^

int^ x; // x-coordinate private^

int^ y; // y-coordinate โ€ฆ / = repr. of this point in form**

โ€œ (x, y) โ€

public^ String

toString() { return^ โ€œ(โ€ + x + โ€œ, โ€ + y + โ€œ)โ€;} }

Point@fa

Point x^9

y^5

Function toString should give the values in thefields in a format that makes sense for the class.

(9,^ 5) docsity.com

Intro^

to^ static

components

W@af

W

lname^ โ€œ

Omโ€ boss null ssn^35 isBoss(W)

W@b

W

lname^

โ€œJoโ€ boss W@af ssn^21 isBoss(W)

/** =^ โ€œthis object is c

โ€™s bossโ€

Pre: c is not null. */ public^ boolean

isBoss(W c) { return this

== c.boss; } keyword

this^ refers to the name of the object in which

it appears

x.isBoss(y) is

false y W@af y.isBoss(x) isx W@b

true

Intro^

to^ static

components

W@af

W

lname^ โ€œ

Omโ€ boss null ssn^35 isBoss(W)

W@b

W

lname^

โ€œJoโ€ boss W@af ssn^21 isBoss(W)

/** =^ โ€œb is c

โ€™s bossโ€

Pre: b and c are not null. */ public^ static

boolean

isBoss(W b, W c) { return^ b == c.getBoss();}

isBos(W,W)

W@afy x^ W@b

static:^ there

is^ only^ one copy^ of^

the^ method.

It is^ not^ in

each^ object W^ (objects,

static components)

x.isBoss(x,

y) y.isBoss(x,

y) Preferred: W.isBoss(x,

y)

Java^ application

Java^ application:

bunch

of^ classes

with^ at

least^

one^ class

that^ has

this

procedure:^ public

static

void^ main(String[]

args)

{^ โ€ฆ^ }

Type^ String[]:

array^ of elements

of^ type^

String. We^ will^

discuss^ later

Running

the^ application

consists

of^ calling

that^ method

main

Convention:

the^ method

main^ doesnโ€™t

use

parameter

args,^ then

call^ it^ with

argument

null