class - Rational Number Java Project -
for project have create rational number class has 2 parts int numerator , int denominator. had add together 2 contractions negative denominator has moved numerator. added getters , setters , tostring(). info should print numerator/denominator. had code fellow member methods addition, subtraction, multiplications, , partition , negate(?) not sure lastly part means.
i have class done eclipse giving me error add together , subtract method around part typed "temp". please allow me know if have wrong or if missing something.
public class rational { private int numerator; private int denominator; public rational() { numerator = 0; denominator = 1; } public rational(int n, int d, int num, int denom) { if (d < 0) { num = -n; denom = d; } else if (d == 0) { num = n; denom = 1; } else { num = n; denom = 0; } } public int getnumerator() { homecoming numerator; } public int getdenominator() { homecoming denominator; } public void setnumerator(int n) { numerator = n; } public void setdenominator(int n, int d, int num, int denom) { denominator = d; if (d < 0) { num = -n; denom = d; } else if (d == 0) { num = n; denom = 1; } else { num = n; denom = 0; } } public string tostring() { homecoming numerator + "/" + denominator; } public boolean equals (rational other) { if(numerator * other.denominator == denominator * other.numerator) homecoming true; else homecoming false; } public boolean notequals(rational other) { if (numerator * other.denominator != denominator * other.numerator) homecoming true; else homecoming false; } //subtract method public rational subtract(rational other) { rational temp; temp.numerator = numerator * other.denominator - denominator * other.numerator; temp.denominator = denominator * other.denominator; homecoming temp; } //add method public rational add(rational other) { rational temp; temp.numerator = numerator * other.denominator + denominator * other.numerator; temp.denominator = denominator * other.denominator; homecoming temp; } public boolean lessthan(rational other) { return(numerator * other.denominator < denominator * other.numerator); } public boolean greterthan(rational other) { return(numerator * other.denominator > denominator * other.numerator); } public boolean lessthanequalto(rational other) { return(numerator * other.denominator <= denominator * other.numerator); } public boolean greaterthanequal(rational other) { return(numerator * other.denominator >= denominator * other.numerator); } }
i struggling main test each method. here have far:
public class project4 { public static void main(string[] args) { rational = new rational(); rational b = new rational(); rational c; c = a.add(b); } }
//here goes commented code: public class rational { private int numerator; // can declare private variables here right private int denominator; // constructor creates new object of class rational. objects instances of class, right? public rational() { numerator = 0; denominator = 1; //private int numerator = 0; //private int denominator = 1; // it, need them when not giving arguments, improve stuff in constructors. improve seek create declarations in constructor , not in begining of class, if possible. } // here constructor overriden, can create instance contains 4 arguments. (n,d,num,denom) good. public rational(int n, int d, int num, int denom) { // repeat in constructor. // public void setdenominator(int n, int d, int num, int denom) same thing right? // don't remember now, think on instantiation can't utilize methods, if don't add together word static. // public static void setdenominator(int n, int d, int num, int denom) create able phone call in constructor! if (d < 0) { num = -n; denom = d; } else if (d == 0) { num = n; denom = 1; } else { num = n; denom = 0; } } public int getnumerator() { homecoming numerator; } public int getdenominator() { homecoming denominator; } public void setnumerator(int n) { numerator = n; } public void setdenominator(int n, int d, int num, int denom) // prepare it. add together static { denominator = d; if (d < 0) { num = -n; denom = d; } else if (d == 0) { num = n; denom = 1; } else { num = n; denom = 0; } } public string tostring() { homecoming numerator + "/" + denominator; } public boolean equals (rational other) { if(numerator * other.denominator == denominator * other.numerator) homecoming true; else homecoming false; } public boolean notequals(rational other) { if (numerator * other.denominator != denominator * other.numerator) homecoming true; else homecoming false; } //subtract method public rational subtract(rational other) { rational temp; temp.numerator = numerator * other.denominator - denominator * other.numerator; temp.denominator = denominator * other.denominator; homecoming temp; } // returns temp object of type rational!!! remember that. //add method public rational add(rational other) { rational temp; temp.numerator = numerator * other.denominator + denominator * other.numerator; temp.denominator = denominator * other.denominator; homecoming temp; }// returns temp object of type rational!!! remember that. public boolean lessthan(rational other) { return(numerator * other.denominator < denominator * other.numerator); }// type method return? here gap must fill. if larn how handel methods return, fine. seek stuff out. public boolean greterthan(rational other) { return(numerator * other.denominator > denominator * other.numerator); } public boolean lessthanequalto(rational other) { return(numerator * other.denominator <= denominator * other.numerator); } public boolean greaterthanequal(rational other) { return(numerator * other.denominator >= denominator * other.numerator); } }
java class main rational
No comments:
Post a Comment