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

Bài tập Employee & StudentMark & Product trong lập trình OOP- lập trình Java căn bản - Java basic BT992

Bài 1.

Cài đặt lớp Employee gồm các thông tin:

-       Họ tên             (String)

-       Giới tính          (String)

-       Quê quán        (String)

-       Chức vụ          (String)

-       Lương             (double)

 

Cài đặt 2 constructors.

Cài đặt các phương thức set/get cho các thuộc tính

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

Cài đặt lớp Test, có hàm main:

Khai báo 2 đối tượng của lớp.

1 đối tượng gọi constructor có tham số. 1 đối tượng gọi constructor không có tham số (phải gọi hàm nhập để lấy thông tin).

Gọi hàm hiển thị để hiển thị kết quả.

 

Bài 2.

Cài đặt lớp StudentMark gồm các thông tin:

-       Rollnumber

-       Họ tên

-       Lớp

-       Môn

-       Điểm.

 

Cài đặt đầy đủ: 2 constructor, các phương thức set/get.

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

Khai báo luôn hàm main trong lớp này:

Khai báo 2 đối tượng của lớp, khởi tạo 2 đối tượng bằng construcor không có tham số. Gọi hàm nhập để nhập vào các thông tin và hàm hiển thị để hiển thị các thông tin.

Hiển thị thông tin của người có điểm cao nhất.

 

Bài 3:

Cài đặt lớp Product gồm các thuộc tính (phải khai báo là private)

-       String maHH;

-       String tenHH;

-       float soLuong;

-       float gia1SP;

Cài đặt 2 construcors, các hàm get/set.

Cài đặt hàm input(), display().

Khai báo hàm main và thực hiện như sau:

-       Khai báo mảng có n phần tử kiểu Product.

-       Gọi nhập thông tin cho các phần tử của mảng.

-       Tìm ra sản phẩm nào có giá bán cao nhất.

Liên kết rút gọn:

https://gokisoft.com/992

Bình luận

avatar
Hoàng Anh [C2010G]
2022-06-30 12:11:21



/*
 * 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 BaiTap.baitap992.Bai3;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

/**
 *
 * @author by bom1
 */
public class Product {

    private String maHH;
    private String tenHH;
    private float soLuong;
    private float gia1SP;

    public Product() {
    }

    public Product(String maHH, String tenHH, float soLuong, float gia1SP) {
        this.maHH = maHH;
        this.tenHH = tenHH;
        this.soLuong = soLuong;
        this.gia1SP = gia1SP;
    }

    public String getMaHH() {
        return maHH;
    }

    public void setMaHH(String maHH) {
        this.maHH = maHH;
    }

    public String getTenHH() {
        return tenHH;
    }

    public void setTenHH(String tenHH) {
        this.tenHH = tenHH;
    }

    public float getSoLuong() {
        return soLuong;
    }

    public void setSoLuong(float soLuong) {
        this.soLuong = soLuong;
    }

    public float getGia1SP() {
        return gia1SP;
    }

    public void setGia1SP(float gia1SP) {
        if (gia1SP >= 0) {
            this.gia1SP = gia1SP;
        } else {
            System.err.println("Nhập sai giá tiền hàng");
        }
    }

    public void input() {
        Scanner input = new Scanner(System.in);

        System.out.println("Nhập mã hàng hóa: ");
        maHH = input.nextLine();
        System.out.println("Nhập tên hàng hóa: ");
        tenHH = input.nextLine();
        System.out.println("Nhập số lượng hàng hóa: ");
        soLuong = Float.parseFloat(input.nextLine());
        System.out.println("Nhập giá hàng hóa: ");
        gia1SP = Float.parseFloat(input.nextLine());
    }

    public void display() {
        System.out.format("Mã: %s \nTên: %S Số lượng: %f \nGiá: %f", maHH, tenHH, soLuong, gia1SP);
    }

    public static void main(String[] args) {
        ArrayList<Product> product = new ArrayList<>();
        Scanner add = new Scanner(System.in);

        System.out.println("Nhập số hàng cần thêm vào: ");
        int n = Integer.parseInt(add.nextLine());

        for (int i = 0; i < n; i++) {
            Product p = new Product();
            p.input();

            product.add(p);
        }
        Collections.sort(product, new Comparator<Product>(){
            @Override
            public int compare(Product o1, Product o2){
                if(o1.gia1SP < o2.gia1SP){
                    return 1;
                }else{
                    return -1;
                }
            } 
        });
        System.out.println("Sản phẩm có giá bán cao nhất: ");
        for(Product products: product){
            products.display();
        }
    }
}


avatar
Hoàng Anh [C2010G]
2022-06-30 12:06:22



/*
 * 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 BaiTap.baitap992.Bai2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

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

    int rollNumber;
    String name;
    String className;
    String subject;
    double marks;

    public StudentMark() {
    }

    public StudentMark(int rollNumber, String name, String className, String subject, double marks) {
        this.rollNumber = rollNumber;
        this.name = name;
        this.className = className;
        this.subject = subject;
        this.marks = marks;
    }

    public int getRollNumber() {
        return rollNumber;
    }

    public void setRollNumber(int rollNumber) {
        this.rollNumber = rollNumber;
    }

    public String getName() {
        return name;
    }

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

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public String getSubject() {
        return subject;
    }

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

    public double getMarks() {
        return marks;
    }

    public void setMarks(double marks) {
        if (marks < 0) {
            System.err.println("Điểm tối thiểu là 0");
        } else if (marks > 10) {
            System.err.println("Điểm tối đa là 10");
        } else {
            this.marks = marks;
        }
    }

    public void input() {
        Scanner input = new Scanner(System.in);

        System.out.println("Nhập mã số của học sinh: ");
        rollNumber = Integer.parseInt(input.nextLine());
        System.out.println("Nhập tên của học sinh: ");
        name = input.nextLine();
        System.out.println("Nhập lớp học: ");
        className = input.nextLine();
        System.out.println("Nhập môn học: ");
        subject = input.nextLine();
        System.out.println("Nhập điểm: ");
        marks = Double.parseDouble(input.nextLine());
    }

    public void display() {
        System.out.format("Rollnum: %d \nTên: %s \nLớp: %s \nMôn học: %s \nĐiểm: %f", rollNumber, name, className, subject, marks);
        System.out.println("========================================================");
    }

    public static void main(String[] args) {
        StudentMark student1 = new StudentMark();

        student1.setRollNumber(1);
        student1.setName("bom");
        student1.setClassName("2108L");
        student1.setSubject("HTML");
        student1.setMarks(10);
        student1.display();

        ArrayList<StudentMark> studentmarks = new ArrayList<>();

        Scanner add = new Scanner(System.in);

        System.out.println("Nhập số học sinh cần thêm: ");
        int n = Integer.parseInt(add.nextLine());
        for (int i = 0; i < n; i++) {
            StudentMark student2 = new StudentMark();
            student2.input();

            studentmarks.add(student2);
            
            
            Collections.sort(studentmarks, new Comparator<StudentMark>() {
                @Override
                public int compare(StudentMark o1, StudentMark o2) {
                    if (o1.marks < o2.marks) {
                        return 1;
                    } else {
                        return -1;
                    }
                }
            });
            System.out.println("Thông tin học sinh có số điểm cao nhất là :");
            for (StudentMark studentmark : studentmarks) {
                student2.display();
            }
        }

    }
}


avatar
Hoàng Anh [C2010G]
2022-06-29 17:31:17



/*
 * 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 BaiTap.baitap992.Bai1;

import java.util.Scanner;

/**
 *
 * @author by Bom1
 */
public class Employee {

    String name;
    String gender;
    String address;
    String position;
    double salary;

    public Employee() {
    }

    public Employee(String name, String gender, String address, String position, double salary) {
        this.name = name;
        this.gender = gender;
        this.address = address;
        this.position = position;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

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

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getAddress() {
        return address;
    }

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

    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        if (salary < 0) {
            System.err.println("Mức lương tối thiểu là 0");
        } else {
            this.salary = salary;
        }
    }

    public void input() {
        Scanner input = new Scanner(System.in);

        System.out.println("Nhập tên của nhân viên: ");
        name = input.nextLine();
        System.out.println("Nhập giới tính của nhân viên: ");
        gender = input.nextLine();
        System.out.println("Nhập quê quán của nhân viên: ");
        address = input.nextLine();
        System.out.println("Nhập chức vụ của nhân viên: ");
        position = input.nextLine();
        System.out.println("Nhập mức lương của nhân viên: ");
        salary = Double.parseDouble(input.nextLine());
    }

    public void display() {
        System.out.format("Tên: %s \nGiới tính: %s \nQuê quán: %s \nChức vụ: %s \nLương: %f", name, gender, address, position, salary);
        System.out.println("========================================================");
    }
}



/*
 * 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 BaiTap.baitap992.Bai1;

/**
 *
 * @author by Bom1
 */
public class Test {
    public static void main(String[] args) {
        Employee test1 = new Employee();
        test1.setName("Bờm");
        test1.setGender("Nam");
        test1.setAddress("Hà Nội");
        test1.setPosition("Học viên");
        test1.setSalary(200000);
        
        test1.display();
        
        
        Employee test2 = new Employee();
        test2.input();
        test2.display();
    }
}


avatar
Trần Việt Đức Anh [C2010L]
2021-07-08 04:06:32

https://github.com/Franchise13/JAVAbasic.git

avatar
Hoàng Anh [community,C2010G]
2021-07-04 15:59:45

BÀI 1

employee.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;
import java.util.Scanner;
/**
 *
 * @author Bom1
 */
public class Employee {
    String name;
    String sex;
    String hometown;
    String position;
    double salary;

    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getHometown() {
        return hometown;
    }

    public void setHometown(String hometown) {
        this.hometown = hometown;
    }

    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        if(salary <= 0){
            System.err.println("Ban da nhap sai muc luong");
        }else{
            this.salary = salary;
        }    
    }
    public void input(){
        Scanner input = new Scanner(System.in);
        System.out.println("\nNhập tên: ");
        name = input.nextLine();
        System.out.println("\nNhập giới tính: ");
        sex = input.nextLine();
        System.out.println("\nNhập quê quán: ");
        hometown = input.nextLine();
        System.out.println("\nNhập chức vụ: ");
        position = input.nextLine();
        System.out.println("\nNhập mức lương: ");
        salary = input.nextDouble();
    }
    public void display(){
        System.out.format("Tên: %s \nGiới tính: %s \nQuê quán: %s \nChức vụ: %s \nLương: %f", name,sex,hometown,position,salary);
    }
}


test.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 test {
    public static void main(String[] args) {
        Employee employee = new Employee();
        
        employee.setName("abc");
        employee.setSex("male");
        employee.setHometown("ha noi");
        employee.setPosition("Student");
        employee.setSalary(150000);
        
        employee.display();
      
        Employee employee1 = new Employee();
        employee1.input();
        employee1.display();
        
    }
}


avatar
Do Trung Duc [T2008A]
2021-02-21 08:22:07



Ba1Employee


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

/**
 *
 * @author TrungDuc
 */
public class employeeBai1 {
    private String Name;
     private String Gender;
    private String Country;
    private String Position;
    private String Salary;
    
    
    public employeeBai1() {};

    public employeeBai1(String Name, String Gender, String Country, String Position, String Salary) {
        this.Name = Name;
        this.Gender = Gender;
        this.Country = Country;
        this.Position = Position;
        this.Salary = Salary;
    }

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

    public void setGender(String Gender) {
        this.Gender = Gender;
    }

    public void setCountry(String Country) {
        this.Country = Country;
    }

    public void setPosition(String Position) {
        this.Position = Position;
    }

    public void setSalary(String Salary) {
        this.Salary = Salary;
    }
    
    public void Display(){
        System.out.println("{" + Name + "," + Gender + "," + Country + "," + Position + "," + Salary);
    }
    
}
    
    



    
    




avatar
Do Trung Duc [T2008A]
2021-02-21 08:21: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 CacbaiOOPcanban;

/**
 *
 * @author TrungDuc
 */
public class mainBai1 {
       public static void main(String[] args) {
           employeeBai1 nhanvien1 = new employeeBai1("DoTrungDuc","Nam","VietNam","Giamdoc","1000");
           
            employeeBai1 nhanvien2 = new employeeBai1();
            nhanvien2.setName("TranVanDiep");
            nhanvien2.setGender("Nam");
            nhanvien2.setCountry("VietNam");
            nhanvien2.setPosition("Chutich");
            nhanvien2.setSalary("10000USD");
           
           nhanvien1.Display();
           nhanvien2.Display();
       }
}


avatar
vuong huu phu [T2008A]
2021-02-05 12:00: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 bt4;

import java.util.Scanner;

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

    String Ho_va_ten;
    String Goi_tinh;
    String Que_quan;
    String Chuc_vu;
    double Luong;
    Scanner scan = new Scanner(System.in);

    public Employee() {
        
    }

    public Employee(String Ho_va_ten, String Goi_tinh, String Que_quan, String Chuc_vu, double Luong) {
        this.Ho_va_ten = Ho_va_ten;
        this.Goi_tinh = Goi_tinh;
        this.Que_quan = Que_quan;
        this.Chuc_vu = Chuc_vu;
        this.Luong = Luong;
       
    }

    public String getHo_va_ten() {
        return Ho_va_ten;
    }

    public void setHo_va_ten(String Ho_va_ten) {
        this.Ho_va_ten = Ho_va_ten;
    }

    public String getGoi_tinh() {
        return Goi_tinh;
    }

    public void setGoi_tinh(String Goi_tinh) {
        this.Goi_tinh = Goi_tinh;
    }

    public String getQue_quan() {
        return Que_quan;
    }

    public void setQue_quan(String Que_quan) {
        this.Que_quan = Que_quan;
    }

    public String getChuc_vu() {
        return Chuc_vu;
    }

    public void setChuc_vu(String Chuc_vu) {
        this.Chuc_vu = Chuc_vu;
    }

    public double getLuong() {
        return Luong;
    }

    public void setLuong(double Luong) {
        this.Luong = Luong;
    }

    public Scanner getScan() {
        return scan;
    }

    public void setScan(Scanner scan) {
        this.scan = scan;
    }
public void nhap(){
 System.out.println();
        System.out.println("Nhap ho va ten: ");
        Ho_va_ten = scan.nextLine();
        System.out.println("Nhap gioi tinh");
        Goi_tinh = scan.nextLine();
        System.out.println("Nhap que quan;");
        Que_quan = scan.nextLine();
        System.out.println("Nhap chuc vu; ");
        Chuc_vu = scan.nextLine();
        System.out.println("Nhap luong");
        Luong = scan.nextDouble();
        System.out.println();
        
}
public void hien_thi(){
 System.out.println();
         System.out.format("%s : %s ; %s ; %s ; %f ",Ho_va_ten, Goi_tinh ,Que_quan,Chuc_vu,Luong);
}
}


avatar
vuong huu phu [T2008A]
2021-02-05 12:00:17



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

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

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Employee yc1 = new Employee();
        yc1.nhap();
        yc1.hien_thi();

        Employee yc2 = new Employee("AAA", "BBB", "CCC", "DDDD", (float) 213.45);
        yc2.hien_thi();

    }

}


avatar
Trần Thị Khánh Huyền [T2008A]
2021-01-25 10:18:35



/*
 * 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 employee;
import java.util.Scanner;
/**
 *
 * @author Admin
 */
public class Employee1 {
   String name, gender, country, work;
   double salary;
   
public Employee1(String name, String gender, String country, String work, float salary){
    this.name=name;
    this.gender=gender;
    this.country=country;
    this.work=work;
    this.salary=salary;
}
public String getName(){
    return name;
}
public void setName(String name){
    this.name=name;
}
public String getGender(){
    return gender;
}
public void setGender(String gender){
    this.gender=gender;
}
public String getCountry(){
    return country;
}
public void setWork(String work){
    this.work=work;
}
public double getSalary(){
    return salary;
}
public void setSalary(double salary){
    this.salary=salary;
}
public void input(){
    Scanner input= new Scanner (System.in);
    System.out.println("Nhap ten: ");
    name=input.nextLine();
    System.out.println("Nhap gioi tinh: ");
    gender=input.nextLine();
    System.out.println("Nhap que quan: ");
    country=input.nextLine();
    System.out.println("Nhap chuc vu: ");
    work=input.nextLine();
    System.out.println("Nhap luong: ");
    salary=input.nextFloat();
}
public void output(){
    System.out.println("Ten l: "+name);
    System.out.println("Gioi tinh: "+gender);
    System.out.println("Que quan: "+country);
    System.out.println("Chuc vu: "+work);
    System.out.println("Luong: "+salary);
}

}
/*
 * 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 employee;

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

    public static void main(String[] args) {
        Employee1 Emp1 = new Employee1("ABC", "femail", "Ha Noi", "staff", 7099);
        Employee1 Emp2 = new Employee1();
        Emp2.input();
        Emp1.output();
        Emp2.output();
      
        
        

    }
}