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.
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![Nguyễn Chí Tâm [java2_online,java1_online]](https://www.gravatar.com/avatar/bb48a776ff978c8a4f8136db2be4f945.jpg?s=80&d=mm&r=g)
Nguyễn Chí Tâm
2022-12-19 04:33:50
import java.util.ArrayList;
import java.util.Scanner;
public class TestProduct {
public static void main(String[] args) {
Scanner scan0 = new Scanner(System.in);
ArrayList<Product> list = new ArrayList<>();
int choose;
do{
Getmenu();
choose = scan0.nextInt();
switch (choose){
case 1 :{
Product pro = new Product();
pro.Nhap();
list.add(pro);
}break;
case 2 : {
for(Product a : list){
a.Display();
}
}break;
case 3:{
Product t;
for (int i=0;i<list.size()-1;i++){
for (int j=0;j<list.size()-1;j++){
if (list.get(j).gia<list.get(j+1).gia ){
t=list.get(j);
list.remove(j);
list.add(j+1, t);
}
}
}
for (Product x : list){
x.Display();
}
}break;
}}while (choose !=4);
}
public static void Getmenu(){
System.out.println("Menu: \n 1. Nhap thong tin san pham \n 2. Hien thi thong tin san pham \n "
+ "3. Thong tin giamr dan theo gia \n 4. thoat \n choose:");
}
}
// create class product://
import java.util.Scanner;
public class Product {
String tenhh,nhasx;
int gia;
public Product() {
}
public Product(String tenhh, String nhasx, int gia) {
this.tenhh = tenhh;
this.nhasx = nhasx;
this.gia = gia;
}
public void Nhap(){
Scanner scan0= new Scanner(System.in);
System.out.println("Nhap ten hang hoa: "); tenhh = scan0.nextLine();
System.out.println("Nhap ten nha san xuat: "); nhasx = scan0.nextLine();
System.out.println("Nhap gia hang hoa: "); gia= scan0.nextInt();
}
public void Display(){
System.out.println(tenhh+" "+nhasx+" "+gia);
}
}
![Hoàng Anh [community,C2010G]](https://www.gravatar.com/avatar/e73c31efff649e599b1e4320b6bfb1a9.jpg?s=80&d=mm&r=g)
Hoàng Anh
2021-07-04 16:27:13
Bài 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 javaapplication4;
import java.util.Scanner;
/**
*
* @author Bom1
*/
public class Book {
String name;
String author;
int page;
double cost;
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 double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost;
}
public void input(){
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 = input.nextInt();
System.out.println("Nhập giá sách: ");
cost = input.nextDouble();
}
public void display(){
System.out.format("\nTên sách: %s\n Tên tác giả: %s\n Số trang: %d\n Giá sách: %f", name,author,page,cost);
}
}
BookTest.java
/*
* 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 javaapplication4;
/**
*
* @author Bom1
*/
public class BookTest {
public static void main(String[] args) {
Book book = new Book();
book.input();
book.display();
Book book1 = new Book();
book1.setName("300 bài code thiếu nhi");
book1.setAuthor("Bờm");
book1.setPage(100);
book1.setCost(200.000);
book1.display();
}
}
![TRẦN VĂN ĐIỆP [Teacher]](https://www.gravatar.com/avatar/fc6ba9324e017d540af3613b3a77dd21.jpg?s=80&d=mm&r=g)
TRẦN VĂN ĐIỆP
2021-01-29 07:30:19
#ProductMenu.java
/*
* 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 aptech.lesson04;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
/**
*
* @author Diep.Tran
*/
public class ProductMenu {
public static void main(String[] args) {
ArrayList<Product> produceList = new ArrayList<>();
Scanner scan = new Scanner(System.in);
int choose, n;
do {
showMenu();
choose = Integer.parseInt(scan.nextLine());
switch (choose) {
case 1:
System.out.println("Nhap so phan tu can them N: ");
n = Integer.parseInt(scan.nextLine());
// Product product = new Product(); Khai bao sai
for (int i = 0; i < n; i++) {
Product product = new Product();
product.input();
produceList.add(product);
}
break;
case 2:
for (int i = 0; i < produceList.size(); i++) {
produceList.get(i).display();
}
break;
case 3:
//Sap xep
Collections.sort(produceList, new Comparator<Product>() {
@Override
public int compare(Product o1, Product o2) {
if(o1.getPrice() < o2.getPrice()) {
return 1;
}
return -1;
}
});
for (int i = 0; i < produceList.size(); i++) {
produceList.get(i).display();
}
break;
case 4:
System.out.println("Thoat!!!");
break;
default:
System.out.println("Nhap sai!!!");
break;
}
} while (choose != 4);
}
static void showMenu() {
System.out.println("1. Nhap N san pham");
System.out.println("2. Hien thi danh sach san pham");
System.out.println("3. Sap xep");
System.out.println("4. Thoat");
System.out.println("Chon: ");
}
}
#Product.java
/*
* 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 aptech.lesson04;
import java.util.Scanner;
/**
*
* @author Diep.Tran
*/
public class Product {
String name, manufacturerName;
float price;
public Product() {
}
public Product(String name, String manufacturerName, float price) {
this.name = name;
this.manufacturerName = manufacturerName;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getManufacturerName() {
return manufacturerName;
}
public void setManufacturerName(String manufacturerName) {
this.manufacturerName = manufacturerName;
}
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("Nhap ten: ");
name = scan.nextLine();
System.out.println("Nhap NSX: ");
manufacturerName = scan.nextLine();
System.out.println("Nhap gia: ");
price = Float.parseFloat(scan.nextLine());
}
public void display() {
// System.out.println(this);
// String str = toString();
// System.out.println(str);
// System.out.println(toString());
// System.out.println("Product{" + "name=" + name + ", manufacturerName=" + manufacturerName + ", price=" + price + '}');
System.out.println("Name: " + name + ", manufacturerName: " + manufacturerName + ", price: " + price);
}
@Override
public String toString() {
return "Product{" + "name=" + name + ", manufacturerName=" + manufacturerName + ", price=" + price + '}';
}
}
![vuong huu phu [T2008A]](https://www.gravatar.com/avatar/307a5cf29780afab49706dc8b15b86c6.jpg?s=80&d=mm&r=g)
vuong huu phu
2021-01-26 04:00:09
Book.java
/*
* 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 buoi.pkg2;
import java.util.Scanner;
/**
*
* @author Admin
*/
public class Book{
public String ten_sach;
public String tac_gia;
public int so_trang;
public float gia_tien;
Book() {
}
Book(String a, String tacg, int i, float d) {
ten_sach = a;
tac_gia = tacg;
so_trang = i;
gia_tien = d;
System.out.println("Ten sach: "+ten_sach);
System.out.println("Ten tac gia: "+tac_gia);
System.out.println("So trang: "+so_trang);
System.out.println("Giatien: "+gia_tien);
}
public void khongcothamso() {
Scanner scan = new Scanner(System.in);
System.out.print("nhap ten sach: ");
ten_sach = scan.nextLine();
System.out.print("nhap ten tac gia: ");
tac_gia = scan.nextLine();
System.out.print("nhap so trang: ");
so_trang = scan.nextInt();
System.out.print("nhap gia tien: ");
gia_tien = scan.nextFloat();
System.out.println();
System.out.println("Ten sach: "+ten_sach);
System.out.println("Ten tac gia: "+tac_gia);
System.out.println("So trang: "+so_trang);
System.out.println("Giatien: "+gia_tien);
}
}
![vuong huu phu [T2008A]](https://www.gravatar.com/avatar/307a5cf29780afab49706dc8b15b86c6.jpg?s=80&d=mm&r=g)
vuong huu phu
2021-01-26 03:59:12
main
/*
* 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 buoi.pkg2;
/**
*
* @author Admin
*/
public class book_test {
public static void main(String[] args) {
Book book = new Book();
book.khongcothamso();
System.out.println();
Book book1 = new Book("abcd","tac gia 1",12, (float) 14.000);
}
}
![Đặng Trần Nhật Minh [T2008A]](https://www.gravatar.com/avatar/ee8dc5a777ad26f3a962e86c233437cf.jpg?s=80&d=mm&r=g)
Đặng Trần Nhật Minh
2021-01-25 09:48:28
package javalesson3.Book;
import java.util.Scanner;
public class Book {
public String name;
public String author;
public int num;
public double price;
public Book() {
}
public Book(String name, String author, int num, double price) {
this.name = name;
this.author = author;
this.num = num;
this.price = price;
}
public void input() {
Scanner r = new Scanner(System.in);
System.out.println("Name: ");
this.name = r.nextLine();
System.out.println("Author: ");
this.author = r.nextLine();
System.out.println("Num: ");
this.num = r.nextInt();
System.out.println("Price: ");
this.price = r.nextDouble();
}
public void output() {
System.out.println("Name: " + name);
System.out.println("Author: " + author);
System.out.println("Num: " + num);
System.out.println("Price: " + price);
}
}
#Test.classpackage javalesson3.Book;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner r = new Scanner(System.in);
Book book1 = new Book("The Miserable", "Victor Hugo", 1521, 15.234);
Book book2 = new Book();
book2.input();
book1.output();
book2.output();
}
}
![Nguyễn Tiến Đạt [T2008A]](https://www.gravatar.com/avatar/b5819cd0adc95c727c7ad0c2bcf6098b.jpg?s=80&d=mm&r=g)
Nguyễn Tiến Đạt
2021-01-25 09:04:13
/*
* 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 lesson3;
import java.util.Scanner;
/**
*
* @author MyPC
*/
public class BookTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Book book1 = new Book();
book1.bookName = scan.nextLine();
book1.authorName = scan.nextLine();
book1.pageNumber = scan.nextInt();
book1.price = scan.nextInt();
book1.Display();
Book book2 = new Book("De men phieu luu ky", "To Hoai", 300, 2032001);
book2.Display();
}
}
/*
* 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 lesson3;
/**
*
* @author MyPC
*/
public class Book {
public String bookName , authorName;
public int pageNumber , price;
public Book(){
}
public Book(String bookName , String authorName , int pageNumber , int price){
this.bookName = bookName;
this.authorName = authorName;
this.pageNumber = pageNumber;
this.price = price;
}
public void Display(){
System.out.format("\nName of book: %s\nName of author: %s\nNumber of pages: %d\nPrice: %d\n", bookName,authorName,pageNumber,price);
}
}
![Nguyễn Huy Hoàng [APROTRAIN_ADF]](https://www.gravatar.com/avatar/1f5dda927d51fc67ea7bb633c72baa79.jpg?s=80&d=mm&r=g)
Nguyễn Huy Hoàng
2020-06-04 14:06:24
import java.util.Scanner;
/**
*
* @author std
*/
public class Book {
String bookname;
String author;
int pageNo;
double price;
public Book() {
}
public Book(String bookname, String author, int pageNo, double price) {
this.bookname = bookname;
this.author = author;
this.pageNo = pageNo;
this.price = price;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getPageNo() {
return pageNo;
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public void input(){
Scanner scanner = new Scanner(System.in);
System.out.println("Bookname");
bookname = scanner.nextLine();
System.out.println("Author");
author = scanner.nextLine();
System.out.println("Page numbers");
pageNo = Integer.parseInt(scanner.nextLine());
System.out.println("Price");
price = Double.parseDouble(scanner.nextLine());
}
public void show(){
System.out.println(bookname + author + pageNo + price);
}
}
public class BookTest {
public static void main(String[] args) {
Book book2 = new Book();
book2.input();
Book book = new Book("ABC","abc",100,56.78);
book2.show();
book.show();
}
}
![cuonglee [C1907L]](https://www.gravatar.com/avatar/b8c48ea2d04ab5a37bda2c50310d8a5c.jpg?s=80&d=mm&r=g)
cuonglee
2020-04-05 14:53:04
/*
* 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 QuanLiHangHoa;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author Admin
*/
public class ProductMenu {
public static void main(String[] args) {
Scanner add = new Scanner(System.in);
ArrayList<Product> list = new ArrayList();// khoi tao mot array list
int choose,n;
do{
show();
choose = add.nextInt();
switch(choose){
case 1:
System.out.println("nhap thong tin nsan pham");
System.out.println("nhap n");
n = add.nextInt();
for(int i = 0; i < n; i++){
Product prd = new Product();
prd.nhap();
list.add(prd);
}
break;
case 2:
System.out.println("hien thi thong tin san pham");
for(int i = 0; i < list.size(); i++){
list.get(i).xuat();
}
break;
case 3:
System.out.println("sap xep");
}
}while(choose != 4);
}
public static void show(){
System.out.println("1. Nhap thong tin N san pham.");
System.out.println("2. Hien thi thong tin vua nhap.");
System.out.println("3. Sap xem thong tin giam dan theo gia va hien thi");
System.out.println("4. Thoat");
System.out.println("==============================");
}
}
/*
* 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 QuanLiHangHoa;
import java.util.Scanner;
/**
*
* @author Admin
*/
public class Product {
String tenHangHoa,nhaSanXuat;
int giaBan;
int n;
public Product() {
}
public Product(String tenHangHoa, String nhaSanXuat, int giaBan) {
this.tenHangHoa = tenHangHoa;
this.nhaSanXuat = nhaSanXuat;
this.giaBan = giaBan;
}
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 void nhap(){
Scanner input = new Scanner(System.in);
System.out.println("nhap ten hang hoa");
tenHangHoa = input.nextLine();
System.out.println("nhap nha san xuat");
nhaSanXuat = input.nextLine();
System.out.println("nhap gia ban");
giaBan = input.nextInt();
}
public void xuat(){
System.out.println("ten hang hoa: " + tenHangHoa);
System.out.println("nha san xuat: " + nhaSanXuat);
System.out.println("gia ban: " + giaBan);
}
}
![cuonglee [C1907L]](https://www.gravatar.com/avatar/b8c48ea2d04ab5a37bda2c50310d8a5c.jpg?s=80&d=mm&r=g)
cuonglee
2020-04-03 14:34:39
/*
* 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 quanLiBook;
/**
*
* @author Admin
*/
public class BookTest {
public static void main(String[] args) {
Book bk1 = new Book();
Book bk2 = new Book();
bk1.nhap();
bk1.xuat();
bk2.xuat();
}
}
/*
* 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 quanLiBook;
import java.util.Scanner;
/**
*
* @author Admin
*/
public class Book {
String tenSach,tacGia;
int soTrang,soTien;
public Book(){
}
public Book(String tenSach, String tacGia, int soTrang, int soTien) {
this.tenSach = tenSach;
this.tacGia = tacGia;
this.soTrang = soTrang;
this.soTien = soTien;
}
public void nhap(){
Scanner add = new Scanner(System.in);
System.out.println("nhap ten sach");
tenSach = add.nextLine();
System.out.println("nhap ten tac gia");
tacGia = add.nextLine();
System.out.println("nhap so trang");
soTrang = add.nextInt();
System.out.println("nhap so tien");
soTien = add.nextInt();
}
public void xuat(){
System.out.println(tenSach);
System.out.println(tacGia);
System.out.println(soTrang);
System.out.println(soTien);
}
}