Java 为数组创建构造函数并用自定义类型填充它
我需要使用位模拟一个长单词,其中数组中的每个索引都包含位。Java 为数组创建构造函数并用自定义类型填充它,java,constructor,interface,Java,Constructor,Interface,我需要使用位模拟一个长单词,其中数组中的每个索引都包含位。 然后通过输入传递一个新的长字,用于比较逻辑。 当我尝试比较两个longwords或将longwords的新值设置为I时,我得到类型位中的方法集(int)不适用于参数(bit) 这是我的构造函数: private bit[] longArr; public longword() { int i; longArr = new bit[32]; for (i = 0; i <
然后通过输入传递一个新的长字,用于比较逻辑。
当我尝试比较两个longwords或将longwords的新值设置为I时,我得到类型位中的方法集(int)不适用于参数(bit)
这是我的构造函数:
private bit[] longArr;
public longword() {
int i;
longArr = new bit[32];
for (i = 0; i < 32; i++) {
longArr[i] = new bit();
}
}
专用位[]longArr;
公共长词(){
int i;
longArr=新位[32];
对于(i=0;i<32;i++){
longArr[i]=新位();
}
}
与逻辑
public longword and(longword other){
bit[] newLongWord = new bit[32];
int i,j;
for (i = 0; i < 32; i++) {
newLongWord[i] = new bit();
}
for (j = 0; j< 32; j++){
if(longArr[j] == other.longArr[j]){
newLongWord[0].set(this.longArr[0].and(other.longArr[0]));
}
else if((longArr[j] != other.longArr[j])){
newLongWord[0].set(this.longArr[0].and(other.longArr[0]));
}
else{
newLongWord[0].set(this.longArr[0].and(other.longArr[0]));
}
}
return other;
}
公共长词和(其他长词){
位[]newLongWord=新位[32];
int i,j;
对于(i=0;i<32;i++){
newLongWord[i]=新位();
}
对于(j=0;j<32;j++){
if(longArr[j]==other.longArr[j]){
newLongWord[0]。集合(this.longar[0]。和(other.longArr[0]);
}
else如果((longArr[j]!=other.longArr[j])){
newLongWord[0]。集合(this.longar[0]。和(other.longArr[0]);
}
否则{
newLongWord[0]。集合(this.longar[0]。和(other.longArr[0]);
}
}
归还他人;
}
我只是想在实现正确的逻辑之前运行代码。
编辑:我的比特类
public class bit implements IBit {
private int val; //provides class access to bit values
public bit() {
val = 0; //declare an initial bit value of 0
}
public void set(int val1) {
if (val >= 0 && val <= 1) { //make sure the bit is in bounds of 0-1
val = val1;
}
else {
System.out.println("Bit can only be 0 or 1"); //throw error when bit isnt 0 or 1
}
}
public void toggle() {
if (val == 0) {
val = 1;
}//switch bit value with opposite i.e 0 to 1
else{
val = 0;
}
}
public void set() {
val = 1; //sets default bit to 1 without parameter
}
public void clear() {
val = 0; //sets bit to 0
}
public int getValue() {
return val; //gets bit value
}
public bit and(bit two) {
bit finished = new bit(); //new bit instance for first bit
if (val == 0) {
finished.set(0); //sees if bit1 is set to 0 for and logic
}
else {
if (two.getValue() == 0) {
finished.set(0);//sees if bit2 is set to 0
}
else {
finished.set(1); //otherwise performs and on both bits creating a 1
}
}
return finished;
}
public bit or(bit two) {
bit finished = new bit(); //instance of bit
if (val == 1){
finished.set(1);
} //if the first bit is 1, sets 1 return bit as 1
else {
if (finished.getValue() == 1) //if first bit is not 1 but new instance is 1, set return bit = 1
finished.set(1);
else
finished.set(0);//else return bit = 0
}
return finished;
}
public bit xor(bit two) {
bit finished = new bit(); //instance of bit
if (val == 1) {
if (two.getValue() == 0){//if second bit = 0, return bit is 1
finished.set(1);
}
else{
finished.set(0);//else return bit = 0
}
}
else {
if (two.getValue() == 1){//If second bit =1 , return bit = 1
finished.set(1);
}
else{
finished.set(0);//else return bit = 0
}
}
return finished;
}
public bit not() {
if (val == 0) {//reversal of bit
set(1);
}
else {
set(0);
}
return this; // Returns the opposite of original bit val
}
public String toString() {
String finalVal = Integer.toString(val);
return finalVal;
}
}
公共类位实现IBit{
private int val;//提供对位值的类访问
公共比特(){
val=0;//声明初始位值0
}
公共无效集(int val1){
如果(val>=0&&val则错误消息将其汇总:和()
返回一个位
,但set()
需要一个int
。创建一个set
函数,该函数接受位
:
public void set(bit other) { set(other.val); }
此外,longword.and()
中的逻辑也可以简化:
public longword and(longword other) {
longword newLongWord = new longword();
for (int j = 0; j < longArr.length; j++) {
newLongWord.longArr[j].set(longArr[j].and(other.longArr[j]));
}
return newLongWord;
}
公共长词和(其他长词){
longword newLongWord=新longword();
对于(int j=0;j
为什么索引总是0
?它应该是newLongWord[j].set(this.longar[j]和(other.longArr[j]);
?如果你也可以发布你的bit
类,那会有帮助。它是否包含set
和方法?如果是,它们有什么类型的参数和返回值?显然,bit.and()
返回一个bit
,但是bit.set()
需要一个int
参数。创建一个bit.set()
函数,该函数将bit
对象作为参数:public void set(bit other){…}
。我在我的bit类中进行了编辑。至于索引,在我能够实现此功能之前,它现在只是一个占位符。不清楚为什么您认为需要一个bit
类(顺便说一句,根据Java命名约定,这应该是bit
).Java已经有了通过BitSet
类操作二进制值中位所需的所有方法。这是一个家庭作业问题,教授要求set(int-value)在他给我们的接口中是int。有没有其他方法解决这个问题?当然,将getValue()
附加到和()
将int
从getValue
传递到set
:newLongWord.longar[j]。set(longArr[j]。和(other.longArr[j]).getValue()
或者,在类中编写多个set
函数。您应该有一个set
函数,它接受int,以满足给定的接口,并且您可以有另一个set
函数,它接受一个位
以用于类似的情况。在Java中,有多个同名函数是很常见的但参数不同。事实上,您已经有两个set
函数(一个接受int,一个没有参数),因此这实际上需要编写第三个。好的,现在我创建了另一个set函数,它接受了一个位。由于类型的原因,我似乎无法将函数中的位设置为1或0mismatch@FrankFiumara我得看看密码,也许还有个问题。