Sponsored Links

# Thread: Rational Number Calculator

1. ## Rational Number Calculator

Sponsored Links1

#### View more random threads:

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.

Sponsored Links

2. What is this i do not understand this.

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:
* 0 : add;
* 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[0].equalsIgnoreCase("q")) return false;
if(lineParts.length == 4) //divide case
{
operation = 3;
operand1 = new RationalNumber(Integer.parseInt(lineParts[0]), Integer.parseInt(lineParts[1]));
operand2 = new RationalNumber(Integer.parseInt(lineParts[2]), Integer.parseInt(lineParts[3]));
}
else //not divide
{
String[] midPart = lineParts[1].trim().split("\\s"); //should yield 3 parts, i.e: 3, +, 4
switch(midPart[1].trim().charAt(0)) // grab the operator
{
case '+':
{
operation = 0;
operand1 = new RationalNumber(Integer.parseInt(lineParts[0].trim()), Integer.parseInt(midPart[0].trim()));
operand2 = new RationalNumber(Integer.parseInt(midPart[2].trim()), Integer.parseInt(lineParts[2].trim()));
break;
}
case '-':
{
operation = 1;
operand1 = new RationalNumber(Integer.parseInt(lineParts[0].trim()), Integer.parseInt(midPart[0].trim()));
operand2 = new RationalNumber(Integer.parseInt(midPart[2].trim()), Integer.parseInt(lineParts[2].trim()));
break;
}
case '*':
{
operation = 2;
operand1 = new RationalNumber(Integer.parseInt(lineParts[0].trim()), Integer.parseInt(midPart[0].trim()));
operand2 = new RationalNumber(Integer.parseInt(midPart[2].trim()), Integer.parseInt(lineParts[2].trim()));
break;
}
case '=':
{
operation = 4;
operand1 = new RationalNumber(Integer.parseInt(lineParts[0].trim()), Integer.parseInt(midPart[0].trim()));
operand2 = new RationalNumber(Integer.parseInt(midPart[2].trim()), Integer.parseInt(lineParts[2].trim()));
break;
}
}
}
return true;
}
/**
* adds
*/
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:
{
add();
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;
* @author GoldfishGrenade
*
*/
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;
}
}
}
}

#### Thread Information

##### Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
-: Vuhelp Disclaimer :-
None of the files shown here are hosted or transmitted by this server. The links are provided solely by this site's users. The administrator's or staff of Vuhelp.net cannot be held responsible for what its users post, or any other actions of its users. You may not use this site to distribute or download any material when you do not have the legal rights to do so. It is your own responsibility to adhere to these terms. If you have any doubts about legality of content or you have any suspicions, feel free to contact us.
Online Education | JhelumSoft | Jobs