0% found this document useful (0 votes)
18 views11 pages

Lập trình Đa thức và Quản lý Sinh viên

The document describes a DaThuc (polynomial) class with methods to calculate the value of a polynomial for a given x value, add two polynomials together, multiply a polynomial by a scalar, solve a polynomial equation, take the derivative of a polynomial, and convert a polynomial to a string. It then provides examples of using the class to add, multiply, take derivatives of, solve equations for, and read polynomials from a file.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views11 pages

Lập trình Đa thức và Quản lý Sinh viên

The document describes a DaThuc (polynomial) class with methods to calculate the value of a polynomial for a given x value, add two polynomials together, multiply a polynomial by a scalar, solve a polynomial equation, take the derivative of a polynomial, and convert a polynomial to a string. It then provides examples of using the class to add, multiply, take derivatives of, solve equations for, and read polynomials from a file.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

ĐA THỨC

public class DaThuc


{
private double a,b,c;
public DaThuc(){a=b=c=0;}
public DaThuc(double x, double y, double z){a=x;b=y;c=z;}
public double GiaTriDaThuc(double x)
{
return a*x*x+b*x+c;
}
public DaThuc CongDaThuc(DaThuc f)
{
return new DaThuc(a+f.a,b+f.b,c+f.c);
}
public static DaThuc CongDaThuc (DaThuc f, DaThuc g)
{
return new DaThuc(f.a+g.a,f.b+g.b,f.c+g.c);
}
public DaThuc NhanSoThuc (double k)
{
return new DaThuc(a*k,b*k,c*k);
}
public String[] GiaiPhuongTrinh()
{
if (a==0)
if(b==0)
if(c==0) {String[] ketqua=new String[1];
ketqua[0]="Phuong trinh vo so nghiem";
return ketqua;}
else {String[] ketqua=new String[1];
ketqua[0]="Phuong trinh vo nghiem";
return ketqua;}
else {String[] ketqua=new String[1];
ketqua[0]=[Link](-c/b);
return ketqua;}
else
{
double delta=b*b-4*a*c;
if (delta<0)
{String[] ketqua=new String[1];
ketqua[0]="Phuong trinh vo nghiem";
return ketqua;}
else {String[] ketqua=new String[2];
ketqua[0]="x1= "+[Link]((-b+[Link](delta))/2/a);
ketqua[1]="x2= "+[Link]((-[Link](delta))/2/a);
return ketqua;}
}
}
public DaThuc DaoHam()
{
return new DaThuc(0,2*a,b);
}
public static DaThuc DaoHam(DaThuc f)
{
return new DaThuc(0,2*f.a,f.b);
}
public String toString()
{
String hs1=a!=0?(a+"x "):"";
String hs2=(b>0?"+":"")+(b!=0?(b+"x"):"");
String hs3=(c>0?"+":"")+(c!=0?(c+""):"");
return a==0&&b==0&&c==0?"0":hs1+hs2+hs3;
}
}
//MAIN
//Cho da thuc f(x)=2x2+4 -9
DaThuc f= new DaThuc(2,4,-9);
[Link]("f(x)= "+f);
[Link]("f(0) = "+[Link](0));
//Gan lai da thuc f va tao da thuc g nhu sau
//f(x)= x2 -4
//g(x)= x2 + 5x -6
f= new DaThuc(1,0,-4);
DaThuc g= new DaThuc(1,5,-6);
[Link]("f(x)= "+f);
[Link]("g(x)= "+g);
double f0= [Link](0);
//Tinh gia tri f(0) va g(-2)
[Link]("f(0)= "+f0);
[Link]("g(-2)= "+[Link](-2));
//In ra dao ham cua g
[Link]("g'(x)= "+[Link]());
//In ra gia tri g'(0)
[Link]("g'(0)= "+([Link]()).GiaTriDaThuc(0));

//Tinh h(x)= f(x)+g(x)


