Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I keep getting error messages. This is just the driver and the Solution class. Here are the files: public class Application { public static void
I keep getting error messages. This is just the driver and the Solution class.
Here are the files:
public class Application
public static void mainString args
House houses new House;
if SolutionsolveRiddlehouses
System.out.printlnSolution found!";
Print the solution
for House house : houses
System.out.printlnhousecolor house.nationality house.drink house.cigar house.pet;
else
System.out.printlnNo solution found!";
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 solveRiddleHouse houses, int index
if index
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
housesindex new Housecolor nationality, drink, cigar, pet;
Check if the current assignment satisfies all constraints
if isValidhouses index && solveRiddlehouses index
return true; Found a valid solution
return false; No valid solution found, backtrack
public static boolean isValidHouse houses, int index
Apply the given clues as constraints
Clue : The Brit lives in the red house
if index && housesindexnationality.equalsBrit && housesindexcolor.equalsred
return false;
Clue : The Swede keeps dogs as pets
if housesindexnationality.equalsSwede && housesindexpet.equalsdogs
return false;
Clue : The dane drinks tea
if housesindexnationality.equalsDane && housesindexdrink.equalstea
return false;
Clue : The green house is on the left of and next to the white house
ifindex && housesindexcolor.equalsgreen && housesindex color.equalswhite
return false;
Clue : The green house owner drinks coffee
ifhousesindexcolor.equalsgreen && housesindexdrink.equalscoffee
return false;
Clue : The person who smokes Pall Malls keeps birds
ifhousesindexcigar.equalsPall Mall" && housesindexpet.equalsbirds
return false;
Clue : The owner of the yellow house smokes Dunhills
ifhousesindexcolor.equalsyellow && housesindexcigar.equalsDunhill
return false;
Clue : The man living in the house right in the center drinks milk
ifindex && housesindexdrink.equalsmilk
return false;
Clue : The man who smokes Blends lives next to the one who keeps cats
ifindex && housesindexcigar.equalsBlends && housesindex pet.equalscats
return false;
Clue : The Norwegian lives in the first house
ifindex && housesindexnationality.equalsNorwegian
return false;
Clue : The man who keeps horses lives next to the one who smokes Dunhills
ifindex && housesindexpet.equalshorses && housesindex cigar.equalsDunhill
return false;
Clue : The owner who smokes Bluemasters drinks beer
ifhousesindexcigar.equalsBluemasters && housesindexdrink.equalsbeer
return false;
Clue : The German smokes Princes
ifhousesindexnationality.equalsGerman && housesindexcigar.equalsPrinces
return false;
Clue : The Norwegian lives next to the blue house
ifindex && housesindexnationality.equalsNorwegian && housesindex color.equalsblue
return false;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started