Ca n you please convert this JAVA code i have here into C#. Please and thank you
ID: 3769952 • Letter: C
Question
Can you please convert this JAVA code i have here into C#. Please and thank you
Part 1 Fraction Main:
/**
* Fraction class used to create fraction objects,
* with a numerator and denominator (like 3/4)
*
* Includes no, one, and two argument constructors,
* equals and toString methods, and methods to
* reduce to lowest terms, add, subt, mult, div fraction
* get/set methods are optional
*/
public class Fraction {
private int num;
private int denom;
/**
* Fraction creates a new fraction
*
* @param n The numerator
* @param d The denominator
*/
public Fraction(int n, int d) {
num = n;
denom = d;
reduce(); // reduce within the constructor
} // end constructor Fraction(int n, int d)
/**
* Fraction creates a default fraction 1/1
*/
public Fraction() {
num = 1;
denom = 1;
} // end constructor Fraction()
/**
* Fraction creates a new fraction with a whole number
*
* @param n The numerator
* @param d The denominator
*/
public Fraction(int n) {
num = n;
denom = 1;
} // end constructor Fraction(int n)
/**
* returns the fraction as a String
* in the from num/dem (like 3/4)
*/
public String toString() {
return (num + "/" + denom);
} // end print
/**
* modifies the numerator and denominator so the fraction
* is reduced to lowest terms
*/
private void reduce() {
boolean negative = false; // assume not negative
if (num < 0) { // if negative, make positive before reducing
negative = true;
num = num * -1;
} // end if
int bound = num; // test if values 2 through num are factors
for (int i = 2; i <= bound; i++) {
// if remainder is zero for both num & den, then factor
if (num%i == 0 && denom%i == 0) {
num = num/i;
denom = denom/i;
// have to check if values is again a factor
//(EX: 80/120 =40/60=20/30=10/15)
i--;
} // end if
} // end for
// if negative, change back to negative after reducing
if (negative) num = num * -1;
} // end reduce
/**
* plus adds this fraction to the parameter and returns the result
*
* @param f The fraction to add to
* @return This fraction plus f
*/
public Fraction plus(Fraction f) {
int newNum = num*f.denom + f.num*denom;
int newDenom = denom*f.denom;
return new Fraction(newNum, newDenom); // create and reduce new fraction
} // end plus
/**
* minus takes this fraction minus the parameter and returns the result
*
* @param f The fraction to subtract
* @return This fraction minus f
*/
public Fraction minus(Fraction f) {
int newNum = num*f.denom - f.num*denom;
int newDenom = denom*f.denom;
return new Fraction(newNum, newDenom); // create and reduce new fraction
} // end minus
/**
* times multiplies this fraction by the parameter and returns the result
*
* @param f The fraction to multiply by
* @return This fraction times f
*/
public Fraction times(Fraction f) {
int newNum = num*f.num;
int newDenom = denom*f.denom;
return new Fraction(newNum, newDenom); // create and reduce new fraction
} // end times
/**
* divide takes this fraction divided by the parameter and returns the result
*
* @param f The fraction to divide by
* @return This fraction divided by f
*/
public Fraction divide(Fraction f) {
int newNum = num*f.denom;
int newDenom = denom*f.num;
return new Fraction(newNum, newDenom); // create and reduce new fraction
} // end divide
/**
* equals determines if two reduced fractions have the same num/denom
*
* @param f The fraction to test for equality
* @return true if equal, else false
*/
public boolean equals (Fraction f) {
return ( f.num == this.num && f.denom == this.denom);
} // end equals
} // end class Fraction
Part 2 Fraction App:
// Works with positives and negative values
// Split equation into first number/fraction, operator, second number/fraction
StringTokenizer st = new StringTokenizer(line, " ");
String first = st.nextToken();
char op = (st.nextToken()).charAt(0);
String second = st.nextToken();
// Determine if first value is a whole number or fraction
String [ ] pieces = first.split("/");
if (pieces.length == 1) // whole number, call one-arg constructor
fractionArray[0] = new Fraction(Integer.parseInt(pieces[0]));
else // fraction, call two-arg constructor
// Split into numeration and denominator and create fraction object
fractionArray[0] = new Fraction(Integer.parseInt(pieces[0]),
Integer.parseInt(pieces[1]));
// Determine if second value is a whole number or fraction
pieces = second.split("/");
if (pieces.length == 1) // whole number, call one-arg constructor
fractionArray[1] = new Fraction(Integer.parseInt(pieces[0]));
else // fraction, call two-arg constructor
// Split into numeration and denominator and create fraction object
fractionArray[1] = new Fraction(Integer.parseInt(pieces[0]),
Integer.parseInt(pieces[1]));
boolean flag = true; // error if operator is not +, -, *, /
switch (op) {
case '+':
// 'plus' returns a reduced fraction object
fractionArray[2] = fractionArray[0].plus(fractionArray[1]);
break;
case '-':
// 'minus' returns a reduced fraction object
fractionArray[2] = fractionArray[0].minus(fractionArray[1]);
break;
case '*':
// 'times' returns a reduced fraction object
fractionArray[2] = fractionArray[0].times(fractionArray[1]);
break;
case '/':
// 'divide' returns a reduced fraction object
fractionArray[2] = fractionArray[0].divide(fractionArray[1]);
break;
default:
System.out.println("Invalid operator. Rerun the program.");
flag = false;
} // end switch-case
if (flag) {
System.out.print(" " + fractionArray[0]); // calls toString method
System.out.print(" " + op + " ");
System.out.print(fractionArray[1]); // calls toString method
System.out.print(" = ");
System.out.print(fractionArray[2]); // calls toString method
System.out.println();
if (fractionArray[0].equals(fractionArray[1]))
System.out.println("Fraction1 equals Fraction2");
else
System.out.println("Fraction1 does NOT equal Fraction2");
} // end if
} // end main
} // end class
Explanation / Answer
public class Fraction {
private int num;
private int denom;
public Fraction(int n, int d) {
this.num = n;
this.denom = d;
this.reduce();
// reduce within the constructor
}
// end constructor Fraction(int n, int d)
public Fraction() {
this.num = 1;
this.denom = 1;
}
// end constructor Fraction()
public Fraction(int n) {
this.num = n;
this.denom = 1;
}
// end constructor Fraction(int n)
public final String toString() {
return (this.num + ("/" + this.denom));
}
// end print
private final void reduce() {
boolean negative = false;
// assume not negative
if ((this.num < 0)) {
// if negative, make positive before reducing
negative = true;
this.num = (this.num * -1);
}
// end if
int bound = this.num;
// test if values 2 through num are factors
for (int i = 2; (i <= bound); i++) {
// if remainder is zero for both num & den, then factor
if ((((this.num % i)
== 0)
&& ((this.denom % i)
== 0))) {
this.num = (this.num / i);
this.denom = (this.denom / i);
// have to check if values is again a factor
// (EX: 80/120 =40/60=20/30=10/15)
i--;
}
// end if
}
// end for
// if negative, change back to negative after reducing
if (negative) {
this.num = (this.num * -1);
}
}
// end reduce
public final Fraction plus(Fraction f) {
int newNum = ((this.num * f.denom)
+ (f.num * this.denom));
int newDenom = (this.denom * f.denom);
return new Fraction(newNum, newDenom);
// create and reduce new fraction
}
// end plus
public final Fraction minus(Fraction f) {
int newNum = ((this.num * f.denom)
- (f.num * this.denom));
int newDenom = (this.denom * f.denom);
return new Fraction(newNum, newDenom);
// create and reduce new fraction
}
// end minus
public final Fraction times(Fraction f) {
int newNum = (this.num * f.num);
int newDenom = (this.denom * f.denom);
return new Fraction(newNum, newDenom);
// create and reduce new fraction
}
// end times
public final Fraction divide(Fraction f) {
int newNum = (this.num * f.denom);
int newDenom = (this.denom * f.num);
return new Fraction(newNum, newDenom);
// create and reduce new fraction
}
// end divide
public final boolean equals(Fraction f) {
return ((f.num == this.num)
&& (f.denom == this.denom));
}
}
// end class Fraction
Part;
2;
Fraction App;
:StringTokenizer st = new StringTokenizer(line, " ");
String first = st.nextToken();
char op = st.nextToken().charAt(0);
String second = st.nextToken();
String[] pieces = first.split("/");
if ((pieces.length == 1)) {
fractionArray[0] = new Fraction(Integer.parseInt(pieces[0]));
}
else {
fractionArray[0] = new Fraction(Integer.parseInt(pieces[0]), Integer.parseInt(pieces[1]));
}
// Determine if second value is a whole number or fraction
pieces = second.split("/");
if ((pieces.length == 1)) {
fractionArray[1] = new Fraction(Integer.parseInt(pieces[0]));
}
else {
fractionArray[1] = new Fraction(Integer.parseInt(pieces[0]), Integer.parseInt(pieces[1]));
}
boolean flag = true;
// error if operator is not +, -, *, /
switch (op) {
case '+':
// 'plus' returns a reduced fraction object
fractionArray[2] = fractionArray[0].plus(fractionArray[1]);
break;
case '-':
// 'minus' returns a reduced fraction object
fractionArray[2] = fractionArray[0].minus(fractionArray[1]);
break;
case '*':
// 'times' returns a reduced fraction object
fractionArray[2] = fractionArray[0].times(fractionArray[1]);
break;
case '/':
// 'divide' returns a reduced fraction object
fractionArray[2] = fractionArray[0].divide(fractionArray[1]);
break;
default:
System.out.println("Invalid operator. Rerun the program.");
flag = false;
break;
}
// end switch-case
if (flag) {
System.out.print((" " + fractionArray[0]));
// calls toString method
System.out.print((" "
+ (op + " ")));
System.out.print(fractionArray[1]);
// calls toString method
System.out.print(" = ");
System.out.print(fractionArray[2]);
// calls toString method
System.out.println();
if (fractionArray[0].equals(fractionArray[1])) {
System.out.println("Fraction1 equals Fraction2");
}
else {
System.out.println("Fraction1 does NOT equal Fraction2");
}
}