Classes and Objects

Classes and Objects
LexiWills Profile Pic
LexiWills,United Kingdom,Professional
Published Date:31-07-2017
Your Website URL(Optional)
Comment
Lecture 4 Classes and Objects Review Solutions 1 public static int getMinIndex(int values) int minValue = Integer.MAX_VALUE; int minIndex = -1; for(int i=0; ivalues.length; i++) if (valuesi minValue) minValue = valuesi; minIndex = i; return minIndex; Solutions 2 public static int getSecondMinIndex(int values) int secondIdx = -1; int minIdx= getMinIndex(values); for(int i=0; ivalues.length; i++) if (i == minIdx) continue; if (secondIdx == -1 valuesi valuessecondIdx) 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(values0 ); // 99 Values 99 100 101 Indexes 0 1 2 Popular Issues 2 • Curly braces … after if/else, for/while for (int i = 0; i 5; i++) System.out.println(“Hi”); System.out.println(“Bye”); • What does this print? Popular Issues 3 • Variable initialization int getMinValue(int vals) int min = 0; for (int i = 0; i vals.length; i++) if (valsi min) min = valsi Problem? • What if vals = 1,2,3? • Set min = Integer.MAX_VALUE or vals0 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 (valsi valssecondIdx) secondIdx = i; • What if vals = 0, 1, 2? • See solutions Popular Issues 5 Defining a method inside a method public static void main(String arguments) public static void foobar () Debugging Notes 1 • Use System.out.println throughout your code to see what it’s doing for ( int i=0; i vals.length; i++) if ( valsi minVal) System.out.println(“cur min: ” + minVal); System.out.println(“new min: ” + valsi); minVal = valsi; Debugging Notes 2 • Formatting • Ctrl-shift-f is your friend for (int i = 0; i vals.length; i++) if (valsi valsminIdx) minIdx=i; return minIdx; • Is there a bug? Who knows Hard to read Today’s Topics Object oriented programming Defining Classes Using Classes References vs Values Static types and methods Today’s Topics Object oriented programming Defining Classes Using Classes References vs Values Static types and methods Object oriented programming • Represent the real world Baby Object oriented programming • Represent the real world Baby Name Sex Weight Decibels poops so far Object Oriented Programming • Objects group together – Primitives (int, double, char, etc..) – Objects (String, etc…) Baby String name boolean isMale double weight double decibels int 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 David2? String nameDavid2; double weightDavid2; Terrible �Why use classes? • Why not just primitives? // little baby alex String nameAlex; double weightAlex; // little baby david String nameDavid; double weightDavid; // little baby david David2? String nameDavid2; double weightDavid2; Terrible � 500 Babies? That Sucks Why use classes? Name Weight Sex … Baby1

Advise: Why You Wasting Money in Costly SEO Tools, Use World's Best Free SEO Tool Ubersuggest.