-
Fractions
I need help with an assignment I have for school. I'm a sophomore in high school in Computer Science 1. We use Eclipse (Java) for our programs.
Here is the PDF for the assignment:
Classes
I uploaded my Classes project too. Here is what I have so far The class is called "FractionClass" and "FractionClassTester".
Fraction Class Assignment.pdf
PLEASE HELP ME!
-
Re: Fractions
Do you have any specific questions about your assignment?
Please post your code and any questions about problems you are having.
Post everything here on the forum. No links.
-
Re: Fractions
Assignment:
You will create a class to represent fractions. The requirements below will establish how this class should be structured.
Part 1
* The class should store a numerator and denominator
(remember, data members should be private at this point in your programs) * The class should have both a default and non*default constructor
* Do not make any set() accessor methods
Part 2
* The class should have the following methods:
+ Fraction add(Fraction other)
+ Fraction subtract(Fraction other)
+ Fraction multiply(Fraction other)
+ Fraction divide(Fraction other)
+ print() : print the fraction to the screen [as a fraction]
** NOTE: none of the arithmetic methods should modify the values of either operand; they should simply return the result of the calculation.
Part 3
The main method in your tester class should have the following...
* 10 different Fraction variables initialized with different values
* 1 additional Fraction variable to store the result of the calculations
* 5 calls to the addition method, print out the results [not within the add method]
* 5 calls to the subtraction method, print out the results [not within the subtract method] * 5 calls to the multiply method, print out the results [not within the multiply method]
* 5 calls to the divide method, print out the results [not within the divide method]
For some reason my code won't open in Eclipse from my USB. But all I had was part one down.
--- Update ---
Please use the link for the Classes. I can't open it for some reason.
-
Re: Fractions
If you have questions about your code, post the code and your questions.
-
Re: Fractions
FractionClass:
public class FractionClass {
private int numerator;
private int denominator;
public FractionClass()
{
numerator = 1;
denominator = 2;
}
public FractionClass(int num, int den)
{
numerator = num;
if (den == 0)
den =1;
denominator = den;
}
public FractionClass add(FractionClass other)
{
return null;
}
public FractionClass subtract(FractionClass other)
{
return null;
}
public FractionClass multiply(FractionClass other)
{
int num = numerator * other.numerator;
int den = denominator * other.denominator;
return new FractionClass(num, den);
}
public FractionClass divide(FractionClass other)
{
int num = numerator * other.denominator;
int den = denominator * other.numerator;
return new FractionClass(num, den);
}
public void printFractionClass()
{
// Print values as a fraction
}
}
FractionClassTester:
public class FractionClassTester {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
-
Re: Fractions
FractionClass:
public class FractionClass {
private int numerator;
private int denominator;
public FractionClass()
{
numerator = 1;
denominator = 2;
}
public FractionClass(int num, int den)
{
numerator = num;
if (den == 0)
den =1;
denominator = den;
}
public FractionClass add(FractionClass other)
{
return null;
}
public FractionClass subtract(FractionClass other)
{
return null;
}
public FractionClass multiply(FractionClass other)
{
int num = numerator * other.numerator;
int den = denominator * other.denominator;
return new FractionClass(num, den);
}
public FractionClass divide(FractionClass other)
{
int num = numerator * other.denominator;
int den = denominator * other.numerator;
return new FractionClass(num, den);
}
public void printFractionClass()
{
// Print values as a fraction
}
}
FractionClassTester:
public class FractionClassTester {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}