Classes and Objects - Introduction to Programming in Java - Lecture Slides, Slides of Java Programming

In this course, Introduction to Programming in Java, we learned all programming concepts and implement them in java. Key points in these lecture slides are: Classes and Objects, Solutions, Minvalue, Minindex, Values, Array Index, Array Value, Popular Issues, Indexes, Print

Typology: Slides

2012/2013

Uploaded on 04/22/2013

sathaye
sathaye 🇮🇳

4.8

(8)

102 documents

1 / 69

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture 4
Classes and Objects
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45

Partial preview of the text

Download Classes and Objects - Introduction to Programming in Java - Lecture Slides and more Slides Java Programming in PDF only on Docsity!

Lecture 4

Classes and Objects

Review

Solutions 2

public static int getSecondMinIndex(int[] values) {

int secondIdx = -1;int minIdx= getMinIndex(values); for(int i=0; i<values.length; i++) {

if (i == minIdx)

continue; if (secondIdx == -1 ||

values[i] < values[secondIdx])^ secondIdx = i;

} return secondIdx; } •^ What happens if values = {0}? values = {0, 0}? values = {0,1}?

Popular Issues 1

•^ Array Index vs Array Value^ int[] values = {99, 100, 101};System.out.println(values[0] );

// 99

Values

Indexes

2 Docsity.com

Popular Issues 3

•^ Variable initialization^ int getMinValue(int[] vals) {

int min = 0;for (int i = 0; i < vals.length; i++) {^ if (vals[i] < min) {

min = vals[i] } } }

Problem?

•^ What if

vals = {1,2,3}

•^ Set

min = Integer.MAX_VALUE

or

vals[0]Docsity.com

Popular Issues 4

•^ Variable Initialization – secondMinIndex^ int minIdx = getMin(vals)int secondIdx = 0;for (int i = 0; i < vals.length; i++) {

if (i == minIdx) continue;if (vals[i] < vals[secondIdx])

secondIdx = i;

} • What if vals = {0, 1, 2}? • See solutions

Debugging Notes 1

•^ Use System.out.println throughout your codeto see what it’s doing^ for ( int i=0; i< vals.length; i++) {^ if ( vals[i] < minVal) {

System.out.println(“cur min: ”

  • minVal);

System.out.println(“new min: ” + vals[i]);minVal = vals[i]; } }

Docsity.com

Debugging Notes 2

•^ Formatting •^ Ctrl-shift-f is your friend^ for (int i

= 0; i < vals.length; i++) { if^

(vals[i] < vals[minIdx]) {

minIdx=i;}return minIdx;} • Is there a bug? Who knows! Hard to read

Today’s Topics

Object oriented programming

Defining ClassesUsing ClassesReferences vs Values

Static types and methods

Object oriented programming

•^ Represent the real world

Baby

Object Oriented Programming

•^ Objects group together^ –

Primitives (int, double, char, etc..) – Objects (String, etc…)

Baby

String nameboolean isMaledouble weightdouble decibelsint numPoops

Why use classes?

•^ Why not just primitives?

// little baby alex String nameAlex;double weightAlex; // little baby david String nameDavid;double weightDavid;

Why use classes?

•^ Why not just primitives?

// little baby alex String nameAlex;double weightAlex; // little baby david String nameDavid;double weightDavid; // little baby david String nameDavid2;double weightDavid2;

David2?Terrible

500 Babies?

That Sucks!

Docsity.com

Why use classes?

NameWeightSex… Baby