0% found this document useful (0 votes)
3 views7 pages

Sa

Uploaded by

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

Sa

Uploaded by

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

import java.util.

Scanner;

public class Sinhvien {


private String msv;
private String hoten;
private int namsinh;

public Sinhvien(String msv, String hoten, int namsinh) {


this.msv = msv;
this.hoten = hoten;
this.namsinh = namsinh;
}

public String getMsv() {


return msv;
}

public String getHoten() {


return hoten;
}

public int getNamsinh() {


return namsinh;
}

public void nhap() {


Scanner input = new Scanner(System.in);
System.out.print("Nhap msv: ");
msv = input.nextLine();
System.out.print("Nhap ho ten: ");
hoten = input.nextLine();
System.out.print("Nhap nam sinh: ");
namsinh = input.nextInt();
// Clear the input buffer
input.nextLine();
System.out.println();
}

public void in() {


System.out.println("msv: " + msv);
System.out.println("hoten: " + hoten);
System.out.println("namsinh: " + namsinh);
System.out.println();
}

public static void sxtheons(Sinhvien[] sv) {


for (int i = 0; i < sv.length; i++) {
for (int j = i + 1; j < sv.length; j++) {
if (sv[i].getNamsinh() > sv[j].getNamsinh()) {
Sinhvien temp = sv[i];
sv[i] = sv[j];
sv[j] = temp;
}
}
}
}

public static void sxtheoten(Sinhvien[] sv) {


for (int i = 0; i < sv.length; i++) {
for (int j = i + 1; j < sv.length; j++) {
if (sv[i].getHoten().compareTo(sv[j].getHoten()) > 0) {
Sinhvien temp = sv[i];
sv[i] = sv[j];
sv[j] = temp;
}
}
}
}

public static Sinhvien timtheoten(Sinhvien[] sv, String key) {


for (int i = 0; i < sv.length; i++) {
if (sv[i].getHoten().equalsIgnoreCase(key)) {
return sv[i];
}
}
return null;
}

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
System.out.print("Nhap so luong sinh vien: ");
int n = input.nextInt();

Sinhvien[] sv = new Sinhvien[n];


for (int i = 0; i < n; i++) {
System.out.println("Nhap sv thu " + (i + 1) + ": ");
sv[i] = new Sinhvien("", "", 0);
sv[i].nhap();
}
System.out.println("Danh sach sinh vien: ");
for (int i = 0; i < n; i++) {
System.out.println("Sinh vien thu " + (i + 1) + ": ");
sv[i].in();
}
sxtheons(sv);
System.out.println("Danh sach sinh vien sau khi sap xep: ");
for (int i = 0; i < n; i++) {
System.out.println("Sinh vien thu " + (i + 1) + ": ");
sv[i].in();
}
sxtheoten(sv);
System.out.println("Danh sach sinh vien sau khi sap xep theo ten: ");
for (int i = 0; i < n; i++) {
System.out.println("Sinh vien thu " + (i + 1) + ": ");
sv[i].in();
}

System.out.print("Nhap ten can tim: ");


String key = input.nextLine();
key = input.nextLine();
System.out.println("Danh sach SV can tim: ");
Sinhvien b = timtheoten(sv, key);
if (b != null) {
b.in();
} else {
System.out.println("Khong tim thay sinh vien co ten " + key);
}
}
}

===================================================================================
===
import java.util.Scanner;

public class Sinhvien {


private String msv;
private String hoten;
private int namsinh;

public Sinhvien(String msv, String hoten, int namsinh) {


this.msv = msv;
this.hoten = hoten;
this.namsinh = namsinh;
}

public String getMsv() {


return msv;
}

public String getHoten() {


return hoten;
}

public int getNamsinh() {


return namsinh;
}

public void nhap() {


Scanner input = new Scanner(System.in);
System.out.print("Nhap msv: ");
msv = input.nextLine();
System.out.print("Nhap ho ten: ");
hoten = input.nextLine();
System.out.print("Nhap nam sinh: ");
namsinh = input.nextInt();
// Clear the input buffer
input.nextLine();
System.out.println();
}

public void in() {


System.out.println("msv: " + msv);
System.out.println("hoten: " + hoten);
System.out.println("namsinh: " + namsinh);
System.out.println();
}

public static void sxtheons(Sinhvien[] sv) {


for (int i = 0; i < sv.length; i++) {
for (int j = i + 1; j < sv.length; j++) {
if (sv[i].getNamsinh() > sv[j].getNamsinh()) {
Sinhvien temp = sv[i];
sv[i] = sv[j];
sv[j] = temp;
}
}
}
}

public static void sxtheoten(Sinhvien[] sv) {


for (int i = 0; i < sv.length; i++) {
for (int j = i + 1; j < sv.length; j++) {
if (sv[i].getHoten().compareTo(sv[j].getHoten()) > 0) {
Sinhvien temp = sv[i];
sv[i] = sv[j];
sv[j] = temp;
}
}
}
}

public static void timtheoten(Sinhvien[] sv, String key) {


for (int i = 0; i < sv.length; i++) {
if (sv[i].getHoten().equalsIgnoreCase(key)) {
System.out.println("msv: " + sv[i].msv);
System.out.println("hoten: " + sv[i].hoten);
System.out.println("namsinh: " + sv[i].namsinh);
System.out.println();
}
}

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
System.out.print("Nhap so luong sinh vien: ");
int n = input.nextInt();

Sinhvien[] sv = new Sinhvien[n];


for (int i = 0; i < n; i++) {
System.out.println("Nhap sv thu " + (i + 1) + ": ");
sv[i] = new Sinhvien("", "", 0);
sv[i].nhap();
}
System.out.println("Danh sach sinh vien: ");
for (int i = 0; i < n; i++) {
System.out.println("Sinh vien thu " + (i + 1) + ": ");
sv[i].in();
}
sxtheons(sv);
System.out.println("Danh sach sinh vien sau khi sap xep: ");
for (int i = 0; i < n; i++) {
System.out.println("Sinh vien thu " + (i + 1) + ": ");
sv[i].in();
}
sxtheoten(sv);
System.out.println("Danh sach sinh vien sau khi sap xep theo ten: ");
for (int i = 0; i < n; i++) {
System.out.println("Sinh vien thu " + (i + 1) + ": ");
sv[i].in();
}

String key;
System.out.print("Nhap ten can tim: ");
key = input.next();
System.out.println("Danh sach SV can tim: ");
timtheoten(sv, key);

}
}

===================================================================================
============

import java.util.Scanner;

public class Sinhvien {


private String msv;
private String hoten;
private int namsinh;

public Sinhvien(String msv, String hoten, int namsinh) {


this.msv = msv;
this.hoten = hoten;
this.namsinh = namsinh;
}

public String getMsv() {


return msv;
}

public String getHoten() {


return hoten;
}

public int getNamsinh() {


return namsinh;
}

public void nhap() {


Scanner input = new Scanner(System.in);
System.out.print("Nhap msv: ");
msv = input.nextLine();
System.out.print("Nhap ho ten: ");
hoten = input.nextLine();
System.out.print("Nhap nam sinh: ");
namsinh = input.nextInt();
// Clear the input buffer
input.nextLine();
System.out.println();
}

public void in() {


System.out.println("msv: " + msv);
System.out.println("hoten: " + hoten);
System.out.println("namsinh: " + namsinh);
System.out.println();
}

public static void sxtheons(Sinhvien[] sv) {


for (int i = 0; i < sv.length; i++) {
for (int j = i + 1; j < sv.length; j++) {
if (sv[i].getNamsinh() > sv[j].getNamsinh()) {
Sinhvien temp = sv[i];
sv[i] = sv[j];
sv[j] = temp;
}
}
}
}

public static void sxtheoten(Sinhvien[] sv) {


for (int i = 0; i < sv.length; i++) {
for (int j = i + 1; j < sv.length; j++) {
if (sv[i].getHoten().compareTo(sv[j].getHoten()) > 0) {
Sinhvien temp = sv[i];
sv[i] = sv[j];
sv[j] = temp;
}
}
}
}

public static Sinhvien timtheoten(Sinhvien[] sv, String key) {


for (int i = 0; i < sv.length; i++) {
if (sv[i].getHoten().equalsIgnoreCase(key)) {
return sv[i];
}
}
return null; // Trả về null nếu không tìm thấy
}

public static void xoaSinhVien(Sinhvien[] sv, String msv) {


int index = -1;
for (int i = 0; i < sv.length; i++) {
if (sv[i].getMsv().equalsIgnoreCase(msv)) {
index = i;
break;
}
}
if (index != -1) {
for (int i = index; i < sv.length - 1; i++) {
sv[i] = sv[i + 1];
}
sv[sv.length - 1] = null; // Set phần tử cuối cùng về null
System.out.println("Da xoa sinh vien co msv: " + msv);
} else {
System.out.println("Khong tim thay sinh vien co msv: " + msv);
}
}

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
System.out.print("Nhap so luong sinh vien: ");
int n = input.nextInt();

Sinhvien[] sv = new Sinhvien[n];


for (int i = 0; i < n; i++) {
System.out.println("Nhap sv thu " + (i + 1) + ": ");
sv[i] = new Sinhvien("", "", 0);
sv[i].nhap();
}
System.out.println("Danh sach sinh vien: ");
for (int i = 0; i < n; i++) {
System.out.println("Sinh vien thu " + (i + 1) + ": ");
sv[i].in();
}
System.out.print("Nhap ma sinh vien can xoa: ");
String msvXoa = input.next();
xoaSinhVien(sv, msvXoa);
System.out.println("Danh sach sinh vien sau khi xoa: ");
for (int i = 0; i < n - 1; i++) {
System.out.println("Sinh vien thu " + (i + 1) + ": ");
sv[i].in();
}
}
}

You might also like