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

Write a Matlab program that does the following: Note well, however, that the fil

ID: 3598470 • Letter: W

Question

Write a Matlab program that does the following:

Note well, however, that the file might have a different number of lines and a different number of numbers on each line.

mat2.txt look like this

10, 5

75 82 48 49 95
74 69 44 95 54
39 31 64 34 13
65 95 70 58 14
17 3 75 22 25
70 43 27 75 84
3 38 67 25 25
27 76 65 50 81
4 79 16 69 24
9 18 11 89 92

• Prompt the user for an input file name and read the user's input to a string variable. Do not assume that the file name will be mat2.txt. (In other words, your script should run fine if the
user enters a different file name that has data in the same format.)

• Open the file name the user has entered for reading and read the first line to see how many lines and columns it has. Use that information to set up a loop to read the rest of the input file
into an array. Close the input file.

• Use a separate loop to print the array to the console in a neat table. Noting that none of the numbers in the input file is larger than 99, choose an output format that results in a neat table with no more than 4 spaces before each number.*

Do not assume that the input file has a fixed number of rows or columns. Assume that that information is on the first line of the file and read it. Your program may be tested with a different file with a different number of rows and columns.
* Note that this will require some thought since the number of columns is can change and your code must adjust for that at runtime. There are several different ways to do this. Among them are creating the format string dynamically as in 2d or using a nested loop to print one column at a time on an output line

Explanation / Answer

package com;
import java.util.ArrayList;

public class ArrayWithExponentAsIndexPolynomial implements PolynomialInterface

{

int polynomial[];

int highExp;

ArrayWithExponentAsIndexPolynomial()

{

polynomial=new int[200];

}

ArrayWithExponentAsIndexPolynomial(String pol)

{

polynomial=new int[200];

highExp=0;

int co=0;//Coefficient

int exp=0;//exponent

//Convert the polynomial string into linked list of polynomial terms

for(int i=0;i<pol.length();i++)

{

co=0;

exp=0;

//Find coefficient

while(pol.charAt(i)!='x' && pol.charAt(i)!='X' )

{

if(pol.charAt(i)=='-')

{

i++;

while(i<pol.length())

{

if(pol.charAt(i)!='x' && pol.charAt(i)!='X' )

{

String sub=pol.substring(i,i+1);

co=co*10+Integer.parseInt(sub);

}

else

break;

i++;

}

co=co*-1;

}

else if (pol.charAt(i)=='+')

{

i++;

}

else

{

String sub=pol.substring(i,i+1);

co=co*10+Integer.parseInt(sub);

i++;

}

if(i>=pol.length())

break;

}

i++;//skip x

if(i==pol.length())

{

if(pol.charAt(i-1)=='x' || pol.charAt(i-1)=='X')

exp=1;

}

i++;//skip ^

if(i<pol.length())

while(pol.charAt(i)!='-' && pol.charAt(i)!='+' )

{

String sub=pol.substring(i,i+1);

exp=exp*10+Integer.parseInt(sub);

i++;

if(i>=pol.length())

break;

}

if(highExp<exp)

highExp=exp;

addATerm(exp,co);

i--;

}

}

// stores the coefficient at index(exp)

void addATerm(int exp,int co)

{

// store the coefficient at index(exp)

polynomial[exp]=co;

}

int getHigh()

{

return highExp;

}

@Override

//Adds two polynomials and returns the resultant polynomial

public PolynomialInterface add(PolynomialInterface other)

{

int high;

ArrayWithExponentAsIndexPolynomial temp=new ArrayWithExponentAsIndexPolynomial();

ArrayWithExponentAsIndexPolynomial otherPoly=(ArrayWithExponentAsIndexPolynomial)other;

if(this.getHigh()<otherPoly.getHigh())

{

high=otherPoly.getHigh();

temp.highExp=otherPoly.getHigh();

}

else

{

high=this.getHigh();

temp.highExp=this.getHigh();

}

for(int i=0;i<=high;i++)

{

if(this.polynomial[i]!=0 && otherPoly.polynomial[i]!=0)

{

temp.polynomial[i]=this.polynomial[i]+otherPoly.polynomial[i];

}

else if (this.polynomial[i]==0 && otherPoly.polynomial[i]!=0)

{

temp.polynomial[i]=otherPoly.polynomial[i];

}

else if (this.polynomial[i]!=0 && otherPoly.polynomial[i]==0)

{

temp.polynomial[i]=this.polynomial[i];

}

}

return temp;

}

@Override

//Substracts one polynomial from another and returns the resultant polynomial

public PolynomialInterface subtract(PolynomialInterface other)

{

int high;

ArrayWithExponentAsIndexPolynomial temp=new ArrayWithExponentAsIndexPolynomial();

ArrayWithExponentAsIndexPolynomial otherPoly=(ArrayWithExponentAsIndexPolynomial)other;

if(this.getHigh()<otherPoly.getHigh())

{

high=otherPoly.getHigh();

temp.highExp=otherPoly.getHigh();

}

else

{

high=this.getHigh();

temp.highExp=this.getHigh();

}

for(int i=0;i<=high;i++)

{

if(this.polynomial[i]!=0 && otherPoly.polynomial[i]!=0)

{

temp.polynomial[i]=this.polynomial[i]-otherPoly.polynomial[i];

}

else if (this.polynomial[i]==0 && otherPoly.polynomial[i]!=0)

{

temp.polynomial[i]=0-otherPoly.polynomial[i];

}

else if (this.polynomial[i]!=0 && otherPoly.polynomial[i]==0)

{

temp.polynomial[i]=this.polynomial[i];

}

}

return temp;

}

public String toString()

{

String poly="";

//Convert the linked list into polynomial string

for(int i=this.getHigh();i>=0;i--)

{

if(polynomial[i]!=0)

{

if(i==1)

{

if(polynomial[i]<0)

poly=poly+"-"+polynomial[i]*-1+"x";

else

poly=poly+polynomial[i]+"x";

}

else if(i!=0)

{

if(polynomial[i]<0)

poly=poly+"-"+polynomial[i]*-1+"x^"+i;

else

{

if(i!=this.getHigh())

poly=poly+"+"+polynomial[i]+"x^"+i;

else

poly=poly+polynomial[i]+"x^"+i;

}

}

else

{

if(polynomial[i]<0)

poly=poly+"-"+polynomial[i]*-1;

else

poly=poly+"+"+polynomial[i];

}

}

}

return poly;

}


}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote