Prolog Suppose we have a collection of rules about what people are wearing. We w
ID: 3798395 • Letter: P
Question
Prolog
Suppose we have a collection of rules about what people are wearing. We will represent an item of clothing by a structure with function name "clothing" and three parameters: the type of clothing (hat, shirt, etc.), the color (blue, green, etc.), and the size (a whole number). For example:
wearing( julia, clothing( hat, red, 3 ) ).
wearing( julia, clothing( shirt, blue, 4 ) ).
wearing( jack, clothing( shirt, purple, 6 ) ).
wearing( bill, clothing( hat, red, 10 ) ).
wearing( marie, clothing( shirt, blue, 2 ) ).
wearing( jack, clothing( hat, green, 10 ) ).
Write a rule (and then test it) to find facts of who will be pinched on Saint Patrick’s Day for not wearing green.
Explanation / Answer
package Other;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeMap;
public class Chegg_1 {
public static void main(String[] args) {
TreeMap<Clothing,String> map=new TreeMap<Clothing,String>(new pinchWinner());
map.put(new Clothing("hat","red",3),"julia");
map.put(new Clothing("shirt","blue",4),"julia");
map.put(new Clothing("shirt","purple",6),"jack");
map.put(new Clothing("hat","red",10),"bill");
map.put(new Clothing("shirt","blue",2),"marie");
map.put(new Clothing("hat","green",10),"jack");
Set<Clothing> keys=map.keySet();
for(Clothing key:keys){
System.out.println(key);
}
}
}
class pinchWinner implements Comparator<Clothing>{
public int compare(Clothing c1,Clothing c2){
if(c1.getColour().equalsIgnoreCase("green")){
return 0;
}else{
return 1;
}
}
}
class typeComparator implements Comparator<Clothing>{
public int compare(Clothing c1,Clothing c2){
return c1.getType().compareTo(c2.getType());
}
}
class colourComparator implements Comparator<Clothing>{
public int compare(Clothing c1,Clothing c2){
return c1.getColour().compareTo(c2.getColour());
}
}
class sizeComparator implements Comparator<Clothing>{
public int compare(Clothing c1,Clothing c2){
if(c1.getSize()>c2.getSize()){
return 1;
}else{
return -1;
}
}
}
class Clothing{
private String type;
private String colour;
private int size;
public Clothing(String type, String colour,int size){
this.type = type;
this.colour = colour;
this.size=size;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getColour() {
return colour;
}
public void setColour(String colour) {
this.colour = colour;
}
public int getSize(){
return size;
}
public void setSize(int size){
this.size=size;
}
public String toString(){
return "Type: "+this.type+"-- Colour: "+this.colour+"---Size:"+this.colour;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.