By GokiSoft.com|
18:57 15/02/2023|
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 - C2206L
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 com.gokisoft.lesson08.bt2208;
import java.util.Scanner;
/**
*
* @author diepvan
*/
public class Computer extends Device{
String cpu;
String ram;
@Override
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();
}
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;
}
@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 com.gokisoft.lesson08.bt2208;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author diepvan
*/
public class Device {
int id;
String name;
String manufacturer;
Date 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 (dd/MM/yyyy):");
String importedDateStr = scan.nextLine();
try {
importedDate = new SimpleDateFormat("dd/MM/yyyy").parse(importedDateStr);
} catch (ParseException ex) {
Logger.getLogger(Device.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void display() {
System.out.println(this);
}
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 Date getImportedDate() {
return importedDate;
}
public void setImportedDate(Date importedDate) {
this.importedDate = importedDate;
}
@Override
public String toString() {
return "id=" + id + ", name=" + name + ", manufacturer=" + manufacturer + ", importedDate=" + importedDate;
}
}
#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 com.gokisoft.lesson08.bt2208;
import java.util.Scanner;
/**
*
* @author diepvan
*/
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Device[] list = new Device[5];
int choose;
//Cau 1:
for (int i = 0; i < 5; i++) {
showDeviceFactory();
choose = Integer.parseInt(scan.nextLine());
switch (choose) {
case 1:
list[i] = new Computer();
break;
default:
list[i] = new Monitor();
break;
}
list[i].input();
}
//Cau 2:
System.out.println("Danh sach thiet bi");
for (Device device : list) {
device.display();
}
//Cau 3:
int computerCount = 0;
for (Device device : list) {
if(device instanceof Computer) {
computerCount++;
}
}
System.out.println("So computer: " + computerCount);
//Cau 4:
int monitorCount = 0;
for (Device device : list) {
if(device instanceof Monitor) {
monitorCount++;
}
}
System.out.println("So monitor: " + monitorCount);
//Cau 5:
System.out.println("Thoat!!!");
}
static void showDeviceFactory() {
System.out.println("1. Computer");
System.out.println("2. Monitor");
System.out.println("Chon: ");
}
}
#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 com.gokisoft.lesson08.bt2208;
import java.util.Scanner;
/**
*
* @author diepvan
*/
public class Monitor extends Device{
String size;
@Override
public void input() {
super.input();
Scanner scan = new Scanner(System.in);
System.out.println("Nhap kich thuoc: ");
size = scan.nextLine();
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
@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)