CIT 590 Homework 6 Fractions

Similar documents
Recitation #2 Abstract Data Types, Collection Classes, and Linked List

Object Oriented Design: Building a Fraction Class

Creating a new data type

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

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

Rules of Exponents Part 1[Algebra 1](In Class Version).notebook. August 22, 2017 WARM UP. Simplify using order of operations. SOLUTION.

CS 101 Fall 2006 Midterm 3 Name: ID:

Rational numbers as decimals and as integer fractions

Chapter 4 Section 2 Operations on Decimals

coe318 Lab 2 ComplexNumber objects

Reteaching. Comparing and Ordering Integers

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

Building a fraction class.

Integers are whole numbers; they include negative whole numbers and zero. For example -7, 0, 18 are integers, 1.5 is not.

CMSC 132, Object-Oriented Programming II Summer Lecture 1:

What is a Fraction? A fraction is a part or piece of something. The way we write fractions tells us the size of the piece we are referring to

Using APIs. Chapter 3. Outline Fields Overall Layout. Java By Abstraction Chapter 3. Field Summary static double PI

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

CIT 590 Homework 5 HTML Resumes

What is a Fraction? Fractions. One Way To Remember Numerator = North / 16. Example. What Fraction is Shaded? 9/16/16. Fraction = Part of a Whole

Name: Date: Review Packet: Unit 1 The Number System

( 3) ( 4 ) 1. Exponents and Radicals ( ) ( xy) 1. MATH 102 College Algebra. still holds when m = n, we are led to the result

Rational Expressions Sections

Algebra 1 Review. Properties of Real Numbers. Algebraic Expressions

3.1 Dividing a Whole into Fractional Parts. 3.1 Dividing a Set into Fractional Parts. 3.2 Identifying Parts of Wholes.

Controls Structure for Repetition

Algebra Homework: Chapter 1 (Homework is listed by date assigned; homework is due the following class period) Day Date In-Class Homework

Technical Section. Lab 4 while loops and for loops. A. while Loops or for loops

SOLUTION: Because the fractions have a common denominator, compare the numerators. 5 < 3

Lecture 13. Example. Encapsulation. Rational numbers: a number is rational if it can be defined as the ratio between two integers.

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Unit 3: Multiplication and Division Reference Guide pages x 7 = 392 factors: 56, 7 product 392

Name Student ID Number. Group Name. Group Members. Fractions

Coding Standards for Java

Unit 2: Accentuate the Negative Name:

Section 1.2 Fractions

Fraction to Percents Change the fraction to a decimal (see above) and then change the decimal to a percent (see above).

Learning Log Title: CHAPTER 3: ARITHMETIC PROPERTIES. Date: Lesson: Chapter 3: Arithmetic Properties

Integers and Rational Numbers

Any Integer Can Be Written as a Fraction

CSI33 Data Structures

Rational Number is a number that can be written as a quotient of two integers. DECIMALS are special fractions whose denominators are powers of 10.

Section A Arithmetic ( 5) Exercise A

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

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

Programming Project 5: NYPD Motor Vehicle Collisions Analysis

(-,+) (+,+) Plotting Points

Tips from the experts: How to waste a lot of time on this assignment

Lab 1: Silver Dollar Game 1 CSCI 2101B Fall 2018

CA4003 Compiler Construction Assignment Language Definition

Chapter 10 Writing Classes

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

50 MATHCOUNTS LECTURES (6) OPERATIONS WITH DECIMALS

Jim Lambers ENERGY 211 / CME 211 Autumn Quarter Programming Project 4

For Module 2 SKILLS CHECKLIST. Fraction Notation. George Hartas, MS. Educational Assistant for Mathematics Remediation MAT 025 Instructor

College Prep Algebra II Summer Packet

CSE 142 Su 04 Computer Programming 1 - Java. Objects

