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

Java basic- OOP - căn bản - Tổng hợp ví dụ lập trính hướng đổi tượng trong java - mới bắt đầu với OOP

Bài tập 1:

Tạo class Book có các thuộc tính: Tên sách, tác giả số trang, giá tiền.

-       Tạo 2 constructor (1 có tham số và một không có tham số).

-       Tạo phương thức nhập và hiển thị.

Tạo class BookTest và khai báo hàm main.

-       Tạo 2 đối tượng của lớp Book. Một đối tượng được khởi tạo bằng constructor không có tham số và sau đó gọi hàm nhập.

-       Đối tượng còn lại khởi tạo bằng constructor có tham số.

Gọi phương thức hiển thị của 2 đối tượng này.

 

Bài tập 2:

Tạo class Product gồm các thuộc tính:

-       Tên hàng hóa

-       Nhà sản xuất

-       Giá bán

+ Tạo 2 constructor cho lớp này.

+ Cài đặt hàm nhập và hiển thị.

 

Tạo class ProductMenu, khai báo hàm main và tạo menu sau:

1.    Nhập thông tin cho n sản phẩm

2.    Hiển thị thông tin vừa nhập

3.    Sắp xếp thông tin giảm dần theo giá và hiển thị

4.    Thoát.

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

5

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

Nguyễn Hoàng Anh [C1907L]
Nguyễn Hoàng Anh

2020-03-18 13:48:30



/*
 * 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 baitapmoi;
import java.util.*;
/**
 *
 * @author Redmibook 14
 */
 class PriceComparator implements Comparator<Product> {
    public int compare(Product s1,Product s2){
        if(s1.price == s2.price){
            return 0;
        }else if(s1.price > s2.price){
            return 1;
        }else{
            return -1;
        }
    }
}



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

/**
 *
 * @author Redmibook 14
 */
public class Product {

    public String name, manufacturer;
    public float price;
    public Product() {
    }
    public Product(String name, String manufacturer, float price) {
        this.name = name;
        this.manufacturer = manufacturer;
        this.price = price;
    }
    public void add(String name, String manufacturer, float price) {
        this.name = name;
        this.manufacturer = manufacturer;
        this.price = price;
    }
    public void show() {
        System.out.println("Name : " + name);
        System.out.println("Manufacturer : " + manufacturer);
        System.out.println("Price : " + price);
    }
}



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

import java.util.*;
/**
 *
 * @author Redmibook 14
 */
public class ProductMenu {
    public static void sort(){
        
    }
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n;

        ArrayList<Product> arr = new ArrayList<Product>();
        while (true) {
            System.out.println("1.Nhập thông tin cho sản phẩm");
            System.out.println("2.Hiển thị thông tin vừa nhập");
            System.out.println("3.Sắp xếp thông tin giảm dần theo giá và hiển thị");
            System.out.println("4.Thoát.");
            String choice = input.nextLine();
            switch (choice) {
                case "1":
                    System.out.println("Product " + (arr.size() + 1));
                    System.out.println("Name : ");
                    String name = input.nextLine();
                    System.out.println("Manufacturer name : ");
                    String manufacturer = input.nextLine();
                    System.out.println("Price : ");
                    float price = Float.parseFloat(input.nextLine());
                    Product newproduct = new Product(name, manufacturer, price);
                    arr.add(newproduct);
                    
                    break;
                case "2":
                    for (int i = 0; i < arr.size(); i++) {
                        System.out.printf("%n");
                        System.out.println("Product " + (i + 1) + " :");
                       
                        arr.get(i).show();

                    }
                    break;
                case "3":
                  Collections.sort(arr, new PriceComparator());
                   for (int i = 0; i < arr.size(); i++) {
                        System.out.println("Product " + (i + 1) + " :");
                        arr.get(i).show();

                    }
                    break;
                case "4":
                    return;
            }
        }
    }
}



Nguyễn Hoàng Anh [C1907L]
Nguyễn Hoàng Anh

2020-03-18 13:47:58



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

/**
 *
 * @author Redmibook 14
 */
public class Book {

    public String name, author;
    public int pages;
    public float price;

    public void add(String name, String author, int pages, float price) {
        this.name = name;
        this.author = author;
        this.pages = pages;
        this.price = price;
    }
     public Book(String name, String author, int pages, float price) {
        this.name = name;
        this.author = author;
        this.pages = pages;
        this.price = price;
    }
     public Book(){}

    public void show() {
        System.out.println("Name : " + name);
        System.out.println("Author : " + author);
        System.out.println("Pages : " + pages);
        System.out.println("Price : " + price);

    }
}



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

/**
 *
 * @author Redmibook 14
 */
public class BookTest {
    public static void main(String[] args){
        
        Book book = new Book();
        book.add("SomeBook","SomeAuthor", 100, 32000);
        book.show();
        Book book2 = new Book("SomeOtherBook","SomeOtherAuthor", 120, 34000);
        book2.show();
    }
}



Hoàng Quang Huy [C1907L]
Hoàng Quang Huy

2020-03-18 10:45:16



//Bài 1 - Class Book
package aptech;

import java.util.*;

public class Book {

    public String name;
    public String author;
    public int page;
    public int price;

    public Book() {
    }

    public Book(String name, String author, int page, int price) {
        this.name = name;
        this.author = author;
        this.page = page;
        this.price = price;
    }

    public String getName() {
        return name;
    }

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

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPrice() {
        return price;
    }

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

    public void addInfor() {
        Scanner input = new Scanner(System.in);
        System.out.println("Nhập tên sách: ");
        name = input.nextLine();
        System.out.println("Nhập tên tác giả: ");
        author = input.nextLine();
        System.out.println("Nhập số trang: ");
        page = Integer.parseInt(input.nextLine());
        System.out.println("Nhập giá tiền: ");
        price = Integer.parseInt(input.nextLine());
    }

    public void showInfor() {
        System.out.println("Tên sách: " + name);
        System.out.println("Tác giả: " + author);
        System.out.println("Số trang: " + page);
        System.out.println("Giá tiền: " + price);
    }
}

---------------------------------------------------------------------
//BookTest
package aptech;

public class BookTest {

    public static void main(String[] args) {
        Book book1 = new Book();
        Book book2 = new Book();
        
        book1.setAuthor("Nguyễn Văn A");
        book1.setName("Sách 1");
        book1.setPage(50);
        book1.setPrice(20000);
        
        book2.addInfor();
        book1.showInfor();
        book2.showInfor();
    }
}

--------------------------------------------------------------------
// Bài 2 
//- Class Product
package aptech;
import java.util.*;

public class Product {

    public String name;
    public String manufacturer;
    public int price;
    
    public Product() {

    }

    public Product(String name, String manufacturer, int price) {
        this.name = name;
        this.manufacturer = manufacturer;
        this.price = price;
    }

    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 int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }
    
    public void addInfor(){
        Scanner input = new Scanner(System.in);
        System.out.println("Tên hàng hóa: ");
        name = input.nextLine();
        System.out.println("Tên nhà sản xuất: ");
        manufacturer = input.nextLine();
        System.out.println("Giá tiền: ");
        price = Integer.parseInt(input.nextLine());
    }
    public void showInfor() {
        System.out.println("Tên hàng hóa: " + name);
        System.out.println("Tên nhà sản xuất: " + manufacturer);
        System.out.println("Giá tiền: " + price);
    }
}

-------------------------------------
//Class ProductMenu
package aptech;

import java.util.*;

public class ProductMenu {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = 0;
        int choice;
        boolean flag = true;
        ArrayList<Product> pro = new ArrayList<>();
        while (flag) {
            System.out.println("1. Nhập thông tin cho n sản phẩm");
            System.out.println("2. Hiển thị thông tin vừa nhập");
            System.out.println("3. Sắp xếp thông tin giảm dần theo giá và hiển thị");
            System.out.println("4. Thoát.");
            System.out.println("Lựa chọn của bạn: ");
            choice = Integer.parseInt(input.nextLine());
            switch (choice) {
                case 1:
                    System.out.println("Nhập n: ");
                    n = Integer.parseInt(input.nextLine());
                    for (int i = 0; i < n; i++) {
                        Product product = new Product();
                        System.out.println("- Nhập thông tin cho sản phẩm thứ " + (i + 1));
                        product.addInfor();
                        pro.add(product);
                    }
                    break;
                case 2:
                    for (int i = 0; i < pro.size(); i++) {
                        System.out.println("- Thông tin sản phẩm thứ " + (i + 1));
                        pro.get(i).showInfor();
                    }
                    break;
                case 3:
                    for (int i = 0; i < pro.size() - 1; i++) {
                        for (int j = i + 1; j < pro.size(); j++) {
                            if (pro.get(i).getPrice() > pro.get(j).getPrice()) {
                                Collections.swap(pro, i, j);
                            }
                        }
                    }
                    System.out.println("- Thông tin sản phẩm sau khi sắp xếp: ");
                    for (int i = 0; i < pro.size(); i++) {
                        pro.get(i).showInfor();
                        System.out.println("-----------------");
                    }
                    break;
                case 4:
                    System.out.println("Thoát chương trình");
                    flag = false;
                    break;
                default:
                    System.out.println("Lựa chọn không hợp lệ");
                    break;
            }
        }
    }
}




trung [C1907L]
trung

2020-03-17 16:51:09



package test;

import java.util.Scanner;

/**
 *
 * @author prdox
 */
public class Book {
    String tenSach;
    String tacGia;
    Integer soTrang;
    Integer gia;

    public Book() {
        
    }

    public Book(String tenSach, String tacGia, Integer soTrang, Integer gia) {
        this.tenSach = tenSach;
        this.tacGia = tacGia;
        this.soTrang = soTrang;
        this.gia = gia;
    }
    
    public void nhap(){
        Scanner input = new Scanner(System.in);
        System.out.println("Nhap vao ten sach: ");
        this.tenSach = input.nextLine();
        System.out.println("Nhap vao tac gia: ");
        this.tacGia = input.nextLine();
        System.out.println("Nhap vao so trang: ");
        this.soTrang = Integer.parseInt(input.nextLine());
        System.out.println("Nhap vao ten sach: ");
        this.gia = Integer.parseInt(input.nextLine());
    }
    
    public void hienThi(){
        System.out.format("ten sach: %s,",this.tenSach);
        System.out.format("tac gia: %s,",this.tacGia);
        System.out.format("so trang: %d,",this.soTrang);
        System.out.format("ten sach: %d,",this.gia);
    }
}

package test;

import java.util.Scanner;
import java.lang.Math;
import java.util.ArrayList;

/**
 *
 * @author prdox
 */

//Viết chương trình hiển thị thông tin cá nhân của bạn bao gồm (tên, tuổi, địa chỉ, email, sđt)
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Book book1 = new Book();
        Book book2 = new Book("Chiec la cuoi cung","O Henry",30,40000);
        book1.nhap();
        book1.hienThi();
        book2.hienThi();
    }

}






package test;

import java.util.Scanner;

/**
 *
 * @author prdox
 */
public class Product {
    String tenHangHoa;
    String nhaSanXuat;
    Integer giaBan;

    public Product() {
    }

    public Product(String TenHangHoa, String NhaSanXuat, Integer GiaBan) {
        this.tenHangHoa = TenHangHoa;
        this.nhaSanXuat = NhaSanXuat;
        this.giaBan = GiaBan;
    }
    
    public void nhap() {
        Scanner input = new Scanner(System.in);
        System.out.println("Nhap vao ten hang hoa: ");
        this.tenHangHoa = input.nextLine();
        System.out.println("Nhap vao nha san xuat: ");
        this.nhaSanXuat = input.nextLine();
        System.out.println("Nhap vao gia ban: ");
        this.giaBan = Integer.parseInt(input.nextLine());
    }
    
    public void hienThi() {
        Scanner input = new Scanner(System.in);
        System.out.format("ten hang hoa: %s\n",this.tenHangHoa);
        System.out.format("nha san xuat: %s\n",this.nhaSanXuat);
        System.out.format("gia ban: %d\n",this.giaBan);
    }
    
}

package test;

import java.util.Scanner;
import java.lang.Math;
import java.util.ArrayList;

/**
 *
 * @author prdox
 */
//Viết chương trình hiển thị thông tin cá nhân của bạn bao gồm (tên, tuổi, địa chỉ, email, sđt)
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int N = 0;
        ArrayList<Product> arr = new ArrayList<Product>();
        boolean exit = false;
        while (!exit) {
            System.out.println("1. Nhập thông tin cho n sản phẩm");
            System.out.println("2. Hiển thị thông tin vừa nhập");
            System.out.println("3. Sắp xếp thông tin giảm dần theo giá và hiển thị");
            System.out.println("4. Thoát.");
            int option = Integer.parseInt(input.nextLine());
            switch (option) {
                case 1:
                    System.out.println("N:= ");
                    N = Integer.parseInt(input.nextLine());
                    for (int i = 0; i < N; i++) {
                        Product temp = new Product();
                        temp.nhap();
                        arr.add(temp);
                    }
                    break;
                case 2:
                    for (int i = 0; i < N; i++) {
                        arr.get(i).hienThi();
                    }
                    break;
                case 3:
                    for (int i = 0; i < N - 1; i++) {
                        for (int j = i + 1; j < N; j++) {
                            if (arr.get(i).giaBan<arr.get(j).giaBan){
                                Product temp = arr.get(j);
                                arr.set(j, arr.get(i));
                                arr.set(i,temp);
                            }
                        }
                    }
                    for (int i = 0; i < N; i++) {
                        arr.get(i).hienThi();
                    }
                    break;
                case 4:
                    exit = true;
                    break;

            }
        }
    }

}



Vũ Việt Đức [C1907L]
Vũ Việt Đức

2020-03-16 16:50:55



package aptech;
import java.util.Scanner;
// Bài 2:
class Product {
    public String tenHangHoa, nhaSanXuat;
    public int giaBan;

    public Product(String tenHangHoa, String nhaSanXuat, int giaBan) {
        this.tenHangHoa = tenHangHoa;
        this.nhaSanXuat = nhaSanXuat;
        this.giaBan = giaBan;
    }

    public Product() {
    }

    public String getTenHangHoa() {
        return tenHangHoa;
    }

    public void setTenHangHoa(String tenHangHoa) {
        this.tenHangHoa = tenHangHoa;
    }

    public String getNhaSanXuat() {
        return nhaSanXuat;
    }

    public void setNhaSanXuat(String nhaSanXuat) {
        this.nhaSanXuat = nhaSanXuat;
    }

    public int getGiaBan() {
        return giaBan;
    }

    public void setGiaBan(int giaBan) {
        this.giaBan = giaBan;
    }
    
}

public class Homework {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n;
        Product[] product = new Product[n];
        
        while(true) {
            System.out.println("\nVui lòng trọn: \n1.Nhập thông tin cho n sẩn phẩm\n2.Hiển thị thông tin vừa nhập\n3.Sắp xếp thông tin giảm dần và hiển thị\n4.Thoát");
            int chon = Integer.parseInt(input.nextLine());
            switch(chon) {
                case 1: 
                    System.out.print("Nhập số lượng sản phẩm: ");
                    n = Integer.parseInt(input.nextLine());
                    for(int i = 0; i < n; i++) {
                        System.out.print("Nhập tên hàng hóa: ");
                        product[i].setTenHangHoa(input.nextLine());
                        System.out.print("Nhà sản xuất: ");
                        product[i].setNhaSanXuat(input.nextLine());
                        System.out.print("Nhập giá bán: ");
                        product[i].setGiaBan(Integer.parseInt(input.nextLine()));
                    }
                    break;
                case 2: 
                    System.out.println("Thông tin danh sách hàng hóa:");
                    for(int i = 0; i < n; i++){
                        System.out.println("Tên hàng hóa: " + product[i].getTenHangHoa() + "Nhà sản xuất: " + product[i].getNhaSanXuat() + "Giá: " + product[i].getGiaBan());
                    }
                    break;
                case 3: 
                    for(int i = 0; i < n; i++) {
                        for(int y = i + 1; y < n; y++) {
                            if(product[i].getGiaBan() < product[y].getGiaBan()){
                                Product temp = product[i];
                                product[i] = product[y];
                                product[y] = temp;
                            }
                        }
                    }
                    System.out.println("Thông tin danh sách hàng hóa sau khi sắp xếp giảm dần: ");
                    for(int i = 0; i < n; i++){
                        System.out.println("Tên hàng hóa: " + product[i].getTenHangHoa() + "Nhà sản xuất: " + product[i].getNhaSanXuat() + "Giá: " + product[i].getGiaBan());
                    }
                    break;
                case 4: 
                    break;
                default:
                    System.out.println("Vui lòng chọn các số trên!");
            }
            
            if(chon == 4) {
                break;
            }
        }


    }
}



Vũ Việt Đức [C1907L]
Vũ Việt Đức

2020-03-16 16:16:19



package aptech;
import java.util.Scanner;
// Bài 1:
class Book {
    String tenSach, tacGia;
    int soTrang, giaTien;

    public Book() {
    }

    public Book(String tenSach, String tacGia, int soTrang, int giaTien) {
        this.tenSach = tenSach;
        this.tacGia = tacGia;
        this.soTrang = soTrang;
        this.giaTien = giaTien;
    }

    public String getTenSach() {
        return tenSach;
    }

    public void setTenSach(String tenSach) {
        this.tenSach = tenSach;
    }

    public String getTacGia() {
        return tacGia;
    }

    public void setTacGia(String tacGia) {
        this.tacGia = tacGia;
    }

    public int getSoTrang() {
        return soTrang;
    }

    public void setSoTrang(int soTrang) {
        this.soTrang = soTrang;
    }

    public int getGiaTien() {
        return giaTien;
    }

    public void setGiaTien(int giaTien) {
        this.giaTien = giaTien;
    }
    
    public void show() {
        System.out.println("Thông tin sách: \nTên sách: " + this.tenSach + "\nTác giả: " + this.tacGia + "\nSố trang: " + this.soTrang + "\nGía tiền: " + this.giaTien );       
    }
}

public class BookTest {
    public static void main(String[] args) {
        Book book = new Book("Tấu hài","Vô danh", 999, 99999);
        book.show();
    }
}



Lê Minh Bắc [T1907A]
Lê Minh Bắc

2020-03-16 12:47:38

Bài 1:

package BT441;
import java.util.Scanner;
public class Book {
    String bookName;
    String bookAuthor;
    int pages;
    float price;        

    public Book() {
    }

    public Book(String bookName, String bookAuthor, int pages, float price) {
        this.bookName = bookName;
        this.bookAuthor = bookAuthor;
        this.pages = pages;
        this.price = price;
    }
    
    public void input() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhập tên sách: ");
        bookName = scan.nextLine();
        System.out.println("Nhập tên tác giả: ");
        bookAuthor = scan.nextLine();
        System.out.println("Nhập số trang: ");
        pages = Integer.parseInt(scan.nextLine());
        System.out.println("Nhap gia: ");
        price = Float.parseFloat(scan.nextLine());
    }
    
    public void display() {
        System.out.println("Tên sách: " + bookName);
        System.out.println("Tác giả: " + bookAuthor);
        System.out.println("Số trang: " + pages + " trang");
        System.out.println("Giá: " + price + " đồng");
    } 
}



package BT441;

public class BookTest {
    public static void main(String[] agrs) {
        Book book = new Book();
        book.input();
        Book bok = new Book("Bắc đẹp trai", "Lê Minh Bắc", 120,125000);
        book.display();
        bok.display();
    }
}

Bài 2:
package BT442;
import java.util.Scanner;
public class Product {
    String productName;
    String producer;
    float price;

    public Product() {
    }

    public Product(String productName, String producer, float price) {
        this.productName = productName;
        this.producer = producer;
        this.price = price;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getProducer() {
        return producer;
    }

    public void setProducer(String producer) {
        this.producer = producer;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }
    
    
    
    public void input() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhập tên sản phẩm: ");
        productName = scan.nextLine();
        System.out.println("Nhập tên nhà sản xuất: ");
        producer = scan.nextLine();
        System.out.println("Nhap giá: ");
        price = Float.parseFloat(scan.nextLine());
    }
    
    public void display() {
        System.out.println("Sản phẩm: " + productName);
        System.out.println("Nhà sản xuất: " + producer);
        System.out.println("Giá: " + price + " đồng");
    } 
}



package BT442;
import java.util.*;
public class ProductMenu {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int choose;
        ArrayList<Product> list = new ArrayList<>();
        do {
            showMenu();
            choose = scan.nextInt();
            switch (choose) {
                case 1:
                    System.out.print("Nhập số sản phẩm N: ");
                    int n = scan.nextInt();
                    for (int i = 0; i < n; i++) {
                        Product product = new Product();
                        System.out.println("Thêm sản phẩm thứ " + (i+1) + " : ");
                        product.input();
                        list.add(product);
                    }
                    break;
                case 2:
                    for (int i = 0; i < list.size(); i++) {
                        list.get(i).display();
                    }
                    break;
                case 3:
                    Collections.sort(list, new Comparator<Product>() {
                        @Override
                        public int compare(Product t0, Product t1) {
                            if (t0.getPrice() < t1.getPrice()) {
                                return 1;
                            }
                            return -1;
                        }
                    });
                    for (int i = 0; i < list.size(); i++) {
                        list.get(i).display();
                    }
                    break;
                case 4:
                    System.out.println("Thoát!!!!");
                    break;
                default:
                    System.err.println("Nhập lỗi mời nhập lại!!!");
                    break;
            }

        } while (choose != 4);

    }

    public static void showMenu() {
        System.out.println("1.Nhập thông tin cho n sản phẩm");
        System.out.println("2.Hiển thị thông tin vừa nhập");
        System.out.println("3.Sắp xếp thông tin giảm dần theo giá và hiển thị");
        System.out.println("4. Thoát.");
        System.out.print("Nhập lựa chọn: ");
    }
}