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

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.

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-21 12:05:08



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

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

    String name, sex, placeOfBirth, position;
    double income;

    Employee(String name, String sex, String placeOfBirth, String position, double income) {
        this.name = name;
        this.sex = sex;
        this.placeOfBirth = placeOfBirth;
        this.position = position;
        this.income = income;
    }

     Employee() {
    }

    public void input(String name, String sex, String placeOfBirth, String position, double income) {
        this.name = name;
        this.sex = sex;
        this.placeOfBirth = placeOfBirth;
        this.position = position;
        this.income = income;
    }

    public void display() {
        System.out.println("Name : " + name);
        System.out.println("sex : " + sex);
        System.out.println("Place Of Birth : " + placeOfBirth);
        System.out.println("Position : " + position);
        System.out.println("Income : " + income);
    }
}



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

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

    public static void main(String[] args) {
        Employee employee = new Employee("Nguyen Van A", "nam", "Hue", "Truong phong", 2000000);
        Employee employee2 = new Employee();
        employee2.input("Nguyen Van B", "nam", "Ha Noi", "Giam doc", 20000000);
        employee.display();
        employee2.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 baitapCoBan;

import java.util.*;

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

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

    Product() {
    }

    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 String getTenHH() {
        return tenHH;
    }

    public float getSoLuong() {
        return soLuong;
    }

    public float getGia1SP() {
        return gia1SP;
    }

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

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

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

    public void setGia1SP(float gia1SP) {
        this.gia1SP = gia1SP;
    }

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

    public void display() {
        System.out.println("Ma hang hoa : " + maHH);
        System.out.println("Ten hang hoa : " + tenHH);
        System.out.println("So luong : " + soLuong);
        System.out.println("Gia 1 san pham : " + gia1SP);
    }

    public static void main(String[] args) {
        ArrayList<Product> arr = new ArrayList();
        Scanner input = new Scanner(System.in);
        while (true) {
            System.out.println("1.Nhap thong tin hang hoa ");
            System.out.println("2.Xem thong tin hang hoa");
            System.out.println("3.Xem thong tin hang hoa co gia ban cao nhat ");
            System.out.println("4.Thoat ");
            int choice = Integer.parseInt(input.nextLine());
            switch (choice) {
                case 1:
                    System.out.println("Nhap ma hang hoa");
                    String ma = input.nextLine();
                    System.out.println("Nhap ten hang hoa");
                    String ten = input.nextLine();
                    System.out.println("Nhap so luong hang hoa");
                    float soluong = Float.parseFloat(input.nextLine());
                    System.out.println("Nhap gia 1 san pham");
                    float gia = Float.parseFloat(input.nextLine());
                    Product product = new Product(ma, ten, soluong, gia);
                    arr.add(product);
                    break;
                case 2:

                    for (int i = 0; i < arr.size(); i++) {
                        arr.get(i).display();
                        System.out.println("..........");
                    }
                    break;
                case 3:
                     System.out.println("San pham co gia cao nhat : ");
                    Product productmax = new Product();
                    for (int i = 0; i < arr.size(); i++) {
                        for (int j = 0; j < arr.size(); j++) {
                            if (arr.get(i).gia1SP < arr.get(j).gia1SP) {
                                productmax = arr.get(j);
                            }
                        }
                    }
                   productmax.display();
                    break;
                case 4:
                   return;
            }
        }

    }
}



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

import java.util.*;

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

    int Rollnumber;
    String name, Class, subject;
    float mark;

    StudentMark() {
    }

    StudentMark(int Rollnumber, String name, String Class, String subjects, float mark) {
        this.Rollnumber = Rollnumber;
        this.name = name;
        this.Class = Class;
        this.subject = subjects;
        this.mark = mark;
    }

    public void input(int Rollnumber, String name, String Class, String subjects, float mark) {
        this.Rollnumber = Rollnumber;
        this.name = name;
        this.Class = Class;
        this.subject = subjects;
        this.mark = mark;
    }

    public void display() {
        System.out.println("Rollnumber : " + Rollnumber);
        System.out.println("Name : " + name);
        System.out.println("Class : " + Class);
        System.out.println("Subject : " + subject);
        System.out.println("Mark : " + mark);
    }

    public static void main(String[] args) {
        ArrayList<StudentMark> arr = new ArrayList();
        StudentMark studentmark = new StudentMark();
        StudentMark studentmark2 = new StudentMark();
        studentmark.input(12, "Nguyen Van A", "C1903L", "Java", 8);
        studentmark2.input(14, "Nguyen Van B", "C1903L", "Java", 9);
        studentmark.display();
        System.out.println("................");
        studentmark2.display();
        arr.add(studentmark);
        arr.add(studentmark2);
        System.out.println("Hs co diem cao nhat : ");
        StudentMark studentmax = new StudentMark();
        for(int i = 0;i < arr.size();i++){
            for(int j = 0 ; j< arr.size();j++){
                if(arr.get(i).mark < arr.get(j).mark){
                    studentmax = arr.get(j);
                }
            }
        }
         studentmax.display();
    }
}



Ngô Quang Huy [C1907L]
Ngô Quang Huy

2020-03-21 03:11:00



package aptech;

import java.util.Scanner;

public class Employee {
    String Name;
    String Gender;
    String Country;
    String Work;
    int Salary;

    public Employee() {
    }

    public Employee (String Name, String Gender, String Country, String Work, int 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 setCountry(String Country) {
        this.Country = Country;
    }

    public String getWork() {
        return Work;
    }

    public void setWork(String Work) {
        this.Work = Work;
    }

    public int getSalary() {
        return Salary;
    }

    public void setSalary(int Salary) {
        this.Salary = Salary;
    }
    
    public void inputEm(){
        Scanner input = new Scanner(System.in);
        System.out.print("Nhap vao Họ tên: ");
        Name = input.nextLine();
        System.out.print("Nhap vao Giới tính: ");
        Gender = input.nextLine();
        System.out.print("Nhap vao Quê quán: ");
        Country = input.nextLine();
        System.out.print("Nhap vao Chức vụ: ");
        Work = input.nextLine();
        System.out.print("Nhap vao Lương: ");
        Salary = input.nextInt();
    }
    
    public void outputEm(){
        System.out.println("Họ tên la: "+Name);
        System.out.println("Giới tính la: "+Gender);
        System.out.println("Quê quán la: "+Country);
        System.out.println("Chức vụ la: "+Work);
        System.out.println("Lương la: "+Salary);
    }
}


package aptech;

public class TestEmployee {
    public static void main(String[] args) {
        Employee nv1 = new Employee();
        nv1.inputEm();
        Employee nv2 = new Employee("Tran Van A","Nam","Ha Nam","Ke toan",1200000);
        nv1.outputEm();
        nv2.outputEm();
    }
}



package aptech;

import java.util.Scanner;

public class StudentMark {
    String Rollnumber;
    String Name;
    String ClassRoom;
    String Subject;
    int Grade;

    public StudentMark() {
    }

    public StudentMark(String Rollnumber, String Name, String ClassRoom, String Subject, int Grade) {
        this.Rollnumber = Rollnumber;
        this.Name = Name;
        this.ClassRoom = ClassRoom;
        this.Subject = Subject;
        this.Grade = Grade;
    }

    public String getRollnumber() {
        return Rollnumber;
    }

    public void setRollnumber(String Rollnumber) {
        this.Rollnumber = Rollnumber;
    }

    public String getName() {
        return Name;
    }

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

    public String getClassRoom() {
        return ClassRoom;
    }

    public void setClassRoom(String ClassRoom) {
        this.ClassRoom = ClassRoom;
    }

    public String getSubject() {
        return Subject;
    }

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

    public int getGrade() {
        return Grade;
    }

    public void setGrade(int Grade) {
        this.Grade = Grade;
    }

     
    public void inputStudent(){
        Scanner input = new Scanner(System.in);
        System.out.print("Insert Rollnumber: ");
        Rollnumber = input.nextLine();
        System.out.print("Insert name: ");
        Name = input.nextLine();
        System.out.print("Insert Class: ");
        ClassRoom = input.nextLine();
        System.out.print("Insert Subject: ");
        Subject = input.nextLine();
        System.out.print("Insert Grade: ");
        Grade = input.nextInt();
    }
    
