Answered step by step
Verified Expert Solution
Link Copied!

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 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!");
}
}
}
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 (index !=0 && 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;
}
// 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(index !=4 && 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(index !=4 && 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(index !=4 && 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(index !=4 && houses[index].nationality.equals("Norwegian") && houses[index +1].color.equals("blue")){
return false;
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 3 Lnai 9286

Authors: Albert Bifet ,Michael May ,Bianca Zadrozny ,Ricard Gavalda ,Dino Pedreschi ,Francesco Bonchi ,Jaime Cardoso ,Myra Spiliopoulou

1st Edition

ISBN: 3319234609, 978-3319234601

More Books

Students also viewed these Databases questions