Reading input into an ArrayList
Hey guys I need help reading input into an ArrayList which I will use to construct my polynomials
any ideas on how to do this ??
My code is below
Code Java:
*/
package poly;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
*
*/
public class Poly {
private ArrayList<Integer> coefficients;
public Poly()
{
}
public Poly(ArrayList<Integer> coefficient) {
this.coefficients = coefficient;
}
public void show() {
for (int i = coefficients.size() - 1; i >= 0; i--) {
if(coefficients.get(i)>=0 && i!=coefficients.size()-1)
{
System.out.print("+");
}
System.out.print(coefficients.get(i));
if (i != 0) {
System.out.print("x");
System.out.print("^" + i);
System.out.print(" ");
}
}
System.out.println("");// prints new line
}
public Poly add(Poly otherPoly) {
//We are going to add the coefficients of the current object as well as the
//object that is being passed on to the method
ArrayList<Integer> sum = new ArrayList<Integer>();
//We are checking if the degree of coefficients of the current object and the
//object being passed into the method are the same
if (this.coefficients.size() == otherPoly.coefficients.size()) {
//Iterating over each element in the object.
for (int i = 0; i < coefficients.size(); i++) {
//Adding the coefficients of the currrent object and the correspondeing
//coefficients the object that is being passed.
sum.add(this.coefficients.get(i) + otherPoly.coefficients.get(i));
}
}
//Creating a new polynomial to pass the arraylist that has just been created.
Poly sumPoly = new Poly(sum);
//Returning the polynomial.
return (sumPoly);
}
public Poly subtract(Poly otherPoly) {
//We are going to add the coefficients of the current object as well as the
//object that is being passed on to the method
ArrayList<Integer> takeAway = new ArrayList<Integer>();
//We are checking if the degree of coefficients of the current object and the
//object being passed into the method are the same
if (this.coefficients.size() == otherPoly.coefficients.size()) {
//Iterating over each element in the object.
for (int i = 0; i < coefficients.size(); i++) {
//Adding the coefficients of the currrent object and the correspondeing
//coefficients the object that is being passed.
takeAway.add(this.coefficients.get(i) - otherPoly.coefficients.get(i));
}
}
//Creating a new polynomial to pass the arraylist that has just been created.
Poly takeAwayPoly = new Poly(takeAway);
//Returning the polynomial.
return (takeAwayPoly);
}
public void read()
{
Scanner in= new Scanner(System.in);
System.out.println("Please Enter the degree of the polynomial");
int degree=in.nextInt();
for(int i=0; i<=degree; i++)
{
System.out.println("Please enter the enter coefficient for the"+ i+"exponent");
}
int[] coefficientArray=new int[degree];
}
//MAIN CLASS BELOW
public static void main(String[] args) {
// int[] coefficient = {4, 3, 2};
ArrayList<Integer> coefficients = new ArrayList<Integer>();
coefficients.add(4);
coefficients.add(2);
coefficients.add(1);
coefficients.add(4);
Poly one = new Poly(coefficients);
ArrayList<Integer> coefficients_2 = new ArrayList<Integer>();
coefficients_2.add(5);
coefficients_2.add(3);
coefficients_2.add(1);
coefficients_2.add(5);
Poly two = new Poly(coefficients_2);
one.show();
two.show();
//Need to confirm if not printing the error mesage is okay.
Poly three = one.subtract(two);
three.show();
Poly four = one.add(two);
four.show();
}
}
Re: Reading input into an ArrayList
Please edit your post and wrap the code with code tags, and be sure the formatting is 'decent' at least. See the forum announcements page if you have questions on using tags.