Theo mình thì mô hình đối tượng bạn nên làm như sau: (Mình ví dụ đối tượng sinh viên chẳng hạn)
Bước 1: Tạo class định nghĩa object sinh viên:
Code:
import java.util.Scanner;
public class sv {
private String hoten;
private String gioitinh;
private int diem;
/*
*
* Getters and Setters
*
*/
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 int getDiem() {
return diem;
}
public void setDiem(int diem) {
this.diem = diem;
}
/*
*
* inPut method
*
*/
public void inPut()
{
Scanner scan = new Scanner(System.in);
System.out.println("Nhap ho ten sv:");
hoten = scan.nextLine();
System.out.println("Nhap gioi tinh:");
gioitinh = scan.nextLine();
System.out.println("Nhap diem thi:");
diem = scan.nextInt();
}
/*
*
* outPut method
*
*/
public void outPut()
{
System.out.println("Ho va ten:" + hoten);
System.out.println("Gioi tinh:" + gioitinh);
System.out.println("Diem thi:" + diem);
}
/*
*
* conStructors
*
*/
public sv()
{
super();
}
public sv(String hoten, String gioitinh, int diem)
{
super();
this.hoten = hoten;
this.gioitinh = gioitinh;
this.diem = diem;
}
}
Bước 2: Tạo một class xử lý các method mà class sv vừa định nghĩa:
Code:
import java.util.Scanner;
public class imPlementSV {
public static void main(String[] args) {
sv[] sinhvien;
Scanner scanner = new Scanner(System.in);
int numSV,i;
System.out.println("Nhap so Sinh Vien:");
numSV = scanner.nextInt();
sinhvien = new sv[numSV+1];
for(i=0;i<numSV;i++)
{
System.out.printf("\n Nhap thong tin sv thu : %d \n",i+1 );
sinhvien[i] = new sv();
sinhvien[i].inPut();
}
for(i=0;i<numSV;i++)
{
System.out.printf("\n Thong tin sv thu : %d \n",i+1 );
sinhvien[i].outPut();
}
}
}
Như vậy ta đã tạo được 1 chương trình nhập xuất đơn giản rồi. Good luck!
Bookmarks