CS 110 Spring 2009 Program 4: Fraction Class Implementation and Arithmetic Operations, Lab Reports of Computer Science

The learning objectives and instructions for program 4 of cs 110 spring 2009. Students are required to expand their fraction class by adding instance methods for addition, subtraction, multiplication, division, and determining if the fraction is proper or improper. The tostring() method should display the fraction type based on the isproperfraction() method. In the fractiondemo main method, users input the numerator and denominator for two fraction objects, display their values, and execute the arithmetic methods.

Typology: Lab Reports

Pre 2010

Uploaded on 08/19/2009

koofers-user-q0g
koofers-user-q0g 🇺🇸

8 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 110 Spring 2009
Program 4
Learning Objectives
Demonstrate the ability to write a simple Java project
that includes a user-defined class: Fraction
Demonstrate how to use instance variables and
methods in your user-defined class
Demonstrate the use of objects in your driver program
Understand simple decision making concepts.
Program 4 Assignment
This assignment builds on the two .java files you did for Lab4.
Begin by copying from your Lab4 folder, the Fraction.java and
FractionDemo.java files, to your Program4 folder.
Expand your Fraction class from Lab 4 by adding five more
instance methods: add, subtract, multiply, divide, and a
method to determine if the fraction is a proper or improper
fraction. In a proper fraction, the numerator is less than the
denominator; in an improper fraction the numerator is greater
than or equal to the denominator. Please take note of the
isProperFraction( ) method’s return type designated in the UML
diagram.
Methods add, subtract, multiply, and divide should accept a
Fraction argument passed as the parameter and return a
Fraction.
The toString( ) method of the Fraction class should declare a
local boolean variable, which is set to either true or false by calling the isProperFraction( )
method. Inside the toString( ) there will be an if / else decision making process which depends
on the value returned from the isProperFraction( ) method. A different String message is
returned, depending on which of these if / else statements execute.
In your FractionDemo main method, implement the following:
1. Read in the numerator and demoninator for two Fraction objects from the user.
2. Display their values as fractions (using toString). Recall that the toString( ) method
will display whether the fraction is proper or improper.
3. Display the results of the four arithmetic methods.
Fraction Arithmetic
In case you have forgotten your fraction arithmetic, here is a quick crib sheet for addition,
subtraction, multiplication, and division. Do not worry about reducing your fractions
(e.g. 4 / 8 is fine, do not reduce it to 1 / 2)
Page 1 of 3
Fraction
- numerator : int
- denominator : int
+ Fraction (n: int, d: int)
+ setNumerator (n: int) : void
+ getNumerator ( ) : int
+ setDenominator (d: int) : void
+ getDenominator ( ) : int
+ setFraction (n: int, d:int) : void
+ getAsDouble ( ) : double
+ isProperFraction( ) : boolean
+ toString ( ) : String
+ add (f2: Fraction): Fraction
+ subtract (f2: Fraction): Fraction
+ multiply (f2: Fraction): Fraction
+ divide (f2: Fraction): Fraction
pf3

Partial preview of the text

Download CS 110 Spring 2009 Program 4: Fraction Class Implementation and Arithmetic Operations and more Lab Reports Computer Science in PDF only on Docsity!

CS 110 Spring 2009

Program 4

Learning Objectives

 Demonstrate the ability to write a simple Java project that includes a user-defined class : Fraction  Demonstrate how to use instance variables and methods in your user-defined class  Demonstrate the use of objects in your driver program  Understand simple decision making concepts.

Program 4 Assignment

This assignment builds on the two .java files you did for Lab4. Begin by copying from your Lab4 folder, the Fraction.java and FractionDemo.java files, to your Program4 folder. Expand your Fraction class from Lab 4 by adding five more instance methods: add, subtract, multiply, divide, and a method to determine if the fraction is a proper or improper fraction. In a proper fraction, the numerator is less than the denominator; in an improper fraction the numerator is greater than or equal to the denominator. Please take note of the isProperFraction( ) method’s return type designated in the UML diagram. Methods add, subtract, multiply, and divide should accept a Fraction argument passed as the parameter and return a Fraction. The toString( ) method of the Fraction class should declare a local boolean variable, which is set to either true or false by calling the isProperFraction( ) method. Inside the toString( ) there will be an if / else decision making process which depends on the value returned from the isProperFraction( ) method. A different String message is returned, depending on which of these if / else statements execute. In your FractionDemo main method, implement the following:

  1. Read in the numerator and demoninator for two Fraction objects from the user.
  2. Display their values as fractions (using toString). Recall that the toString( ) method will display whether the fraction is proper or improper.
  3. Display the results of the four arithmetic methods.

Fraction Arithmetic

In case you have forgotten your fraction arithmetic, here is a quick crib sheet for addition, subtraction, multiplication, and division. Do not worry about reducing your fractions (e.g. 4 / 8 is fine, do not reduce it to 1 / 2) Fraction

  • numerator : int
  • denominator : int
  • Fraction (n: int, d: int)
  • setNumerator (n: int) : void
  • getNumerator ( ) : int
  • setDenominator (d: int) : void
  • getDenominator ( ) : int
  • setFraction (n: int, d:int) : void
  • getAsDouble ( ) : double
  • isProperFraction( ) : boolean
  • toString ( ) : String
  • add (f2: Fraction): Fraction
  • subtract (f2: Fraction): Fraction
  • multiply (f2: Fraction): Fraction
  • divide (f2: Fraction): Fraction

Sample Output

What to turn in

  1. Fraction.java and FractionDemo.java program printouts
  2. Be sure your files are saved in your CS110 account on the Computer Science network: U:..\Program4\ folder on the computer science network. (Note: it is ok to make subfolders to separate labs from programs)
  3. The assignment is due at the start of Lab5.

Grading Criteria

50 points maximum  26 points - Output Correctness o The program reads in the initial values for the two fractions from the user (5) o The program produces the correct result for addition, subtraction, multiplication, and division (5 points each) o The Fraction toString( ) method correctly displays the type of fraction (proper or improper) (1 point)  9 points - Class Definition o The Fraction class is defined correctly with  attributes (fields) declared as private (2)