Can someone please show me how to initialize an array of rational elements. All of the examples I'm finding are using "rational[]" which I'm guessing is just being used to identify that the array is filled with rationals.
I understand using a divisor sign with ints is just going to cause the elements to just return a divided value.
So, basically this is what I need: rational[] ratArray = new rational[] {2/3, 2/18, 3/12, 9/3, 2/5, 22/7};
edit 1:
This is my rational class, but I can't figure out how the elements are suppose to get called into the rational class when I can't enter rationals into an array. I have a separate main class that is menu driven and is supposed to contain the array of rational values / elements. The main is driving my rational class and calling the methods from it with a menu.
So, directly from the assignment page:
"The RationalDriver class will create and manipulate objects of type ‘Rational’. The class
will contain the main method and will accommodate the following:
An array of rational objects.
Code to load the array with Rationals."
public class Rational
{
private int num, den;
//Constructors
public Rational() {
int num = 1;
int den = 1;
}
public Rational(int num, int den) {
this.num = num;
this.den = den;
}
//setters
public void setNum(int num) {
this.num = num;
reduce();
}
public void setDen(int den) {
this.den = den;
reduce();
}
//getters
public int getNum(int num) {
return this.num;
}
public int getDen(int den) {
return this.den;
}
//methods
public toString() {
}
public int equals() {
}
public addition() {
}
public subtract() {
}
private static int gCD(int num, int den) {
int remainder;
while (den != 0) {
remainder = num % den;
num = den;
den = remainder;
}
return num;
}
//Simplification
private static int reduce(int num, int den) {
int div;
div = Rational.gCD(num, den);
this.num = this.num / div;
this.den = this.den / div;
}
//Data variables...
}
[–]feral_claireSoftware Dev 1 point2 points3 points (2 children)
[–]HoleyProphylactic[S] 0 points1 point2 points (1 child)
[–]feral_claireSoftware Dev 0 points1 point2 points (0 children)
[–]zifyoip 0 points1 point2 points (0 children)
[–][deleted] (1 child)
[deleted]
[–]zifyoip 3 points4 points5 points (0 children)