    public void outputStudent(){
        System.out.println("Rollnumber is: "+Rollnumber);
        System.out.println("Name is: "+Name);
        System.out.println("ClassRoom is: "+ClassRoom);
        System.out.println("Subject is: "+Subject);
        System.out.println("Grade is: "+Grade);
    }
    
    public static void main(String[] args) {
        StudentMark st1 = new StudentMark();
        st1.inputStudent();
        StudentMark st2 = new StudentMark("11225","Tran Van A","Cksjd","Java",5);
        System.out.println("người có điểm cao nhất:");
        if(st1.Grade>st2.Grade){
            st1.outputStudent();
        }else{
            st2.outputStudent();
        }
        
    }
}



package aptech;

import java.util.ArrayList;
import java.util.Scanner;

public class Product {
    private String Code;
    private String Name;
    private float quantity;
    private float price;

    public Product() {
    }

    public Product(String Code, String Name, float quantity, float price) {
        this.Code = Code;
        this.Name = Name;
        this.quantity = quantity;
        this.price = price;
    }

    public String getCode() {
        return Code;
    }

    public void setCode(String Code) {
        this.Code = Code;
    }

    public String getName() {
        return Name;
    }

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

    public float getQuantity() {
        return quantity;
    }

    public void setQuantity(float quantity) {
        this.quantity = quantity;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }
    
    public void inputProduct(){
        Scanner input = new Scanner(System.in);
        System.out.print("Insert Code: ");
        Code = input.nextLine();
        System.out.print("Insert name: ");
        Name = input.nextLine();
        System.out.print("Insert quantity: ");
        quantity = Float.parseFloat(input.nextLine());
        System.out.print("Insert Price: ");
        price = Float.parseFloat(input.nextLine());
    }
    
    public void outputProduct(){
        System.out.println("Code is: "+Code);
        System.out.println("Name is: "+Name);
        System.out.println("quantity is: "+quantity);
        System.out.println("price is: "+price);
    }
    
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Number of product:");
        int n = input.nextInt();
        ArrayList<Product> prod = new ArrayList<>();
        float max=0;
        int vt=0;
        for(int i=0;i<n;i++){
            Product p = new Product();
            p.inputProduct();
            if(p.price>max){
                vt=i;
                max=p.price;
            }
            prod.add(p);
        }
        System.out.println("\nProduct with highest price:");
        prod.get(vt).outputProduct();
    }
}



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

2020-03-20 16:01:10




package Homework;
import java.util.Scanner;
import java.util.ArrayList;
// Bai 3:
public class Product {
    String maHH, tenHH;
    float soLuong, 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) {
        this.gia1SP = 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 void display(){
        System.out.println(toString());
    }

    @Override
    public String toString() {
        return "Product{" + "maHH=" + maHH + ", tenHH=" + tenHH + ", soLuong=" + soLuong + ", gia1SP=" + gia1SP + '}';
    }
    
    public void input(){
        Scanner input = new Scanner(System.in);
        System.out.print("Mã hàng hóa: ");
        maHH = input.nextLine();
        System.out.print("Tên hàng hóa: ");
        tenHH = input.nextLine();
        System.out.print("Số lượng hàng hóa: ");
        soLuong = Float.parseFloat(input.nextLine());
        System.out.print("Giá sản phẩm: ");
        gia1SP = Float.parseFloat(input.nextLine());
    }
    
    public static void main(String[] args){
        ArrayList<Product> listProduct = new ArrayList<>();
        Scanner input = new Scanner(System.in);
        
        System.out.print("Nhập số lượng hàng hóa: ");
        int n = Integer.parseInt(input.nextLine());
        for(int i = 0; i < n; i++){
            Product product = new Product();
            product.input();
            listProduct.add(product);
        }
        
        float max = 0;
        for(Product p: listProduct){
            if(p.getGia1SP() > max){
                max = p.getGia1SP();
            }
        }
        System.out.println("Sản phẩm có giá cao nhất: ");
        for(Product p: listProduct){
            if(p.getGia1SP() == max){
                p.display();
            }
        }
    }
}



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

2020-03-20 15:38:23




