In: Computer Science
How do I fix my code?
public class Fraction
{
private int numerator, denominator, numberOfFraction;
public Fraction ()
{
numerator = 0;
denominator = 1;
numberOfFraction++;
}
public Fraction (int n, int d)
{
numerator = n;
denominator = d;
numberOfFraction++;
}
private int gcd (int num1, int num2)
{
if (num1 == 0)
return num2;
return gcd (num2 % num1, num1);
}
public Fraction add (Fraction third)
{
int n = numerator * third.denominator + third.numerator * denominator;
int d = denominator * third.denominator;
int a = gcd (n, d);
n = n / a;
d = d / a;
numberOfFraction++;
return new Fraction (n, d);
}
public Fraction subtract (Fraction third)
{
int n = numerator * third.denominator - third.numerator * denominator;
int d = denominator * third.denominator;
int a = gcd (n, d);
n = n / a;
d = d / a;
numberOfFraction++;
return new Fraction (n, d);
}
public Fraction multiply (Fraction third)
{
int n = numerator * third.numerator;
int d = denominator * third.denominator;
int a = gcd (n, d);
n = n / a;
d = d / a;
numberOfFraction++;
return new Fraction (n, d);
}
public Fraction divide (Fraction third)
{
int n = numerator * third.denominator;
int d = denominator * third.numerator;
int a = gcd (n, d);
n = n / a;
d = d / a;
numberOfFraction++;
return new Fraction (n, d);
}
public String toString ()
{
String str;
int a = gcd (numerator, denominator);
numerator = numerator / a;
denominator = denominator / a;
str = numerator + "/" + denominator;
return str;
}
public static float printAsFloat (Fraction a)
{
return a.numerator / (float) a.denominator;
}
public static int numberOfFraction (Fraction a)
{
return a.numberOfFraction;
}
}
Here is the test class
import java.util.Scanner;
public class FractionDemo
{
public static void main (String[]args)
{
Scanner reader = new Scanner (System.in);
int totalFraction = 0, counter = 0;
while (true)
{
System.out.println ("Please enter two fractions---");
System.out.println ("Fraction 1:");
System.out.print ("Enter an integer numerator: ");
int n1 = reader.nextInt ();
System.out.print ("Enter an integer numerator: ");
int d1 = reader.nextInt ();
Fraction a;
if (d1 == 0)
{
System.out.println ("denominator canot be 0");
System.out.println ("the fraction is set to 0/1");
a = new Fraction ();
}
else
{
a = new Fraction (n1, d1);
}
System.out.println ("Fraction 2:");
System.out.print ("Enter an integer numerator: ");
int n2 = reader.nextInt ();
System.out.print ("Enter an integer numerator: ");
int d2 = reader.nextInt ();
Fraction b;
if (d2 == 0)
{
System.out.println ("denominator canot be 0");
System.out.println ("the fraction is set to 0/1");
b = new Fraction ();
}
else
{
b = new Fraction (n2, d2);
}
Fraction c;
c = a.add (b);
totalFraction += numberOfFraction (c);
System.out.println (a + " + " + b + " = " + c + " = " + printAsFloat (c));
c = a.subtract (b);
totalFraction += numberOfFraction (c);
System.out.println (a + " - " + b + " = " + c + " = " + printAsFloat (c));
c = a.multiply (b);
totalFraction += numberOfFraction (c);
System.out.println (a + " * " + b + " = " + c + " = " + printAsFloat (c));
c = a.divide (b);
totalFraction += numberOfFraction (c);
System.out.println (a + " / " + b + " = " + c + " = " + printAsFloat (c));
System.out.print ("Do You want to continue ('Y' or 'y' for yes)");
reader.nextLine ();
String choice = reader.nextLine ();
counter++;
if (!choice.equalsIgnoreCase ("y"))
{
System.out.println (totalFraction + 2 * counter +
" fractions have been created");
break;
}
}
}
}
Here are the errors
FractionDemo.java:50: error: cannot find symbol
totalFraction += numberOfFraction (c);
^
symbol: method numberOfFraction(Fraction)
location: class FractionDemo
FractionDemo.java:52: error: cannot find symbol
System.out.println (a + " + " + b + " = " + c + " = " + printAsFloat (c));
^
symbol: method printAsFloat(Fraction)
location: class FractionDemo
FractionDemo.java:54: error: cannot find symbol
totalFraction += numberOfFraction (c);
^
symbol: method numberOfFraction(Fraction)
location: class FractionDemo
FractionDemo.java:55: error: cannot find symbol
System.out.println (a + " - " + b + " = " + c + " = " + printAsFloat (c));
^
symbol: method printAsFloat(Fraction)
location: class FractionDemo
FractionDemo.java:57: error: cannot find symbol
totalFraction += numberOfFraction (c);
^
symbol: method numberOfFraction(Fraction)
location: class FractionDemo
FractionDemo.java:58: error: cannot find symbol
System.out.println (a + " * " + b + " = " + c + " = " + printAsFloat (c));
^
symbol: method printAsFloat(Fraction)
location: class FractionDemo
FractionDemo.java:60: error: cannot find symbol
totalFraction += numberOfFraction (c);
^
symbol: method numberOfFraction(Fraction)
location: class FractionDemo
FractionDemo.java:61: error: cannot find symbol
System.out.println (a + " / " + b + " = " + c + " = " + printAsFloat (c));
^
symbol: method printAsFloat(Fraction)
location: class FractionDemo
8 errors
The error was due to numberOfFraction(Fraction) and printAsFloat(Fraction) being declared as static in your Fraction class and you are using it in your Demo class's code directly.
If you have to use them in your main class(FractionDemo), then you have to use them by calling them by class name for ex: Fraction.numberOfFraction(Fraction) and Fraction.printAsFloat(Fraction) as i have used in the below code.
below is the code(modified is marked as bold):
import java.util.Scanner;
public class FractionDemo {
public static void main(String[] args) {
Scanner reader = new
Scanner(System.in);
int totalFraction = 0, counter =
0;
while (true)
{
System.out.println("Please enter two fractions---");
System.out.println("Fraction 1:");
System.out.print("Enter an integer numerator: ");
int n1 =
reader.nextInt();
System.out.print("Enter an integer numerator: ");
int d1 =
reader.nextInt();
Fraction
a;
if (d1 ==
0)
{
System.out.println("denominator canot be
0");
System.out.println("the fraction is set to
0/1");
a = new Fraction();
}
else
{
a = new Fraction(n1, d1);
}
System.out.println("Fraction 2:");
System.out.print("Enter an integer numerator: ");
int n2 =
reader.nextInt();
System.out.print("Enter an integer numerator: ");
int d2 =
reader.nextInt();
Fraction
b;
if (d2 ==
0)
{
System.out.println("denominator canot be
0");
System.out.println("the fraction is set to
0/1");
b = new Fraction();
}
else
{
b = new Fraction(n2, d2);
}
Fraction
c;
c =
a.add(b);
totalFraction +=
Fraction.numberOfFraction(c);
System.out.println(a + " + " + b + " = " + c + " = " +
Fraction.printAsFloat(c));
c =
a.subtract(b);
totalFraction +=
Fraction.numberOfFraction(c);
System.out.println(a + " - " + b + " = " + c + " = " +
Fraction.printAsFloat(c));
c =
a.multiply(b);
totalFraction +=
Fraction.numberOfFraction(c);
System.out.println(a + " * " + b + " = " + c + " = " +
Fraction.printAsFloat(c));
c =
a.divide(b);
totalFraction +=
Fraction.numberOfFraction(c);
System.out.println(a + " / " + b + " = " + c + " = " +
Fraction.printAsFloat(c));
System.out.print("Do You want to continue ('Y' or 'y' for
yes)");
reader.nextLine();
String choice =
reader.nextLine();
counter++;
if
(!choice.equalsIgnoreCase("y"))
{
System.out.println(totalFraction + 2 * counter
+
"
fractions have been created");
break;
}
}
reader.close();//for no leakage of resource
}
}
you were getting error because of these 8 issues, which have been resolved above.
when you run above demo class output will be:
if you need any help please comment!!