Mình đang học java mình đang gặp rắc rối ở vụ swap , phương thức swap đặt trong 1 class qua một class mới có hàm main tạo ra đối tượng và gọi đến nó các cao nhân chỉ điểm.
Rac roi o ham swap no ko thuc thi theo y minh va copy mot mang contrucstion
import java.util.*;
public class Gach{
private Scanner sc =new Scanner(System.in);
private String MS , Mau;
private int SLVTH,CDVG,CNVG;
private long GB1H;
public Gach() {
MS = ""; Mau = "";SLVTH = 0 ;CDVG = 0 ; CNVG = 0 ;GB1H = 0; }
public Gach(String MS1, String Mau1, int SLVTH1 , int CDVG1,int CNVG1,long GB1H1) {
MS = MS1 ;Mau = Mau1; SLVTH = SLVTH1;
CDVG = CDVG1 ;CNVG = CNVG1; GB1H = GB1H1; }
public Gach(Gach copy) {
MS =copy.MS; Mau = copy.Mau; SLVTH = copy.SLVTH; CDVG = copy.CDVG;
CNVG = copy.CNVG ; GB1H = copy.GB1H ; }
public void nhapTT() {
System.out.print("Nhập vào MS ");
setMS(MS=sc.nextLine());
System.out.print("Nhập vào Màu ");
setMau(Mau=sc.nextLine());
System.out.print("Nhập vào số lượng viên gạch trên 1 hộp gạch ");
setSLVTH(SLVTH=sc.nextInt());
System.out.print("Nhập vào CDVG ");
setCDVG(CDVG=sc.nextInt());
System.out.print("Nhập vào CNVG ");
setCNVG(CNVG=sc.nextInt());
System.out.print("Nhập vào giá bán 1 hộp ");
setGB1H(GB1H = sc.nextLong());
sc.nextLine(); }
public void hienThi() {
System.out.println("-------Thông tin viên gạch---------");
System.out.println("MS: "+getMS());
System.out.println("Màu "+getMau());
System.out.println("SLVTH: "+getSLVTH());
System.out.println("CDVG: "+getCDVG());
System.out.println("CNVG: "+getCNVG());
System.out.println("GB1H: "+getGB1H());
}
public String getMS() { return MS;}
public void setMS(String mS) { MS = mS; }
public String getMau() { return Mau; }
public void setMau(String mau) { Mau = mau ;}
public int getSLVTH() { return SLVTH; }
public void setSLVTH(int sLVTH) { SLVTH = sLVTH ;}
public int getCDVG() { return CDVG; }
public void setCDVG(int cDVG) { CDVG = cDVG; }
public int getCNVG() {return CNVG;}
public void setCNVG(int cNVG) {CNVG = cNVG;}
public long getGB1H() {return GB1H;}
public void setGB1H(long gB1H) {GB1H = gB1H;}
public float giaBanLe(){
float gBL = (float) ((float)GB1H/SLVTH+(0.2*(float)GB1H/SLVTH));
return gBL;}
public float SMax(){
float Smax;
return Smax = CNVG*CDVG*SLVTH ;}
public float soLuongHop(int D,int N){
float soluonghop = (float)((D*N / (float)(CNVG*CDVG))/(float)SLVTH);
return soluonghop;}
public void sWap(Gach B){
Gach A = new Gach(this);
Gach temp = A;
A = B;
B = temp;}
}
Còn đây là class main
import java.util.*;
public class SDGach
{
public static Scanner sc = new Scanner(System.in);
public static void main(String[] args)
{ System.out.println("----------Mở Rộng-----------");
System.out.print("Nhập vào n viên gạch ");
int n ;n = sc.nextInt();
Gach First[] = new Gach[n] ;long X ;
for(int i = 0 ; i < n ; i++) {
First[i] = new Gach();
System.out.println("Nhập thông tin vien gạch "+i+":");
X = sc.nextLong() ;
First[i].setGB1H(X);}
//////So sánh rùi sắp xếp
for (int i = 0 ; i < First.length - 1; i++)
for (int j = i + 1; j < First.length; j++)
if(First[i].getGB1H()>First[j].getGB1H())
First[i].sWap(First[j]);
///In ra
for(int i = 0 ; i < First.length;i++)
First[i].hienThi();
}
}