Help in JAVA:
This program should input numerator and denominator from a file, create a Fraction object
(reducing the fraction if necessary) and then save the fraction to an ArrayList. This list will then
be sorted, and output.
Make one method to input, create, and add to the ArrayList. Another method call to sort. And the
third method to output the contents of the (sorted) ArrayList.
The input file will consist of an (unknown) quantity of ints representing numerator denominator
pairs, which may be negative or zero.
You will need to think about your constructor - you will find it easier if you follow these rules
1. If both numerator and denominator are negative make them both positive
2. if the numerator is positive but the denominator is negative switch both so that the numerator
is negative and the denominator positive
3. any other case leave as is.
what I have so far:
public static class Fraction {
private int numerator;
private int denominator;
public Fraction() {
numerator = 0;
denominator = 1;
}
public Fraction(int n, int d) {
int g = gcd(n, d);
numerator = n/g;
denominator = d/g;
}
public int getNumerator() {
return numerator;
}
public void setNumerator(int n) {
int d = denominator;
int g = gcd(n, d);
numerator = n / g;
denominator= d/g;
}
public int getDenominator() {
return denominator;
}
public void setDenominator(int d) {
int n = numerator;
int g = gcd(n, d);
denominator = d / g;
numerator= n/g;
}
public Fraction add(Fraction g) {
int a = this.numerator;
int b = this.denominator;
int c = g.numerator;
int d = g.denominator;
Fraction v = new Fraction(a * d + b * c, b * d);
return v;
}
public Fraction subtract(Fraction g) {
int a = this.numerator;
int b = this.denominator;
int c = g.numerator;
int d = g.denominator;
Fraction v = new Fraction(a * d - b * c, b * d);
return v;
}
public Fraction multiply(Fraction g) {
int a = this.numerator;
int b = this.denominator;
int c = g.numerator;
int d = g.denominator;
Fraction v = new Fraction(a * c, b * d);
return v;
}
public Fraction divide(Fraction g) {
int a = this.numerator;
int b = this.denominator;
int c = g.numerator;
int d = g.denominator;
Fraction v = new Fraction(a * d, b * c);
return v;
}
public String toString() {
return numerator + \"/\" + denominator;
}
private int gcd(int int1, int int2) {
int i = 0;
int smallest=0;
if (int2>0){
if (int1 < int2) {
smallest=int1;
}
else{
smallest= int2;
}
for (i = smallest; i > 0; i--) {
if ((int1 % i == 0) && (int2 % i == 0)) {
break;
}
}
}
return i;
}
public int input(){
File inFile = new File(\"h7.txt\");
Scanner fileInput = null;
try {
fileInput = new Scanner(inFile);
} catch (FileNotFoundException ex) {
}
fileInput.nextInt();
}
public int sort(int input){
Collections.sort();
}
public int output(){
System.out.println();
}
}
public static void main(String[] args) {
}
}
Solution
Fraction class is used to represents fractions. It is always used to reduced to lowest terms.If
fraction is negative then the numerator will always be negative and all op.
Help in JAVAThis program should input numerator and denominator f.pdf
1. Help in JAVA:
This program should input numerator and denominator from a file, create a Fraction object
(reducing the fraction if necessary) and then save the fraction to an ArrayList. This list will then
be sorted, and output.
Make one method to input, create, and add to the ArrayList. Another method call to sort. And the
third method to output the contents of the (sorted) ArrayList.
The input file will consist of an (unknown) quantity of ints representing numerator denominator
pairs, which may be negative or zero.
You will need to think about your constructor - you will find it easier if you follow these rules
1. If both numerator and denominator are negative make them both positive
2. if the numerator is positive but the denominator is negative switch both so that the numerator
is negative and the denominator positive
3. any other case leave as is.
what I have so far:
public static class Fraction {
private int numerator;
private int denominator;
public Fraction() {
numerator = 0;
denominator = 1;
}
public Fraction(int n, int d) {
int g = gcd(n, d);
numerator = n/g;
denominator = d/g;
}
public int getNumerator() {
return numerator;
}
public void setNumerator(int n) {
int d = denominator;
int g = gcd(n, d);
numerator = n / g;
denominator= d/g;
}
2. public int getDenominator() {
return denominator;
}
public void setDenominator(int d) {
int n = numerator;
int g = gcd(n, d);
denominator = d / g;
numerator= n/g;
}
public Fraction add(Fraction g) {
int a = this.numerator;
int b = this.denominator;
int c = g.numerator;
int d = g.denominator;
Fraction v = new Fraction(a * d + b * c, b * d);
return v;
}
public Fraction subtract(Fraction g) {
int a = this.numerator;
int b = this.denominator;
int c = g.numerator;
int d = g.denominator;
Fraction v = new Fraction(a * d - b * c, b * d);
return v;
}
public Fraction multiply(Fraction g) {
int a = this.numerator;
int b = this.denominator;
int c = g.numerator;
int d = g.denominator;
Fraction v = new Fraction(a * c, b * d);
return v;
}
public Fraction divide(Fraction g) {
int a = this.numerator;
int b = this.denominator;
3. int c = g.numerator;
int d = g.denominator;
Fraction v = new Fraction(a * d, b * c);
return v;
}
public String toString() {
return numerator + "/" + denominator;
}
private int gcd(int int1, int int2) {
int i = 0;
int smallest=0;
if (int2>0){
if (int1 < int2) {
smallest=int1;
}
else{
smallest= int2;
}
for (i = smallest; i > 0; i--) {
if ((int1 % i == 0) && (int2 % i == 0)) {
break;
}
}
}
return i;
}
public int input(){
File inFile = new File("h7.txt");
Scanner fileInput = null;
try {
fileInput = new Scanner(inFile);
} catch (FileNotFoundException ex) {
}
fileInput.nextInt();
4. }
public int sort(int input){
Collections.sort();
}
public int output(){
System.out.println();
}
}
public static void main(String[] args) {
}
}
Solution
Fraction class is used to represents fractions. It is always used to reduced to lowest terms.If
fraction is negative then the numerator will always be negative and all operators leave results
which is to be stored in lowest terms.
In this Class is not complete, only addition operation is implemented.
Here is the program with some modification:
import java.util.*;
import java.lang.*;
import java.io.*;
public class Fraction {
private int numerator; // Fraction numerator
private int denominator; // Fraction denominator
/*-----------------------------------------------------------------
* constructor
* Takes no parameters, initializes the object to 0/1
*/
public Fraction() {
numerator = 0;
denominator = 1;
5. }
/*-----------------------------------------------------------------
* constructor
* Takes parameter, the numerator, initializes denominator to 1
* so object is numerator/1
*/
public Fraction(int num) {
numerator = num;
denominator = 1;
}
/*-----------------------------------------------------------------
* constructor
* If fraction is negative, put negative number in numerator
*/
public Fraction(int num, int denom) {
numerator = (denom < 0 ? -num : num);
if (denom == 0) {
denominator = 1;
}
denominator = (denom < 0 ? -denom : denom);
reduce();
}
/*-----------------------------------------------------------------
* setNumerator
* numerator is set to be the given parameter
*/
public void setNumerator(int num) {
numerator = num;
reduce();
}
/*-----------------------------------------------------------------
* getNumerator
* return numerator
*/
public int getNumerator() {
return numerator;
6. }
/*-----------------------------------------------------------------
* setDenominator
* denominator is set to be the given parameter (zero is ignored),
* if denominator is negative, numerator is adjusted
*/
public void setDenominator(int denom) {
if (denom > 0) {
denominator = denom;
reduce();
}
else if (denom < 0) {
numerator = -numerator;
denominator = -denom;
reduce();
}
}
/*-----------------------------------------------------------------
* getDenominator
* return denominator
*/
public int getDenominator() {
return denominator;
}
/*-----------------------------------------------------------------
* addTo
* add the parameter Fraction to the current object Fraction
*/
public Fraction addTo(Fraction rhs) {
Fraction sum = new Fraction();
sum.denominator = denominator * rhs.denominator;
sum.numerator = numerator * rhs.denominator
+ denominator * rhs.numerator;
sum.reduce();
return sum;
}
7. /*-----------------------------------------------------------------
* toString
* convert the Fraction to a String object, e.g., 2/3
*/
public String toString() {
return numerator + "/" + denominator;
}
/*-----------------------------------------------------------------
* equals
* compare the parameter Fraction to the current object Fraction
*/
public boolean equals(Fraction rhs) {
return (numerator == rhs.numerator) && (denominator == rhs.denominator);
}
/*-----------------------------------------------------------------
* reduce
* reduce Fraction to lowest terms by finding largest common denominator
* and dividing it out
*/
private void reduce() {
// find the larger of the numerator and denominator
int n = numerator, d = denominator, largest;
if (numerator < 0) {
n = -numerator;
}
if (n > d) {
largest = n;
}
else {
largest = d;
}
// find the largest number that divide the numerator and
// denominator evenly
int gcd = 0;
for (int i = largest; i >= 2; i--) {
if (numerator % i == 0 && denominator % i == 0) {
8. gcd = i;
break;
}
}
// divide the largest common denominator out of numerator, denominator
if (gcd != 0) {
numerator /= gcd;
denominator /= gcd;
}
}
}