struct _Rational { int64_t Top; // numerator int64_t Bottom; // denominator }; typedef struct _Rational Rational;

************ THIS PROGRAM IS NOT ELIGIBLE FOR LATE SUBMISSION. ALL SUBMISSIONS MUST BE RECEIVED BY THE DUE DATE/TIME INDICATED ABOVE HERE

Full file at

Pre-Algebra Notes Unit Five: Rational Numbers and Equations

Lab 3: Call to Order CSCI 2101 Fall 2018

Lab 3: Call to Order CSCI 2101 Fall 2017

Topic 6: Partial Application, Function Composition and Type Classes

Topic 6: Partial Application, Function Composition and Type Classes

Mini-Lectures by Section

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:

Equality for Abstract Data Types

Learning Log Title: CHAPTER 3: PORTIONS AND INTEGERS. Date: Lesson: Chapter 3: Portions and Integers

CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM

Inheritance and Interfaces

AP Computer Science. TextLab05 Java Assignment. Assignment Purpose: Do not copy this file, which is provided. TextLab05 Student Version

Matrices. A Matrix (This one has 2 Rows and 3 Columns) To add two matrices: add the numbers in the matching positions:

CMSC 341. Nilanjan Banerjee

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

AP Computer Science Java Mr. Clausen Program 6A, 6B

Adding and subtracting rational expressions is quite similar to adding and subtracting rational numbers (fractions).

Claremont McKenna College Computer Science

4. Java Project Design, Input Methods

Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue

EXAMPLE 1. Change each of the following fractions into decimals.

Number System. Introduction. Natural Numbers (N) Whole Numbers (W) Integers (Z) Prime Numbers (P) Face Value. Place Value

Accuplacer Arithmetic Study Guide

Grade 6 Integers. Answer the questions. Choose correct answer(s) from the given choices. For more such worksheets visit

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

Java Review. Fundamentals of Computer Science

Math Glossary Numbers and Arithmetic

CSC 222: Computer Programming II. Spring 2005

H212 Introduction to Software Systems Honors

Spring, 2014 CIT 590. Programming Languages and Techniques Homework 7

Pre-Algebra Notes Unit Five: Rational Numbers and Equations

Is the statement sufficient? If both x and y are odd, is xy odd? 1) xy 2 < 0. Odds & Evens. Positives & Negatives. Answer: Yes, xy is odd

Operators. Lecture 3 COP 3014 Spring January 16, 2018

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.

EE 152 Advanced Programming LAB 7

CS3110 Spring 2017 Lecture 10 a Module for Rational Numbers

CMPSCI 187 / Spring 2015 Postfix Expression Evaluator

Lesson 1: Arithmetic Review

Example: Which of the following expressions must be an even integer if x is an integer? a. x + 5

Operators & Expressions

Transcription:

CIT 590 Homework 6 Fractions Purposes of this assignment: Get you started in Java and Eclipse Get you comfortable using objects in Java Start looking at some common object uses in Java. General Idea of the Assignment In this assignment, you will implement two Java classes, Fraction.java and FractionCalculator.java. Create a project Fraction and within it a package called fraction. Your classes, Fraction and FractionCalculator, will be within this package. The Fraction API Your task will be to implement the API (Application Programmer s Interface) and write a small program that uses it. The API is basically a collection of all methods defined in a given class if another programmer would like to use the class, they have to refer to the API to see what methods are available. In other words, Fraction isn't a program in itself; it is designed to be used by programmers who, for whatever reason, need to use fractions in their programs. This means Fraction.java should have no main method. The main method will be in FractionCalculator.java. The following lists some information about your Fraction class. Instance Variables How printed Restrictions Normalization Two private integers, known as the numerator and the denominator. n/d, where n is the numerator and d is the denominator. This is achieved in the tostring() method defined below. The denominator may not be zero. The fraction is always kept in the lowest terms, that is, the Greatest Common Divisor (GCD) of n and d is 1 (use Euclid's algorithm you can look this up; I also showed an example in the Python overview lecture). The denominator is never negative. (You can give a negative number for

the denominator to the constructor when you create the fraction, but a negative fraction should be represented internally with a negative numerator.) Represent zero as 0/1. You should have the following constructors. public Fraction(int numberator, int denominator) public Fraction(int wholenumber) Parameters are the numerator and the denominator. Normalize the fraction as you create it. For instance, if the parameters are (8, -12), create a Fraction with numerator -2 and denominator 3. The parameter is the numerator, and denominator is implicitly 1. The parameter is a String such as "5/ -10", or "2" (allow blanks around the integers, but not within). public Fraction(String fraction) You can use split("/") to get the two parts, then trim() both parts, then use Integer.parseInt(part) to convert each part from type String to type int. Note that Java allows you to have more than one constructor, so long as they have different numbers or types of parameters. For example, if you call new Fraction(3) you will get the second constructor listed above. Fractions should be constructed in normalized form. For example, if the user says new Fraction(6, 8), the fraction should be created with a numerator of 3 and a denominator of 4 (not 6 and 8). Similarly, if the user says new Fraction(1, -3), the fraction should be created with a numerator of -1 and a denominator of 3 (not 1 and -3). Finally, you should implement the following methods. Public Fraction add(fraction f) public Fraction subtract(fraction f) the sum of this and that: a/b + c/d is (ad + bc)/bd the difference of this minus that:

a/b - c/d is (ad - bc)/bd public Fraction multiply(fraction f) public Fraction divide(fraction f) public Fraction abs() public Fraction negate() public Fraction inverse() public int compareto(fraction f) public String tostring() the product of this minus that: (a/b) * (c/d) is (a*c)/(b*d) the quotient of this minus that: (a/b) / (c/d) is (a*d)/(b*c) the absolute value of this fraction. Returns a new Fraction that has the same numeric value as this fraction but the opposite sign. Inverse of a/b is b/a. Any negative int if this is less than f. Zero if this is equal to f. Any positive int if this is greater than f. Returns a String of the form n/d, unless d is 1, in which case it returns just n. Notes Fractions should be immutable, that is, there should be no way to change their components after the numbers have been created. Most of your methods simply return a new number. One way to achieve this is to keep instance variables private (without any methods available to modify them). Depending on how fast we go, we may not cover the tostring() method before the assignment is due. All you need to know for this assignment is that it is defined as shown above and that it is called internally when you want to print an object. For example, the following two print statements will print the same thing: Fraction f = new Fraction(6,11); System.out.println(f); System.out.println(f.toString());

We haven t covered unit testing in Java yet, so for this assignment you are not required to submit any test files. However, for your own benefit (and to make sure your program is correct) you should still write some tests. To test your files, you can write a FractionTest.java file. This file should contain a public static void main(string[] args) method to test your methods. You should not submit this file. The FractionCalculator Write a FractionCalculator class (containing a main method) that does calculations with Fractions. The first thing this program does is print a zero (indicating the current contents of the calculator), and a prompt (print some message to the user). It then accepts commands from the user, and after each command, prints the result, and a new prompt. It should accept exactly the following commands, and nothing else: a To take the absolute value of the number currently displayed. c To clear (reset to zero) the calculator. i To invert the number currently displayed. s n To discard the number currently displayed and replace it with n. q To quit the program. + n To add n to the number currently displayed. - n To subtract n from the number currently displayed. * n To multiply the number currently displayed by n. / n To divide the number currently displayed by n. In each case, the user may enter either a whole number or a fraction for n. Fractions may be written with or without spaces, as for example 27 / 99 or 27/99. You can require that there be no space after a unary minus, so for example -3 is legal, but - 3 is not. You don't have to handle unary +, i.e., assume + if always followed by a fraction (and space). You can require at least one space after the initial +, -, *, /, or s; for example a - -3/5 is legal, but --3/5 is not. If the user enters a different command than the ones defined above, your program should not crash. Instead, the program should print a short error message and keep running. Illegal input should not affect the state of the computation. You can assume that fractions are well formatted, i.e., in any command that contains a fraction you can just parse the fraction assuming that it is indeed a fraction (though you need to handle the case where the fraction is a whole number). In other words, you don t need to worry about checking whether the string contains a fraction (which requires exception handling in java, and we haven t covered that yet). You will need to use the Scanner. You can find a description (skip over the confusing details) in the Scanner page in the Java API. Recommendation: Just use the Scanner's nextline() method, and use String methods to work with the input. Some useful methods are trim(), length(),

and substring(beginindex). I found this somewhat easier that working with the Scanner methods to read the input in smaller pieces. Submission and Due Date Zip your Eclipse project and turn it in to Canvas before 11:59pm Tuesday, October 31.