By GokiSoft.com| 19:09 24/04/2024|
Java Basic

Java Basic- OOP - Interface - Quản lý mèo (cat) trong java

Bài tập về kế thừa:

(Kế thừa có thể là từ một lớp trừu tượng (lớp ảo) hoặc là từ một giao diện).

 

* Kế thừa từ 1 giao diện: Làm bài tập sau đây.

Hãy xây dựng một giao diện ICat nằm trong gói cat.exam gồm có thuộc tính và phương thức sau:

-          public final String ten = “Meo”;

-          public void nhap();

-          public void hienthi();

 

+ Xây dựng lớp CatDetail nằm trong gói cat.exam  và thực thi giao diện Cat trên rồi có thêm các thuộc tính:

-          String loai;

-          String mau (Chú ý : Màu nhập vào phải nằm trong ColorManager)

-          String noisong;

 

Cài đặt các Constructor, các phương thức set/get cho các thuộc tính của lớp và Override các phương thức trong giao diện Cat.

+ Xây dựng lớp ColorManager -> quản lý mày đặt trong package cat.color

- ArrayList<String> colorList -> quản lý danh sach màu của mèo.

- nhập và hiển thị mã màu trong lớp này. 

+ Cài đặt 1 lớp ManagerCat nằm trong gói cat.manager

- Khai báo thuộc tính catList kiểu dữ liệu là ArrayList -> được sử dụng để quản lý danh sách mèo nhập vào.

+ Cài đặt 1 lớp UsingManagerCat nằm trong gói cat có menu sau:

                0.   Nhập danh sách mã màu

1.      Nhập thông tin của n con mèo

2.      Hiển thị thông tin

3.      Sắp xếp danh sách theo mau

4.      Tìm kiếm thông tin theo loai

5.    Sắp xếp danh sách màu theo bảng màu trong lớp ColorManager

6.      Thoát.

 

Nhiệm vụ:

            Trong lớp UsingManagerCat phải khai báo 1 đối tượng của lớp ManagerCat và viết hàm để nhập vào thông tin của n con mèo.

Các nhiệm vụ: Hiển thị, sắp xếp, tìm kiếm thực hiện trên danh sách thông qua đối tượng của lớp ManagerCat. 

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

nguyễn thị bích thủy [C1907L]
nguyễn thị bích thủy

2020-03-25 15:06:21



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Cat;
import java.util.ArrayList;
import java.util.Scanner;
/**
 *
 * @author Admin
 */
public class Cat {
 static  class CatDetail extends Cat{
        String Loai;
        String Mau;
        String NoiSong;
        public String getLoai(){
            return Loai;
        }
        public void setLoai( String Loai){
            this.Loai=Loai;
        }
        public String getMau(){
            return Mau;
        }
        public void setMau( String Mau){
            this.Mau=Mau;
        }
        public String getNoiSong(){
            return NoiSong;
        }
        public void setNoiSong( String NoiSong){
            this.NoiSong=NoiSong;
        }
        ArrayList<CatDetail> listcat = new ArrayList<CatDetail>();
        int n;
        Scanner scan = new Scanner(System.in);
        public  void nhapcat(){
            System.out.println("Nhap so luong meo: ");
            n = Integer.parseInt(scan.nextLine());
            for (int i = 0; i < n; i++) {
                System.out.println("Nhap con meo thu: " + (i + 1));
                CatDetail cat = new CatDetail();
                listcat.add(cat);
            }
        }
        public void hienthicat(){
            System.out.println("Thong tin cac con mao: ");
            for (int i = 0; i < n; i++) {
            System.out.println("Con meo thu "+(i+1) );
            System.out.println("thuoc loai: "+Loai);
            System.out.println( "Co Mau: "+Mau);
            System.out.println("Noi Song "+NoiSong);
            }
        }
    }
}
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Cat;
import java.util.ArrayList;
import java.util.Scanner;
/**
 *
 * @author Admin
 */
public class ColorManager {
   ArrayList<String> colorList = new ArrayList<String>();
   String color;
   int n;
    public void nhapdanhsachmau(){
        System.out.println("Nhap so luong mau: ");
        Scanner scan = new Scanner(System.in);
        n = Integer.parseInt(scan.nextLine());
        for (int i = 0; i < n; i++) {
            System.out.println("Nhap mau : ");
            color = scan.nextLine();
            colorList.add(color);
        }
    }
    public void hienthimau(){
        for (int i = 0; i < n; i++) {
            System.out.println("Mau: "+color);
        }
    }
}
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Cat;

import java.util.Scanner;

/**
 *
 * @author Admin
 */
public class UsingManagerCat {
    public static void Menu() {
        System.out.println("0.Nhập danh sách mã màu");
        System.out.println("1.Nhập thông tin của n con mèo");
        System.out.println("2.Hiển thị thông tin");
        System.out.println("3.Sắp xếp danh sách theo mau");
        System.out.println("4.Tìm kiếm thông tin theo loai");
        System.out.println("5.Sắp xếp danh sách màu theo bảng màu trong lớp ColorManager");
        System.out.println("6.Thoát.");
    }
    public static void main(String[] args ){
       Scanner input = new Scanner(System.in);
       ColorManager color = new ColorManager();
       Cat.CatDetail cat = new Cat.CatDetail();
        boolean   exit =  false;
        while(true){
            Menu();
            int n = input.nextInt();
            switch(n){
                case 0:
                    color.nhapdanhsachmau();
                    break;
                case 1: 
                    cat.nhapcat();
                    break;
                case 2:
                    cat.hienthicat();
                    break;
                case 3:
                    cat.sapxep1();
                    break;
                case 4: 
                    cat.timkiem();
                    break;
                case 5:
                    cat.sapxep2();
                    break;
                case 6:
                    exit = true;
                    break;  
            }if (exit) {
                break;
            }   

        }
    }
}



Nguyễn Hữu Đạt [C1907L]
Nguyễn Hữu Đạt

2020-03-25 14:46:53




import color.ColorManager;
import exam.ManagerCat;
import java.util.Scanner;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Laptop88
 */
public class UsingManagerCat {

    public static void main(String[] args) {
        ManagerCat managerCat = new ManagerCat();
        Scanner scanner = new Scanner(System.in);
        String choose = null;
        boolean exit = false;
        showMenu();
        while (true) {
            choose = scanner.nextLine();
            switch (choose) {
                case "0":
                    ColorManager color = new ColorManager();
                    color.nhap();
                    break;
                case "1":
                    managerCat.nhap();
                    break;
                case "2":
                    managerCat.hienThi();
                    break;
                case "3":
                    managerCat.sapxep();
                    break;
                case "4":
                    managerCat.timKiem();
                    break;
                case "5":
                    break;
                case "6":
                    System.out.println("Thoat.");
                    break;
                default:
                    System.err.println("Nhap sai !!!");
                    break;
            }
            if (exit) {
                break;
            }
            showMenu();
        }
    }

    public static void showMenu() {
        System.out.println("0.Nhập danh sách mã màu");
        System.out.println("1.Nhập thông tin của n con mèo");
        System.out.println("2.Hiển thị thông tin");
        System.out.println("3.Sắp xếp danh sách theo mau");
        System.out.println("4.Tìm kiếm thông tin theo loai");
        System.out.println("5.Sắp xếp danh sách màu theo bảng màu trong lớp ColorManager");
        System.out.println("6.Thoát.");
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package color;

import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * @author Laptop88
 */
public class ColorManager {
    ArrayList<String> colorList = new ArrayList<>();
    
    public void nhap(){
        Scanner scan = new Scanner(System.in);

        System.out.println("Nhap so mau can them : ");
        int n = Integer.parseInt(scan.nextLine());

        for (int i = 0; i < n; i++) {
            System.out.println("Nhap mau : ");
            String color = scan.nextLine();

            colorList.add(color);
        }
    }
    
    public void hienThi() {
        for (String color : colorList) {
            System.out.println("color : " + color);
        }
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package exam;

import java.util.Scanner;

/**
 *
 * @author Laptop88
 */
public class CatDetail implements Cat {

    String loai;

    String mau;

    public String getLoai() {
        return loai;
    }

    public void setLoai(String loai) {
        this.loai = loai;
    }

    public String getMau() {
        return mau;
    }

    public void setMau(String mau) {
        this.mau = mau;
    }

    public String getNoisong() {
        return noisong;
    }

    public void setNoisong(String noisong) {
        this.noisong = noisong;
    }

    public CatDetail() {
    }

    String noisong;

    @Override
    public void nhap() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Loai meo: ");
        this.loai = scan.nextLine();
        System.out.println("Mau meo: ");
        this.mau = scan.nextLine();
        System.out.println("Noi song: ");
        this.noisong = scan.nextLine();
    }

    @Override
    public void hienthi() {
        System.out.println( this.loai + " \t " + this.mau + " \t " + this.noisong);
    }

}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package exam;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

/**
 *
 * @author Laptop88
 */
public class ManagerCat {
    ArrayList<CatDetail> catList = new ArrayList<>();
    Scanner scan = new Scanner(System.in);

    public void nhap() {

        System.out.println("Nhap vao so meo: ");
        int c = Integer.parseInt(scan.nextLine());
        for (int i = 0; i < c; i++) {
            System.out.println("Nhap thong tin con meo thu: " + (i + 1));
            CatDetail catDetail = new CatDetail();
            catDetail.nhap();
            catList.add(catDetail);
        }
    }

    public void hienThi() {
        System.out.println("Loai \t Mau \t  Noi Song");
        for (int i = 0; i < catList.size(); i++) {
            catList.get(i).hienthi();
        }
    }

    public void sapxep() {
        Collections.sort(catList, new Comparator<CatDetail>() {
            @Override
            public int compare(CatDetail catDetail, CatDetail t1) {
                return catDetail.getMau().compareToIgnoreCase(t1.getMau());
            }
        });
        hienThi();
    }
    
    public void timKiem() {
        System.out.println("Nhap loai ban muon tim kiem: ");
        String loai = scan.nextLine();
        for (int i = 0; i < catList.size(); i++) {
            if (catList.get(i).getLoai().equalsIgnoreCase(loai)) {
                System.out.println("Loai \t Mau \t  Noi Song");
                catList.get(i).hienthi();
            }
        }
    }
}



Đỗ Văn Huấn [T1907A]
Đỗ Văn Huấn

2020-03-20 02:44:02


package BaiTapNgay6_3_2020.bai51.cat.manager;

import BaiTapNgay6_3_2020.bai51.exam.CatDetail;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public class ManagerCat {
ArrayList<CatDetail> listCat = new ArrayList<>();
Scanner scan = new Scanner(System.in);

public void input() {

System.out.println("Nhap vao so meo: ");
int c = Integer.parseInt(scan.nextLine());
for (int i = 0; i < c; i++) {
System.out.println("Nhap thong tin con meo thu: " + (i + 1));
CatDetail catDetail = new CatDetail();
listCat.add(catDetail);
catDetail.input();
}
}

public void output() {
for (int i = 0; i < listCat.size(); i++) {
listCat.get(i).output();
}
}

public void sapxep() {
Collections.sort(listCat, new Comparator<CatDetail>() {
@Override
public int compare(CatDetail catDetail, CatDetail t1) {
return catDetail.getColor().compareToIgnoreCase(t1.getColor());
}
});
output();
}

public void seach() {
System.out.println("Nhap loai ban muon tim kiem: ");
String loai = scan.nextLine();
for (int i = 0; i < listCat.size(); i++) {
if (listCat.get(i).getSpecies().equalsIgnoreCase(loai)) {
listCat.get(i).output();
}
}
}

}
package BaiTapNgay6_3_2020.bai51.color;

import java.util.ArrayList;
import java.util.Scanner;

public class ColorManager {
ArrayList<String> colorList = new ArrayList<>();

public void input() {
Scanner scan = new Scanner(System.in);

System.out.println("Nhap so mau can them : ");
int n = Integer.parseInt(scan.nextLine());

for (int i = 0; i < n; i++) {
System.out.println("Nhap mau : ");
String color = scan.nextLine();

colorList.add(color);
}
}

public void output() {
for (String color : colorList) {
System.out.println("color : " + color);
}
}

public ArrayList<String> getColorList() {
return colorList;
}
}

package BaiTapNgay6_3_2020.bai51;

import BaiTapNgay6_3_2020.bai51.color.ColorManager;
import BaiTapNgay6_3_2020.bai51.cat.manager.ManagerCat;

import java.util.Scanner;

public class UsingManagerCat {
public static void main(String[] args) {
ManagerCat managerCat = new ManagerCat();
Scanner scan = new Scanner(System.in);
int choose;
do {
System.out.print("Nhap vao lua chon: ");
choose = Integer.parseInt(scan.nextLine());
switch (choose) {
case 0:
ColorManager color = new ColorManager();
color.input();
break;
case 1:
managerCat.input();
break;
case 2:
managerCat.output();
break;
case 3:
managerCat.sapxep();
break;
case 4:
managerCat.seach();
break;
case 5:
break;
case 6:
System.out.println("Thoat.");
break;
default:
System.err.println("Nhap sai !!!");
break;
}
} while (choose != 6);
}

public static void showMenu() {
System.out.println("0.Nhập danh sách mã màu");
System.out.println("1.Nhập thông tin của n con mèo");
System.out.println("2.Hiển thị thông tin");
System.out.println("3.Sắp xếp danh sách theo mau");
System.out.println("4.Tìm kiếm thông tin theo loai");
System.out.println("5.Sắp xếp danh sách màu theo bảng màu trong lớp ColorManager");
System.out.println("6.Thoát.");
}
}



Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó