Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Wednesday, November 30, 2016

Cài đặt thuật toán Dijskstra bằng java

Bố cuc:
1. Lớp Bài toán: Cài đặt thuật toán Dijstra
2.Lớp test

1 .Lớp cài đặt thuật toán Dijkstra
BaiToan.java
package baocaovantruhoc_nhom13;

/**
 *
 * @TranVanLinh
 */
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

public class BaiToan
{
    int soDinh;//so dinh cua do thi
    int G[][];//ma tran trong so
    int diemDau;//diem dau
    int doDai[];//do dai toi dinh i
    int daXet[];// danh dau la dinh co duong di la ngan nhat
    int dinhTruoc[];//luu vet dinh truoc
    int oo = 0;//gia tri vo cung
    public BaiToan()
    {
        soDinh = 0;
        G = null;
        doDai = null;
        daXet=null;
        dinhTruoc = null;
        diemDau = 0;
        oo = 0;
    }
    //Doc du lieu
    public boolean getDuLieuTuFile(String tenfile)
    {
        String chuoifile[]=tenfile.split(".");
        String path="G://"+tenfile;
        try
        {
            File file=new File(path);
            if(!file.exists())
            {
                return false;
            }
            FileInputStream input = new FileInputStream(path);
            InputStreamReader istream=new InputStreamReader(input);
            BufferedReader reader=new BufferedReader(istream);
            String sc;
            //doc so dinh cua do thi , dinh bat dau , dinh dich
            sc = reader.readLine();
            String temp[] = sc.split(" ");
            soDinh = Integer.parseInt(temp[0]);
            diemDau = Integer.parseInt(temp[1]);
            G=new int[soDinh][soDinh];
            int row=0;
            //doc matran do thi
            while ((sc = reader.readLine()) != null)
            {
                temp = sc.split(" ");
                for (int col = 0; col < soDinh; col++)
                {
                     G[row][col] = Integer.parseInt(temp[col]);
                }
                row++;
            }
          
            //dong file
            reader.close();
            istream.close();
            input.close();
        }
        catch(FileNotFoundException ex)
        {
            System.err.println("LOi file");
        }
        catch (IOException ex)
        {
                System.err.println("Ngoai le xay ra.!");
        }
        return true;
    }
    public void thuatToan_Dijkstra()
    {
        oo=999999999;
        //Gan trong so cho cac canh khong co duong di la vong cung
        for (int i = 0; i < soDinh; i++)
        {
            for (int j = 0; j < soDinh; j++)
            {
                if (i != j && G[i][j] == 0)
                {
                    G[i][j] = oo;
                }
            }
        }
        diemDau--;
        doDai= new int[soDinh];
        daXet=new int[soDinh];
        dinhTruoc=new int[soDinh];
        for (int i = 0; i < soDinh; i++)
        {
            doDai[i] = oo;                   // khoi tao do dai tu a toi moi dinh la vo cung
            daXet[i] = 0;                       // danh sach cac diem da xet
            dinhTruoc[i] = diemDau;                   // dat diem bat dau cua moi diem la a
        }
        int i=0;
         //Khoi tao d(diemDau,diemDau)=0;
        doDai[diemDau] = 0;
        for(int dinh=0;dinh<soDinh;dinh++)
        {
               for (i = 0; i < soDinh; i++)   // tim mot diem chua xet ma co duong di < vo cung
               {
                   if (daXet[i] != 1 && doDai[i] < oo)
                   {
                       break;
                   }
               }
               if(i==soDinh)//khong co dinh nao thoa man
               {
                   break;
               }
               for (int j = 0; j < soDinh; j++)// tim dinh ma co do dai la nho nhat
               {  
                   if (daXet[j]!=1 && doDai[i] > doDai[j])
                   {
                       i = j;
                   }
               }
               daXet[i] = 1;    // cho i vao danh sach xet roi
         
               for (int j = 0; j < soDinh; j++) // tinh lai do dai cua cac diem chua xet
               {  
                   if (daXet[j]!=1 && doDai[i] + G[i][j] < doDai[j])
                   {
                       doDai[j] = doDai[i] + G[i][j];   // thay doi do dai cua d[i,j]
                       dinhTruoc[j] = i;                       // danh dau diem truoc j la i
                   }
               }
        }
    }
    public  void inDuongDi()
    {
        for(int dinh=0;dinh<soDinh;dinh++)
        if(dinh!=diemDau)
        {
             //in duong di
            if(doDai[dinh] < oo)
            {
                System.out.print("\t\tĐộ dài đường đi từ đỉnh " + ((char)(diemDau+65)) + " đến đỉnh " +  ((char)(dinh+65)) + " là: " + doDai[dinh]+"\t----");
                int mang[] = new int[soDinh];
                int dem = 0;
                int i=dinh;
                while (i != diemDau)
                {
                    mang[dem++] = i;
                    i = dinhTruoc[i];
                }
                System.out.print("\tChi tiết: " +  ((char)(diemDau+65)));
                for (int k = dem - 1; k >= 0; k--) {
                    System.out.print("-->" + (char)(mang[k]+65));
                }
            }
            else
            {
                System.out.println("\t\tKhông có đường đi từ đỉnh "+( ((char)(diemDau+65)))+" đến đỉnh " + ( ((char)(dinh+65))));
            }
            System.out.println("\n");
        }
    }
    public  void inDuong2Di()
    {
        System.out.println("Nhap dinh dich: ");
        Scanner sc=new Scanner(System.in);
        int diemCuoi=sc.nextInt();
      
     
             //in duong di
            if(doDai[diemCuoi] < oo)
            {
                System.out.print("\t\tĐộ dài đường đi từ đỉnh " + ((char)(diemDau+65)) + " đến đỉnh " +  ((char)(diemCuoi+65)) + " là: " + doDai[diemCuoi]+"\t----");
                int mang[] = new int[soDinh];
                int dem = 0;
                int i=diemCuoi;
                while (i != diemDau)
                {
                    mang[dem++] = i;
                    i = dinhTruoc[i];
                }
                System.out.print("\tChi tiết: " +  ((char)(diemDau+65)));
                for (int k = dem - 1; k >= 0; k--) {
                    System.out.print("-->" + (char)(mang[k]+65));
                }
            }
            else
            {
                System.out.println("\t\tKhông có đường đi từ đỉnh "+( ((char)(diemDau+65)))+" đến đỉnh " + ( ((char)(diemCuoi+65))));
            }
            System.out.println("\n");
      
    }
    public void inMatran()
    {
        for(int i=0;i<soDinh;i++)
        {
            System.out.print("\t\t");
            for(int j=0;j<soDinh;j++)
            {
                if(G[i][j]==0)
                {
                    System.out.print("oo\t");
                }
                else
                {
                    System.out.print(G[i][j]+"\t");
                }
            }
            System.out.println();
        }
        System.out.println();
    }
}

2. Lớp test 
BaoCaoVanTruHoc_Nhom13.java
 package baocaovantruhoc_nhom13;
import java.util.Scanner;

/**
 *
 * @TranVanLinh
 *
 */
public class BaoCaoVanTruHoc_Nhom13 {

    public static void main(String[] args)
    {
        System.out.println("\t\t\tBáo Cáo Bài Tập Lớn Môn Vận Trù Học");
        System.out.println("\t\tNhóm 13\tĐề tài: Cài đặt thuật toán Dijkstra\n");
       
        String tenTapTin;
        BaiToan thuatToan;
        do
        {
            System.out.print("\t\tNhập tên tập tin (.txt) : ");
            tenTapTin=(new Scanner(System.in)).nextLine();
            System.out.println("");
            thuatToan=new BaiToan();
            if(!thuatToan.getDuLieuTuFile(tenTapTin))
            {
                System.out.println("\t\tTập tin không tồn tại, Nhập lại.!");
            }
        }while(!thuatToan.getDuLieuTuFile(tenTapTin));
        System.out.println("\t\tMa trận kề : ");
        thuatToan.inMatran();
        thuatToan.thuatToan_Dijkstra();
        thuatToan.inDuongDi();
    }
}

Vd: Tìm đường đi của từ đỉnh x1 đến tất cả các đỉnh của đồ thị sau:



File input2.txt

 7 4
0 3 5 4 2 7 5
3 0 0 0 2 0 0
5 0 0 4 0 3 0
4 0 4 0 0 0 2
2 2 0 0 0 0 4
7 0 3 0 0 0 6
5 0 0 2 4 6 0


Kết quả:



Friday, November 11, 2016

Lập trình hướng đối tượng với Java số 11

1. Lớp HinhCN

public class HinhCN implements Comparable
{
    private double cdai;
    private double crong;
   
    public HinhCN()
    {
    }
    public HinhCN(double cdai,double crong)
    {
        this.cdai=cdai;
        this.crong=crong;
    }
   
    public double getCRong()
    {
        return crong;
    }
     public double getCDai()
    {
        return cdai;
    }
  public void setCRong(double a)
    {
          this.crong=a;
    }
     public void setCDai(double a))
    {
         this.cdai=a;

    }
    public double tinhDT()
    {
        return cdai*crong;
    }
    public double tinhCV()
    {
        return (cdai+crong)*2;
    }

    public boolean equals(HinhCN h2)
    {
        if(this.tinhDT()==h2.tinhDT())
        {
            return true;
        }
        return false;
    }
   
    public int compareTo(Object h2)
    {
        HinhCN hcn=(HinhCN)h2;
        if(this.tinhDT()<hcn.tinhDT())
        {
            return -1;
        }
        if(this.tinhDT()==hcn.tinhDT())
        {
            return 0;
        }
        return 1;
    }
}

2.Lớp test

public class Test
{
    public static void main(String [] args)
    {
        HinhCN a=new HinhCN(5,6);
        HinhCN b=new HinhCN(5,6);
        System.out.println("Su dung phuong thuc equals: ");
        if(a.equals(b))
        {
            System.out.println("Dien tich hai hinh bang nhau");
        }
        else
        {
            System.out.println("Dien tich hai hinh khong bang nhau");
        }
       
        HinhCN c=new HinhCN(9,9);
        HinhCN d=new HinhCN(10,9);
        System.out.println("Su dung phuong thuc compareTo: ");
        if(c.compareTo(d)==-1)
        {
            System.out.println("Dien tich c<d");
        }
        else if(c.compareTo(d)==0)
        {
            System.out.println("Dien tich c=d");
        }else
        {
             System.out.println("Dien tich c>d");
        }
    }
}

3.Kết quả:



Saturday, October 29, 2016

Vẽ chữ Z bằng vòng lặp for trong java


public class VeHinh
{
    public static void main(String[] args)
    {
        int row=7;
        int col=7;
        System.out.println("Images 1");
        for(int i=1;i<=row;i++)
        {
                if(i==1||i==row)
                {
                   for(int l=1;l<=col;l++)
                   {
                       System.out.print("* ");
                   }
                   System.out.println();
                }
                if(i!=1&&i<row)
                {
                    for(int j=1;j<i;j++)
                    {
                         System.out.print("  ");
                    }
                    System.out.println("*");  
                }
               
        }
         System.out.println("\nImages 2");
         for(int i=1;i<=row;i++)
        {
                if(i==1||i==row)
                {
                   for(int l=1;l<=col;l++)
                   {
                       System.out.print("* ");
                   }
                   System.out.println();
                }
                if(i!=1&&i<row)
                {
                    for(int j=1;j<=col-i;j++)
                    {
                         System.out.print("  ");
                    }
                    System.out.print("*");  
                    for(int k=1;k<i;k++)
                    {
                         System.out.print("  ");
                    }
                    System.out.println();
                }
               
        }
    }
}

Kết quả:
        

Monday, October 17, 2016

Cài đặt thuật toán suy diễn tiến bằng Java

Giao diện chương trình: 


  

Link file exe: https://drive.google.com/file/d/0BxuqCqJeS-9XeHNMU3RFRjhyeDA/view?usp=sharing

Monday, June 13, 2016

Bài tập lập trình hướng đối tượng java Số 9 (Chữa bài kiểm tra học kỳ )

//Họ tên: Trần Văn Linh
//Msv:581597
//Lớp:K58QLTT

Đề 03;



public class Circle
{
    private double radius;
    private String color;
   
    public Circle()
    {
        this.radius=1.0;
    }
    public Circle(double radius)
    {
        this.radius=radius;
    }
    public double getRadius()
    {
        return this.radius;
    }
     public double getArea()
    {
        return Math.PI*this.radius*this.radius;
    }
}
 //----------------------------------------------------
public class Cylinder extends Circle
{
    private double height;
    //contructor
    public Cylinder()
    {
        this.height=1.0;
    }
    public Cylinder(double radius)
    {
         super(radius);
         this.height=1.0;
    }
    public Cylinder(double radius,double height)
    {
         super(radius);
         this.height=height;
    }
   
    public double getHeight()
    {
        return this.height;
    }
    //ham tinh the tich
    public double getVolume()
    {
        return super.getArea()*this.height;
    }
    //overide equals
    public boolean equals(Object obj)
    {
        Cylinder tg=(Cylinder)obj;
        if(this.getRadius()==tg.getRadius()&&this.height==tg.getHeight())
        {
            return true;
        }
        return false;
    }
}
 //---------------------------------------------------------
public class Test
{
   public static void main(String[] args)
   {
      //su dung ham tao Cylinder();
       Cylinder ht1=new Cylinder();
       System.out.println("Ban kinh :"+ht1.getRadius());
       System.out.println("Chieu cao: "+ht1.getHeight());
       System.out.println("Dien tich mot mat: "+ht1.getArea());
       System.out.println("The tich: "+ht1.getVolume());
      
       //su dung ham tao Cylinder(5.0,2.0);
       Cylinder ht2=new Cylinder(5.0,2.0);
       System.out.println("Ban kinh :"+ht2.getRadius());
       System.out.println("Chieu cao: "+ht2.getHeight());
       System.out.println("Dien tich mot mat: "+ht2.getArea());
       System.out.println("The tich: "+ht2.getVolume());
   }
}

Wednesday, April 27, 2016

Bài tập hướng đối tượng với Java (số 8)

//Họ Tên: Trần Văn Linh
//MaSV:581597
//Lớp:K58QLTT

Đề:
1. Hãy xây dựng lớp DaGiac gồm có các thuộc tính
- Số cạnh của đa giác
- Mảng các số nguyên chứa kích thước các cạnh của đa giác
Các phương thức:
- Tính chu vi
- In giá trị các cạnh của đa giác.
2. Xây dựng lớp TamGiac kế thừa từ lớp DaGiac, trong đó viết đè các hàm tính chu vi và xây dựng thêm
phương thức kiểm tra tính hợp lệ 3 cạnh của tam giác, tính diện tích tam giác
3. Xây dựng một ứng dụng Java để nhập vào một dãy gồm n tam giác rồi in ra màn hình các cạnh của các tam giác có diện tích lớn nhất.
--------------------------------------------------------------------------------------------------------------------------

Giải:
//Lớp DaGiac

public class DaGiac
{
    protected int soCanh;
    protected int a[];
 
    public DaGiac()
    {
     
    }
    public DaGiac(int soCanh,int a[])
    {
        this.soCanh=soCanh;
        this.a=a;
    }
    public int tinhChuVi()
    {
        int cv=0;
        for(int i=0;i<this.soCanh;i++)
            cv=cv+a[i];
        return cv;
    }
    public void inCanh()
    {
        for(int i=0;i<this.soCanh;i++)
        {
            System.out.println("Canh thu "+(i+1)+" : "+this.a[i]);
        }
    }
}
//Lớp TamGiac
import java.util.Scanner;
public class TamGiac extends DaGiac
{
    TamGiac()
    {
        this.a=new int[3];
        this.soCanh=3;
    }
    public int  tinhChuVi()
    {
        int cv=0;
        for(int i=0;i<3;i++)
             cv=+a[i];
           
        return cv;
    }
    public boolean ktHopLe()
    {
        int A=this.a[0];
        int B=this.a[1];
        int C=this.a[2];
        if(A+B>C && A+C>B && B+C>A)
            return true;
        return false;
    }
    public double tinhDT()
    {
        int A=this.a[0];
        int B=this.a[1];
        int C=this.a[2];
        int P=(A+B+C)/2;
        return (double) Math.sqrt(P*(P-A)*(P-B)*(P-C));
    }
    public void nhap()
    {
        Scanner sc=new Scanner(System.in);
        for(int i=0;i<3;i++)
        {
            System.out.print("Nhap canh thu "+(i+1)+": ");
            this.a[i]=sc.nextInt();
        }
    }
}
//Lop test
import java.util.ArrayList;
public class Test
{
    public static void main(String[] args)
    {
        ArrayList<TamGiac> ds=new ArrayList<TamGiac>();
        for(int i=0;i<4;i++)
        {
            System.out.println("Nhap tam giac thu "+(i+1)+" :");
            TamGiac tg=new TamGiac();
            tg.nhap();
            ds.add(tg);
        }
        double max=0;
        int Max=0;
        for(int i=0;i<ds.size();i++)
            if(ds.get(i).ktHopLe())
            {
               if(ds.get(i).tinhDT()>max)
               {
                    max=ds.get(i).tinhDT();
                    Max=i;
               }
           }
        System.out.println("Tam Giac Co Dien tich lon nhat la: "+max+", cac canh cua tam giac nay : ");
        ds.get(Max).inCanh();
    }
 

}




Bài tập trình hướng đối tượng với Java (Số 7)

//Họ Tên: Trần Văn Linh
//MaSV:581597
//Lớp:K58QLTT

Đề :
Một công ty được giao nhiệm vụ quản lý các phương tiện giao thông gồm các loại: ô tô, xe máy, xe tải.
+ mỗi loại phương tiện giao thông cần quản lý: Hãng sản xuất, năm sản xuất, giá bán và màu.
+ Các ô tô cần quản lý: số chỗ ngồi, kiểu động cơ
+ Xe máy cần quản lý: công suất
+ Xe tải cần quản lý: trọng tải.
2. Xây dựng các lớp XeTai, XeMay, OTo kế thừa từ lớp PTGT.
3. Xây dựng các hàm để truy nhập (get), thay đổi (set) và hiển thị các thuộc tính của các lớp.
4. Xây dựng lớp QLPTGT cài đặt các phương thức thực hiện các chức năng sau:
- Nhập đăng ký phương tiện
- Tìm phương tiện theo màu hoặc năm sản xuất

--------------------------------------------------------------------------------------------------------------------------

Giải:/
//Lop PTGT
import java.util.Date;
public class PTGT
{
    private String hangSX;
    private int namSX;
    private double gia;
    private String mau;
 
    public PTGT()
    {
    }
    public PTGT(String hangSX,int namSX,double gia,String mau)
    {
        this.hangSX=hangSX;
        this.namSX=namSX;
        this.gia=gia;
        this.mau=mau;
    }
 
    public void setHangSx(String hangSX)
    {
        this.hangSX=hangSX;
    }
    public void setNamSx(int namSX)
    {
        this.namSX=namSX;
    }
    public void setGia(double gia)
    {
        this.gia=gia;
    }
    public void setMau(String mau)
    {
        this.mau=mau;
    }
 
    public String getHangSX()
    {
        return this.hangSX;
    }
    public int getNamSX()
    {
        return this.namSX;
    }
    public double getGia()
    {
        return this.gia;
    }
    public String getMau()
    {
        return this.mau;
    }
 
    public void hienThongTin()
    {
        System.out.println("Hang san xuat: "+this.hangSX);
        System.out.println("Nam san xuat: "+this.namSX);
        System.out.println("Gia : "+this.gia);
        System.out.println("Mau: "+this.mau);
     
    }

}
//Lop XeTai

public class XeTai extends PTGT
{
    private double trongTai;
    
    public XeTai()
    {
    
    }
    public XeTai(String hangSX,int namSX,double gia,String mau,double trongTai)
    {
        super(hangSX,namSX,gia,mau);
        this.trongTai=trongTai;
    }
    
    public void setTrongTai(double trongTai)
    {
        this.trongTai=trongTai;
    }
    public double getTrongTai()
    {
        return this.trongTai;
    }
    
    public void hienThongTin()
    {
        super.hienThongTin();
        System.out.println("Trong tai cua xe la: "+this.trongTai);
    }
}
//Lop Oto

public class Oto extends PTGT
{
    private int soCho;
    private String kieuDongCo;
    
    public Oto()
    {
    }
    public Oto(String hangSX,int namSX,double gia,String mau,int soCho,String kieuDongCo)
    {
        super(hangSX,namSX,gia,mau);
        this.soCho=soCho;
        this.kieuDongCo=kieuDongCo;
    }
    
    public void setSoChoNgoi(int soCho)
    {
        this.soCho=soCho;
    }
    public void setKieuDongCo(String kieuDongCo)
    {
        this.kieuDongCo=kieuDongCo;
    }
    
    public void hienThongTin()
    {
        super.hienThongTin();
        System.out.println("So Cho ngoi: "+this.soCho);
        System.out.println("Kieu dong co: "+this.kieuDongCo);
    }
}
//Lop XeMay

public class XeMay extends PTGT
{
    private double congSuat; 
    
    public XeMay(String hangSX,int namSX,double gia,String mau,double congSuat)
    {
        super(hangSX,namSX,gia,mau);
        this.congSuat=congSuat;
    }
    
    public void setCongSuat(double congSuat)
    {
        this.congSuat=congSuat;
    }
    public double getCongSuat()
    {
        return this.congSuat;
    }
    
    public void hienThongTin()
    {
        super.hienThongTin();
        System.out.println("Cong Suat: "+this.congSuat);
    }
}
//lop QuanLyPhuongTienGT
import java.util.Date;
import java.util.ArrayList;
import java.util.Scanner;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class QuanLyPhuongTienGT
{
    private ArrayList<PTGT> ds=new ArrayList<PTGT>();
    
    public PTGT nhapOto(Scanner sc) 
    {
        System.out.print("Nhap hang san xuat: ");
        String hangSx=sc.nextLine();
        
        System.out.print("Nhap nam san xuat:");
        int namSX=sc.nextInt();
        
        System.out.print("Nhap gia xe:");
        double gia=sc.nextDouble();
        sc.nextLine();
        
        System.out.print("Nhap mau xe:");
        String mau=sc.nextLine();
        
        System.out.print("Nhap so cho ngoi:");
        int soCho=sc.nextInt();
        sc.nextLine();
        
        System.out.print("Nhap kieu dong co:");
        String kdc=sc.nextLine();
        
        return new Oto(hangSx,namSX,gia,mau,soCho,kdc);
    }
    public PTGT nhapXeMay(Scanner sc) 
    {
        System.out.print("Nhap hang san xuat: ");
        String hangSx=sc.nextLine();
        
        System.out.print("Nhap nam san xuat:");
        int namSX=sc.nextInt();
        
        System.out.print("Nhap gia xe:");
        double gia=sc.nextDouble();
        sc.nextLine();
        
        System.out.print("Nhap mau xe:");
        String mau=sc.nextLine();
        
        System.out.print("Nhap Cong Suat:");
        double congSuat=sc.nextDouble();
        sc.nextLine();
        
        return new XeMay(hangSx,namSX,gia,mau,congSuat);
    }
    public PTGT nhapXeTai(Scanner sc) 
    {
        System.out.print("Nhap hang san xuat: ");
        String hangSx=sc.nextLine();
        
        System.out.print("Nhap nam san xuat:");
        int namSX=sc.nextInt();
        
        System.out.print("Nhap gia xe:");
        double gia=sc.nextDouble();
        sc.nextLine();
        
        System.out.print("Nhap mau xe:");
        String mau=sc.nextLine();
        
        System.out.print("Nhap trong tai cua xe:");
        double trongTai=sc.nextDouble();
        sc.nextLine();
        
        return new XeTai(hangSx,namSX,gia,mau,trongTai);
    }
    public void  nhapDangKiPhuongTien(Scanner sc)
    {
        char c;
        int chon=0;
        while(true)
        {
            System.out.print("Ban muon nhap loai xe nao (1-XeTai,2-Xemay,3-Oto):");
            chon=sc.nextInt();
            sc.nextLine();
            switch(chon)
            {
                case 1:{
                    ds.add(nhapXeTai(sc));
                    break;
                }
                case 2:{
                    ds.add(nhapXeMay(sc));
                    break;
                }
                case 3:{
                    ds.add(nhapOto(sc));
                    break;
                }
                default:
                {
                    ds.add(nhapXeTai(sc));
                    break;
                }
            }
            System.out.println("Ban co muon nhap nua khong: (Y/N)");
            c=sc.nextLine().charAt(0);
            if(c=='N'||c=='n')
                break;
        }
    }
    public void timKiemTheoMau(Scanner sc)
    {
        System.out.print("Nhap mau can tim kiem:");
        String mauTk=sc.nextLine();
        for(int i=0;i<ds.size();i++)
        {
            String mau=ds.get(i).getMau();
            if(mau.equalsIgnoreCase(mauTk))
                ds.get(i).hienThongTin();
        }
    }
    public void timKiemTheoNamSX(Scanner sc)
    {
        System.out.print("Nhap nam  de tim kiem:");
        int nam=sc.nextInt();
        int tg=0;
        for(int i=0;i<ds.size();i++)
        {
           tg=ds.get(i).getNamSX();
           if(tg==nam)
                ds.get(i).hienThongTin();
        }
    }
    public static void main(String[] args)
    {
        QuanLyPhuongTienGT PT=new QuanLyPhuongTienGT();
        Scanner sc=new Scanner(System.in);
        PT.nhapDangKiPhuongTien(sc);
        PT.timKiemTheoMau(sc);
        PT.timKiemTheoNamSX(sc);
    }
}


Bài tập lập trình hướng đối tượng với Java (số 6)

//Họ Tên: Trần Văn Linh
//MaSV:581597
//Lớp:K58QLTT

Đề : Để quản lý các biên lai thu tiền điện, người ta cần các thông tin như sau:
- Với mỗi biên lai, có các thông tin sau: thông tin về hộ sử dụng điện, chỉ số cũ, chỉ số mới, số tiền phải
trả của mỗi hộ sử dụng điện
- Các thông tin riêng của mỗi hộ sử dụng điện gồm: Họ tên chủ hộ, số nhà, mã số công tơ của hộ dân sử dụng điện.
1. Hãy xây dựng lớp KhachHang để lưu trữ các thông tin riêng của mỗi hộ sử dụng điện.
2. Xây dựng lớp BienLai để quản lý việc sử dụng và thanh toán tiền điện của các hộ dân.
3. Xây dựng các phương thức nhập, và hiển thị một thông tin riêng của mỗI hộ sử dụng điện.
4. Cài đặt chương trình thực hiện các công việc sau:
+ Nhập vào các thông tin cho n hộ sử dụng điện
+ Hiển thị thông tin về các biên lai đã nhập
+ Tính tiền điện phải trả cho mỗi hộ dân, nếu giả sử rằng tiền phải trả được tính theo công thức sau:
số tiền phải trả=(Số mới - số cũ) * 750.

--------------------------------------------------------------------------------------------------------------------------
   Giải:
//Lop KhachHang
import java.util.Scanner;
public class KhachHang
{
    private String hoTen;
    private int soNha;
    private String maCongTo;
 
    public KhachHang()
    {
    }
    public KhachHang(String hoTen,int soNha,String maCongTo)
    {
        this.hoTen=hoTen;
        this.soNha=soNha;
        this.maCongTo=maCongTo;
    }
    //-----------
    public void nhapThongTin()
    {
        Scanner sc=new Scanner(System.in);
        System.out.print("Nhap ho ten khach hang:");
        this.hoTen=sc.nextLine();
        System.out.print("Nhap so nha :");
        this.soNha=sc.nextInt();
        sc.nextLine();
        System.out.print("Nhap ma cong to:");
        this.maCongTo=sc.nextLine();
    }
    public void hienThongTin()
    {
        System.out.println("Ho Ten: "+this.hoTen);
        System.out.println("So nha: "+this.soNha);
        System.out.println("Ma cong to: "+this.maCongTo);
    }
}
//Lớp BienLai
import java.util.Scanner;
public class BienLai
{
    private KhachHang kh;
    private int chiSoCu;
    private int chiSoMoi;
    private double tienTra;
    public BienLai()
    {
     
    }
    public BienLai(KhachHang kh,int chiSoCu,int chiSoMoi,double tienTra)
    {
        this.kh=kh;
        this.chiSoCu=chiSoCu;
        this.chiSoMoi=chiSoMoi;
        this.tienTra=tienTra;
    }
    //---------------------
    public void nhapThongTin()
    {
        Scanner sc=new Scanner(System.in);
        this.kh=new KhachHang();
        this.kh.nhapThongTin();
        System.out.print("Nhap chi so cu:");
        this.chiSoCu=sc.nextInt();
        System.out.print("Nhap chi so moi:");
        this.chiSoMoi=sc.nextInt();
        this.tienTra= (double)(this.chiSoMoi-this.chiSoCu)*750;
    }
    //--------------------
    public void hienThongTin()
    {
        this.kh.hienThongTin();
        System.out.println("Chi so cu: "+this.chiSoCu);
        System.out.println("Chi so moi: "+this.chiSoMoi);
        System.out.println("So tien phai tra: "+this.tienTra);
    }

}
//Lớp Test
import java.util.ArrayList;
import java.util.Scanner;
import java.util.InputMismatchException ;

public class Test
{
   public static void main(String[] args)
   {
       Scanner sc=new Scanner(System.in);
       int n;
       try
       {
           System.out.println("Ban muon nhap bao nhieu ho gia dinh : ");
           n=sc.nextInt();
       }catch(InputMismatchException e) //su ly ngoai le khi n khong phai la so
       {
           n=0;
       }
       
       ArrayList<BienLai> ds=new ArrayList<BienLai>(100);
       for(int i=0;i<n;i++)
       {
           BienLai b=new BienLai();
           b.nhapThongTin();
           ds.add(b);
       }
       for(int i=0;i<n;i++)
       {
           System.out.println("Ho gia dinh thu : "+(i+1));
           ds.get(i).hienThongTin();
           System.out.println("====================");
       }
   }
}