DaThuc h= [Link](f, g);
[Link]("f(x)+ g(x)= "+h);
//Tinh f(g(-1)) v g(f(2))
[Link]("f(g(-1)) = "+[Link]([Link](-1)));
[Link]("g(f(2)) = "+[Link]([Link](2)));
//Tinh g(x)-f(x)
DaThuc m= [Link](-1);
DaThuc n = [Link](g, m);
[Link]("g(x)-f(x)= "+n);
//4f(x)-2g(x)
m= [Link](4);
n=[Link](-2);
[Link]("4f(x)-2g(x)= "+[Link](m,n));
//Giai PT f(x)=0
String[] kq=[Link]();
for (int i=0;i<[Link];i++)
[Link](kq[i]);
//Giai PT g(x)=0
kq=[Link]();
for (int i=0;i<[Link];i++)
[Link](kq[i]);
//Tim giao diem cua y=3 va y=x
kq=([Link](new DaThuc(0,0,3),(new DaThuc(0,1,0).NhanSoThuc(-
1)))).GiaiPhuongTrinh();
for (int i=0;i<[Link];i++)
[Link](kq[i]);
//Duong thang y=2x+3 cat f(x) tai dau?
DaThuc y= new DaThuc(0,2,3);
kq=([Link](f,[Link](-1))).GiaiPhuongTrinh();
for (int i=0;i<[Link];i++)
[Link](kq[i]);
//Doc va giai PTB2 trong tap tin
File file = new File("D:\\[Link]");
try
{
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
while ((st = [Link]()) != null)
{
String[] tokens = [Link](",");
DaThuc k= new
DaThuc([Link](tokens[0]),[Link](tokens[1]),[Link](tokens[2]));
String[] res=[Link]();
[Link]("Giai PT "+k+" = 0");
for (int i=0;i<[Link];i++) [Link](res[i]+" ");
[Link]();
}
}
catch (Exception e) {}

SINHVIEN

CLASS SINHVIEN
package demo;

public class SinhVien


{
private String MSSV;
private String HoLot;
private String Ten;
private String Nganh;
private String QueQuan;
private int Ngay,Thang,Nam;
public SinhVien(){}
public SinhVien(String mssv,String holot, String ten, String nganh, String quequan, int ngay, int thang,
int nam)
{
MSSV=mssv; HoLot=holot;Ten=ten;Nganh=nganh;QueQuan=quequan;
Ngay=ngay; Thang=thang; Nam=nam;
}
public String toString()
{
String kq=MSSV+", "+HoLot+" "+Ten+", "+Ngay+"/"
+Thang+"/"+Nam+", "+Nganh+", "+QueQuan;
return kq;
}
}

/*
static final int MAX=4000;
static SinhVien dssv[]=new SinhVien[MAX];
static int slsv=0;
public static void main(String[] args) {
slsv=[Link]("D:\\[Link]",dssv);
for (int i=0;i<slsv;i++)
[Link](dssv[i]);
[Link](slsv);
*/

//CLASS QLY SINHVEIN


package demo;

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
class TenComparator implements Comparator <SV>
{
public int compare(SV a, SV b)
{
return -([Link]([Link]));
}
}
class QueQuanComparator implements Comparator <SV>
{
public int compare(SV a, SV b)
{
return ([Link]([Link]));
}
}
class SV
{
public String MSSV;
public String HoLot;
public String Ten;
public String Nganh;
public String QueQuan;
public int Ngay,Thang,Nam;
public SV(){}
public SV(String mssv,String holot, String ten, String nganh, String quequan, int ngay, int thang, int
nam)
{
MSSV=mssv; HoLot=holot;Ten=ten;Nganh=nganh;QueQuan=quequan;
Ngay=ngay; Thang=thang; Nam=nam;
}
//public String getTen(){return Ten;}
public String toString()
{
String kq=MSSV+", "+HoLot+" "+Ten+", "+Ngay+"/"
+Thang+"/"+Nam+", "+Nganh+", "+QueQuan;
return kq;
}
}

class ThangSinhComparator implements Comparator <SV>


