Question

In: Computer Science

How do I fix my code? public class Fraction {    private int numerator, denominator, numberOfFraction;    public...

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

Solutions

Expert Solution

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!!


Related Solutions

Java Implement a class named “Fraction” with the following properties: numerator: int type, private denominator: int...
Java Implement a class named “Fraction” with the following properties: numerator: int type, private denominator: int type, private and the following methods: one default constructor which will create a fraction of 1/1. one constructor that takes two parameters which will set the values of numerator and denominator to the specified parameters. int getNum() : retrieves the value of numerator int getDenom(): retrieves the value of the denominator Fraction add(Fraction frac): adds with another Fraction number and returns the result in...
The denominator of a fraction is 4 more than the numerator. If both the numerator and...
The denominator of a fraction is 4 more than the numerator. If both the numerator and the denominator of the fraction are increased by 3, the new fraction is 5/6 . Find the original fraction.
First lab: Create a Fraction class Create member variables to store numerator denominator no additional member...
First lab: Create a Fraction class Create member variables to store numerator denominator no additional member variable are allowed Create accessor and mutator functions to set/return numerator denominator Create a function to set a fraction Create a function to return a fraction as a string ( common name ToString(), toString())  in the following format: 2 3/4 use to_string() function from string class to convert a number to a string; example return to_string(35)+ to_string (75) ; returns 3575 as a string Create...
With the code that is being tested is: import java.util.Random; public class GVdate { private int...
With the code that is being tested is: import java.util.Random; public class GVdate { private int month; private int day; private int year; private final int MONTH = 1; private final int DAY = 9; private static Random rand = new Random(); /** * Constructor for objects of class GVDate */ public GVdate() { this.month = rand.nextInt ( MONTH) + 1; this.day = rand.nextInt ( DAY );    } public int getMonth() {return this.month; } public int getDay() {return this.day;...
JAVA: How do I fix the last "if" statement in my code so that outputs the...
JAVA: How do I fix the last "if" statement in my code so that outputs the SECOND HIGHEST/MAXIMUM GPA out of the given classes? public class app { private static Object minStudent; private static Object maxStudent; public static void main(String args[ ]) { student st1 = new student("Rebecca", "Collins", 22, 3.3); student st2 = new student("Alex", "White", 19, 2.8); student st3 = new student("Jordan", "Anderson", 22, 3.1); student[] studentArray; studentArray = new student[3]; studentArray[0] = st1; studentArray[1] = st2; studentArray[2]...
public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
1. Consider the following code: public class Widget implements Serializable { private int x; public void...
1. Consider the following code: public class Widget implements Serializable { private int x; public void setX( int d ) { x = d; } public int getX() { return x; } writeObject( Object o ) { o.writeInt(x); } } Which of the following statements is true? I. The Widget class is not serializable because no constructor is defined. II. The Widget class is not serializable because the implementation of writeObject() is not needed. III. The code will not compile...
7. Fractions You can express a fraction as a list: [numerator, denominator]. For example 1 2...
7. Fractions You can express a fraction as a list: [numerator, denominator]. For example 1 2 can be expressed as the list [1,2]. (a) Write a function called factionAdd() that takes two fractions as lists and adds them. For example, fraction([1,2], [3,4]) returns [5,4] (b) Write a function fractionMult() that multiplies two fractions that are passed as lists. [HINT: You may use the following function gcd ( x ; y ) to help you calculate the Greatest Common Divisor, which...
class A { public: //constructors // other members private: int a; int b; }; Give declatations...
class A { public: //constructors // other members private: int a; int b; }; Give declatations of operator functions for each of the following ways to overload operator + You must state where the declatation goes, whether within the class in the public or private section or outside the class. The operator + may be overloaded. a) as friend function b) as member function c) as non-friend, non-member function
I was wondering why my merger class isn't compiling - Java -------------------------------------------------------------------------------------------------------------------- public class Person{ private...
I was wondering why my merger class isn't compiling - Java -------------------------------------------------------------------------------------------------------------------- public class Person{ private String firstName; private String lastName; private int age; private String email; private String phone; private String address;       public Person(String firstName, String lastName, int age, String email, String phone, String address){ setFirstName(firstName); setLastName(lastName); setAge(age); setEmail(email); setPhone(phone); setAddress(address); } public Person(String firstName, String lastName, String email){ setFirstName(firstName); setLastName(lastName); setEmail(email); }    public String getFirstName(){ return firstName; }    public String getLastName(){ return lastName; }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT