vendredi 27 mars 2015

7 fractions sorted in an array, added together then reduced. in java



Here is my problem, I need to create a program that runs the following instructions.


Create java class named Fraction. This class is used to represent a ratio of two integers. Include mutator methods that allow the user to set the numerator and the denominator. Also include a method to display the fraction on the screen as a ration (e.g. 5/9). This method does not need to reduce the fraction to lowest terms.


The fraction class should contain the following:


• Private instance variables to store the numerator, denominator, and the ratio_value.


• Constructor(s) that set all of the instance variables.


• Public methods to get and set the instance variables.


• A public method named reduce( ) that returns lowest terms of a fraction.


• A public method named toString( ) that returns a String containing the fraction as a ratio.


• A private method name gcd() that return the greatest common divisor of two integers.


Create a test program that allows the user to create array of 7 fractions. Then the program will sort the fraction in ascending order. The highest and the lowest fractions are thrown away and the remaining fractions are added together. The program should display all the fractions and their sum. The sum should be reduced to lowest terms and displayed on the screen. For example, if the sum is 20/60, the program should display 1/3.


Write a sort method in the test program to sort the array of fractions and calculate the sum.


Assume you have the following 7 fractions: 6/7, 2/4, 3/4, 3/18, 1/8, 10/20, 2/6, then an example of the output after throwing away the lowest and the largest fractions, will be:


3 / 18 + 2 / 6 + 2 / 4 + 10 / 20 + 3 / 4 = 9 / 4


I completely lost on how to solve this and stuck where I am at. Below is a copy of my class and .main file. I have some different things that I have tried commented out with '//' so sorry for the long codes.



package homework_5;

import java.io.*;
import java.util.*;
import java.util.regex.Pattern;

public class Arrays_hw5 {

private static final Scanner keyb = null;
public static void main(String[] args) {

Fraction [] fr = new Fraction[7];
// Fraction fr1 = new Fraction();
// Fraction fr2 = new Fraction();
// Fraction fr3 = new Fraction();
// Fraction fr4 = new Fraction();
// Fraction fr5 = new Fraction();
// Fraction fr6 = new Fraction();
// Fraction fr7 = new Fraction();
String reduce = "";
int numerator = 0, denominator = 0;

Scanner keyb = null;
FileInputStream fis = null;
// Scanner keyb = new Scanner(System.in);

try {
fis = new FileInputStream(new File("fractions"));
keyb = new Scanner(fis);
} catch (FileNotFoundException e) {
e.printStackTrace();
}


// keyb.useDelimiter("/");
// keyb.useDelimiter(" ");
// fr1.setNumerator(keyb.nextInt());
// fr1.setDenominator(keyb.nextInt());
// System.out.println(fr1);
// fr2.setNumerator(keyb.nextInt());
// fr2.setDenominator(keyb.nextInt());
// System.out.println(fr2);
// fr3.setNumerator(keyb.nextInt());
// fr3.setDenominator(keyb.nextInt());
// System.out.println(fr3);
// fr4.setNumerator(keyb.nextInt());
// fr4.setDenominator(keyb.nextInt());
// System.out.println(fr4);
// fr5.setNumerator(keyb.nextInt());
// fr5.setDenominator(keyb.nextInt());
// System.out.println(fr5);
// fr6.setNumerator(keyb.nextInt());
// fr6.setDenominator(keyb.nextInt());
// System.out.println(fr6);
// fr7.setNumerator(keyb.nextInt());
// fr7.setDenominator(keyb.nextInt());
// System.out.println(fr7);



for (int i = 0; i < fr.length; i++)
{
//System.out.println("Enter numerator then denominator, hit enter after each entry: ");
fr[i] = new Fraction(i, i);
fr[i].setNumerator(keyb.nextInt());
fr[i].setDenominator(keyb.nextInt());
System.out.print(fr[i] + " ");
//System.out.print(fr[i].decimal(numerator, denominator));
}

// Arrays.sort(fr);

// for (int i = 0; i < fr.length; i++)
// System.out.println(fr[i].decimal(numerator, denominator));

// Fraction [] frac = {fr[1], fr[2], fr[3], fr[4], fr[5], fr[6], fr[7]};
// Arrays.sort(frac);
// for (int i = 0; i < 7; i++)
// System.out.println(frac[i]);

}
public static void selectionSort(int[]arr) //sorting array
{
int smallest = 0;
for (int outer = 0; outer < arr.length - 1; outer++)
{
smallest = outer;
for(int inner = outer + 1; inner < arr.length; inner++)
{
if (arr[inner] < arr[smallest])
smallest = inner;
}
int v = arr[outer];
arr[outer] = arr[smallest];
arr[smallest] = v;
}


}

}


here is my Fraction class



package homework_5;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Fraction {
public int numerator = 1;
public int denominator = 1;
public int gcd;

public Fraction() {
super();
}
public Fraction(int n, int d) {
numerator = n;
denominator = d;
}

public int getNumerator() {
return numerator;
}
public void setNumerator(int numerator) {
this.numerator = numerator;
}
public int getDenominator() {
return denominator;
}
public void setDenominator(int denominator) {
this.denominator = denominator;
}


private static int gcd(int numerator, int denominator) {
return denominator == 0 ? numerator : gcd(denominator, numerator % denominator);
}
public double decimal(double numerator, double denominator) {
return numerator / denominator;
}


public static String reduce(int numerator, int denominator) {
int gcd = gcd(numerator, denominator);
return (numerator / gcd) + "/" + (denominator / gcd);
}


@Override
public String toString() {
return numerator + "/" + denominator;
}


}


So far all I am able to do is print out the 7 fractions from a .txt file formatted like:6 7 2 4 3 4 3 18 1 8 10 20 2 6 but I want to have a text file formatted like:6/7 2/4 3/4 3/18 1/8 10/20 2/6 I can not figure out how to get a deliminator to read the '/' and the ' '.


Thank you for any help.




Aucun commentaire:

Enregistrer un commentaire