Sie sind auf Seite 1von 4

Bi 01:

M:

import java.util.Scanner; public class Arithmetic { int getMax(int a[]){ int max = a[0]; for(int i = 1; i < a.length; i++){ if(max < a[i]){ max = a[i]; } } return max; } boolean isSort(int a[]){ for(int i = 0; i < a.length; i++){ for(int j = i + 1; j < a.length; j++){ if(a[j] < a[i]){ System.out.println("This array is not sorted."); return false; } } } System.out.println("This array has been sorted."); return true; } void sortAsc(int a[]){ for(int i = 0; i < a.length; i++){ for(int j = i + 1; j < a.length; j++){ if(a[j] < a[i]){ int temp = a[i]; a[i] = a[j]; a[j] = temp; } } } for(int i = 0; i < a.length; i++){ System.out.println(a[i]); } } void findPos(int a[], int num){ System.out.print("Please enter a number you want to find its positions: "); Scanner sc = new Scanner(System.in); num = sc.nextInt(); System.out.print("Their position is: "); for(int i = 0; i < a.length; i++){ if(a[i] == num){ System.out.print(i + "\t"); } } }

public static void main(String[] args) { Arithmetic arith = new Arithmetic(); Scanner sc = new Scanner(System.in); System.out.println("Please enter the number of elements of the array: "); int n = sc.nextInt(); int a[] = new int[n]; for(int i = 0; i < a.length; i++){ a[i] = sc.nextInt(); } System.out.println("Max is: " + arith.getMax(a)); arith.isSort(a); arith.sortAsc(a); arith.findPos(a, n); }

Bi 2 + 3: chy tt ri (tuy cha chuyn nghip nhng ng yu cu ca bi) Student:

M:

public class Student { int id; String name; float fee; final float totalFee = 400; }

StudentManage:

M:

import java.util.Scanner; public class StudentManage { public StudentManage() { } // Nhap du tu ban phim va tra ve sinh vien public Student createStudent(){ Student std = new Student(); Scanner sc = new Scanner(System.in); System.out.println("Id: "); std.id = sc.nextInt(); System.out.println("Name: "); std.name = sc.next(); System.out.println("Fee: "); std.fee = sc.nextFloat(); return std; }

// Cap nhat thong tin sinh vien tu ban phim public void updateStudent(Student t){ Scanner sc = new Scanner(System.in); System.out.println("Name: "); t.name = sc.next(); System.out.println("Fee: "); t.fee = sc.nextFloat(); } // Xoa sinh vien trong mang public void delete(Student s[], int id){ for(int i = 0; i < s.length; i++){ if(id == s[i].id){ for(int j = i; j < s.length - 1; j++){ s[j] = s[j + 1]; } } } } // Tim sinh vien va tra ve public Student findByID(Student s[], int id){ for(int i = 0; i < s.length; i++){ if(s[i].id == id){ return s[i]; } } System.out.println("This student does not exist!"); return null; } // Tra ve no cua sinh vien public float getDebt(Student t){ return (t.totalFee - t.fee); } // Tra ve so no lon nhat cua cac sinh vien public float findMaxDebt(Student s[]){ float maxDebt = 0; for(int i = 0; i < s.length; i++){ if(maxDebt < getDebt(s[i])){ maxDebt = getDebt(s[i]); } } return maxDebt; } // Tra ve tong chi phi cua tat ca sinh vien public float totalFee(Student s[]){ float total = 0; for(int i = 0; i < s.length - 1; i++){ total += s[i].totalFee; } return total; } // Hien thi thong tin cua sinh vien public void display(Student t){

System.out.println(t.id + "\t" + t.name + "\t" + t.fee + "\t" + getDebt(t));

StudentTest:

M:

import java.util.Scanner; public class StudentTest { static Student s[]; public StudentTest() { Scanner sc = new Scanner(System.in); System.out.println("Please enter the number of elements of the array: "); int n = sc.nextInt(); s = new Student[n]; StudentManage st = new StudentManage(); for(int i = 0; i < n; i++){ System.out.println("Please enter the information of student " + (i + 1) + " :"); s[i] = st.createStudent(); } } public static void main(String[] args) { StudentTest std = new StudentTest(); StudentManage st = new StudentManage(); Scanner sc = new Scanner(System.in); System.out.println("Please enter the id of student you want to update: "); int updateID = sc.nextInt(); st.updateStudent(st.findByID(s, updateID)); System.out.println("Please enter any id of student you want to delete: "); int deleteID = sc.nextInt(); st.delete(s, deleteID); System.out.println("The information of remain students: "); System.out.println("Id\tName\tFee\tDebt"); for(int i = 0; i < s.length - 1; i++){ st.display(s[i]); } System.out.println("The total fee of remain students: " + st.totalFee(s)); System.out.println("The information of student that has the maximum of debt: " + st.findMaxDebt(s)); } }

Das könnte Ihnen auch gefallen