Skip to content
Snippets Groups Projects
Commit 6ce706e1 authored by Adam Blank's avatar Adam Blank
Browse files

Initial Commit

parents
No related branches found
No related tags found
No related merge requests found
import java.lang.reflect.Field;
public abstract class Model {
public abstract boolean sat();
public abstract boolean implies(boolean p, boolean q);
public String toString() {
StringBuilder result = new StringBuilder();
try {
Field[] fields = this.getClass().getDeclaredFields();
for (Field f : fields) {
if (f.getType().isArray() && f.getType().getComponentType() == boolean.class) {
f.setAccessible(true);
result.append(f.getName() + " = " + java.util.Arrays.toString((boolean[])f.get(this)));
result.append(", ");
}
}
} catch (IllegalAccessException e) {
throw new RuntimeException("This should never happen. Contact Course Staff!");
}
result.delete(result.length() - 2, result.length());
return "{" + result.toString() + "}";
}
public static boolean[] booleans() {
return new boolean[]{true, false};
}
}
If you want to use git, you can clone this repository. If you do not want to
use git, download the two following Java files. Note that Model.java is *different*
than it was on HW1.
- https://gitlab.cs.washington.edu/cse311-17sp/hw2-lying-tas-redux/blob/master/Model.java
- https://gitlab.cs.washington.edu/cse311-17sp/hw2-lying-tas-redux/blob/master/Situation1NPeople.java
import java.util.List;
import java.util.ArrayList;
/**
* Now, we generalize the work you did on HW 1. Instead of saying there are a particular
* number of people, we will allow an arbitrary number of people in the model.
*
* Perhaps unsurprisingly, this means that instead of a bunch of boolean variables, we will
* be using an *array*. The important idea here is that the "array" is exactly like the
* predicates we've been discussing in class. In particular, given a "person", the IsTA
* array tells us if that person is a TA.
*
* This time, we make the assumption that the "0th person" is the one who speaks.
*
* You should begin by copying over your sat implementation from HW1.
*
* Then, you should fill in correct implementations for allStudentOrTA,
* noneStudentAndTA, and someoneIsTA.
*
* Finally, you should combine your old sat with the three methods you wrote above.
**/
public class Situation1NPeople {
private static class Situation1Model extends Model {
private int NUM_PEOPLE;
private boolean[] IsTA, IsStudent;
public Situation1Model(boolean[] IsTA, boolean[] IsStudent) {
this.IsTA = IsTA;
this.IsStudent = IsStudent;
this.NUM_PEOPLE = this.IsTA.length;
}
public boolean implies(boolean p, boolean q) {
return !p || q;
}
public boolean allStudentOrTA() {
// This is not correct!
return true;
}
public boolean noneStudentAndTA() {
// This is not correct!
return true;
}
public boolean someoneIsTA() {
// This is not correct!
return true;
}
public boolean sat() {
// Combine your HW1 implementation with the methods you just wrote.
return true;
}
}
/**
* This method returns all the Strings made up of T's and F's
* of the provided length.
**/
public static List<String> allTFStrings(int length) {
if (length == 0) {
List<String> result = new ArrayList<>();
result.add("");
return result;
}
else {
List<String> newResult = new ArrayList<>();
List<String> next = allTFStrings(length - 1);
for (String s : next) {
newResult.add(s + "T");
newResult.add(s + "F");
}
return newResult;
}
}
/**
* This method converts a String of "T"s and "F"s to an array
* of booleans.
**/
public static boolean[] TFStringToArray(String bools) {
boolean[] result = new boolean[bools.length()];
for (int i = 0; i < result.length; i++) {
result[i] = bools.charAt(i) == 'T';
}
return result;
}
/**
* This method returns an array of all the models for a particular
* number of people. Why can we allocate the array up-front? Why
* is the size what it is?
*/
public static Model[] allModels(int i) {
Model[] models = new Model[(int)Math.pow(2, 2 * i)];
int idx = 0;
for (String taBools : allTFStrings(i)) {
for (String studentBools : allTFStrings(i)) {
models[idx] = new Situation1Model(TFStringToArray(taBools), TFStringToArray(studentBools));
idx++;
}
}
return models;
}
public static void main(String[] args) {
for (int i = 2; i < 6; i++) {
System.out.println("The models for " + i + " people are...");
for (Model model : allModels(i)) {
if (model.sat()) {
System.out.println(" " + model);
}
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment