473,395 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Homework HELP!!! Add/Sub/Mult/Div Fractions

I am having trouble figuring out how to add, subtract, multiply, and divide fractions in my Java program. First off I will tell you what I already did. At first, the assignment was to:

Define a class called Rational. This class is used to represent a ratio of two integers. Include mutator functions that allow the user to set the numerator or denominator as a double. Include additional member function that outputs the value of the fraction reduced to lowest terms. This will require finding the greatest common divisor for the numerator and denominator, then dividing both by that number. Embed your class in a test program.

Okay so I did all of that fine. Everything worked and it was all good, but then, my teacher wanted us to insert another fraction to the mix to do the math operations on it. I am having trouble figuring out how to do that. I keep getting errors upon errors. Also, I am having trouble reducing the fractions now since a new fraction is now in the program.

So, I am asking anyone to help assist me in fixing this problem. Any feedback whatsoever would be greatly appreciated. If you need any more information on the program, please let me know! Thanks!

P.S. Here is my program:



/**
Description: Program to represent a ratio of two integers.
Author:
Email Address:
Last Changed: September 25, 2006
**/

import java.util.Scanner;
import java.text.DecimalFormat;

public class Rational1
{
public static int num1; //num1 = numerator of first fraction
public static int den1; //den1 = denominator of first fraction
public static int num2; //num2 = numerator of second fraction
public static int den2; //den2 = denominator of second fraction

public Rational1()
{
num1 = 0;
num2 = 0;
den1 = 0;
den2 = 0;

}

public Rational1 (int n1, int d1, int n2, int d2)
{
num1 = n1;
den1 = d1;
num2 = n2;
den2 = d2;

if((d1 == 0) || (d2 == 0))
{
System.out.println("Error - denominator cannot be zero!");
}
}

public static void main(String[] args)
{
int lowestNum1 = 0;
int lowestDen1 = 0;
int lowestNum2 = 0;
int lowestDen2 = 0;
int gcd = 0;

System.out.println("Welcome to the world of ratios!");
Rational1 yourRatio = new Rational1();
yourRatio.setRatio();
System.out.println("Your first fraction: " + num1 + "/" + den1);
System.out.println("Your numerator as a double: ");
yourRatio.toDouble(num1, num2);
System.out.println("Your denominator as a double: ");
yourRatio.toDouble(den1, den2);
gcd = yourRatio.gcd(num1, den1);
System.out.println("Your first fraction's GCD: " + gcd);
lowestNum1 = yourRatio.reduceNum(num1, gcd);
lowestDen1 = yourRatio.reduceDen(den1, gcd);
System.out.println("Your reduced fraction:" + lowestNum1 + "/" + lowestDen1);
System.out.println("Your second fraction: " + num2 + "/" + den2);
System.out.println("Your numerator as a double: ");
// yourRatio.toDouble(num2);
System.out.println("Your denominator as a double: ");
// yourRatio.toDouble(den2);
gcd = yourRatio.gcd(num2, den2);
System.out.println("Your second fraction's GCD: " + gcd);
lowestNum2 = yourRatio.reduceNum(num2, gcd);
lowestDen2 = yourRatio.reduceDen(den2, gcd);
System.out.println("Your reduced fraction:" + lowestNum2 + "/" + lowestDen2);
System.out.println("Thank you for visiting!");
}

public void setRatio()
{
System.out.println("Enter an integer for the numerator followed by another integer for the denominator to make a ratio.");
System.out.println("(Do not use the / sign to separate the numbers, just insert a space.)");
Scanner ratio = new Scanner(System.in);
num1 = ratio.nextInt();
den1 = ratio.nextInt();
num2 = ratio.nextInt();
den2 = ratio.nextInt();
}

public void toDouble(int n1, int n2)
{
double dubN1;
double dubN2;
dubN1 = n1;
dubN2 = n2;

System.out.println(dubN1);
System.out.println(dubN2);
}

/* public static int abs(int n, int d)
{
n = abs(n); //n = numerator
d = abs(d); //d = denominator

while(n !=d)
{
if(n > d)
{
n = n - d;
t = n;
}
else
{
n = d - n;
d = t;
}
g = n;
}
} */

/* public static int abs(int n)
{
return n > 0 ? n : - n;
}*/

public static int gcd(int a, int b)
{
/* int t; //t = temp
n = abs(n);
d = abs(d);

while(n != d)
{
if(n > d)
{
n = n - d;
t = n;
}
else
{
n = n - d;
d = t;
}
return n;
*/
if(b == 0)
{
return a;
}
else
{
return gcd(b, a % b);
}

}

public void reduce()
{
int n1 = num1;
int d1 = den1;
int n2 = num2;
int d2 = den2;
int g = gcd(n, m);

num1 = num1 / g;
den1 = den1 / g;
num2 = num2 / g;
den2 = den2 / g;
}

public int reduceNum(int a, int gcd)
{
int rNum1 = a / gcd;
int rNum2 = a / gcd;

return rNum1;
return rNum2;
}

public int reduceDen(int b, int gcd)
{
int rNum1 = b / gcd;
int rNum2 = b / gcd;

return rNum1;
return rNum2;
}

public Rational1 add(Rational1 Q)
{
Rational1 R;
int den = den1 * den2;
int num = den2 * num1 + den1 * num2;
R = new Rational1(n, d);
R = R.reduce();
return R;
}

public Rational1 multiply(Rational1 Q)
{
Rational1 R;
int den = den1 * den2;
int num = num1 * num2;
R = new Rational1(n,d);
R = R.reduce();
return R;
}

public Rational1 subtract(Rational1 Q)
{
Rational1 R;
int den = den1 * den2;
int num = den2 * num1 = den1 * num2;
R = R.reduce();
return R;
}

public Rational1 divide(Rational1 Q)
{
Rational1 R;
int den = den1 * den2;
int num = num1 / num2;
R = R.reduce();
return R;
}

public static void mathOp(int num1, int den1, int num2, int den2, char op)
{
Rational1 result = new Rational1(); //just in case

f1.write();
prt(op);
f2.write();
prt( " = " );
switch(op)
{
case '+':
result = num1 * den2 + num2 * den1;
break;
case '-':
result = num1 * den2 - num2 * den1;
break;
case '*':
result = num1 * num2;
result = den1 * den2;
break;
case '/':
result = num1 / num2;
result = den1 / den2;
break;
default:
prtln( "Operator!? ..." );
}

result.write( );
DecimalFormat threeDigits = new DecimalFormat("#0.000");
prtln( " = " + (threeDigits.format(Result.toDouble( ))));
}
}
Sep 25 '06 #1
1 8553
r035198x
13,262 8TB
Next time you want to post code, please use code tags.
You could have given your variables names like numerator, denominator, etc.
You do not need to define the other fraction inside your Rational class. It is in fact a Rational itself. Declare it in the main method where you want to test it.
The switch construct should be avoided where possible. In this case, I would suggest you define methods inside your Rational class to perform the arithmetic operations.(If you do not understand this point, have a look at the definition of the java.math.BigDecimal class.
Sep 25 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Allens Mail | last post by:
Help, Due Mon I cant find the error. Keeps saying illegal token } have gone though code and balance all French braces and parenthesis. Please help going crazy. Code below Thanks Allen ...
3
by: Amy | last post by:
Hi, I have 6 If Then Else statements I was supposed to write. I did so but I know that they have to be wrong because they all look the same. Could someone take a look at them and point me in the...
7
by: mastern200 | last post by:
For Homework, i have to debug a program (I am a noob!) it is a voting program where you put in the amount of votes a certain candidate gets and evaluates by showing how many votes, what percentage of...
1
by: mastern200 | last post by:
I need some homework help with an assignment due wed. I need to make a program where in this program calculates the average of a set of test scores. The program will ask the user how many scores...
2
by: mia23 | last post by:
Hello, I am a java beginner and I need series help in solving this program; my assignment is due after 2 days and I can't understand this program . This is THE PROGRAM:  The ABC medical clinic...
5
by: karafire2003 | last post by:
I was wondering if someone can help me with my homework. here's the assignment: a. Write a C program that has a declaration in main() to store the string "What's for lunch?" into an array named...
1
by: itgetsharder | last post by:
Hey, i was wondering if anyone could help me. i have two questions that i cannot complete for a homework assignment: This method should convert its parameter (a string like "3.1415") to the...
2
by: DonE | last post by:
Help me please. I am having problems with my homework assignment. We are to create a product class that hold the item number, the name of the product, the number of units in stock, and the price of...
1
by: 1337kamikaze | last post by:
I can't get the common denominator to work. Please help. import java.util.*; import java.lang.*; public class Fraction { /** * @param args
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.