convert java code to matlab code

조회 수: 10 (최근 30일)
nadia nadi
nadia nadi 2022년 5월 6일
편집: TED MOSBY 대략 16시간 전
Hello,
I need to convert this code to matlab. I have no idea about java. could anyone help me please.
public class NextFit extends Packing {
protected final boolean shouldDecrease = false;
public NextFit(double[] items) {
super(items);
}
@Override
protected void pack(double item) {
int binSize = this.bins.size();
boolean canFit = binSize > 0 && this.bins.get(binSize - 1).remainingCapacity >= item;
int binIndex = binSize - (canFit ? 1 : 0);
this.addItem(item, binIndex);
}
@Override
protected boolean shouldDecrease() {
return this.shouldDecrease;
}
}
Thanks

답변 (1개)

TED MOSBY
TED MOSBY 대략 16시간 전
편집: TED MOSBY 대략 16시간 전
I can give guidelines on things to keep in mind while converting you code:
  • In MATLAB, classes are defined in separate files with the same name as the class. The file should be named NextFit.m.
  • MATLAB does not use public, protected, or private keywords in the same way Java does. MATLAB uses properties and methods blocks to control access.
  • MATLAB methods are defined within a methods block. The @Override annotation doesn't exist in MATLAB.
  • MATLAB uses properties block to define class properties.
It should look something like this, feel free to adjust it accordingly:
classdef NextFit < Packing
properties (Access = protected, Constant)
shouldDecreaseFlag = false;
end
methods
function obj = NextFit(items)
obj@Packing(items); % Call the superclass constructor
end
function pack(obj, item)
binSize = numel(obj.bins);
canFit = binSize > 0 && obj.bins{binSize}.remainingCapacity >= item;
binIndex = binSize - (canFit * 1);
obj.addItem(item, binIndex);
end
function result = shouldDecrease(obj)
result = obj.shouldDecreaseFlag;
end
end
end
Here I changed the name of the property ‘shouldDecrease’ to ‘shouldDecreaseFlag’ to store the Boolean value and then returned that boolean value in the function ‘shouldDecrease’ inside the methods. I did this as we cannot write the property and the function with the same name in the same class in MATLAB as it creates ambiguity
Hope this helps!

카테고리

Help CenterFile Exchange에서 Call Java from MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by