package Homework;
import java.util.Scanner;
// Bai 2:
public class StudentMark {
    String Rollnumber, fullName, className, subject;
    float scores;

    public String getRollnumber() {
        return Rollnumber;
    }

    public void setRollnumber(String Rollnumber) {
        this.Rollnumber = Rollnumber;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    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 float getScores() {
        return scores;
    }

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

    public StudentMark(String Rollnumber, String fullName, String className, String subject, float scores) {
        this.Rollnumber = Rollnumber;
        this.fullName = fullName;
        this.className = className;
        this.subject = subject;
        this.scores = scores;
    }
    
    public void show(){
        System.out.println(toString());
    }

    @Override
    public String toString() {
        return "StudentMark{" + "Rollnumber=" + Rollnumber + ", fullName=" + fullName + ", className=" + className + ", subject=" + subject + ", scores=" + scores + '}';
    }
    
    public void input(){
        Scanner input = new Scanner(System.in);
        System.out.print("Nhập Rollnumber: ");
        Rollnumber = input.nextLine();
        System.out.print("Nhập họ và tên: ");
        fullName = input.nextLine();
        System.out.print("Nhập tên lớp: ");
        className = input.nextLine();
        System.out.print("Nhập tên môn: ");
        subject = input.nextLine();
        System.out.print("Nhập điểm: ");
        scores = Float.parseFloat(input.nextLine());
    }
    public static void main(String[] args){
        StudentMark std1 = new StudentMark("xxx","Vũ Việt Đức","C1907L","Java", 9.9f);
        StudentMark std2 = new StudentMark();
        std2.input();
        std1.show();
        std2.show();
        if(std1.getScores() > std2.getScores()){
            System.out.println("Học sinh có điểm cao nhất là: ");
            std1.show();
        }
        else{
            System.out.println("Học sinh có điểm cao nhất là: ");
            std2.show();
        }
    }

    
}



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

2020-03-20 15:17:54




package Homework;

// Bai1
public class Test {
    public static void main(String[] args){
        Employee nv1 = new Employee("Vũ Việt Đức","Nam","Hải Phòng","Học sinh",999999);
        Employee nv2 = new Employee();
        nv2.input();
        nv1.show();
        nv2.show();
    }
}



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

2020-03-20 15:17:25




package Homework;
import java.util.Scanner;
// Bai 1: 
public class Employee {
    String hoTen, gioiTinh, queQuan, chucVu;
    double luong;

    public String getHoTen() {
        return hoTen;
    }

    public void setHoTen(String hoTen) {
        this.hoTen = hoTen;
    }

    public String getGioiTinh() {
        return gioiTinh;
    }

    public void setGioiTinh(String gioiTinh) {
        this.gioiTinh = gioiTinh;
    }

    public String getQueQuan() {
        return queQuan;
    }

    public void setQueQuan(String queQuan) {
        this.queQuan = queQuan;
    }

    public String getChucVu() {
        return chucVu;
    }

    public void setChucVu(String chucVu) {
        this.chucVu = chucVu;
    }

    public double getLuong() {
        return luong;
    }

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

    public Employee() {
    }

    public Employee(String hoTen, String gioiTinh, String queQuan, String chucVu, double luong) {
        this.hoTen = hoTen;
        this.gioiTinh = gioiTinh;
        this.queQuan = queQuan;
        this.chucVu = chucVu;
        this.luong = luong;
    }
    
    public void input() {
        Scanner input = new Scanner(System.in);
        System.out.print("Nhập họ và tên: ");
        hoTen = input.nextLine();
        System.out.print("Nhập giới tính: ");
        gioiTinh = input.nextLine();
        System.out.print("Nhập quê quán: ");
        queQuan = input.nextLine();
        System.out.print("Nhập chức vụ: ");
        chucVu = input.nextLine();
        System.out.print("Nhập lương: ");
        luong = Double.parseDouble(input.nextLine());
    }
    
    public void show() {
        System.out.println(toString());
    }

    @Override
    public String toString() {
        return "Employee{" + "hoTen=" + hoTen + ", gioiTinh=" + gioiTinh + ", queQuan=" + queQuan + ", chucVu=" + chucVu + ", luong=" + luong + '}';
    }
}