By GokiSoft.com| 15:15 23/06/2023|
Java Basic

Java basic- Overview - phân tích thiết kế lớp đối tượng trong java

Các bài tập dưới đây yêu cầu bạn phải tự biết khai báo các thuộc tính cho lớp, cài đặt đầy đủ 2 constructor, các phương thức set/get, 2 hàm input() và display() để nhập và hiển thị các thuộc tính trong lớp đó.

Sau khi đã cài đặt lớp xong thì cài đặt thêm class Test để khởi tạo đối tượng của lớp (có thể tạo mảng đối tượng và ít nhất cũng phải tạo 2 đối tượng để khởi tạo bằng constructor có tham số và constructor không có tham số).

Bài 1.

Cài đặt lớp để mô tả về đối tượng Computer .

Bài 2.

Cài đặt lớp để mô tả đối tượng House

Bài 3.

Cài đặt lớp để mô tả đối tượng Car

Bài 4.

Cài đặt lớp để mô tả đối tượng Mark (điểm thi)

Bài 5.

Cài đặt lớp để mô tả đối tượng Country

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

5

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

Trần Nhựt Linh [java1_online]
Trần Nhựt Linh

2023-04-07 16:13:57



/*
 * 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.mycompany.qlthuvienapt;

import java.util.Scanner;

/**
 *
 * @author mymem
 */
public class NhaXuatBan {
     String ten, ngayTL, tenNDD,namTL;

    public String getTen() {
        return ten;
    }

    public String getNgayTL() {
        return ngayTL;
    }

    public String getTenNDD() {
        return tenNDD;
    }

    public String getNamTL() {
        return namTL;
    }

    public void setTen(String ten) {
        this.ten = ten;
    }

    public void setNgayTL(String ngayTL) {
        this.ngayTL = ngayTL;
    }

    public void setTenNDD(String tenNDD) {
        this.tenNDD = tenNDD;
    }

    public void setNamTL(String namTL) {
        this.namTL = namTL;
    }

   

    public NhaXuatBan() {
    }

    public NhaXuatBan(String ten, String ngayTL, String tenNDD, String namTL) {
        this.ten = ten;
        this.ngayTL = ngayTL;
        this.tenNDD = tenNDD;
        this.namTL = namTL;
    }
    public void input(){
        Scanner input = new Scanner(System.in);
        System.out.print("Nhap ten NXB: ");
        ten = input.nextLine();
        System.out.print("Nhap ngay thanh lap: ");
        ngayTL= input.nextLine();
        System.out.print("Nhap ten NDD: ");
        tenNDD = input.nextLine();
        System.out.print("Nhap nam TL ");
        namTL = input.nextLine();
    }

    @Override
    public String toString() {
        return "NhaXuatBan{" + "ten=" + ten + ", ngayTL=" + ngayTL + ", tenNDD=" + tenNDD + ", namTL=" + namTL + "} ";
    }

    public void display(){
        System.out.print(toString());
    }
    
}



Nguyễn Anh Vũ [T2008A]
Nguyễn Anh Vũ

2021-03-03 07:06:42


#Computer
/*
 * 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 Computer;

/**
 *
 * @author Admin
 */
public class Computer {

    String monitor;
    String keyboard;
    String mouse;
    String Case;
    String main;
    String cpu;
    String fan;
    String ram;
    String rom;
    String gpu;

    Computer() {
    }

    Computer(String monitor, String keyboard, String mouse,
            String Case, String main, String cpu, String fan, String ram, String rom, String gpu) {
        this.monitor = monitor;
        this.keyboard = keyboard;
        this.mouse = mouse;
        this.Case = Case;
        this.cpu = cpu;
        this.main = main;
        this.fan = fan;
        this.gpu = gpu;
        this.ram = ram;
        this.rom = rom;
    }

    public void input(String monitor, String keyboard, String mouse,
            String Case, String main, String cpu, String fan, String ram, String rom, String gpu) {
        this.monitor = monitor;
        this.keyboard = keyboard;
        this.mouse = mouse;
        this.Case = Case;
        this.cpu = cpu;
        this.main = main;
        this.fan = fan;
        this.gpu = gpu;

    }

    public void display() {
        System.out.println("Monitor : " + monitor);
        System.out.println("Keyboard : " + keyboard);
        System.out.println("Mouse : " + mouse);
        System.out.println("Case : " + Case);
        System.out.println("CPU : " + cpu);
        System.out.println("Main : " + main);
        System.out.println("Fan : " + fan);
        System.out.println("GPU : " + gpu);
    }

    public String getMonitor() {
        return monitor;
    }

    public String getKeyboard() {
        return keyboard;
    }

    public void setMonitor(String monitor) {
        this.monitor = monitor;
    }

    public void setKeyboard(String keyboard) {
        this.keyboard = keyboard;
    }

    public void setMouse(String mouse) {
        this.mouse = mouse;
    }

    public void setCase(String Case) {
        this.Case = Case;
    }

    public void setMain(String main) {
        this.main = main;
    }

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

    public void setFan(String fan) {
        this.fan = fan;
    }

    public void setRam(String ram) {
        this.ram = ram;
    }

    public void setRom(String rom) {
        this.rom = rom;
    }

    public void setGpu(String gpu) {
        this.gpu = gpu;
    }

    public String getMouse() {
        return mouse;
    }

    public String getCase() {
        return Case;
    }

    public String getMain() {
        return main;
    }

    public String getCpu() {
        return cpu;
    }

    public String getFan() {
        return fan;
    }

    public String getRam() {
        return ram;
    }

    public String getRom() {
        return rom;
    }

    public String getGpu() {
        return gpu;
    }
    
}



Nguyễn Anh Vũ [T2008A]
Nguyễn Anh Vũ

2021-03-03 07:03:12


#House
/*
 * 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 House;

import java.util.Scanner;

/**
 *
 * @author Admin
 */
public class House {
    String Address;
    int Area;

    public House() {
    }

    public House(String Address, int Area) {
        this.Address = Address;
        this.Area = Area;
    }
    
    public void input(){
        Scanner input = new Scanner(System.in);
        System.out.println("Address:");
        Address = input.nextLine();
        System.out.println("Area:");
        Area = Integer.parseInt(input.nextLine());
    }

    @Override
    public String toString() {
        return "House{" + "Address=" + Address + ", Area=" + Area + '}';
    }
    
    public void output(){
        System.out.println(toString());
    }

    public String getAddress() {
        return Address;
    }

    public int getArea() {
        return Area;
    }

    public void setAddress(String Address) {
        this.Address = Address;
    }

    public void setArea(int Area) {
        this.Area = Area;
    }
    
    
}



Nguyễn Anh Vũ [T2008A]
Nguyễn Anh Vũ

2021-03-03 07:02:12



/*
 * 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 Car;

import java.util.Scanner;

/**
 *
 * @author Admin
 */
public class Car {
    String Name,Brand;
    
    public Car() {
    }

    public Car(String Name, String Brand) {
        this.Name = Name;
        this.Brand = Brand;
    }

    public String getName() {
        return Name;
    }

    public String getBrand() {
        return Brand;
    }

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

    public void setBrand(String Brand) {
        this.Brand = Brand;
    }
    
    public void input(){
        Scanner input = new Scanner(System.in);
        System.out.println("Car name:");
        Name = input.nextLine();
        System.out.println("Brand name:");
        Brand = input.nextLine();
    }
    
    public void output(){
        System.out.println(toString());
    }

    @Override
    public String toString() {
        return "Car{" + "Name=" + Name + ", Brand=" + Brand + '}';
    }
}



Nguyễn Anh Vũ [T2008A]
Nguyễn Anh Vũ

2021-03-03 07:01:22


#Mark
/*
 * 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 Mark;

import java.util.Scanner;

/**
 *
 * @author Admin
 */
public class Mark {
    String Name;
    float Value;

    public Mark() {
    }

    public Mark(String Name, float Value) {
        this.Name = Name;
        this.Value = Value;
    }
    
    public void input(){
        Scanner input = new Scanner(System.in);
        System.out.println("Student name");
        this.Name = input.nextLine();
        System.out.println("Mark: ");
        this.Value = Float.parseFloat(input.nextLine());
    }

    @Override
    public String toString() {
        return "Mark{" + "Name=" + Name + ", Value=" + Value + '}';
    }
    
    public void output(){
        System.out.println(toString());
    }

    public String getName() {
        return Name;
    }

    public float getValue() {
        return Value;
    }

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

    public void setValue(float Value) {
        this.Value = Value;
    }
    
    
    
    
}



Nguyễn Anh Vũ [T2008A]
Nguyễn Anh Vũ

2021-02-24 10:18:08


#Country
/*
 * 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 Country;

import java.util.Scanner;

public class Country {
    String Name;
    String Language;

    public Country() {
    }

    public Country(String Name, String Language) {
        this.Name = Name;
        this.Language = Language;
    }

    public String getName() {
        return Name;
    }

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

    public String getLanguage() {
        return Language;
    }

    public void setLanguage(String Language) {
        this.Language = Language;
    }
    
    
    public void inputCtr(){
        Scanner input = new Scanner(System.in);
        System.out.println("Insert Country Name:");
        Name = input.nextLine();
        System.out.println("Insert Country Language:");
        Language = input.nextLine();
    }
    
    public void outputCtr(){
        System.out.println("Country Name:"+Name);
        System.out.println("Country Language:"+Language);
    }
}



Nguyễn Anh Vũ [T2008A]
Nguyễn Anh Vũ

2021-02-24 09:50:47



/*
 * 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 Test;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Computer Com = new Computer();
        Com.inputCom();
        House Hou= new House();
        Hou.inputHou();
        Car Cr= new Car("Special Edition","Volkswagen");
        Mark Grd= new Mark("Mr B",6);
        Country Cnt= new Country("USA","English");
        
        Com.outputCom();
        Hou.outputHou();
        Cr.outputCar();
        Grd.outputGrd();
        Cnt.outputCtr();
    }
}



nguyễn Sử [T2008A]
nguyễn Sử

2021-02-24 09:23:08




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 WIN10
 */
public class country {
    private String name;
    private String address;

    public country(String name, String address) {
        this.name = name;
        this.address = address;
    }

    public String getName() {
        return name;
    }

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
    
    public void input (){
        Scanner input = new Scanner(System.in);
        System.out.print("tên: ");
        name = input.nextLine();
        System.out.println("địa chỉ: ");
        address = input.nextLine();
    }
    public void display(){
        System.out.println(toString ());
    }

    @Override
    public String toString() {
        return "country{" + "name=" + name + ", address=" + address + '}';
    }
    
    
    
}



nguyễn Sử [T2008A]
nguyễn Sử

2021-02-24 09:22:58




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 WIN10
 */
public class Mark {

    private String namesubject;
    private int dateexam;
    float scores;

    public Mark(String namesubject, int dateexam, float scores) {
        this.namesubject = namesubject;
        this.dateexam = dateexam;
        this.scores = scores;
    }

    public String getNamesubject() {
        return namesubject;
    }

    public void setNamesubject(String namesubject) {
        this.namesubject = namesubject;
    }

    public int getDateexam() {
        return dateexam;
    }

    public void setDateexam(int dateexam) {
        this.dateexam = dateexam;
    }

    public float getScores() {
        return scores;
    }

    public void setScores(float scores) {
        this.scores = scores;
    }

    public Mark() {
    }
    public void input(){
        Scanner input = new Scanner(System.in);
        System.out.print("Môn: ");
        namesubject = input.nextLine();
        System.out.println("ngày kiểm tra: ");
        dateexam = input.nextInt();
        System.out.print("Điểm: ");
        scores = Float.parseFloat(input.nextLine());
    }
    public void display() {
        System.out.println(toString());
    }    
    
    @Override
    public String toString() {
        return "Mark{" + "namesubject=" + namesubject + ", dateexam=" + dateexam + ", scores=" + scores + '}';
    }
}



nguyễn Sử [T2008A]
nguyễn Sử

2021-02-24 09:22:31



/*
 * 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 WIN10
 */
public class Car {

    public Car() {
    }
    private String colorcar;
    private String numbercar;
    private String manufacturercar;
    private String yearsxcar;

    public Car(String colorcar, String numbercar, String manufacturercar, String yearsxcar) {
        this.colorcar = colorcar;
        this.numbercar = numbercar;
        this.manufacturercar = manufacturercar;
        this.yearsxcar = yearsxcar;
    }

    public String getColorcar() {
        return colorcar;
    }

    public void setColorcar(String colorcar) {
        this.colorcar = colorcar;
    }

    public String getNumbercar() {
        return numbercar;
    }

    public void setNumbercar(String numbercar) {
        this.numbercar = numbercar;
    }

    public String getManufacturercar() {
        return manufacturercar;
    }

    public void setManufacturercar(String manufacturercar) {
        this.manufacturercar = manufacturercar;
    }

    public String getYearsxcar() {
        return yearsxcar;
    }

    public void setYearsxcar(String yearsxcar) {
        this.yearsxcar = yearsxcar;
    }

    @Override
    public String toString() {
        return "Car{" + "colorcar=" + colorcar + ", numbercar=" + numbercar + ", manufacturercar=" + manufacturercar + ", yearsxcar=" + yearsxcar + '}';
    }
    
    public void display() {
        System.out.println(toString());
    }    

}