Skip to content

Instantly share code, notes, and snippets.

/Assignment Secret

Created March 2, 2014 06:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/6604f427cc9d17391478 to your computer and use it in GitHub Desktop.
Save anonymous/6604f427cc9d17391478 to your computer and use it in GitHub Desktop.
package assignment1;
import java.util.*;
class Assignment1_test
{
static private Scanner scanner;
private static Fraction readFraction() {
System.out.print( "\nTry to read a fraction x/y, please enter x y : " );
int numerator = scanner.nextInt();
int denominator = scanner.nextInt();
Fraction f = new Fraction(numerator, denominator);
System.out.println( "\t\tRead OK:"+f);
return f;
}
private static void printOperations() {
System.out.println("==============================================");
System.out.println("\nOperations:");
System.out.println(" 0) exit \t1) add \t\t2) subtract \t3) multiply \t4) divide");
System.out.println(" 5) compareTo \t6) equals \t7) recipocal \t8) switchSign \t9) getSign ");
System.out.println(" 10) getNumerator 11) getDenominator 12) setFraction (x/y) ");
System.out.print( "\nEnter an operation number: ");
}
public static void main( String args[] )
{
scanner = new Scanner( System.in ); // scanner for input
boolean continueLoop = true; // determines if more input is needed
Fraction n1=null;
Fraction n2=null;
int op,x,y;
do
{
try // read two numbers and calculate quotient
{
printOperations();
op= scanner.nextInt();
if (op == 0) {
break;
} else if ((op >0) && (op <7)) {
n1 = readFraction();
n2 = readFraction();
} else if ((op > 6) && (op < 12)) {
n1 = readFraction();
} else if (op == 12) {
n1 = new Fraction();
} else if (op == 13) {
n1 = new Fraction();
} else {
System.out.print( "\nInvalid input... try again\n" );
continue;
}
System.out.println("\nTests:\n");
switch (op) {
case 1:
System.out.println("\t" + n1 + " + " + n1 + " = " + n1.add(n1));
System.out.println("\t" + n2 + " + " + n2 + " = " + n2.add(n2));
System.out.println("\t" + n1 + " + " + n2 + " = " + n1.add(n2));
System.out.println("\t" + n2 + " + " + n1 + " = " + n2.add(n1));
break;
case 2:
System.out.println("\t" + n1 + " - " + n1 + " = " + n1.subtract(n1));
System.out.println("\t" + n2 + " - " + n2 + " = " + n2.subtract(n2));
System.out.println("\t" + n1 + " - " + n2 + " = " + n1.subtract(n2));
System.out.println("\t" + n2 + " - " + n1 + " = " + n2.subtract(n1));
break;
case 3:
System.out.println("\t" + n1 + " * " + n1 + " = " + n1.multiply(n1));
System.out.println("\t" + n2 + " * " + n2 + " = " + n2.multiply(n2));
System.out.println("\t" + n1 + " * " + n2 + " = " + n1.multiply(n2));
System.out.println("\t" + n2 + " * " + n1 + " = " + n2.multiply(n1));
break;
case 4:
System.out.println("\t" + n1 + " / " + n1 + " = " + n1.divide(n1));
System.out.println("\t" + n2 + " / " + n2 + " = " + n2.divide(n2));
System.out.println("\t" + n1 + " / " + n2 + " = " + n1.divide(n2));
System.out.println("\t" + n2 + " / " + n1 + " = " + n2.divide(n1));
break;
case 5:
System.out.println("\t" + n1 + " ct " + n1 + " = " + n1.compareTo(n1));
System.out.println("\t" + n2 + " ct " + n2 + " = " + n2.compareTo(n2));
System.out.println("\t" + n1 + " ct " + n2 + " = " + n1.compareTo(n2));
System.out.println("\t" + n2 + " ct " + n1 + " = " + n2.compareTo(n1));
break;
case 6:
System.out.println("\t" + n1 + " eq "+ n1 + " = " + n1.equals(n1));
System.out.println("\t" + n2 + " eq "+ n2 + " = " + n2.equals(n2));
System.out.println("\t" + n1 + " eq "+ n2 + " = " + n1.equals(n2));
System.out.println("\t" + n2 + " eq "+ n1 + " = " + n2.equals(n1));
break;
case 7:
System.out.println("\t" + n1 + " getReciprocal= " + n1.getReciprocal());
break;
case 8:
System.out.print("\t" + n1 );
n1.switchSign();
System.out.println(" switchSign - = " + n1);
System.out.print("\t" + n1 );
n1.switchSign();
System.out.println(" switchSign + = " + n1);
break;
case 9:
System.out.println("\t" + n1 + " getSign = " + n1.getSign());
break;
case 10:
System.out.println("\t" + n1 + " getNumerator = " + n1.getNumerator());
break;
case 11:
System.out.println("\t" + n1 + " getDenominator = " + n1.getDenominator());
break;
case 12:
System.out.print( "read a fraction x/y, please enter x y : " );
x = scanner.nextInt();
y = scanner.nextInt();
System.out.print("\t" + n1 + " setFraction = ");
n1.setFraction(x,y);
System.out.println(n1);
break;
}
} // end try
catch ( ArithmeticException arithmeticException )
{
System.err.printf( "\nException: %s\n", arithmeticException );
} // end catch
} while ( continueLoop ); // end do...while
} // end main
} // end class DivideByZeroWithExceptionHandling
package assignment1;
/*************************************************************************************
*
* This class represents a fraction whose numerator and denominator are integers.
*
* Requirements:
* Implement interfaces: FractionInterface and Comparable (i.e. compareTo())
* Implement methods equals() and toString() from class Object
* Should work for both positive and negative fractions
* Must always reduce fraction to lowest term
* Must throw excpetion in case of errors
* Must not add new or modify existing data fields
* Must not add new public methods
* May add private methods
* For number such as 10/-3, it is same as -10/3 (see hints 2. below)
*
* Hints:
*
* 1. To reduce a fraction such as 4/8 to lowest terms, you need to divide both
* the numerator and the denominator by their greatest common denominator.
* The greatest common denominator of 4 and 8 is 4, so when you divide
* the numerator and denominator of 4/8 by 4, you get the fraction 1/2.
* The recursive algorithm which finds the greatest common denominator of
* two positive integers is implemnted (see code)
*
* 2. It will be easier to determine the correct sign of a fraction if you force
* the fraction's denominator to be positive. However, your implementation must handle
* negative denominators that the client might provide.
*
* 3. You need to downcast reference parameter FractionInterface to Fraction before
* you can use it as Fraction. See add, subtract, multiply and divide methods
*
************************************************************************************/
public class Fraction implements FractionInterface, Comparable<Fraction>
{
private int numerator;
private int denominator;
public Fraction()
{
setFraction(0, 1);
}
public Fraction(int initialNumerator, int initialDenominator)
{
setFraction(initialNumerator, initialDenominator);
}
public void setFraction(int newNumerator, int newDenominator)
{
if (newDenominator == 0) throw new ArithmeticException("denominator == 0!");
else{
numerator=newNumerator;
denominator=newDenominator;
}
}
public int getNumerator()
{
return numerator;
}
public int getDenominator()
{
return denominator;
}
public char getSign()
{
if(numerator*denominator < 0){return '-';}
else{
return '+';}
}
public void switchSign()
{
if (denominator < 0){
denominator=-denominator;
numerator=-numerator;}
}
public FractionInterface add(FractionInterface operand)
{
int n1=((this.numerator*operand.getDenominator())+(this.denominator*operand.getNumerator()));
int d1=(this.denominator*operand.getDenominator());
FractionInterface f1=new Fraction(n1,d1);
return f1;
} // end add
public FractionInterface subtract(FractionInterface operand)
{
int n1=((this.numerator*operand.getDenominator())-(this.denominator*operand.getNumerator()));
int d1=(this.denominator*operand.getDenominator());
FractionInterface f1=new Fraction(n1,d1);
return f1;
}
public FractionInterface multiply(FractionInterface multiplier)
{
int n1=(this.numerator*multiplier.getNumerator());
int d1=(this.denominator*multiplier.getDenominator());
FractionInterface f1=new Fraction(n1,d1);
return f1;
}
public FractionInterface divide(FractionInterface divisor)
{
if(divisor.getNumerator()==0)throw new ArithmeticException("denominator == 0!");
else{
int d1=(divisor.getNumerator()*this.denominator);
int n1=(divisor.getNumerator()*this.denominator);
FractionInterface f1=new Fraction(n1,d1);
return f1;}
}
public FractionInterface getReciprocal()
{
// return ArithmeticException if divisor is 0
return new Fraction(this.denominator, this.numerator);
/* int d1 = this.denominator;
int n1 = this.numerator;;
FractionInterface f1 = new Fraction(n1, d1);
f1.numerator = d1;
f1.denominator = n1;
return f1; */
}
public boolean equals(Object other) {
if (other.equals(this.numerator) && other.equals(this.denominator))
return true;
else
return false;
}
public int compareTo(Fraction other) {
long nod = ((long) numerator) * other.denominator;
long don = ((long) denominator) * other.numerator;
return (nod < don) ? -1 : ((nod > don) ? +1 : 0);
}
public String toString()
{
return numerator + "/" + denominator;
}
/** Task: Reduces a fraction to lowest terms. */
//-----------------------------------------------------------------
// private methods start here
//-----------------------------------------------------------------
private void reduceToLowestTerms()
{
// implement this method!
//
// Outline:
// compute GCD of numerator & denominator
// greatestCommonDivisor works for + numbers.
// So, you should eliminate - sign
// then reduce numbers : numerator/GCD and denominator/GCD
this.switchSign();
int i1 = greatestCommonDivisor(this.numerator, this.denominator);
this.numerator = this.numerator / i1;
this.denominator = this.denominator / i1;
} // end reduceToLowestTerms
/** Task: Computes the greatest common divisor of two integers.
* @param integerOne an integer
* @param integerTwo another integer
* @return the greatest common divisor of the two integers */
private int greatestCommonDivisor(int integerOne, int integerTwo)
{
int result;
if (integerOne % integerTwo == 0)
result = integerTwo;
else
result = greatestCommonDivisor(integerTwo, integerOne % integerTwo);
return result;
} // end greatestCommonDivisor
//-----------------------------------------------------------------
// Simple test driver is provided here
public static void main(String[] args)
{
FractionInterface firstOperand = null;
FractionInterface secondOperand = null;
FractionInterface result = null;
Fraction nineSixteenths = new Fraction(9, 16); // 9/16
Fraction oneFourth = new Fraction(1, 4); // 1/4
// 7/8 + 9/16
firstOperand = new Fraction(7, 8);
result = firstOperand.add(nineSixteenths);
System.out.println("The sum of " + firstOperand + " and " +
nineSixteenths + " is \t\t" + result);
// 9/16 - 7/8
firstOperand = nineSixteenths;
secondOperand = new Fraction(7, 8);
result = firstOperand.subtract(secondOperand);
System.out.println("The difference of " + firstOperand +
" and " + secondOperand + " is \t" + result);
// 15/-2 * 1/4
firstOperand.setFraction(15, -2);
result = firstOperand.multiply(oneFourth);
System.out.println("The product of " + firstOperand +
" and " + oneFourth + " is \t" + result);
// (-21/2) / (3/7)
firstOperand.setFraction(-21, 2);
secondOperand.setFraction(3, 7);
result = firstOperand.divide(secondOperand);
System.out.println("The quotient of " + firstOperand +
" and " + secondOperand + " is \t" + result);
// -21/2 + 7/8
firstOperand.setFraction(-21, 2);
secondOperand.setFraction(7, 8);
result = firstOperand.add(secondOperand);
System.out.println("The sum of " + firstOperand +
" and " + secondOperand + " is \t\t" + result);
System.out.println();
// equality check
if (firstOperand.equals(firstOperand))
System.out.println("Idenominatortity of fractions OK");
else
System.out.println("ERROR in idenominatortity of fractions");
secondOperand.setFraction(-42, 4);
if (firstOperand.equals(secondOperand))
System.out.println("Equality of fractions OK");
else
System.out.println("ERROR in equality of fractions");
// comparison check
Fraction first = (Fraction)firstOperand;
Fraction second = (Fraction)secondOperand;
if (first.compareTo(second) == 0)
System.out.println("Fractions == operator OK");
else
System.out.println("ERROR in fractions == operator");
second.setFraction(7, 8);
if (first.compareTo(second) < 0)
System.out.println("Fractions < operator OK");
else
System.out.println("ERROR in fractions < operator");
if (second.compareTo(first) > 0)
System.out.println("Fractions > operator OK");
else
System.out.println("ERROR in fractions > operator");
System.out.println();
try {
Fraction a1 = new Fraction(1, 0);
}
catch ( ArithmeticException arithmeticException )
{
System.err.printf( "\nException: %s\n", arithmeticException );
} // end catch
try {
Fraction a2 = new Fraction();
Fraction a3 = new Fraction(1, 2);
a3.divide(a2);
}
catch ( ArithmeticException arithmeticException )
{
System.err.printf( "\nException: %s\n", arithmeticException );
} // end catch
} // end main
} // end Fraction
package assignment1;
/* This file specifies methods for FractionInterface */
/* Do not modify this file!! */
public interface FractionInterface
{
/** Task: Sets a fraction to a given value.
* @param newNumerator the integer numerator
* @param newDenominator the integer denominator
* @throws ArithmeticException if denominator=0 */
public void setFraction(int newNumerator, int newDenominator);
/** Task: Gets the fraction's numerator.
* @return the fraction's numerator */
public int getNumerator();
/** Task: Gets the fraction's denominator.
* @return the fraction's denominator */
public int getDenominator();
/** Task: Gets the fraction's sign.
* @return the fraction's sign '+' or '-' */
public char getSign();
/** Task: Switch the fraction's sign,
* i.e. (+ to -) OR (- to +) */
public void switchSign();
/** Task: Adds two fractions.
* @param operand a fraction that is the second operand of the addition
* @return the sum of the invoking fraction and the second operand */
public FractionInterface add(FractionInterface operand);
/** Task: Subtracts two fractions.
* @param operand a fraction that is the second operand of the subtraction
* @return the difference of the invoking fraction and the second operand */
public FractionInterface subtract(FractionInterface operand);
/** Task: Multiplies two fractions.
* @param operand a fraction that is the second operand of the multiplication
* @return the product of the invoking fraction and the second operand */
public FractionInterface multiply(FractionInterface multiplier);
/** Task: Divides two fractions.
* @param operand a fraction that is the second operand of the division
* @return the quotient of the invoking fraction and the second operand
* @throws ArithmeticException if divisor=0 */
public FractionInterface divide(FractionInterface divisor);
/** Task: Get's the fraction's reciprocal
* @return the reciprocal of the invoking fraction
* @throws ArithmeticException if the new number with denominator=0*/
public FractionInterface getReciprocal();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment