ArrayList Class (Correction>> ArrList)
import java.util.NoSuchElementException;
ArrList, not public class ArrayList<E> { //ArrayList.
private E a[];
private int size;
public ArrayList() {
a = (E[]) new Object[1];
size = 0;
}
public E peek(int k) {
if (size == 0 || k >= size)
throw new NoSuchElementException();
return a[k];
}
public boolean isEmpty() {
if (size == 0)
return true;
return false;
}
public void insertLast(E newItem) {
if (size == a.length)
resize(2 * a.length);
a[size++] = newItem;
}
private void resize(int newSize) {
E[] t = (E[]) new Object[newSize];
for (int i = 0; i < size; i++)
t[i] = a[i];
a = t;
}
public void insert(E newItem, int k) {
if (size == a.length)
resize(2 * a.length);
for (int i = size - 1; i >= k; i--)
a[i + 1] = a[i];
a[k] = newItem;
size++;
}
public void insert(E newItem) {
insertLast(newItem);
}
public E delete(int k) {
if (isEmpty())
throw new NoSuchElementException();
E item = a[k];
for (int i = k; i < size; i++)
a[i] = a[i + 1];
size--;
if (size > 0 && size <= a.length / 4)
resize(a.length / 2);
return item;
}
public void print() {
for (int i = 0; i < a.length; i++) {
if (i < size)
System.out.printf(a[i] + "\t");
else
System.out.printf("null\t");
}
System.out.println();
}
}
You must inherit the above code to implement ArrListPoly.
Where intk
: Highest order / int[] arr
: I don't know how to implement method hapPoly, a coefficient of polynomial.
The ArrListPoly class and main function we have created so far are as follows.
import java.util.NoSuchElementException;
class ArrListPoly extends ArrList{
int highestDegree;
private int k;
private int[] arr=new int[20];
public void setPoly(int highestDegree, int[] arr) {
int i;
for(i=arr.length-1;i>=0;i--) {
super.insertLast(arr[i]);
}
this.highestDegree=highestDegree;
}
public int getHighestDegree() {
return this.highestDegree;
}
void setPoly(int k){
this.k=k;
for(int i=0;i<=k;i++) { this.arr[i] = 0; }
}
public int getDegree() { return this.k; }
public int getArr(int i) { return this.arr[i]; }
public int setArr(int i, int arr) { return this.arr[i]=arr; }
}
public class ArrListPolyTest {
public static void main(String[] args) {
ArrListPoly p1 = new ArrListPoly();
int[] poly1 = { 2, 8, -4, 9, 1 };
int p1highestDegree = poly1.length - 1;
p1.setPoly(p1highestDegree, poly1);
printPolyEq(p1, 1);
ArrListPoly p2 = new ArrListPoly();
int[] poly2 = { 6, 2, 9 };
int p2highestDegree = poly2.length - 1;
p2.setPoly(p2highestDegree, poly2);
printPolyEq(p2, 3);
ArrListPolyp3 = p1.hapPoly(p2); // Blocked in its implementation.
printPolyEq(p3, 1);
}
public static void printPolyEq(ArrListPoly p, int nTabs) {
for (int i = 0; i < nTabs; i++)
System.out.printf("\t");
for (int i = p.getHighestDegree(); i >= 0; i--)
System.out.printf("%+d x%d\t", p.peek(i), i);
System.out.println();
}
}
ArrList is correct, not ArrayList. ArrList is also correct in inheritance.
"intk: Highest order / int[]arr: Coefficients of polynomials" is used in the method setPoly(k,arr).
I want to store polynomials in setPoly, add p1 and p2 to order in HapPoly, and store them in new polynomials p3, but I don't know what to doㅠ<
java
I think it's a question that I'm asking because I don't have a good idea, so I'll give you a hint.
To produce a result of calculating two lists from hapPoly()
, you must have access to the list ArrList.a
with the actual values of p1
and p2
in the hapPoly()
method. (2)You must also have access to highestDegree
.
int highestDegree;
public ArrListPoly hapPoly(ArrListPoly p2) {
System.out.println(this.highestDegree);
System.out.println(p2.highestDegree);
}
(2) is already possible, so if you look at (1) again:
public class ArrList<E> {
private E a[];
private int size;
...
public int getSize() {
return this.size;
}
}
public ArrListPoly hapPoly(ArrListPoly p2) {
for (int i = 0; i < super.getSize(); ++i) {
int ele = (Integer) super.peek(i);
System.out.println("index: " + i + ", value: " + ele);
}
for (int i = 0; i < p2.getSize(); ++i) {
int ele = (Integer) p2.peek(i);
System.out.println("index: " + i + ", value: " + ele);
}
// Do something with p1(super) and a of p2
// Create a new list, assign the desired value, and return it
}
This is how ArrList
must provide the method. In this example, only the method getSize()
was added, and then ArrList.peak()
was used. Other than this, there are ways to return ArrList.a
directly or provide an interpreter, but the gist is that hapPoly
should be able to access the value of ArrList.a
.
You can then support, stir-fry, and return the values.
© 2024 OneMinuteCode. All rights reserved.