Question: This code results in No solution found, which is not correct. I just need help with the logic. public class House { String color; String

This code results in No solution found, which is not correct. I just need help with the logic.
public 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 = cigar;
this.pet = pet;
}
}
public 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){
// Apply the given clues as constraints
// Clue 1: The Brit lives in the red house
if (houses[index].nationality.equals("Brit") && !houses[index].color.equals("red")){
return false;
}
// Clue 2: The Swede keeps dogs as pets
if (houses[index].nationality.equals("Swede") && !houses[index].pet.equals("dogs")){
return false;
}
// Add more constraints for the remaining clues
// Clue 3: The dane drinks tea
if (houses[index].nationality.equals("Dane") && !houses[index].drink.equals("tea")){
return false;
}
// Clue 4: The green house is on the left of and next to the white house
if(houses[index].color.equals("green") && houses[index +1].color.equals("white")){
return false;
}
// Clue 5: The green house owner drinks coffee
if(houses[index].color.equals("green") && !houses[index].drink.equals("coffee")){
return false;
}
// Clue 6: The person who smokes Pall Malls keeps birds
if(houses[index].cigar.equals("Pall Mall") && !houses[index].pet.equals("birds")){
return false;
}
// Clue 7: The owner of the yellow house smokes Dunhills
if(houses[index].color.equals("yellow") && !houses[index].cigar.equals("Dunhill")){
return false;
}
// Clue 8: The man living in the house right in the center drinks milk
if(index ==2 && !houses[index].drink.equals("milk")){
return false;
}
// Clue 9: The man who smokes Blends lives next to the one who keeps cats
if(houses[index].cigar.equals("Blends") && houses[index +1].pet.equals("cats")){
return false;
}
// Clue 10: The Norwegian lives in the first house
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(houses[index].pet.equals("horses") && 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(houses[index].nationality.equals("Norwegian") && houses[index +1].color.equals("blue")){
return false;
}
// Clue 15: The man who smokes Blends has a neighbor who drinks water
if(houses[index].cigar.equals("Blends") && houses[index +1].drink.equals("water")){
return false;
}
return true;
}
public static void main(String[] args){

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!