1. ## Rational Number Calculator

Here is the code I need to write, I Already completed the first part, but I need help with Problem 2:

Problem 1: Write a RationalNumber class having public methods for
public RationalNumber() // Initializes to 0/1
public RationalNumber(int n, int d) // Initializes to n/d
public RationalNumber(RationalNumber x) // Initializes to xs values
public void add(RationalNumber other) // this = this + other
public void subtract(RationalNumber other) // this = this - other
public void multiply(RationalNumber other) // this = this * other
public void divide(RationalNumber other) // this = this / other
public String toString() // Creates string representation
public boolean equals(RationalNumber other) // Is this == other?
Notes:
- Any methods not listed above and all data fields should be private.
- r.equals(s) should return true if r and s have the same value. You know enough to do a decent job of
this method already, but if you want to do an even better job, read Pages 573-578 before writing equals.
- toString should create a String representation of the rational, in reduced form. If a number is negative
the string should begin with -. If it is a whole number (including zero) it should be shown as over 1. For
example. If I initialize with n=4 and d=-2, toString should return "-2/1".
- If d=0 is passed to the constructor, throw an IllegalArgumentException.

Problem 2: Write a rational number calculator that repeatedly waits for an input line and then prints
the result of the calculation using your RationalNumber class. The calculator should stop once the user
types q. Your program should behave like this:
> 1 / 2 + 1 / 2
1/1
> 1 / 2 - 1 / 2
0/1
> 10 / 1 * -1 / 10
-1/1
> -64 / 128 / 128 / -64
1/4
> 2 / -4 = -3 / 6
true
> q
Each line has the following format <int> / <int> <op> <int> / <int>, so it should be easy to parse using
Scanner methods. <int> / <int> represents a rational number, and <op> is a single character indicating
the desired operation. You may assume that every input follows this format without error.  Reply With Quote

2. What is this i do not understand this.  Reply With Quote

3. ## Java

Its a java based computer science assignment

I have to make a rational number calculator, it will constantly ask for input from the user and will act like the example above. I need it in a more basic version.

Here is a program I found that does the job, but its way to complex for my class level:

import java.util.Scanner;

public class RationalCalculator2 {

Scanner kbd;
private RationalNumber operand1;
private RationalNumber operand2;
/**
* operation:
* 1 : subtract;
* 2 : multiply;
* 3 : divide;
* 4 : compare;
*/
private int operation;

public RationalCalculator2()
{
kbd = new Scanner(System.in);
}
/**
* gets input from console line, and initializes operands and operation
* @return true if new expression is inputed, false if user wants to quit
*/
public boolean getInput()
{
String[] lineParts = kbd.nextLine().split("/");
if(lineParts.length == 1)
if(lineParts.equalsIgnoreCase("q")) return false;
if(lineParts.length == 4) //divide case
{
operation = 3;
operand1 = new RationalNumber(Integer.parseInt(lineParts), Integer.parseInt(lineParts));
operand2 = new RationalNumber(Integer.parseInt(lineParts), Integer.parseInt(lineParts));
}
else //not divide
{
String[] midPart = lineParts.trim().split("\\s"); //should yield 3 parts, i.e: 3, +, 4
switch(midPart.trim().charAt(0)) // grab the operator
{
case '+':
{
operation = 0;
operand1 = new RationalNumber(Integer.parseInt(lineParts.trim()), Integer.parseInt(midPart.trim()));
operand2 = new RationalNumber(Integer.parseInt(midPart.trim()), Integer.parseInt(lineParts.trim()));
break;
}
case '-':
{
operation = 1;
operand1 = new RationalNumber(Integer.parseInt(lineParts.trim()), Integer.parseInt(midPart.trim()));
operand2 = new RationalNumber(Integer.parseInt(midPart.trim()), Integer.parseInt(lineParts.trim()));
break;
}
case '*':
{
operation = 2;
operand1 = new RationalNumber(Integer.parseInt(lineParts.trim()), Integer.parseInt(midPart.trim()));
operand2 = new RationalNumber(Integer.parseInt(midPart.trim()), Integer.parseInt(lineParts.trim()));
break;
}
case '=':
{
operation = 4;
operand1 = new RationalNumber(Integer.parseInt(lineParts.trim()), Integer.parseInt(midPart.trim()));
operand2 = new RationalNumber(Integer.parseInt(midPart.trim()), Integer.parseInt(lineParts.trim()));
break;
}
}
}
return true;
}
/**
*/
private void add(){ // operand1 = operand1 + operand2
//operand1.den = operand1.den * operand2.den; //wrong output because you change den to new value,
operand1.num = operand1.num * operand2.den + operand2.num * operand1.den; // but you want the old value here.
operand1.den = operand1.den * operand2.den;
operand1.reduce();
//toString();
}
/**
* subtracts
*/
private void subtract(){ // operand1 = operand1 - operand2
// operand1.den = operand1.den * operand2.den; //see add explanation
operand1.num = operand1.num * operand2.den - operand2.num * operand1.den;
operand1.den = operand1.den * operand2.den;
operand1.reduce();
//toString ();
}

/**
* multiplies
*/
private void multiply(){ // operand1 = operand1 * operand2
operand1.den = operand1.den * operand2.den;
operand1.num = operand1.num * operand2.num;
operand1.reduce();
//toString ();
}
/**
* divides
*/
private void divide(){ // operand1 = operand1 / operand2
operand1.den = operand1.den * operand2.num;
operand1.num = operand1.num * operand2.den;
operand1.reduce();
//toString ();
}
/**
* compares
* @return
*/
private boolean equals(){ // Is this == other?

operand1.reduce();
operand2.reduce();
if( operand1.num == operand2.num && operand1.den == operand2.den){
return true;
}else{
return false;
}
}

/**
* takes operation and performs it on operands
* @return answer in string form;
*/
public String calculate()
{
switch(operation)
{
case 0:
{
break;
}
case 1:
{
subtract();
break;
}
case 2:
{
multiply();
break;
}
case 3:
{
divide();
break;
}
case 4:
{
return ""+equals();
}
}
return operand1.toString();
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
RationalCalculator2 calc = new RationalCalculator2();
System.out.println("Enter an equation of two rational number in format: \"n / d operation n /d\"\nType \"q\" to quit.");
calc.getInput();
do
{
System.out.println(""+calc.calculate());
}while(calc.getInput());
}

/**
* Rational Number.
* This is all a number should know: it's value and how to reduce itself;
*
*/
class RationalNumber
{
private int num; //numerator
private int den; //denominator

public RationalNumber(int num, int den)
{
this.num = num;
this.den = den;
}
public String toString()
{
reduce();
return ""+num+" / "+den;

}
private void reduce(){
int yes = 0;
int smaller;
int num1=this.num,den1=this.den;
// you forgot the if num = 0 case
if(num == 0)
{
den = 1;
return;
}
if(num < 0 && den < 0)
{
num *= -1;
den *= -1;
}
if(num<0)
{ num1=-1*num;}
if(den<0)
{ den1=-1*den;}
if ( num1 < den1 ){
smaller = num1;
}else{
smaller = den1;}

for ( int div = smaller; div >= 2; div-- ){
if ( num1 % div == 0 && den1 % div == 0 ){
yes = div;
break;
}
}
if ( yes != 0 ){
num /= yes;
den /= yes;
}
}
}
}  Reply With Quote