{
public int compare(SV a, SV b)
{
return -([Link]);
}
}
class NgaySinhComparator implements Comparator <SV>
{
public int compare(SV a, SV b)
{
return ([Link])!=0?([Link]):
(([Link])!=0?([Link]):[Link]);
}
}
public class QuanLySV {
static ArrayList<SV> dssv=new ArrayList<SV>();
static int ReadDataToArrayList(String fname, ArrayList<SV> ds)
{
int soluong=0;
File f = new File(fname);
try
{
BufferedReader br = new BufferedReader(new FileReader(f));
String st;
while ((st = [Link]()) != null)
{
String[] tokens = [Link](",");
soluong++;
FillDataToArrayList(tokens,ds);
}
}
catch (Exception e) {[Link]("Loi tap tin");}
return soluong;
}

static void FillDataToArrayList(String[] s, ArrayList<SV> ds)


{
SV sv = new SV();
[Link]=s[0];
[Link]=s[1];
[Link]=s[2];
String[] tokens = s[3].split("-");
[Link]=[Link](tokens[0]);
[Link]=[Link](tokens[1]);
[Link]=[Link](tokens[2]);
[Link]=s[4];
[Link]=s[5];
[Link](sv);
}

public static void main(String[] args) {


int slsv=ReadDataToArrayList("D:\\[Link]",dssv);
//for (int i=0;i<slsv;i++)
// [Link]([Link](i));
[Link](slsv);
//Sap xep theo ngay sinh (nam-thang-ngay
NgaySinhComparator cp= new NgaySinhComparator();

[Link](dssv,cp);
//Dao nguoc danh sach
[Link](dssv);
for (int i=0;i<slsv;i++)
[Link]([Link](i));
//Tim Kiem sinh vien ten Thanh
SV kq = [Link]()
.filter(sv -> "Thanh".equals([Link]))
.findAny()
.orElse(null);
[Link]("===>"+kq);
//Tim Kiem sinh vien co que quan Thanh Hoa
kq = [Link]()
.filter(sv -> "Thanh Hoa".equals([Link]))
.findAny()
.orElse(null);
[Link]("===>"+kq);
//Tim Kiem sinh vien sinh thang 4 nam 2001
kq = [Link]()
.filter(sv -> [Link]==4 && [Link]==2000)
.findAny()
.orElse(null);
[Link]("===>"+kq);
//Sap xep bang cach dung cu phap Lambda 1.8
[Link]((SV sv1, SV sv2) -> {
return [Link]([Link]);
});
//Duyet danh sach theo cu phap Lambda
//de in ra cac sinh vien sinh ngay 1 thang 4
[Link](
sv ->
{
if ([Link]==1&&[Link]==4)
[Link](sv);}
);
}
}

//MY LIBRARY
package demo;

import [Link].*;
import [Link];
class MyLibrary {
static void FillData(String[] s, SinhVien[] ds, int pos)
{
String mssv=s[0];
String holot=s[1];
String ten=s[2];
String[] tokens = s[3].split("-");
int ngay=[Link](tokens[0]);
int thang=[Link](tokens[1]);
int nam=[Link](tokens[2]);
String nganh=s[4];
String quequan=s[5];
ds[pos]= new SinhVien(mssv,holot,ten,nganh,quequan,ngay,thang,nam);
}

static int ReadData(String fname, SinhVien[] ds)


{
int soluong=0;
File f = new File(fname);
try
{
BufferedReader br = new BufferedReader(new FileReader(f));
String st;
while ((st = [Link]()) != null)
{
String[] tokens = [Link](",");
soluong++;
FillData(tokens,ds,soluong);
}
}
catch (Exception e) {[Link]("Loi tap tin");}
return soluong;
}
}

//MAIN
package demo;

public class Main


{
static final int MAX=4000;
static SinhVien dssv[]=new SinhVien[MAX];
static int slsv=0;
public static void main(String[] args) {
slsv=[Link]("D:\\[Link]",dssv);
for (int i=0;i<slsv;i++)
[Link](dssv[i]);
[Link](slsv);
}

PHAN SO
package demo;

public class PhanSo {


private int tu, mau;

public PhanSo(int tu, int mau) {


[Link] = tu;
[Link] = mau;
}

public int getTu() {


return tu;
}
public void setTu(int tu) {
[Link] = tu;
}

public int getMau() {


return mau;
}

public void setMau(int mau) {


[Link] = mau;
}

public PhanSo() {
tu=0;
mau=1;
}
public PhanSo(PhanSo ps) {
tu=[Link];
mau=[Link];
}
public int timUSCLN(int a, int b) {
while (a != b) {
if (a > b) {
a -= b;
} else {
b -= a;
}
}
return a;
}
public PhanSo RutGonPS()
{
int i = timUSCLN(tu, mau);
[Link]([Link]() / i);
[Link]([Link]() / i);
PhanSo p= new PhanSo(tu,mau);
return p;
}

public PhanSo congPhanSo(PhanSo p) {


int tuSo= tu*[Link]+mau*[Link];
int mauSo= mau*[Link];

PhanSo tong= new PhanSo(tuSo,mauSo);


return [Link]();

}
public PhanSo nhanPhanSo(PhanSo p) {
int tuSo= tu*[Link];
int mauSo= mau*[Link];
PhanSo tich= new PhanSo(tuSo,mauSo);
return [Link]();
}

public PhanSo nghichDaoPS() {

PhanSo dao= new PhanSo(mau,tu);


return dao;
}
public double GiaTriPS() {
return (double) [Link]/[Link];
}

public PhanSo nhanVoiSo(int n) {


PhanSo p= new PhanSo([Link]*n,[Link]);
return [Link]();
}
@Override
public String toString() {
return "PhanSo " + tu + "/" + mau;
}

//MAIN
public class Test {
public static void main(String[] args) {

PhanSo A= new PhanSo(5,6);


PhanSo B= new PhanSo(12, 36);
[Link](A);
[Link]("Tong cua 2 phan so A va B: "+ [Link](B));
[Link]("Ket qua cua phep chia phan so A voi phan so B:
"+[Link](B).nghichDaoPS());
[Link]("Phan so toi gian cua phan so B: "+ [Link]());
// PhanSo kq;
// kq=[Link](5).congPhanSo([Link](10));
// [Link]("Ket qua cua 5*A+10*B o dang toi gian: "+kq);
// PhanSo kq2;
// PhanSo AB,BA;
// AB= [Link]([Link]());
// [Link](AB);
// BA= [Link]([Link]());
// [Link](BA);
// kq2= [Link](2).congPhanSo([Link](-4));
// [Link]("Ket qua cua 2(A/B)-4(B/A): "+kq2);
ArrayList<PhanSo> L= new ArrayList<PhanSo>();
[Link](new PhanSo(1,1));
[Link](new PhanSo(2,1));
[Link](new PhanSo(0,1));
[Link](new PhanSo(1,3));
[Link](new PhanSo(1,4));
[Link](new PhanSo(4,1));
[Link](new PhanSo(4,3));

[Link]("Tong cac cap phan so trong danh sach: ");


for(int i=0; i<[Link]();i++)
{
for(int j=0;j<[Link]();j++)
{
if(i!=j)
{
[Link]([Link](i).congPhanSo([Link](j)));
}
}
PhanComparator ps= new PhanComparator();
PhanSo min=[Link](0);
for (PhanSo phanSo : L) {
if([Link](min, phanSo)>0)
min=phanSo;

}
[Link]("Phan So nho nhat trong danh sach: "+ min);
PhanSo max=[Link](0);
for (PhanSo phanSo : L) {
if([Link](max, phanSo)<0)
max= phanSo;
}
[Link]("Phan So lon nhat trong danh sach: "+ max);
[Link](L,ps);
for (PhanSo phanSo : L) {
[Link](phanSo);
}
}

}
}

Common questions

Powered by AI

Arrays provide fixed-size data collection, which is efficient for memory management but lacks flexibility in resizing. The "SinhVien" objects were initially handled in arrays, requiring predefinition of maximum size. ArrayLists, used in the "QuanLySV" example, offer dynamic resizing and ease of use with additional functionality such as sorting and manipulating elements, as seen in complex sorting operations. ArrayLists facilitate more complex operations like filtering and stream processing with functional paradigms, thus are more suitable for applications with varying data sizes and complex data manipulations .

The "SinhVien" class creates a structured way to store and manage student data by encapsulating fields like student ID, name, major, and birthdate, which can be individually accessed or modified. It provides methods such as constructors for creating new student objects, setters, and getters for attributes, along with a method for formatted output, thus facilitating organized data management and retrieval .

Lambda expressions simplify sorting operations by providing a concise way to implement inline comparisons without the need for separate comparator classes. In the "QuanLySV" program, they are used to sort "SV" objects directly in the "dssv" ArrayList by passing a lambda expression to the sort method, which defines the basis of comparison, such as student names, reducing boilerplate code and increasing the clarity of the sorting logic .

The algorithm reads birthdate strings from a file, splits them into day, month, and year components using delimiters, and parses these into integers to store in a "SinhVien" object. The program attempts to read line-by-line, parsing and handling any IOException (e.g., file not found) gracefully by reporting "Loi tap tin" to the console, which is crucial for maintaining user awareness and ensuring program robustness against unexpected I/O issues or incorrect data formatting .

Creating an ArrayList of "PhanSo" objects allows for dynamic collection handling, enabling storage and manipulation of multiple fractions. Operations such as addition among these objects are performed in loops, utilizing iterator features to evaluate all combinations, demonstrating the flexibility of ArrayLists for batch processing tasks. This practice enables bulk mathematical operations, easing the complexity of operations through iteration and functional programming concepts .

In the "PhanSo" class, reducing a fraction is done using the "RutGonPS" method, which utilizes the greatest common divisor method "timUSCLN" to find the largest number that divides both the numerator and the denominator. The numerator and the denominator are then divided by this number to simplify the fraction. The purpose is to convert the fraction into its simplest form, which makes operations with fractions more efficient and the outputs more readable .

Custom comparators like "TenComparator", "QueQuanComparator", "ThangSinhComparator", and "NgaySinhComparator" in the "QuanLySV" program allow sorting of student objects based on different criteria such as name, place of origin, month of birth, and full birthdate. They implement the "Comparator" interface by overriding the "compare" method to define how two student objects should be ordered based on specific attributes, enabling flexible and customizable sorting within the student management system .

The instance method "CongDaThuc" in "DaThuc" class adds the current object's coefficients with another DaThuc object, returning a new DaThuc with summed coefficients. The static method "CongDaThuc" takes two DaThuc objects as parameters and returns a new DaThuc object with their respective coefficients added together, without relying on a specific instance .

The "DaoHam" method in the "DaThuc" class computes the derivative of a polynomial by reducing the power of each term by one and multiplying the coefficient by the original power. For instance, for a polynomial 2x² + 3x + 4, the derivative is calculated as 2*2x + 3 = 4x + 3. The importance of calculating the derivative lies in applications such as finding slope and rate of change, which are critical in various domains of science and engineering .

The "DaThuc" class solves a quadratic equation using the method "GiaiPhuongTrinh" which considers the coefficients 'a', 'b', and 'c'. If 'a' is zero, it checks if 'b' is also zero. When both 'a' and 'b' are zeros, it determines infinite solutions if 'c' is zero or no solution if 'c' is nonzero. If 'b' is nonzero, the solution is simply -c/b. When 'a' is nonzero, it calculates the discriminant delta ('b^2 - 4ac'). If delta is less than zero, there are no real solutions. Otherwise, it calculates two solutions using the quadratic formula ((-b±sqrt(delta))/2a).

You might also like