Building a fraction class.

Similar documents
Object Oriented Design: Building a Fraction Class

CIT 590 Homework 6 Fractions

CISC 3115 Modern Programming Techniques Spring 2018 Section TY3 Exam 2 Solutions

Lab 9: Pointers and arrays

More on Classes. The job of this method is to return a String representation of the object. Here is the tostring method from the Time class:

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

Programming via Java Defining classes

ARTIFICIAL INTELLIGENCE (CS 370D)

CSC242: Intro to AI. Lecture 8 LOGIC

Use lists. Use loops. Use conditionals. Define and use functions. Create and use code modules

CSC242: Intro to AI. Lecture 10. Tuesday, February 26, 13

CLASSES AND OBJECTS. Fundamentals of Computer Science I

CIT 590 Homework 10 Battleship

Introduction to Classes and Objects. David Greenstein Monta Vista High School

Object Oriented Modeling

Chapter 10 Writing Classes

Chapter 4 Defining Classes I

Classes and objects. Chapter 2: Head First Java: 2 nd Edi4on, K. Sierra, B. Bates

More on inheritance CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2014

Object Oriented Programming COP3330 / CGS5409

JAVA OPERATORS GENERAL

What will this print?

Chapter 7. Defining Your Own Classes Part 2. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

CSE 373. Objects in Collections: Object; equals; compareto; mutability. slides created by Marty Stepp

Assignment WS 2012/2013

CLASSES AND OBJECTS. Fundamentals of Computer Science I

Constructors.

Learning Rule Based Programming. Using Games

COP 3330 Final Exam Review

Knowledge Representation. CS 486/686: Introduction to Artificial Intelligence

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Rational number operations can often be simplified by converting mixed numbers to improper fractions Add EXAMPLE:

CMPSCI 187: Programming With Data Structures. Lecture #15: Thinking Recursively David Mix Barrington 10 October 2012

Math Modeling in Java: An S-I Compartment Model

ASSIGNMENT 2. COMP-202A, Fall 2013, All Sections. Due: October 20 th, 2013 (23:59)

Utilities (Part 2) Implementing static features

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

CMP 326 Midterm Fall 2015

C212 Fall 2010 Final Exam Answers

Computer Programming: C++

CS-140 Fall 2017 Test 2 Version A Nov. 29, 2017

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

Variables and data types

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Inheritance (IS A Relationship)

Built-in data types. logical AND logical OR logical NOT &&! public static void main(string [] args)

Lab 4: Super Sudoku Solver CSCI 2101 Fall 2017

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2013

For next Tuesday. Read chapter 8 No written homework Initial posts due Thursday 1pm and responses due by class time Tuesday

Sudoku-4: Logical Structure

Inheritance and objects

Algebra II Chapter 8 Part 2: Rational Functions

Built-in data types. public static void main(string [] args) logical AND logical OR logical NOT &&! Fundamentals of Computer Science

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

CmpSci 187: Programming with Data Structures Spring 2015

Slide 1 Side Effects Duration: 00:00:53 Advance mode: Auto

Designing data types. Overview. Object Oriented Programming. Alan Kay. Object Oriented Programming (OOP) Data encapsulation. Checking for equality

The State Universtiy of New York, Korea Computer Science

Static methods. Not actually a valid Java static method. Fundamentals of Computer Science Keith Vertanen

CS 101 Fall 2006 Midterm 3 Name: ID:

Prof. Navrati Saxena TA: Rochak Sachan

CSCI 102 Fall 2010 Exam #1

CSC 222: Object-Oriented Programming. Fall 2017

Classes and Objects in Java Instance Methods

In our classes, we've had

The Bracket Strategy

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

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

Programming Problems 22nd Annual Computer Science Programming Contest

Section 2.3 Rational Numbers. A rational number is a number that may be written in the form a b. for any integer a and any nonzero integer b.

More on methods and variables. Fundamentals of Computer Science Keith Vertanen

COMP-202 More Complex OOP

Java GUI Test #1 Solutions 7/10/2015

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

Chapter 10 Recursion

A Fraction Class. Using a Fraction class, we can compute the above as: Assignment Objectives

CSCI 136: Fundamentals of Computer Science II. Keith Vertanen Museum

CS211 Computers and Programming Matthew Harris and Alexa Sharp July 9, Boggle

Declaring and ini,alizing 2D arrays

Student Success Center Arithmetic Study Guide for the ACCUPLACER (CPT)

CS 251 Intermediate Programming Methods and Classes

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Lab 9: Creating a Reusable Class

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

CIS133J. Working with Numbers in Java

Java generics. h"p:// h"p://

Accuplacer Arithmetic Study Guide

Initializers: Array initializers can be used with class base types as well. The elements of the initializer can be expressions (not just constants).

CS 231 Data Structures and Algorithms, Fall 2016

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

Slide 1 CS 170 Java Programming 1 Multidimensional Arrays Duration: 00:00:39 Advance mode: Auto

Carleton University Department of Systems and Computer Engineering SYSC Foundations of Imperative Programming - Winter Lab 8 - Structures

CS 455 Midterm Exam 2 Fall 2015 [Bono] Nov. 10, 2015

Assignment 5: Part 1 (COMPLETE) Sprites on a Plane

Assignment 4. Aggregate Objects, Command-Line Arguments, ArrayLists. COMP-202B, Winter 2011, All Sections. Due: Tuesday, March 22, 2011 (13:00)

CSC 1214: Object-Oriented Programming

CSE 131 Introduction to Computer Science Fall Exam II

Topic 3: Fractions. Topic 1 Integers. Topic 2 Decimals. Topic 3 Fractions. Topic 4 Ratios. Topic 5 Percentages. Topic 6 Algebra

Mini-Lesson 1. Section 1.1: Order of Operations PEMDAS

Transcription:

Building a fraction class http://www.zazzle.com/fraction+tshirts CSCI 135: Fundamentals of Computer Science Keith Vertanen Copyright 2013

Overview Object oriented techniques Constructors Methods that take another object of same type Private helper methods Fraction class Create a class to represent fractions 2

Wumpus World And What are Objects Anyway? The Game. The Wumpus World game takes place in a cave with different rooms in it. You can think of the cave as an NxM rectangular grid. The player always starts in position 0,0 (in the lower left hand corner of the cave), which is guaranteed to be safe (but it may still be smelly or breezy or glittery). The objective of the game is to find the gold. The player will know when he/she is in a room with the gold because there will be a "glitter" in that room. If the player detects a glitter, he/she can pick up the gold and the game is won. Bottomless pits are present in some of the rooms. There is a 20% chance that any given room will have a pit. All rooms adjacent to a pit are breezy, that is, a player entering a room adjacent to a pit will detect a breeze. If the player moves into the room with a pit, he/she falls in and dies a horrible death. "Adjacent" to means up down, left or right - you don't have to worry about diagonals. There is only one wumpus in the cave, and he is also placed at random. Rooms adjacent to the wumpus are smelly, that is, a player will detect a stench in a room adjacent to a wumpus. The wumpus cannot move. If the player enters a room with the wumpus, he/she will be eaten, and, once again, die a horrible death. There is also only one room in the cave that contains the gold. Unlike the other objects, the player has to be in the same room as the gold in order to detect a glitter. Like the wumpus, the gold is placed at random. The player can move up, down, left, or right. The player also has one arrow. Once it's used up, it's gone. It can be used to shoot a wumpus, and can be shot in any direction the player can move in. The wumpus does not need to be in the next room in a given direction - an arrow shot in some direction should travel in that direction until it either hits a wall or the wumpus. If the player is successful in shooting the wumpus, the wumpus will emit a blood-curdling scream, and will no longer be a threat. The only other action the player can perform is to "grab gold". When the player first starts the game, he/she does not know (and cannot see) where the location of pits, gold and the wumpus are. The only clues are whether the current room is breezy, smelly, or glittery. 3

What is a Fraction? Well it has a numerator and a denominator. You can perform mathematical operations on it, like addition, subtraction, multiplication and division. You can reduce it to its lowest terms. If you do any operations on it, you probably want to print the results out. 4

Hey objects, where did you come from? "Dude, don't you know where objects come from?!? The object stork totally dropped us off." 0.1, 0.5 r=0.1 0.9, 0.6 r=0.15 0.5, 0.3 r=0.2 public Ball(double x, double y, double r) posx = x; posy = y; radius = r; Constructor = the object stork 5

Automatic default constructors Rule 1: If you do not create a constructor one will be automatically created for you Default no-arg constructor Doesn't do anything, no code runs All instance variable are whatever you initialized them to (or their default value) public class Fraction private int num; private int denom; // numerator (upstairs) // denominator (downstairs) 6

Creating with default constructor To create object using no-arg constructor Use empty ()'s after the new Parameter list always sent when new'ing object Java needs to know which constructor to run public class FractionClient public static void main(string [] args) Fraction a = new Fraction(); Fraction [] fracs = new Fraction[2]; fracs[0] = new Fraction(); fracs[1] = new Fraction(); 7

Declaring your own constructor Rule 2: If you declare any constructor, a default one will not be automatically created public class Fraction private int num; private int denom; // numerator (upstairs) // denominator (downstairs) public Fraction(int n, int d) num = n; denom = d; 8

Creating with default constructor public class FractionClient public static void main(string [] args) Fraction a = new Fraction(); Fraction [] fracs = new Fraction[2]; fracs[0] = new Fraction(); fracs[1] = new Fraction(); We broke all the calls to create a Fraction object since there no longer exists a no-arg version of the constructor. % javac FractionClient.java FractionClient.java:5: error: constructor Fraction in class Fraction cannot be applied to given types; Fraction a = new Fraction(); ^ required: int,int found: no arguments reason: actual and formal argument lists differ in length 9

Constructor overloading Rule 3: You can declare as many constructor versions as you need/want public class Fraction private int num; private int denom; // numerator (upstairs) // denominator (downstairs) Hooray! Now our code using a no-arg constructor will work again. public Fraction() public Fraction(int n, int d) num = n; denom = d; 10

Parameters of your own type Create a new object based on another instance of the same type A copy constructor public class Fraction private int num; private int denom; // numerator (upstairs) // denominator (downstairs) // Create a new Fraction object that has the same // values as some other fraction object. public Fraction(Fraction other) num = other.num; denom = other.denom; You can access private instance variables of another object of the same type inside a method of that type. 11

Multiplying fractions Goal: Given two fraction objects, return a new fraction that is the multiplication of the two public class FractionClient public static void main(string [] args) Fraction a = new Fraction(1, 2); Fraction b = new Fraction(2, 3); Fraction c = a.multiply(b); System.out.println(a + " * " + b + " = " + c); % java FractionClient 1/2 * 2/3 = 1/3 12

Multiply method public class Fraction private int num; private int denom; // numerator (upstairs) // denominator (downstairs)... public Fraction multiply(fraction other) Fraction result = new Fraction(num * other.num, denom * other.denom); return result; Denominator of the object that called multiply (before the dot). Fraction c = a.multiply(b); Denominator of the object passed as a parameter to the multiply() method 13

Multiplying fractions Attempt 1: Hmmmm, we forgot something public class FractionClient public static void main(string [] args) Fraction a = new Fraction(1, 2); Fraction b = new Fraction(2, 3); Fraction c = a.multiply(b); System.out.println(a + " * " + b + " = " + c); % java FractionClient Fraction@164f1d0d * Fraction@23fc4bec = Fraction@8dc8569 14

Multiplying fractions Attempt 2: Close, but not in lowest terms public class Fraction private int num; private int denom; // numerator (upstairs) // denominator (downstairs)... public String tostring() return "" + num + "/" + denom; % java FractionClient 1/2 * 2/3 = 2/6 An object's tostring() method is called automatically whenever you attempt to print an object 15

Lowest terms Attempt 3: Add code to reduce to lowest terms public class Fraction... public Fraction multiply(fraction other) Fraction result = new Fraction(num * other.num, denom * other.denom); int i = Math.min(Math.abs(result.num), Math.abs(result.denom)); if (i == 0) return result; while ((result.num % i!= 0) (result.denom % i!= 0)) i--; Fraction result2 = new Fraction(result.num / i, result.denom / i); return result2; % java FractionClient 1/2 * 2/3 = 1/3 16

Divide method Very similar method for division: public class Fraction... public Fraction divide(fraction other) Fraction result = new Fraction(num * other.denom, denom * other.num); int i = Math.min(Math.abs(result.num), Math.abs(result.denom)); if (i == 0) return result; while ((result.num % i!= 0) (result.denom % i!= 0)) i--; Fraction result2 = new Fraction(result.num / i, result.denom / i); return result2; 17

Repeated code is evil public Fraction multiply(fraction other) Fraction result = new Fraction(num * other.num, denom * other.denom); int i = Math.min(Math.abs(result.num), Math.abs(result.denom)); if (i == 0) return result; while ((result.num % i!= 0) (result.denom % i!= 0)) i--; Fraction result2 = new Fraction(result.num / i, result.denom / i); return result2; Where should this code really live? There are a number of choices, but not here for sure. We'd have to repeat it in the divide(), add(), and subtract() methods as well. 18

Helper methods Add a private helper method, reduce() public class Fraction private void reduce() int i = Math.min(Math.abs(num), Math.abs(denom)); if (i == 0) return; while ((num % i!= 0) (denom % i!= 0)) i--; num = num / i; denom = denom / i; public Fraction multiply(fraction other) Because it is a private method, can only be called inside other methods in the Fraction class Fraction result = new Fraction(num * other.num, denom * other.denom); result.reduce(); return result; 19

Fill in the missing code public class Fraction public Fraction multiply(fraction other) Fraction result = new Fraction(num * other.num, result.reduce(); return result; public boolean equals(fraction other) public Fraction reciprocal() public Fraction add(fraction other) denom * other.denom); public Fraction subtract(fraction other) 20

Objects Summary No-arg default constructors Passing objects of same type to method Private helper methods Fraction object Built an object to represent a fraction 21