Created
February 3, 2014 12:07
-
-
Save DavidCulpepper/8782793 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
public class Fraction { | |
private int numerator; | |
private int denominator; | |
public Fraction() { | |
this.numerator = 1; | |
this.denominator = 1; | |
} | |
public Fraction(int numerator, int denominator) { | |
if (denominator == 0) { | |
throw new IllegalArgumentException("Denominator must be greater than zero"); | |
} | |
this.numerator = numerator; | |
this.denominator = denominator; | |
} | |
public int getNumerator() { | |
return numerator; | |
} | |
public void setNumerator(int numerator) { | |
this.numerator = numerator; | |
} | |
public int getDenominator() { | |
return denominator; | |
} | |
public void setDenominator(int denominator) { | |
if (denominator == 0) { | |
throw new IllegalArgumentException("Denominator must be greater than zero"); | |
} | |
this.denominator = denominator; | |
} | |
public double getReal() { | |
return (double) numerator / denominator; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment