By GokiSoft.com| 14:16 03/10/2022|
Java Basic

[Source Code] Exam - Kiểm tra 60 phút lập trình OOP - Quản lý thiết bị máy tính - Lập trình OOP - C2109I

Exam - Kiểm tra 60 phút lập trình OOP - Quản lý thiết bị máy tính - Lập trình OOP


#Computer.java


/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package bt2208;

import java.util.Scanner;

/**
 *
 * @author diepvan
 */
public class Computer extends Device{
    String cpu;
    String ram;

    public Computer() {
    }

    public String getCpu() {
        return cpu;
    }

    public void setCpu(String cpu) {
        this.cpu = cpu;
    }

    public String getRam() {
        return ram;
    }

    public void setRam(String ram) {
        this.ram = ram;
    }
    
    public void input() {
        super.input();
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap CPU: ");
        cpu = scan.nextLine();
        System.out.println("Nhap ram: ");
        ram = scan.nextLine();
    }

    @Override
    public String toString() {
        return super.toString() + ", cpu=" + cpu + ", ram=" + ram;
    }
}


#Device.java


/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package bt2208;

import java.util.Scanner;

/**
 *
 * @author diepvan
 */
public class Device {
    int id;
    String name;
    String manufacturer;
    String importedDate;

    public Device() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public void setManufacturer(String manufacturer) {
        this.manufacturer = manufacturer;
    }

    public String getImportedDate() {
        return importedDate;
    }

    public void setImportedDate(String importedDate) {
        this.importedDate = importedDate;
    }
    
    public void input() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap ID: ");
        id = Integer.parseInt(scan.nextLine());
        System.out.println("Nhap ten: ");
        name = scan.nextLine();
        System.out.println("Nhap NSX: ");
        manufacturer = scan.nextLine();
        System.out.println("Nhap ngay nhap: ");
        importedDate = scan.nextLine();
    }

    @Override
    public String toString() {
        return "id=" + id + ", name=" + name + ", manufacturer=" + manufacturer + ", importedDate=" + importedDate;
    }
    
    public void display() {
        System.out.println(this);
    }
}


#Main.java


/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package bt2208;

import java.util.Scanner;

/**
 *
 * @author diepvan
 */
public class Main {
    static Device[] list = new Device[5];
    static Scanner scan = new Scanner(System.in);

    public static void main(String[] args) {

        int choose;

        do {
            showMenu();
            choose = Integer.parseInt(scan.nextLine());

            switch (choose) {
                case 1:
                    input();
                    break;
                case 2:
                    display();
                    break;
                case 3:
                    countComputer();
                    break;
                case 4:
                    countMonitor();
                    break;
                case 5:
                    System.out.println("Thoat!!!");
                    break;
                default:
                    System.out.println("Nhap sai!!!");
            }
        } while (choose != 5);
    }

    static void showMenu() {
        System.out.println("1. Nhap");
        System.out.println("2. Hien thi");
        System.out.println("3. Dem so may tinh");
        System.out.println("4. Dem so man hinh");
        System.out.println("5. Thoat");
        System.out.println("Chon: ");
    }

    private static void input() {
        int choose;
        
        for (int i = 0; i < 5; i++) {
            System.out.println("===================");
            System.out.println("1. May tinh");
            System.out.println("2. Man hinh");
            System.out.println("Chon: ");
            choose = Integer.parseInt(scan.nextLine());
            System.out.println("===================");
            if(choose == 1) {
                list[i] = new Computer();
            } else {
                list[i] = new Monitor();
            }
            
            list[i].input();
        }
    }

    private static void display() {
        for (int i = 0; i < 5; i++) {
            list[i].display();
        }
    }

    private static void countComputer() {
        int count = 0;
        for (int i = 0; i < 5; i++) {
            if(list[i] instanceof Computer) {
                count++;
            }
        }
        System.out.println("So may tinh: " + count);
    }

    private static void countMonitor() {
        int count = 0;
        for (int i = 0; i < 5; i++) {
            if(list[i] instanceof Monitor) {
                count++;
            }
        }
        System.out.println("So man hinh: " + count);
    }
}


#Monitor.java


/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package bt2208;

import java.util.Scanner;

/**
 *
 * @author diepvan
 */
public class Monitor extends Device{
    String size;

    public Monitor() {
    }

    public String getSize() {
        return size;
    }

    public void setSize(String size) {
        this.size = size;
    }
    
    public void input() {
        super.input();
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap kich co: ");
        size = scan.nextLine();
    }

    @Override
    public String toString() {
        return super.toString() + ", size=" + size;
    }
    
}


Tags:



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

5

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

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

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