By GokiSoft.com| 19:27 19/04/2024|
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)

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

2021-02-24 09:22:07



/*
 * 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 house {

    public house() {
    }
    private String numberhouse;
    private String addresshouse;
    private String colorhouse;

    public house(String numberhouse, String addresshouse, String colorhouse) {
        this.numberhouse = numberhouse;
        this.addresshouse = addresshouse;
        this.colorhouse = colorhouse;
    }
    
    public String getNumberhouse() {
        return numberhouse;
    }

    public void setNumberhouse(String numberhouse) {
        this.numberhouse = numberhouse;
    }

    public String getAddresshouse() {
        return addresshouse;
    }

    public void setAddresshouse(String addresshouse) {
        this.addresshouse = addresshouse;
    }

    public String getColorhouse() {
        return colorhouse;
    }

    public void setColorhouse(String colorhouse) {
        this.colorhouse = colorhouse;
    }
   @Override
    public String toString() {
        return "house{" + "numberhouse=" + numberhouse + ", addresshouse=" + addresshouse + ", colorhouse=" + colorhouse + '}';
    } 
    
    public void display() {
        System.out.println(toString());
    }    
    

}



Nguyên Phấn Đông [T2008A]
Nguyên Phấn Đông

2021-02-24 05:49:04



package homework1;
import java.util.Scanner;
// Bai 4:
public class Mark {
    String subject;
    float scores;

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public float getScores() {
        return scores;
    }

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

    public Mark(String subject, float scores) {
        this.subject = subject;
        this.scores = scores;
    }

    public Mark() {
    }
    
    public void input(){
        Scanner input = new Scanner(System.in);
        System.out.print("Môn: ");
        subject = input.nextLine();
        System.out.print("Điểm: ");
        scores = Float.parseFloat(input.nextLine());
    }
    
    public void display(){
        System.out.print(toString());
    }

    @Override
    public String toString() {
        return "Mark{" + "subject=" + subject + ", scores=" + scores + '}';
    }

}



package homework1;
import java.util.Scanner;
//Bai 3:
public class Car {
    String name,color;

    public String getName() {
        return name;
    }

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

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public Car() {
    }

    public Car(String name, String color) {
        this.name = name;
        this.color = color;
    }
    
    public void input(){
        Scanner input = new Scanner(System.in);
        System.out.print("Tên: ");
        name = input.nextLine();
        System.out.print("Màu: ");
        color = input.nextLine();
    }
    
    public void display(){
        System.out.print(toString());
    }

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



package homework1;
import java.util.Scanner;
// Bai 2:
public class House {
    String diaChi, ten;

    public String getDiaChi() {
        return diaChi;
    }

    public void setDiaChi(String diaChi) {
        this.diaChi = diaChi;
    }

    public String getTen() {
        return ten;
    }

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

    public House() {
    }

    public House(String diaChi, String ten) {
        this.diaChi = diaChi;
        this.ten = ten;
    }
    
    public void display(){
        System.out.print(toString());
    }

    @Override
    public String toString() {
        return "House{" + "diaChi=" + diaChi + ", ten=" + ten + '}';
    }
    



package homework1;
import java.util.Scanner;
// Bai 1:
public class Computer {
    String hang, ten, loai;

    public String getHang() {
        return hang;
    }

    public void setHang(String hang) {
        this.hang = hang;
    }

    public String getTen() {
        return ten;
    }

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

    public String getLoai() {
        return loai;
    }

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

    public Computer() {
    }

    public Computer(String hang, String ten, String loai) {
        this.hang = hang;
        this.ten = ten;
        this.loai = loai;
    }
    
    public void display(){
        System.out.print(toString());
    }

    @Override
    public String toString() {
        return "Computer{" + "hang=" + hang + ", ten=" + ten + ", loai=" + loai + '}';
    }
    
    public void input(){
        Scanner input = new Scanner(System.in);
        System.out.print("Nhập tên hãng: ");
        hang = input.nextLine();
        System.out.print("Nhập tên máy: ");
        ten = input.nextLine();
        System.out.print("Nhập loại máy: ");
        loai = input.nextLine();
    }
}



Nguyên Phấn Đông [T2008A]
Nguyên Phấn Đông

2021-02-24 05:46:27



/*
 * 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 homework1;
import java.util.Scanner;
// Bai 5: 
public class Country {
    String name, area;

    public String getName() {
        return name;
    }

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

    public String getArea() {
        return area;
    }

    public void setArea(String area) {
        this.area = area;
    }

    public Country() {
    }

    public Country(String name, String area) {
        this.name = name;
        this.area = area;
    }
    
    public void input(){
        Scanner input = new Scanner(System.in);
        System.out.print("Tên: ");
        name = input.nextLine();
        System.out.print("Khu vực: ");
        area = input.nextLine();
    }
    
    public void display(){
        System.out.print(toString());
    }

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



Nguyên Phấn Đông [T2008A]
Nguyên Phấn Đông

2021-02-24 05:45:57



package homework1;

public class Test {
    public static void main(String[] args){
        Car car = new Car();
        Computer computer = new Computer();
        House house = new House();
        Mark mark = new Mark("Java", 9.9f);
        Country country = new Country("Viêt Nam", "Châu Á");
        
        car.input();
        computer.input();
        house.input();
        
        car.display();
        computer.display();
        house.display();
        mark.display();
        country.display();
    }    
}



Trần Văn Lâm [T2008A]
Trần Văn Lâm

2021-02-23 13:34:01



public class Country {
    String name;
    Float S;
    String climate;
    public Country(){
    
    }

    public Country(String name, Float S, String climate) {
        this.name = name;
        this.S = S;
        this.climate = climate;
    }

    public String getName() {
        return name;
    }

    public Float getS() {
        return S;
    }

    public String getClimate() {
        return climate;
    }

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

    public void setS(Float S) {
        this.S = S;
    }

    public void setClimate(String climate) {
        this.climate = climate;
    }
    public void inputCoutry(){
        Scanner input = new Scanner(System.in);
        System.out.println("Nhap ten quoc gia:");
        name = input.nextLine();
        System.out.println("Dien tich:");
        S = input.nextFloat();
        System.out.println("Khi hau:");
        climate = input.nextLine();
    }
    public void displayCoutry(){
        System.out.println("Ten quoc gia:" +name);
        System.out.println("Dien tich:" +S);
        System.out.println("Khi hau:" +climate);
    }
}



Trần Văn Lâm [T2008A]
Trần Văn Lâm

2021-02-23 13:33:44



public class Mark {
    String nameStudent;
    String nameObject;
    String mark;
    
    public Mark(){
        
    }

    public Mark(String nameStudent, String nameObject, String mark) {
        this.nameStudent = nameStudent;
        this.nameObject = nameObject;
        this.mark = mark;
    }

    public String getNameStudent() {
        return nameStudent;
    }

    public String getNameObject() {
        return nameObject;
    }

    public String getMark() {
        return mark;
    }

    public void setNameStudent(String nameStudent) {
        this.nameStudent = nameStudent;
    }

    public void setNameObject(String nameObject) {
        this.nameObject = nameObject;
    }

    public void setMark(String mark) {
        this.mark = mark;
    }
    public void inputMark(){
        Scanner input = new Scanner(System.in);
        System.out.println("Nhap ten Hoc Sinh");
        nameStudent  = input.nextLine();
        System.out.println("Nhap ten mon hoc:");
        nameObject = input.nextLine();
        System.out.println("Diem:");
        mark = input.nextLine();
    }
    public void displayMark(){
        System.out.println("Ten hoc sinh:" +nameStudent);
        System.out.println("Ten mon hoc" +nameObject);
        System.out.println("Diem:"+mark);
    }    
    }



Trần Văn Lâm [T2008A]
Trần Văn Lâm

2021-02-23 13:33:19



public class House {
    String address;
    String price;
    String color;
    
    public House(){
    
    }

    public House(String address, String price, String color) {
        this.address = address;
        this.price = price;
        this.color = color;
    }

    public String getAddress() {
        return address;
    }

    public String getPrice() {
        return price;
    }

    public String getColor() {
        return color;
    }

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

    public void setPrice(String price) {
        this.price = price;
    }

    public void setColor(String color) {
        this.color = color;
    }
    
    public void inputHouse(){
        Scanner input = new Scanner(System.in);
        System.out.println("Dia chi:");
        address = input.nextLine();
        System.out.println("Gia ban:");
        price = input.nextLine();
        System.out.println("Mau son:");
        color = input.nextLine();
    }
    
    public void displayHouse(){
        System.out.println("Dia chi:" + address);
        System.out.println("Gia ban:" + price);
        System.out.println("Mau son" + color);
    }



Trần Văn Lâm [T2008A]
Trần Văn Lâm

2021-02-23 13:32:49



public class Computer {
    String name;
    String price;
    
    public Computer(){
    }

    public Computer(String name, String price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public String getPrice() {
        return price;
    }

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

    public void setPrice(String price) {
        this.price = price;
    }
    
    public void inputComputer(){
        Scanner input = new Scanner(System.in);
        System.out.println("Ten:");
        name = input.nextLine();
        System.out.println("Gia ban:");
        price = input.nextLine();
    }

    
    public void displayComputer(){
        System.out.println("Ten:" +name);
        System.out.println("Gia:" +price);
    }



vuong huu phu [T2008A]
vuong huu phu

2021-02-23 13:17: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.
 */
package baittt;

/**
 *
 * @author Admin
 */
public class test_house {
    public static void main(String[] args) {
        House house1 = new House();
        house1.Nhap();
        house1.Hien_thi();
        
           House house2 = new House("b","b",3,89);
    }
 
}



vuong huu phu [T2008A]
vuong huu phu

2021-02-23 13:17:09


bai 2
/*
 * 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 baittt;

import java.util.Locale;
import java.util.Scanner;

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

    String Dia_chi, Chu_nha;
    int Tang, So_nha;

    Scanner scan = new Scanner(System.in);

    public House() {
    }

    public House(String Dia_chi, String Chu_nha, int Tang, int So_nha) {
        this.Dia_chi = Dia_chi;
        this.Chu_nha = Chu_nha;
        this.Tang = Tang;
        this.So_nha = So_nha;
        System.out.format("%s , %s , %d , %d :: ",Dia_chi,Chu_nha,Tang,So_nha );
        System.out.println();
    }

    public String getDia_chi() {
        return Dia_chi;
    }

    public void setDia_chi(String Dia_chi) {
        this.Dia_chi = Dia_chi;
    }

    public String getChu_nha() {
        return Chu_nha;
    }

    public void setChu_nha(String Chu_nha) {
        this.Chu_nha = Chu_nha;
    }

    public int getTang() {
        return Tang;
    }

    public void setTang(int Tang) {
        this.Tang = Tang;
    }

    public int getSo_nha() {
        return So_nha;
    }

    public void setSo_nha(int So_nha) {
        this.So_nha = So_nha;
    }

    public void Nhap() {
        System.out.println("Nhap dia chi : ");
        Dia_chi = scan.nextLine();
        System.out.println("Nhap Ten chu nha : ");
        Chu_nha = scan.nextLine();
        System.out.println("Nhap so tang : ");
        Tang = Integer.parseInt(scan.nextLine());
        System.out.println("Nhap so nha : ");
        So_nha = Integer.parseInt(scan.nextLine());
    }
public void Hien_thi(){
System.out.format("%s , %s , %d , %d :: ",Dia_chi,Chu_nha,Tang,So_nha );
System.out.println();
}
}