Skip to content

Instantly share code, notes, and snippets.

@Jordan-Cottle
Created October 18, 2019 19:42
Show Gist options
  • Save Jordan-Cottle/b8ee329fee3497d05d718cdead1b50da to your computer and use it in GitHub Desktop.
Save Jordan-Cottle/b8ee329fee3497d05d718cdead1b50da to your computer and use it in GitHub Desktop.
/**
* Write a description of class Fraction here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Fraction
{
private int numerator;
private int denominator;
public Fraction(int numerator, int denominator){
this.numerator = numerator;
this.denominator = denominator;
}
public Fraction simplify(){
return new Fraction(0,0);
}
public Fraction add(Fraction other){
return new Fraction(0,0);
}
public Fraction subtract(Fraction other){
return new Fraction(0,0);
}
public Fraction multiply(Fraction other){
return new Fraction(0,0);
}
public Fraction divide(Fraction other){
return new Fraction(0,0);
}
public boolean equals(Object o){
Fraction other = (Fraction) o;
return this.numerator == other.numerator
&& this.denominator == other.denominator;
}
public int compareTo(Fraction other){
return -1;
}
public String toString(){
return String.format("%d/%d", numerator, denominator);
}
}
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* The test class FractionTest.
*
* @author (your name)
* @version (a version number or a date)
*/
public class FractionTest
{
@Test
public void testSimplify(){
int [][] inputs = {
{8, 16},
{6, 15},
{8,12},
{14, 24},
{9, 21}
};
int [][] answers = {
{1, 2},
{2, 5},
{3, 4},
{7, 12},
{3 ,7}
};
for(int i = 0; i < inputs.length; i++){
int numerator = inputs[i][0];
int denominator = inputs[i][1];
Fraction a = new Fraction(numerator, denominator);
Fraction answer = new Fraction(answers[i][0], answers[i][1]);
assertEquals(answer, a.simplify());
}
}
@Test
public void testAdd(){
Fraction [][] inputs = {
{new Fraction(3,5), new Fraction(1, 5)},
{new Fraction(3,8), new Fraction(5, 12)},
{new Fraction(5,6), new Fraction(1, 5)},
{new Fraction(4,9), new Fraction(3, 4)}
};
Fraction [] answers = {
new Fraction(4, 5),
new Fraction(19, 24),
new Fraction(31, 30),
new Fraction(43, 36)
};
for(int i = 0; i < inputs.length; i++){
Fraction a = inputs[i][0];
Fraction b = inputs[i][1];
Fraction answer = answers[i];
assertEquals(answer, a.add(b));
}
}
@Test
public void testMultiply(){
Fraction [][] inputs = {
{new Fraction(3,4), new Fraction(2, 3)},
{new Fraction(1,5), new Fraction(3, 7)},
{new Fraction(2,5), new Fraction(3, 4)}
};
Fraction [] answers = {
new Fraction(1, 2),
new Fraction(3, 35),
new Fraction(3, 10)
};
for(int i = 0; i < inputs.length; i++){
Fraction a = inputs[i][0];
Fraction b = inputs[i][1];
Fraction answer = answers[i];
assertEquals(answer, a.multiply(b));
}
}
@Test
public void testDivide(){
Fraction [][] inputs = {
{new Fraction(4,5), new Fraction(1, 4)},
{new Fraction(4,9), new Fraction(15, 20)},
{new Fraction(3,8), new Fraction(5, 6)},
{new Fraction(12,5), new Fraction(20, 12)}
};
Fraction [] answers = {
new Fraction(16, 5),
new Fraction(16, 27),
new Fraction(9, 20),
new Fraction(36, 25)
};
for(int i = 0; i < inputs.length; i++){
Fraction a = inputs[i][0];
Fraction b = inputs[i][1];
Fraction answer = answers[i];
assertEquals(answer, a.divide(b));
}
}
@Test
public void testCompareTo(){
Fraction [][] inputs = {
{new Fraction(2,5), new Fraction(1, 3)},
{new Fraction(3,7), new Fraction(4, 9)},
{new Fraction(5,6), new Fraction(4, 7)},
{new Fraction(4,5), new Fraction(7, 8)}
};
int [] answers = {
1,
-1,
1,
-1
};
for(int i = 0; i < inputs.length; i++){
Fraction a = inputs[i][0];
Fraction b = inputs[i][1];
int answer = answers[i];
assertEquals(answer, a.compareTo(b));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment