CSCI 1103: Simple Objects

Similar documents
CSCI 1103: Object-Oriented Objects

CSCI 1103: Introduction

CSCI 1103: Static Methods

CSCI 1103: Input with TextIO

CS 211: Potpourri Enums, Packages, Unit Tests, Multi-dimensional Arrays Command Line Args

Introduction to Programming Using Java (98-388)

CS 211: Methods, Memory, Equality

CS 3 Introduction to Software Engineering. 3: Exceptions

CS 211: Existing Classes in the Java Library

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Chapter 1: Introduction to Computers, Programs, and Java

Chapter 4 Defining Classes I

CSCI 1103: File I/O, Scanner, PrintWriter

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

Functions. x y z. f (x, y, z) Take in input arguments (zero or more) Perform some computation - May have side-effects (such as drawing)

CS 231 Data Structures and Algorithms, Fall 2016

A Foundation for Programming

C++ for Java Programmers

CSCI 1103: File I/O, Scanner, PrintWriter

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

CMPT 125: Lecture 3 Data and Expressions

Programming Languages and Techniques (CIS120)

CS18000: Programming I

QUIZ. What is wrong with this code that uses default arguments?

CS 211: Recursion. Chris Kauffman. Week 13-1

Chapter 6 Classes and Objects

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

CS 211: Recursion. Chris Kauffman. Week 13-1

Options for User Input

Crash Course in Java. Why Java? Java notes for C++ programmers. Network Programming in Java is very different than in C/C++

CSCI 2041: Deep and Shallow Equality

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

CS 302 Week 9. Jim Williams

CSCI 1103: Basics of Recursion

Java for Non Majors Spring 2018

Enums. In this article from my free Java 8 course, I will talk about the enum. Enums are constant values that can never be changed.

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Java Programming. What is a program? Programs Recipes. Programs as Recipes 8/16/12. Pancakes In one bowl. mix: 1½ cup flour

Lec 3. Compilers, Debugging, Hello World, and Variables

Chapter 1 Getting Started

CSCI 2041: Basic OCaml Syntax and Features

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Java Programming. What is a program? Programs as Recipes. Programs Recipes 8/20/15. Pancakes In one bowl

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

Last Class. Introduction to arrays Array indices Initializer lists Making an array when you don't know how many values are in it

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!

CS 222: Arrays, Strings, Structs

Intro. Scheme Basics. scm> 5 5. scm>

TESTING AND DEBUGGING

EXCEPTIONS. Fundamentals of Computer Science I

Introduction to Java

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

CS106A, Stanford Handout #18. Writing a Class

CS Week 2. Jim Williams, PhD

QUIZ Friends class Y;

Computer Science II (20082) Week 1: Review and Inheritance

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Building Java Programs

CS Week 14. Jim Williams, PhD

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

CSCI 1103: Array-based Data Structures

COE318 Lecture Notes Week 10 (Nov 7, 2011)

COMP-202: Foundations of Programming. Lecture 5: Arrays, Reference Type, and Methods Sandeep Manjanna, Summer 2015

BBM 102 Introduction to Programming II Spring Exceptions

Java Foundations. 7-2 Instantiating Objects. Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Program Development. Program Development. A Foundation for Programming. Program Development

Memory and C++ Pointers

Recursion CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2011

CS121/IS223. Object Reference Variables. Dr Olly Gotel

Object Oriented Programming

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software.

public Twix() { calories = 285; ingredients = "chocolate, sugar, cookie, caramel"; }

Recursion. Fundamentals of Computer Science

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

The Singleton Pattern. Design Patterns In Java Bob Tarr

Programming Languages and Techniques (CIS120)

Ch. 12: Operator Overloading

Closed Computer and Book 1 Double-sided sheet of notes allowed staple to your test when done

[TAP:AXETF] Inheritance

Object Oriented Programming Exception Handling

LAB 5, THE HIDDEN DELIGHTS OF LINKED LISTS

Primitive Data and Objects

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

CSCI 2041: Pattern Matching Basics

Assignment 4: Dodo gets smarter

CHAPTER 7 OBJECTS AND CLASSES

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Exceptions Binary files Sequential/Random access Serializing objects

Primitive Data, Variables, and Expressions; Simple Conditional Execution

CS 222: Pointers and Manual Memory Management

COMP 524 Spring 2018 Midterm Thursday, March 1

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Appendix: Common Errors

Flow of Control: Loops. Chapter 4

C11: Garbage Collection and Constructors

CS171:Introduction to Computer Science II

CS 211: Inheritance. Chris Kauffman. Week 5-2

Transcription:

CSCI 1103: Simple Objects Chris Kauffman Last Updated: Mon Oct 30 14:35:25 CDT 2017 1

Logistics Reading from Eck Ch 5 on Objects/Classes Ch 5.1-2 specifically Goals Simple Objects Arrays of objects Lab08: Simple Object/Methods Stock object Methods in same java file Project 4 In the works Deadline will be adjusted for later release 2

Latin for: All the same VS Different Homogeneous Data Heterogeneous Data All same data type Single name, multiple ints, multiple doubles, etc. Usually indexed by element number (4th elem, 9th elem) Example: arrays, collection of the same thing (homogeneous) Elements accessed via array[index] Before and Now Data types different Single name, multiple values in an combination Example: need 1 int, 1 double, 2 booleans Usually indexed by field name as in mystudent.gpa = 3.91; mystudent.name = "Sam"; Example: classes/objects in Java, grouped data Discussed arrays and Strings previously (homogeneous) Now dealing with classes/objects (heterogeneous) 3

Java Classes.java files define classes Classes serve 2 purposes 1. Group related code together (binary/decimal conversions, Collatz methods, etc.) 2. Define new reference types Mixture of purposes can cause confusion but is somewhat intentional Most of the time Methods and Data Types are tied together 4

Simple Objects Java classes define new reference types by specifying fields // Represent data associated with an omelet. public class Omelet{ public int eggs; // How many eggs in the omelet public int cheese; // How many ounces of cheese public double cookedfor; // How long the omelet has cooked public String extras; // Extra ingredients added Fields are data data associated with the an object which as instance of the class. Every instance has its own fields Instances of objects are created with new and a constructor Omelet om = new Omelet(); Field values can be accessed/changed using dot notation (if they are public) om.eggs = 5; int e = om.eggs; 5

Simple Objects Demo // Represent data associated with an omelet. public class Omelet{ public int eggs; // How many eggs in the omelet public int cheese; // How many ounces of cheese public double cookedfor; // How long the omelet has cooked public String extras; // Extra ingredients added public class OmeletMain{ public static void main(string args[]){ Omelet om = new Omelet(); om.eggs = 3; om.cheese = 4; om.cookedfor = 4.5; om.extras = "ham"; System.out.printf("Omelet has %d eggs\n",om.eggs); System.out.printf("Omelet has %d oz cheese\n",om.cheese); System.out.printf("Omelet cooked for %.1f minutes\n",om.cookedfor); System.out.printf("Omelet has these extras: %s\n",om.extras); > javac OmeletMain.java > java OmeletMain Omelet has 3 eggs Omelet has 4 oz cheese Omelet has these extras: ham Omelet cooked for 4.5 minutes 6

Multiple Objects Each instance of class is its own object, has its own memory Can create as many omelets as you have memory for public class TwoOmelets{ public static void main(string args[]){ Omelet om = new Omelet(); om.eggs = 3; om.cheese = 4; om.cookedfor = 4.5; om.extras = "ham"; Omelet coronary = new Omelet(); coronary.eggs = 6; coronary.cheese = 10; coronary.cookedfor = 6.25; coronary.extras = "bacon"; 7

Exercise: Changes to memory Draw changes to memory for commented lines 1-7 public class TwoOmeletsExercise{ public static void main(string args[]){ Omelet om = new Omelet(); om.eggs = 3; om.cheese = 4; om.cookedfor = 4.5; om.extras = "ham"; Omelet coronary = new Omelet(); coronary.eggs = 6; coronary.cheese = 10; coronary.cookedfor = 6.25; coronary.extras = "bacon"; om.cookedfor += 0.75; // 1 coronary.eggs++; // 2 om.extras = "turkey"; // 3 coronary.extras = // 4 coronary.extras + " sausage"; Omelet x = coronary; // 5 x.cookedfor += 1.0; // 6 om = new Omelet(); // 7 8

Static Methods for Objects Classes/Objects typically have methods associated with them static methods follow same conventions for objects as previously established for other things like arrays and ints Can change the object, return answers, call other methods Exercise: OmeletOps OmMethods.java contains code which operates on Omelets OmeletOps.java has a main() method which uses those methods Show the output of running the main() 9

OmMeth: Omelet Methods // Static methods to work on Omelet objects public class OmMeth{ // Cook the omelet for given time public static void cookfor(omelet om, double time){ om.cookedfor += time; // Add an egg to the omelet public static void addegg(omelet om){ om.eggs++; // Calculate the calorie count of the basic omelet ingredients. public static int getcalories(omelet om){ int EGG_CALORIES = 94; int CHEESE_OZ_CALORIES = 114; return om.eggs * EGG_CALORIES + om.cheese * CHEESE_OZ_CALORIES; // Determine if consumption of the given omelet is risky public static boolean foodpoisoningiminent(omelet om){ return om.cookedfor < (1.0 * om.eggs); // Return a reference to the omelet x or y that has more calories public static Omelet richeromelet(omelet x, Omelet y){ int xcal = getcalories(x); int ycal = getcalories(y); if(xcal > ycal){ return x; else{ return y; 10

OmeletOps: main() method // Demonstrate use of OmMeth methods for omelets // Show output at the commented lines public class OmeletOps{ public static void main(string args[]){ Omelet standard = new Omelet(); standard.eggs = 2; standard.cheese = 4; standard.cookedfor = 0.0; standard.extras = "ham"; OmMeth.addEgg(standard); OmMeth.cookFor(standard, 2.5); int calories = OmMeth.getCalories(standard); boolean safe = OmMeth.foodPoisoningIminent(standard); System.out.printf("Has %d eggs\n", standard.eggs); // 1 System.out.printf("Has %d calories\n", calories); // 2 System.out.printf("Safe to eat? %b\n", safe); // 3 Compile and Run > javac OmeletOps.java > java OmeletOps Has 3 eggs Has 738 calories Safe to eat? true Richer has 6 eggs Omelet coronary = new Omelet(); coronary.eggs = 6; coronary.cheese = 10; Omelet richer = OmMeth.richerOmelet(standard, coronary); System.out.printf("Richer has %d eggs\n", richer.eggs); // 4 11

Distributing Code Across Multiple Files Complex problems involve many parts Code to solve them is often distributed into many.java files Each class in its own.java file Extra helper methods somewhere in another file main() method in its own class and.java file Organization and Navigation of files will start to become more important 12

Class Definition and Methods in One File Commonly the case that the definition of a class and the methods that operate on it are stored in the same.java file For Omelets, file would be something like the following This is the structure we will often use, labs and projects public class Omelet{ // FIELDS public int eggs; public int cheese; public double cookedfor; public String extras; // How many eggs in the omelet // How many ounces of cheese // How long the omelet has cooked // Extra ingredients added // STATIC METHODS // Cook the omelet for given time public static void cookfor(omelet om, double time){ om.cookedfor += time; // Add an egg to the omelet public static void addegg(omelet om){ om.eggs++;... 13

NullPointerExceptions Mistakes with initializing object references often lead to Exceptions Code // Demonstrates a NullPointerException with Omelets public class NullOmelet{ public static void main(string args[]){ Omelet anom = new Omelet(); anom.eggs = 2; anom.cheese = 4; Omelet noom = null; Omelet richer = Omelet.richerOmelet(anOm, noom); System.out.printf("Richer has %d eggs\n", richer.eggs); Result: Exception and Stack Trace > javac NullOmelet.java > java NullOmelet Exception in thread "main" java.lang.nullpointerexception at Omelet.getCalories(Omelet.java:24) at Omelet.richerOmelet(Omelet.java:34) at NullOmelet.main(NullOmelet.java:9) 14

Quick Exercise: Identify errors What seems to be the trouble below? 1 public class OmeletException{ 2 public static void main(string args[]){ 3 Omelet om = new Omelet(); 4 int len = om.extras.length(); 5 System.out.printf("Extra ingredients as %d chars\n",len); 6 7 > javac OmeletException.java > java OmeletException Exception in thread "main" java.lang.nullpointerexception at OmeletException.main(OmeletException.java:4) 15

Answer: Identify errors 1 public class OmeletException{ 2 public static void main(string args[]){ 3 Omelet om = new Omelet(); 4 int len = om.extras.length(); 5 System.out.printf("Extra ingredients as %d chars\n",len); 6 7 > javac OmeletException.java > java OmeletException Exception in thread "main" java.lang.nullpointerexception at OmeletException.main(OmeletException.java:4) Unless set, all fields of om have default values Default value for String extras is null Cannot invoke a length() method on a null pointer: exception Fix with something like Omelet om = new Omelet(); om.extras = "butter"; int len = om.extras.length(); 16

Reading Stack Traces Basic debugging skills will become essential Stack traces in Java provide information on where an error finally causes a problem Frequently this is not where the error originates but reading a stack trace gives someplace to begin the search > java NullOmelet Exception in thread "main" java.lang.nullpointerexception Exception causing failure at Omelet.getCalories(Omelet.java:24) Active method, line num of exception at Omelet.richerOmelet(Omelet.java:34) Next oldest active method at NullOmelet.main(NullOmelet.java:9) Oldest active method:line num 17

Stack Traces During Testing in DrJava When test fails, DrJava can report stack traces Look for the "Stack Trace" button in the lower right corner 18

Exercise: Arrays of Objects Creating arrays of objects has the same feel as primitives One key difference: initial contents of new arrays are the default value for objects: null Array setup bug This code creates an array of Omelet objects but has an error so that the final printing loop will cause an exception. Find the bug and draw some pictures to support your diagnoses. 1 // Demonstrate assigning objects (Omelets) to an 2 // array of objects. This program has a bug. 3 public class OmeletArray{ 4 public static void main(string args[]){ 5 Omelet omarray[] = new Omelet[5]; 6 omarray[0] = new Omelet(); 7 omarray[0].eggs = 2; 8 omarray[0].cheese = 4; 9 omarray[0].extras = "ham mushroom"; 10 11 omarray[1] = new Omelet(); 12 for(int i=0; i<4; i++){ 13 Omelet.addEgg(omArray[1]); 14 15 omarray[1].cheese = 3; 16 omarray[1].cookedfor = 1.0; 17 18 for(int i=3; i<omarray.length; i++){ 19 omarray[i] = new Omelet(); 20 omarray[i].eggs = i; 21 omarray[i].cheese = i/2; 22 omarray[i].extras = ""; 23 24 25 for(int i=0; i<omarray.length; i++){ 26 System.out.printf("Om: %d eggs, %d cheese,", 27 omarray[i].eggs, omarray[i].cheese); 28 29 System.out.printf("%.1f cooked, %s extras\n", 30 omarray[i].cookedfor, omarray[i].extras); 31 32 33 19

Answer: Array setup bug omarray elements are initially null, point at no objects/omelets First two blocks set omarray[0] and omarray[1] to omelets Does not matter in this case that omarray[1].extras is null: will print as "null" Loop sets omarray[3] and omarray[4] to omelets omarray[2] is still null and attempting to access its fields in print loop will cause an exception 1 // Demonstrate assigning objects (Omelets) to an 2 // array of objects. This program has a bug. 3 public class OmeletArray{ 4 public static void main(string args[]){ 5 Omelet omarray[] = new Omelet[5]; 6 omarray[0] = new Omelet(); 7 omarray[0].eggs = 2; 8 omarray[0].cheese = 4; 9 omarray[0].extras = "ham mushroom"; 10 11 omarray[1] = new Omelet(); 12 for(int i=0; i<4; i++){ 13 Omelet.addEgg(omArray[1]); 14 15 omarray[1].cheese = 3; 16 omarray[1].cookedfor = 1.0; 17 18 for(int i=3; i<omarray.length; i++){ 19 omarray[i] = new Omelet(); 20 omarray[i].eggs = i; 21 omarray[i].cheese = i/2; 22 omarray[i].extras = ""; 23 24 25 for(int i=0; i<omarray.length; i++){ 26 System.out.printf("Om: %d eggs, %d cheese,", 27 omarray[i].eggs, omarray[i].cheese); 28 29 System.out.printf("%.1f cooked, %s extras\n", 30 omarray[i].cookedfor, omarray[i].extras); 31 32 33 20

Things that should bug smart, lazy programmers Construction Isn t there a shorter way to create an omelet initially than this? Omelet om = new Omelet(); om.eggs = 3; om.cheese = 4;...; Methods Do I really have to write Omelet.doSomething() for every method? Safety and Consistency I can break omelets super easily! om.eggs = -2; Omelet.cookFor(om, 3.0); om.cookedfor = 0; public and static What s the deal with these words we keep using with methods? public static void addegg(...) Omelet.addEgg(om); Omelet.cookFor(om); Answers coming soon All of these are related. The fun just doesn t stop. 21