Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Fraction Programming Assignment Fraction Programming Assignment CSS 162A Winter 2012 For this assignment you are asked to develop a Java class that implements a basic fraction object, i.e., a ratio of two integers. The fraction value should be represented internally by two private int fields, one for the numerator, the other for the denominator. These values should be maintained so that they have no factors in common (that way the fraction is in lowest terms). The denominator value is always positive; the sign is carried by the numerator. If the denominator is one, the fraction is really just a whole number. Implement the following constructors: public Fraction() public Fraction( int value ) public Fraction( int numerator, int denominator ) public Fraction( String src ) public Fraction( Fraction src ) The default constructor (Fraction()) should set the fraction value to zero. The string constructor should parse the string argument assuming it is either a (decimal) integer, or a decimal integer followed by "/" followed by a nonzero (decimal) integer. You can assume there are no signs, and there is no space between the integers and the "/" character. Implement the following accessors public int getNumerator() public int getDenominator() public int getSign() public double value() and the corresponding mutators public void setNumerator( int newNumerator ) public void setDenominator( int newDenominator ) as well as these tests: public boolean isZero() public boolean isUnity() public boolean equals( Fraction q ) For arithmetic operations, implement the following methods, each of which returns a new Fraction instance (without modifying either this or the argument). public Fraction sum( Fraction augend ) public Fraction difference( Fraction subtrahend ) public Fraction product( Fraction multiplier ) public Fraction quotient( Fraction divisor ) public Fraction inverse() public Fraction negation() Also implement the following "in-place" operations that actually change the this instance: public void add( Fraction augend ) public void subtract( Fraction subtrahend ) public void multiply( Fraction multiplier ) public void divide( Fraction divisor ) public void negate() public void invert() Finally, include a toString method. An attempt to construct a fraction having a zero denominator (which is also an issue in the setDenominator() method) should cause your program to stop executing. Driver Code You are also asked to develop driver software that implements a basic fraction calculator. For this, a text fraction is a string consisting of an integer, followed by a "/" character, followed by another integer. The "/" and second integer are optional; if is omitted, the fraction is treated as a whole number (with denominator 1). Here are some example text fractions: 1/2 500/1000 3/1 3 0/2 0 Here 1/2 and 500/1000 both represent one half, and the remaining examples are actually whole numbers. The fraction calculator reads text fractions separated by a "+" or "-" operator; a fraction expression terminates with a ";" character. The fraction values are most naturally matched by way of regular expressions (implemented by the Pattern class in Java), but they are clumsy to use with the Java Scanner class. So you can make some assumptions, to keep it simple. You can assume the input is valid. There are no spaces around the "/" character in a fraction. There are always spaces around the "+" and "-" operators. There is a space before the final ";" which terminates the expression Your driver program can exit after it computes the first expression result. Here is some example input, and the expected proper output. Input Output 4/8 ;   1/2 1/2 + 1/4 ; 3/4 13/73 - 1 + 1/3 + 4 ; 769/219 Suggestions and Hints The implementation of the fraction calculator developed in class is available here. You are welcome to use it, but keep in mind it needs to be modified to use your fraction operations. As it stands, fractions are converted to floating-point and that's not what you want for this assignment. Also, the code does not check for the whole number case, so you will have to do that as well. All fractions should be represented in lowest terms. You will need to implement a GCD (greatest common divisor) method. Use the Euclidean algorithm for this. Use the lowest common denominator for your addition operations (the product of the denomininators works as a common denominator, but it might be much larger than needed and therefore cause unnecessary overflow). Also, you will get a higher correctness score if your methods minimize overflow. Be sure to document your Java source code appropriately. Javadoc comments are not required, for this assignment, but you are encouraged to get in the habit of using Javadoc commenting conventions. Submission When you are done, submit your source code through the Catalyst dropbox for this assigment. Please submit only your Fraction.java and FractionDriver.java files (please use these exact names). (You need not submit your FractionDriver.java file.)