Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Similar documents
CS 302 Week 9. Jim Williams

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

Java Review. Fundamentals of Computer Science

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

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

BM214E Object Oriented Programming Lecture 8

ECE 122. Engineering Problem Solving with Java

Chapter 6 Lab Classes and Objects

CS1004: Intro to CS in Java, Spring 2005

Classes, interfaces, & documentation. Review of basic building blocks

if (x == 0); System.out.println( x=0 ); if (x = 0) System.out.println( x=0 );

Handout 7. Defining Classes part 1. Instance variables and instance methods.

Chapter 6 Lab Classes and Objects

public static boolean isoutside(int min, int max, int value)

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

COMP 250 Winter 2011 Reading: Java background January 5, 2011

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited

APCS Semester #1 Final Exam Practice Problems

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017

Principles of Object Oriented Programming. Lecture 4

Media Computation. Lecture 15.2, December 3, 2008 Steve Harrison

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Object-Oriented Programming

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

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

Subclassing for ADTs Implementation

Documenting, Using, and Testing Utility Classes

Chapter 15: Object Oriented Programming

Fundamentos de programação

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

Java Fundamentals (II)

COMP 401: THE DUAL ROLE OF A CLASS. Instructor: Prasun Dewan (FB 150,

Object Oriented Programming

Assignment 1 due Monday at 11:59pm

Classes and Methods: Classes

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

This exam is open book. Each question is worth 3 points.

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Recitation 3 Class and Objects

Programming II (CS300)

Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007

Algorithmic Thinking and Structured Programming (in Greenfoot) Teachers: Renske Smetsers-Weeda Sjaak Smetsers Ana Tanase

CS1083 Week 2: Arrays, ArrayList

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Object Oriented Programming

CS 101 Fall 2006 Midterm 3 Name: ID:

Two hours QUESTION PAPER MUST NOT BE REMOVED FROM THE EXAM ROOM UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE

The Singleton Pattern. Design Patterns In Java Bob Tarr

CS 113 MIDTERM EXAM 2 SPRING 2013

Chapter 4 Defining Classes I

The Singleton Pattern. Design Patterns In Java Bob Tarr

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

Chapter 4: Writing Classes

CS 302: Introduction to Programming in Java. Lecture 15

Options for User Input

Defining Classes and Methods

1 If you want to store a letter grade (like a course grade) which variable type would you use? a. int b. String c. char d. boolean

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003

Ch 7 Designing Java Classes & Class structure

TaxiBot New attributes Variables Math! TaxiBot

Java and OOP. Part 2 Classes and objects

Classes. Classes as Code Libraries. Classes as Data Structures. Classes/Objects/Interfaces (Savitch, Various Chapters)

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Software and Programming 1

Ch 7 Designing Java Classes & Class structure. Fields have data values define/describe an instance

University of Palestine. Mid Exam Total Grade: 100

Rules and syntax for inheritance. The boring stuff

A foundation for programming. Classes and objects. Overview. Java primitive types. Primitive types Creating your own data types

BM214E Object Oriented Programming Lecture 7

OBJECTS AND CLASSES CHAPTER. Final Draft 10/30/2011. Slides by Donald W. Smith TechNeTrain.com

CSE 21 Intro to Computing II. Post-Midterm Review

CSE 142 Su 04 Computer Programming 1 - Java. Objects

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

CSCI 1103: Object-Oriented Objects

1.00 Introduction to Computers and Engineering Problem Solving Quiz 1 March 4, 2005

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

CSCI 161 Introduction to Computer Science

CSC-140 Assignment 6

(a) Write the signature (visibility, name, parameters, types) of the method(s) required

What is it? CMSC 433 Programming Language Technologies and Paradigms Spring Approach 1. Disadvantage of Approach 1

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

CS Week 13. Jim Williams, PhD

Java Classes, Inheritance, and Interfaces

Notes on Chapter Three

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Classes. Classes as Code Libraries. Classes as Data Structures

Objects as a programming concept

More Java Basics. class Vector { Object[] myarray;... //insert x in the array void insert(object x) {...} Then we can use Vector to hold any objects.

Express Yourself. Writing Your Own Classes

QUIZ 2 Introduction to Computer Science (COMP 250) Mon. March 2, 2009 Professor Michael Langer

Classes Classes 2 / 36

CompuScholar, Inc. 9th - 12th grades

ITI Introduction to Computing II

Recommended Group Brainstorm (NO computers during this time)

CMSC 202H. Classes and Objects: Reusing Classes with Composition

Chapter 4. Defining Classes I

CLASSES AND OBJECTS. Fundamentals of Computer Science I

Transcription:

Problem Solving Creating a class from scratch 1

Recipe for Writing a Class 1. Write the class boilerplate stuff 2. Declare Fields 3. Write Creator(s) 4. Write accessor methods 5. Write mutator methods 6. Write test cases / test driver 7. Now go back and fill in the implementation 2

Class Boilerplate What is the class name? What package will the class be in? Javadoc Description package xmppackage; /* * Keep track of examples */ class XmpClass { Boilerplate: the parts of your code that is always the same and doesn t change the function but is still needed 3

Declare Fields What data needs to be tracked for this class? What data type does that need to be? As we implement the class, we may modify the list of fields, but we should at least get started. private int xmpfld; // example number 4

Create Constructor(s) What parameters are required? Which fields need / can have initial values? How do we derive initial field values from parameters? Write boilerplate then fill in /* * Create an xmpclass object * @param number to initialize field */ public XmpClass(int initval) { 5

Create getter accessor methods This is just typing really mindless For each field (at least each one you will need outside the class) Method name is get<field_name> Return type is the type of the field Return value is the field name /* * @returns current value of xmpfld */ public int getxmpfld() { return xmpfld; 6

Create non-getter accessor methods What parameters do you get? What value do you need to return? What type is it? How do you get from fields and parameters to return value? Write boilerplate first /* * @returns double field value */ public int getdoublefld() { 7

Accessor method tostring If you want to print your object, think about how you want it formatted. When your object is appended to a string, your tostring method will be invoked If you don t specify a tostring method, a default tostring method will be used it may be good enough. If not write your own. public String tostring() { return xmpfld= +xmpfld; 8

Mutator methods - setters Again, mindless Write only if the outside world directly needs to modify fields For each set field: Returns void Name is set<field_name> Argument is the same type as the field /* * @param new value for field */ public void setxmpfld(int n) { xmpfld=n; 9

Mutator methods - Actions What action is being performed on the object? What parameters are required to perform that action? How should the action modify the fields? Does this action return a value? If so, what type of value? /* * add to the field value * @param value to add * @return new value */ public int add(int n) { 10

Unit Test Driver Test all methods at least once Test all exception cases Read through code what might go wrong What might be null? What might overflow? What causes divide by zero? What might violate your assumptions? Remember if it s really exceptional, let exception handling handle it. 11

In-class vs. External Unit Test Driver In-Class Packaged with the class More likely up to date Easy to run May miss private/public problems External More like how your class will be used Catch all public/private problems Requires more discipline to keep up to date Requires extra management for extra classes 12

Unit Test Boilerplate public static void main(string[] arg) { System.out.println( Test XmpClass creators ); XmpClass test1 = new XmpClass(14); if (! xmpfld=14.equals(test1.tostring())) { System.out.println( new XmpClass(14) failed! ); 13

Problem Write a Java class that models three boxes that can contain balls called Boxes. The Boxes class should have a single field an array of integers that defines how many balls are in each of the three boxes. The creator should take three integer parameters that define the initial state of the boxes the number of balls in each box. The Boxes class should also have a move method, which takes two parameters an integer 0, 1, or 2 to represent the from box, and an integer 0, 1, or 2 to represent the to box. The move method should return a Boolean true value if the move is successful, that is it is possible to move a ball from the from box to the to box. In this case, the move method should update the number of balls in the from box and the number of balls in the to box. If the move is not possible, the move method should return false and not modify the number of balls in the boxes. You may assume the from and to parameters contain valid values. 14

First, write the class boilerplate package quiz1; public class Boxes { 15

Declare Fields Write a Java class that models three boxes that can contain balls called Boxes. The Boxes class should have a single field an array of integers that defines how many balls are in each of the three boxes. The creator should take three integer parameters that define the initial state of the boxes the number of balls in each box. The Boxes class should also have a move method, which takes two parameters an integer 0, 1, or 2 to represent the from box, and an integer 0, 1, or 2 to represent the to box. The move method should return a Boolean true value if the move is successful, that is it is possible to move a ball from the from box to the to box. In this case, the move method should update the number of balls in the from box and the number of balls in the to box. If the move is not possible, the move method should return false and not modify the number of balls in the boxes. You may assume the from and to parameters contain valid values. 16

Declare Fields package quiz1; public class Boxes { int[] balls; 17

Write Creator Boilerplate Write a Java class that models three boxes that can contain balls called Boxes. The Boxes class should have a single field an array of integers that defines how many balls are in each of the three boxes. The creator should take three integer parameters that define the initial state of the boxes the number of balls in each box. The Boxes class should also have a move method, which takes two parameters an integer 0, 1, or 2 to represent the from box, and an integer 0, 1, or 2 to represent the to box. The move method should return a Boolean true value if the move is successful, that is it is possible to move a ball from the from box to the to box. In this case, the move method should update the number of balls in the from box and the number of balls in the to box. If the move is not possible, the move method should return false and not modify the number of balls in the boxes. You may assume the from and to parameters contain valid values. 18

Creator Boilerplate package quiz1; public class Boxes { int[] balls; public Boxes(int p1, int p2, int p3) { 19

Create accessor/action method Write a Java class that models three boxes that can contain balls called Boxes. The Boxes class should have a single field an array of integers that defines how many balls are in each of the three boxes. The creator should take three integer parameters that define the initial state of the boxes the number of balls in each box. The Boxes class should also have a move method, which takes two parameters an integer 0, 1, or 2 to represent the from box, and an integer 0, 1, or 2 to represent the to box. The move method should return a Boolean true value if the move is successful, that is it is possible to move a ball from the from box to the to box. In this case, the move method should update the number of balls in the from box and the number of balls in the to box. If the move is not possible, the move method should return false and not modify the number of balls in the boxes. You may assume the from and to parameters contain valid values. 20

Method Boilerplate package quiz1; public class Boxes { int[] balls; public Boxes(int p1, int p2, int p3) { boolean move(int from,int to) { 21

Implement Creator Write a Java class that models three boxes that can contain balls called Boxes. The Boxes class should have a single field an array of integers that defines how many balls are in each of the three boxes. The creator should take three integer parameters that define the initial state of the boxes the number of balls in each box. The Boxes class should also have a move method, which takes two parameters an integer 0, 1, or 2 to represent the from box, and an integer 0, 1, or 2 to represent the to box. The move method should return a Boolean true value if the move is successful, that is it is possible to move a ball from the from box to the to box. In this case, the move method should update the number of balls in the from box and the number of balls in the to box. If the move is not possible, the move method should return false and not modify the number of balls in the boxes. You may assume the from and to parameters contain valid values. 22

At the start of creator this balls Boxes nill 23

Instantiate the Array!!!!!! package quiz1; public class Boxes { int[] balls; public Boxes(int p1, int p2, int p3) { balls=new int[3]; balls[0]=p1; balls[1]=p2; balls[2]=p3; 24

Alternative Instantiate the Array!!!!!! package quiz1; public class Boxes { int[] balls; public Boxes(int p1, int p2, int p3) { int[] temp={p1,p2,p3; balls=temp; 25

At end of creator this balls Boxes int[] length 3 0 p1 1 p2 2 p3 26

Implement the methods Write a Java class that models three boxes that can contain balls called Boxes. The Boxes class should have a single field an array of integers that defines how many balls are in each of the three boxes. The creator should take three integer parameters that define the initial state of the boxes the number of balls in each box. The Boxes class should also have a move method, which takes two parameters an integer 0, 1, or 2 to represent the from box, and an integer 0, 1, or 2 to represent the to box. The move method should return a Boolean true value if the move is successful, that is it is possible to move a ball from the from box to the to box. In this case, the move method should update the number of balls in the from box and the number of balls in the to box. If the move is not possible, the move method should return false and not modify the number of balls in the boxes. You may assume the from and to parameters contain valid values. 27

Implement move public class Boxes { int[] balls; boolean move(int from,int to) { if (balls[from]==0) return false; balls[from]--; balls[to]++ return true; 28

Implement tostring public class Boxes { int[] balls; public String tostring() { return boxes A[ + balls[0] + ] B[ + balls[1] + C[ + balls[2] ] ; 29

Write Tester package quiz1; class Tester { static public void main(string args[]) { Boxes b = new Boxes(3,2,1); System.out.println("Boxes start at: " + b); while(b.move(0,2)) { System.out.println("After loop, its: " + b); 30

Run Test laptop:~/cs140/quiz1>java -cp. quiz1.tester Boxes start at: Boxes A[3] B[2] C[1] After loop, its: Boxes A[0] B[2] C[4] laptop:~/cs140/quiz1> 31