0% found this document useful (0 votes)
40 views12 pages

Pro192 HW w3 SE17B04

The document describes an object-oriented programming lab assignment to design an Employee class with properties like name, age, address, salary, and salary coefficient. It includes methods to initialize objects, input employee information, and calculate salaries. The student is tasked with writing code to: 1. Input information for 10 employees 2. Display information for each employee 3. Calculate the total salary 4. Sort employees by descending name The code provided implements the Employee class with the required properties and methods. Additional classes are provided to test inputting employee data, displaying outputs, and sorting.

Uploaded by

Diem Hang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views12 pages

Pro192 HW w3 SE17B04

The document describes an object-oriented programming lab assignment to design an Employee class with properties like name, age, address, salary, and salary coefficient. It includes methods to initialize objects, input employee information, and calculate salaries. The student is tasked with writing code to: 1. Input information for 10 employees 2. Display information for each employee 3. Calculate the total salary 4. Sort employees by descending name The code provided implements the Employee class with the required properties and methods. Additional classes are provided to test inputting employee data, displaying outputs, and sorting.

Uploaded by

Diem Hang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

JAVA OOP: LAB2

Design the Employee class including 5 properties name, age, address,


salary, salary coefficient and methods of initialization, information input,
salary calculation (Salary= basic salary * salary coefficient). Know that:
basic salary=1.050.000.

Write programs use Array datastructure allow:


1. Input information for 10 employees
2. Display information for each employee
3. Calculate the total salary.
4. Sort Employee descending by name.

Employee.java

package de170737.htdt;

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

public class Employee {


public static List<Employee> list = new ArrayList<>();

public static String format(long n) {


String ok = "";
int index = 0;
while(n>0) {
ok = (n%10) + ok;
index = (++index) % 3;
if(index == 0)
ok = '.' + ok;
n /= 10;
}
return ok;
}

private final long basic_salary = 1050000;

private String name;


private int age;
private String address;
private double salary;
private double salaryCoefficient;

public Employee() {
name = null;
age = 0;
address = null;
salary = 0;
salaryCoefficient = 0;
}

public Employee(String name, int age, String address, double salary,


double salaryCoefficient) {
this.name = name;
this.age = age;
this.address = address;
this.salary = salary;
this.salaryCoefficient = salaryCoefficient;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public int getAge() {


return age;
}

public void setAge(int age) {


this.age = age;
}

public String getAddress() {


return address;
}

public void setAddress(String address) {


this.address = address;
}

public double getSalary() {


return salary;
}

public void setSalary(double salary) {


this.salary = salary;
}

public double getSalaryCoefficient() {


return salaryCoefficient;
}

public void setSalaryCoefficient(double salaryCoefficient) {


this.salaryCoefficient = salaryCoefficient;
}

public long caculateSalary() {


return (long)(this.salary + this.basic_salary * this.salaryCoefficient);
}
public static void input() {
Scanner sc = new Scanner(System.in);
String cmd;
Employee employee = new Employee();

System.out.println("Input: ");
do{
System.out.println("Enter name: ");
cmd = sc.nextLine();
if(cmd.equals(""))
System.out.println("[Error] Invalid name!");
} while (cmd.equals(""));
employee.setName(cmd);

do{
System.out.println("Enter age: ");
cmd = sc.nextLine();
if(cmd.equals(""))
System.out.println("[Error] Invalid age!");
} while (cmd.equals(""));
employee.setAge(Integer.parseInt(cmd));

do{
System.out.println("Enter adress: ");
cmd = sc.nextLine();
if(cmd.equals(""))
System.out.println("[Error] Invalid address!");
} while (cmd.equals(""));
employee.setAddress(cmd);

do{
System.out.println("Enter salary: ");
cmd = sc.nextLine();
if(cmd.equals(""))
System.out.println("[Error] Invalid salary!");
} while (cmd.equals(""));
employee.setSalary(Double.parseDouble(cmd));

do{
System.out.println("Enter salary coefficient: ");
cmd = sc.nextLine();
if(cmd.equals(""))
System.out.println("[Error] Invalid salary coefficient!");
} while (cmd.equals(""));
employee.setSalaryCoefficient(Double.parseDouble(cmd));

list.add(employee);
}

@Override
public String toString() {
return name + " - " + age + " - " + address + " - " +
Employee.format((long) salary) + " - " + salaryCoefficient + " - " +
Employee.format(caculateSalary());
}
}

HTDH.java

package de170737.htdt;

public class HTDT {

public static void main(String[] args) {


System.out.println("1 - Input information for 10 employees");
long total = 0;

for (int i = 0; i < 10; i++) {


Employee.input();
}
Employee[] list = Employee.list.toArray(new Employee[0]);
System.out.println("2 - Display information for each employee");
for(Employee employee : list) {
System.out.println(employee);
total += employee.caculateSalary();
}

System.out.println("3 - Calculate the total salary");


System.out.println(Employee.format(total));
System.out.println("4 - Sort employee by descending by name");

Employee cmd;
for (int i = 0; i < list.length - 1; i++) {
for (int j = 0; j < list.length; j++) {
if(list[i].getName().compareTo(list[j].getName()) < 0){
cmd = list[i];
list[i] = list[j];
list[j] = cmd;
}
}
}
for(Employee employee : list) {
System.out.println(employee);
}
}
}

HOMEWORK
package DE170737.hang;
import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Homework1 {


private String Name, School;
private double diemso ;

public Homework1() {
}

public Homework1(String Name, String School, double diemso) {


this.Name = Name;
this.School = School;
this.diemso = diemso;
}

public String getName(){


return Name;
}
public void setName(String Name){
this.Name = Name;
}
public String getSchool(){
return School;
}
public void setSchool(String School){
this.School= School;
}
public double getdiemso(){
return diemso;
}
public void setdiemso(double diemso){
this.diemso= diemso;
}
public String quality(double diemso){
if (diemso >=8.5 && diemso <= 10){
return "gioi";
}
if (diemso >=6 && diemso < 8.5){
return "kha";
}
if (diemso >=4 && diemso < 6){
return "trung binh";
}
if (diemso >=0 && diemso < 4){
return "yeu";
}
return "error!";
}

public void scan(){

Scanner sc= new Scanner(System.in);


System.out.println("Ten: ");
String Name= sc.nextLine();
System.out.println("Truong: ");
String School= sc.nextLine();
System.out.println("Diem so: ");
double diemso =sc.nextDouble();
Homework1 homework1 = new Homework1(Name, School, diemso);

System.out.println("Ten: " + homework1.getName() + ", truong: " +


homework1.getSchool());
System.out.println("diem so: " +homework1.getdiemso());
double score = homework1.getdiemso();
System.out.println("loai: "+ quality(score));
}

public static void main(String[] args) {


Homework1 homework= new Homework1();
homework.scan();
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy