Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Have a program that will compile but has some run time errors involving number formating. I was hoping you could help me out with this.

Have a program that will compile but has some run time errors involving number formating. I was hoping you could help me out with this. its involving the section with "parseInt". The section is at the bottom in the Main class

import java.util.Scanner    // Polynomial class   public class Polynomial {       private Term head;         public Polynomial() {           head = null;       }         public Term getHead() {           return head;       }         // Function to insert a new node in the list       public void insertNode(int coeff, int exp)       {           Term newNode = new Term(coeff, exp);           // If list is empty           if (head == null)               head = newNode;           else {               Term temp = head;               // traverse the list till the end               while (temp.next != null)                   temp = temp.next;               // add the node to the end               temp.next = newNode;           }       }         // Function to sort the list       public void sortList()       {           Term temp1 = head;           Term temp2;           if (head == null)               return;           while (temp1.next != null) {               temp2 = temp1.next;               while (temp2 != null) {                   // checking for descending order                   if (temp1.exp < temp2.exp) {                       int c = temp1.coeff;                       int e = temp1.exp;                       temp1.coeff = temp2.coeff;                       temp1.exp = temp2.exp;                       temp2.coeff = c;                       temp2.exp = e;                   }                   temp2 = temp2.next;               }               temp1 = temp1.next;           }       }         // Function to evaluate the polynomial       public int evaluate(int x)       {           int sum = 0;           Term temp = head;           while (temp != null) {               sum += temp.coeff * Math.pow(x, temp.exp);               temp = temp.next;           }           return sum;       }         // Function to print the polynomial       public void printPolynomial()       {           Term temp = head;           while (temp != null) {               if (temp.coeff > 0)                   System.out.print("+");               if (temp.coeff != 0) {                   System.out.print(temp.coeff);                   if (temp.exp > 0)                       System.out.print("x^" + temp.exp);               }               temp = temp.next;           }       }   }       // Term class   public class Term {       public int coeff;       public int exp;       public Term next;         public Term(int c, int e)       {           coeff = c;           exp = e;           next = null;       }   }     // Main class   public class Main {      public static void main(String[] args) {          // creating object of the Polynomial class          Polynomial poly = new Polynomial();                      // read input from file              Scanner scanner = new Scanner("lab2.txt");              while (scanner.hasNextLine()) {                  String line = scanner.nextLine();                  String[] values = line.split(" ");                  int coeff = Integer.parseInt(values[0]);                  int exp = Integer.parseInt(values[1]);                  poly.insertNode(coeff, exp);              }              scanner.close();                    // sorting the list          poly.sortList();          // print the polynomial          poly.printPolynomial();          int x = 3;          // evaluate the polynomial          int result = poly.evaluate(x);          System.out.println(" = " + result + " FOR X = " + x);      }  }   

Step by Step Solution

There are 3 Steps involved in it

Step: 1

The issue in the provided code lies in the Main class section where it reads input from a file Heres a breakdown of the problem and the solution Probl... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Document Format ( 2 attachments)

PDF file Icon
663dc38c682ba_962361.pdf

180 KBs PDF File

Word file Icon
663dc38c682ba_962361.docx

120 KBs Word File

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Auditing Cases An Interactive Learning Approach

Authors: Steven M Glover, Douglas F Prawitt

4th Edition

0132423502, 978-0132423502

More Books

Students also viewed these Operating System questions

Question

What is intrinsic motivation? (p. 257)

Answered: 1 week ago

Question

600 lb 20 0.5 ft 30 30 5 ft

Answered: 1 week ago