Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

//THE ERROR HAPPENS ON THE FOR LOOP LINE 77 //how can i fix it?... thank you imp

ID: 662548 • Letter: #

Question

//THE ERROR HAPPENS ON THE FOR LOOP LINE 77

//how can i fix it?... thank you

import java.io.*;
import java.util.*;
import java.awt.*;
import java.math.*;

public class Example
{
   public static void main(String[] args)
   {

        
      Rational r1 = new Rational(2, 5);

       Rational r2 = new Rational(3, 5);
       Rational result = r1.add(r2);

       int a = result.getNumerator();
       int b = result.getDenominator();
       System.out.println("2/5 + 3/5 = "+ a +"/"+ b);

       result = r1.sub(r2);
       a = result.getNumerator();
       b = result.getDenominator();
       System.out.println("2/5 - 3/5 = "+ a +"/"+ b);

       result = r1.muti(r2);
       a = result.getNumerator();
       b = result.getDenominator();
       System.out.println("2/5 * 3/5 = "+ a +"/"+ b);

       result = r1.div(r2);
       a = result.getNumerator();
       b = result.getDenominator();
       System.out.println("(2/5) / (3/5) = "+ a +"/"+ b);
    
       System.out.println("2/1 + 3/2 + 5/3+ 8/5...10");
       int n = 10;
       int k = 0;
       Rational sum = new Rational(0, 10);
       Rational item = new Rational(2, 1);
       while (k < n)
       {
           sum = sum.add(item);
           k++;
           int numerator = sum.getNumerator();
           int denominator = sum.getDenominator();
           item.setNumeratorAndDenominator(numerator + denominator, denominator);
       }

       a = sum.getNumerator();
       b = sum.getDenominator();
       System.out.println(":");
       System.out.println(a +"/"+ b);
       double doubleResult = (a*1.0) / b;
       System.out.println(":");
       System.out.println(doubleResult);    
   }

}

public class TestRational extends Rational
{
public TestRational() {}

   Rational r1= new Rational (3,5);
   Rational r2= new Rational (4,7);
   Rational r3=r1.add(r2);


   Rational a[] = new Rational(a[10]);
   Rational b[] = new Rational(b[10]);


   for(int i=0; i < 10; i++)
   {
a[i] = (int)(Math.random() * 8 + 1);
   }

   for(int j=0; j < 10; j++)
   {
b[j] = (int)(Math.random() * 8 + 1);
   }





   private JTextField field1;
   private JTextField field2;
   private JTextArea dis;

   public TestRational()
   {

JButton addition = new JButton("+");
JButton subtract = new JButton("-");
JButton divide = new JButton("/");
JButton multiply = new JButton("*");
JButton sortt = new JButton("Sort");

JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());

addition.addActionListener(new ActionListener()
{
   public void actionPerformed(ActionEvent e)
   {
Rational b1 = new Rational();
b1.add(a);
   }
});

subtract.addActionListener(new ActionListener()
{
   public void actionPerformed(ActionEvent f)
   {
Rational b2 = new Rational();
b2.subtract(a);
   }
});

divide.addActionListener(new ActionListener()
{
   public void actionPerformed(ActionEvent g)
   {
Rational b3 = new Rational();
b3.divide(a);
   }
});

multiply.addActionListener(new ActionListener()
{
   public void actionPerformed(ActionEvent h)
   {
Rational b4 = new Rational();
b4.multiply(a);
   }
});

sortt.addActionListener(new ActionListener()
{
   public void actionPerformed(ActionEvent p)
   {
Arrays.sort(a);
for(int k=0; k<a.length; i++)
   field2.setText("" + a[i] + " ");

   }
});



buttonPanel.add(addition);
buttonPanel.add(subtract);
buttonPanel.add(divide);
buttonPanel.add(multiply);
buttonPanel.add(sort);



//Jpanel for fraction area
JPanel mainDisplay = new JPanel();
field2 = new JTextField(20);
dis = new JTextArea(10,20);
JScrollPane scroll = new JScrollPane(dis);
mainDisplay.setLayout(new BorderLayout());
mainDisplay.add(dis);

setSize(300,300);
setVisible(true);


   }


}

Explanation / Answer

You can fix the code by simpling compiling and saving the file containing the Rational Class. Currently The Rational class is not present.Thats why there are errors.