Question: Right logic, bad output ( repeated ) public class Application { public static void main ( String [ ] args ) { House [ ]

Right logic, bad output(repeated)
public class Application {
public static void main(String[] args){
House[] houses = new House[5];
if (Solution.solveRiddle(houses,0)){
System.out.println("Solution found!");
// Print the solution
for (House house : houses){
System.out.println(house.color +""+ house.nationality +""+ house.drink +""+ house.cigar +""+ house.pet);
}
} else {
System.out.println("No solution found!");
}
}
}
class Solution {
static String[] possibleColors ={"red", "green", "white", "yellow", "blue"};
static String[] possibleNationalities ={"Brit", "Swede", "Dane", "Norwegian", "German"};
static String[] possibleDrinks ={"tea", "coffee", "milk", "beer", "water"};
static String[] possibleCigars ={"Pall Mall", "Dunhill", "Blends", "Bluemasters", "Princes"};
static String[] possiblePets ={"dogs", "birds", "cats", "horses", "fish"};
public static boolean solveRiddle(House[] houses, int index){
if (index ==5){
return true; // Base case: all houses have been assigned attributes
}
// Try all possible attributes for the current house
for (String color : possibleColors){
for (String nationality : possibleNationalities){
for (String drink : possibleDrinks){
for (String cigar : possibleCigars){
for (String pet : possiblePets){
houses[index]= new House(color, nationality, drink, cigar, pet);
// Check if the current assignment satisfies all constraints
if (isValid(houses, index) && solveRiddle(houses, index +1)){
return true; // Found a valid solution
}
}
}
}
}
}
return false; // No valid solution found, backtrack
}
public static boolean isValid(House[] houses, int index){
if (index ==5){
return true; // Reached the end of the houses array
}
// Check if the house at index is null
if (houses[index]== null){
return false;
}
// Apply the given clues as constraints
if (index !=0 && houses[index].nationality.equals("Brit") && !houses[index].color.equals("red")){
return false;
}
if (houses[index].nationality.equals("Swede") && !houses[index].pet.equals("dogs")){
return false;
}
if (houses[index].nationality.equals("Dane") && !houses[index].drink.equals("tea")){
return false;
}
if (index !=4 && houses[index].color.equals("green") && houses[index +1]!= null && houses[index +1].color.equals("white")){
return false;
}
if (houses[index].color.equals("green") && !houses[index].drink.equals("coffee")){
return false;
}
if (houses[index].cigar.equals("Pall Mall") && !houses[index].pet.equals("birds")){
return false;
}
if (houses[index].color.equals("yellow") && !houses[index].cigar.equals("Dunhill")){
return false;
}
if (index ==2 && !houses[index].drink.equals("milk")){
return false;
}
if (index !=4 && houses[index].cigar.equals("Blends") && houses[index +1]!= null && houses[index +1].pet.equals("cats")){
return false;
}
if (index ==0 && !houses[index].nationality.equals("Norwegian")){
return false;
}
// Clue 11: The man who keeps horses lives next to the one who smokes Dunhills
if (index !=4 && houses[index].pet.equals("horses") && houses[index +1]!= null && houses[index +1].cigar.equals("Dunhill")){
return false;
}
// Clue 12: The owner who smokes Bluemasters drinks beer
if (houses[index].cigar.equals("Bluemasters") && !houses[index].drink.equals("beer")){
return false;
}
// Clue 13: The German smokes Princes
if (houses[index].nationality.equals("German") && !houses[index].cigar.equals("Princes")){
return false;
}
// Clue 14: The Norwegian lives next to the blue house
if (index !=4 && houses[index].nationality.equals("Norwegian") && houses[index +1]!= null && houses[index +1].color.equals("blue")){
return false;
}
return true;
}
}
class House {
String color;
String nationality;
String drink;
String cigar;
String pet;
public House(String color, String nationality, String drink, String cigar, String pet){
this.color = color;
this.nationality = nationality;
this.drink = drink;
this.cigar

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!