Em chào mọi người. Em đang có đọc và làm một số code ví dụ java trong sách program development in java của tác giả Barbara Liskov với John Guttag. Em vẫn không hiểu lắm trong 2 phần chương 5 và chương 6 trong sách vì trong sách có hướng dẫn cách cài đặt 1 lớp Poly nhưng em vẫn chưa biết cách truyền giá trị và cách hiển thị như nào. Có ai đã làm rồi hay biết cách thì làm ơn khai sáng cho em với. Em cảm ơn ạ
Đây là link tải sách ạ: http://wavelino.coffeecup.com/pdf/Program_Development%20_in%20_Java.pdf
Đây là code mà em đã viết được ạ:
Poly:
import java.lang.Exception;
import java.util.Iterator;
public class Poly implements Cloneable {
//Overviews: Poly là đa thức bất biến với hệ số nguyên.
//A typical Poly is C0 + C1x + ...
private int[] trms;
private int deg;
//contructors
Poly() {
//Effects:
trms = new int[1];
deg = 0;
}
Poly(int c, int n) throws Exception {
if(n < 0)
throw new Exception("Poly(int, int) Contructors");
if(c == 0) {
trms = new int[1];
deg = 0;
return;
}
trms = new int[n + 1];
for(int i = 0; i < n; i++) trms[i] = 0;
trms[n] = c;
deg = n;
}
Poly (int n){
trms = new int[n + 1];
deg = n;
}
//method
public int degree(){
//Effects:
return deg;
}
public int coeff(int d){
//Effects:
if(d < 0 || d > deg) return 0;
else return trms[d];
}
public Poly sub(Poly q) throws NullPointerException{
//Effects:
return add(q.minus());
}
public Poly minus(){
Poly r = new Poly(deg);
for(int i=0; i<deg; i++)
r.trms[i] = -trms[i];
return r;
}
public Poly add(Poly q) throws NullPointerException {
Poly la, sm;
if(deg > q.deg) {
la = this;
sm = q;
}else{
la = q;
sm = this;
}
int newdeg = la.deg;
if(deg == q.deg)
for(int k = deg; k > 0; k--)
if(trms[k] + q.trms[k] != 0) break;
else newdeg++;
Poly r = new Poly(newdeg);
int i;
for(i = 0; i <= sm.deg && i <= newdeg; i++)
r.trms[i] = sm.trms[i] + la.trms[i];
for(int j = i; j <= newdeg; j++)
r.trms[j] = la.trms[j];
return r;
}
public Poly mul(Poly q) throws NullPointerException{
if((q.deg == 0 && q.trms[0] == 0) || deg == 0 && trms[0] == 0)
return new Poly();
Poly r = new Poly(deg + q.deg);
r.trms[deg + q.deg] = 0;
for(int i = 0; i <= deg; i++)
for(int j = 0; j <= q.deg; j++)
r.trms[i + j] = r.trms[i + j] + trms[i] * q.trms[j];
return r;
}
public boolean equals(Poly q) {
if(q == null || deg != q.deg) return false;
for(int i = 0; i <= deg; i++)
if(trms[i] != q.trms[i]) return false;
return true;
}
public boolean equals(Object z) {
if (!(z instanceof Poly)) return false;
return equals((Poly)z);
}
public boolean repOk() {
if(trms == null || deg != trms.length - 1 || trms.length == 0) return false;
if(deg == 0) return true;
return trms[deg] != 0;
}
public Iterator terms() {
return null;
}
// public static void main(String[] args) throws Exception {
// // Poly r = new Poly(5, 5);
// Poly r1 = new Poly(6 , 3);
// Poly r2 = new Poly(7 , 5);
// r1.add(r2);
// System.out.println("Poly = " + r1.add(r2));
// }
}
Pair:
import java.util.Vector;
public class Pair {
int coeff;
int exp;
private Vector trms;
Pair(int c, int n){
coeff = c;
exp = n;
}
public int coeff(int x){
for(int i = 0; i < trms.size(); i++) {
Pair p = (Pair) trms.get(i);
if(p.exp == x) return p.coeff;
}
return 0;
}
}
Comp
import java.util.Iterator;
import java.lang.*;
public class Comp {
public static Poly diff(Poly p) throws Exception {
//Effects:
Poly q = new Poly();
Iterator g = p.terms();
while(g.hasNext()) {
int exp = ((Integer) g.next()).intValue();
if(exp == 0) continue;
q = q.add(new Poly(exp * p.coeff(exp), exp - 1));
}
return q;
}
// public static void printPrimes(int m){
// Iterator g = Num.allPrimes();
// while(true){
// Object p = g.next();
// if(p > m) return;
// System.out.println("The next prime is: " + p.toString());
// }
// }
// public static int max (Iterator g) throws Exception {
// NullPointerException{
// try{
// int m = ((Integer) g.next()).intValue();
// while (g.hasNext()) {
// int x = (int) g.next();
// if(m < x) m = x;
// }
// return m;
// }catch (NullPointerException e){
// throw new Exception("Comp.max");
// }
// }
// }
}
main:
public class main {
public static void main(String[] args) throws Exception {
// Poly r = new Poly(5, 5);
Poly r1 = new Poly(6 ,3);
// System.out.println("Poly = " + );
}
}