public class Domino { private int leftValue; private int rightValue; public Domi
ID: 3793134 • Letter: P
Question
public class Domino {
private int leftValue;
private int rightValue;
public Domino(int leftValue, int rightValue) {
this.leftValue = leftValue;
this.rightValue = rightValue;
}
public int getLeftValue() {
return leftValue;
}
public int getRightValue() {
return rightValue;
}
}
//This creates the Array
Domino[] dominos = new Domino[] {
new Domino(2, 3)
new Domino(3, 2)
new Domino(2, 5)
new Domino(2, 5)
}
//This part Loops and matchs left and right values
Domino previous = null
for (Domino current : dominos) {
if (previous != null) {
if (current.getLeftValue() != previous.getRightValue()) {
throw new Exception("Illegal")
}
}
previous = current
}
I am getting an error on line 19 that says this....
File: /Users/Stephanie/Desktop/Domino.java [line: 19]
Error: /Users/Stephanie/Desktop/Domino.java:19: class, interface, or enum expected
But I don't know how to fix that.
Explanation / Answer
Note: I just rectified the compile time errors .But I dont know the requirement.So Am not changing the logic which you mentioned.Check this code once.Thank You.
______________________
Domino.java
public class Domino {
private int leftValue;
private int rightValue;
public Domino(int leftValue, int rightValue) {
this.leftValue = leftValue;
this.rightValue = rightValue;
}
public int getLeftValue() {
return leftValue;
}
public int getRightValue() {
return rightValue;
}
public static void main(String[] args) {
// This creates the Array
Domino[] dominos = new Domino[] {
new Domino(2, 3),
new Domino(3, 2),
new Domino(2, 5),
new Domino(2, 5) };
// This part Loops and matchs left and right values
Domino previous = null;
for (Domino current : dominos) {
if (previous != null) {
if (current.getLeftValue() != previous.getRightValue()) {
try {
throw new Exception("Illegal");
} catch (Exception e) {
e.printStackTrace();
}
}
}
previous = current;
}
}
}
____________________
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.