Hi, I am quite new to Comp Sci and I just wrote a program in BlueJ that calculates the value of truth tables and validity in arguments. I followed the book's instructions and have a method with the signature: (It's in the class "Main")
public static void main(String[] args) {
code omitted...
}
}
When I call this method in BlueJ it works perfectly fine. However when I run the JAR file, nothing happens.
Any help would be appreciated (this includes direction to material that discuss this).
Thanks in advance!
isaac4luck 0 Newbie Poster
import java.util.*;
/**
* Write a description of class Print here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Interface {
public Interface() {
}
public boolean propSearcher(Logic logic, String a) {
boolean ret = false;
for(String x : logic.getKeys()) {
if(x.equals(a)) ret = true;
}
return ret;
}
public void printMethods() {
System.out.println("To use the operators type 'alpha' then an operator (&, v, >, <->, ~) then type 'beta'");
System.out.println("To negate multiple propositions start by typing '~' first, then type the list of propositions.");
System.out.println("To print a particular type 'alpha' then 'print'.");
System.out.println("To print all type 'PRINT' then 'print'.");
System.out.println("To the names of the propositions type 'EDIT' then 'change name' then type the original name then ");
System.out.println(" type 'to' then type the new name. (Note: if the new name contains more than one word, the spaces");
System.out.println(" will be replaced with periods '.')");
System.out.println("To Check if a particular prop is a tautology type 'CHECK' then 'tautology' then that propositions");
System.out.println("To Check if a particular prop is a logically equivalent to another type 'CHECK' then 'logic' the ");
System.out.println(" names of both propositions.");
System.out.println("To Check for any tautology or logically equivalent propositions just type 'FIND' then either ");
System.out.println(" 'tautology' or 'logic'.");
System.out.println("To Check if an argument is valid simply type 'CHECK' then 'valid' then the propositions in the");
System.out.println(" argument with the conclusion being the last one.");
System.out.println("If you want to see this message again just type 'help'.");
}
/**
* prints p
*/
public void print(Logic logic) {
int keys[] = new int[logic.getNumberOfPropositions()];
Set<String> key = logic.getKeys();
String ke[] = new String[logic.getNumberOfPropositions()];
int i = 0;
for(String x : key) {
keys[i] = x.length();
ke[i] = x;
i++;
}
int max = max(keys);
for(int j = 0; j < logic.getNumberOfPropositions(); j++) {
System.out.print(ke[j] + ": ");
for(int k = max - ke[j].length(); k > 0; k--) System.out.print("-");
ArrayList<Boolean> temp = logic.getP(ke[j]);
ArrayList<String> val = new ArrayList<String>(temp.size());
for(i = 0; i < temp.size(); i++) {
if(temp.get(i)) val.add(temp.get(i) + " ");
else val.add(temp.get(i) + "");
}
System.out.print(val);
System.out.println();
}
}
/**
* used by the print methods to determine the largest key.
*/
private int max(int[] numbers) {
int index = 0;
int max = numbers[index];
while(index < numbers.length) {
if(max > numbers[index]) index++;
else {
max = numbers[index];
index++;
}
}
return max;
}
public void printSpecific(Logic logic, String a) {
System.out.println(a + ": " + logic.getP(a));
}
public void callMain() {
String[] args = new String[0];
Main.main(args);
}
}
The attachment preview is chopped off after the first 10 KB. Please download the entire file.
import java.util.*;
/**
* This program is designed to keep track of
* truth values of propositions. It uses Conditionals (>),
* Biconditionals (<->), Conjunctions (&), and Disjunctions(v).
* It determines whether or not a given proposition is a tautology
* and if it is logically equivalent to any other proposition.
* It also tests for the validity of arguments.
*
* @author (Yitzchok Pinkesz & Aaron Koolyk)
* @version (01.04.2010)
*/
public class Logic {
// instance variables - replace the example below with your own
private HashMap<String,ArrayList<Boolean>> p;
private int length;
private String[] keys;
private ArrayList<Boolean> values;
private boolean full;
/**
* The constructor uses a number specified by the
* user to create a truth table with the given amount
* of propositions. It creates every possible combination.
*
* @param numberOfPropositions
*/
public Logic() {
}
public void start(int numberOfPropositions) {
full = false;
length = (int)(Math.pow(2.0,(double)(numberOfPropositions)));
p = new HashMap<String,ArrayList<Boolean>>(numberOfPropositions);
for(Integer i = 0; i < numberOfPropositions; i++) {
ArrayList<Boolean> tmp = new ArrayList<Boolean>(length);
p.put("" + (i+1),tmp);
}
Boolean nextval = true;
for(int i = 0; i < numberOfPropositions; i++) {
for(int j = 0; j < length; j++) {
ArrayList<Boolean> tmp = p.get("" + (i+1));
tmp.add(nextval);
if((j + 1) % Math.pow(2,i) == 0) nextval = !nextval;
}
}
}
public void start(int numberOfPropositions, String[] a) {
full = true;
length = (int)(Math.pow(2.0,(double)(numberOfPropositions)));
p = new HashMap<String,ArrayList<Boolean>>(numberOfPropositions);
for(int i = 0; i < numberOfPropositions; i++) {
ArrayList<Boolean> tmp = new ArrayList<Boolean>(length);
p.put("" + (a[i]),tmp);
}
Boolean nextval = true;
for(int i = 0; i < numberOfPropositions; i++) {
for(int j = 0; j < length; j++) {
ArrayList<Boolean> tmp = p.get("" + (a[i]));
tmp.add(nextval);
if((j + 1) % Math.pow(2,i) == 0) nextval = !nextval;
}
}
}
public void initializeKeys(int i) {
keys = new String[i];
}
public void addKeys(String a) {
int i = 0;
while(keys[i] != null) {
i++;
}
if(i < keys.length) keys[i] = a;
}
public void initializeValues() {
values = new ArrayList<Boolean>(length);
}
public void addValues(boolean q) {
values.add(q);
}
/**
* Adds a conditional to the list of propositions
*
* @param a,b two Strings that the user inputs to reference two propositions.
*/
public String conditional(String a, String b) {
ArrayList<Boolean> q = new ArrayList<Boolean>(length);
ArrayList<Boolean> tmp1 = p.get(a);
ArrayList<Boolean> tmp2 = p.get(b);
for(int i = 0; i < length; i++) {
if(tmp1.get(i) && !tmp2.get(i)) q.add(false);
else q.add(true);
}
if(!full) {
p.put("(" + a + ">" + b + ")",q);
return "(" + a + ">" + b + ")";
}
else {
p.put("if." + a + ".then." + b + ")",q);
return "if." + a + ".then." + b + ")";
}
}
/**
* Adds a conjunction to the list of propositions.
*
* @param a,b two Strings that the user inputs to reference two propositions.
*/
public String conjunction(String a, String b) {
ArrayList<Boolean> q = new ArrayList<Boolean>(length);
ArrayList<Boolean> tmp1 = p.get(a);
ArrayList<Boolean> tmp2 = p.get(b);
for(int i = 0; i < length; i++) {
if(tmp1.get(i) && tmp2.get(i)) q.add(true);
else q.add(false);
}
if(!full) {
p.put("(" + a + "&" + b + ")",q);
return "(" + a + "&" + b + ")";
}
else {
p.put(a + ".&." + b,q);
return a + ".&." + b;
}
}
/**
* Adds a disjunction to the list of proposition.
*
* @param a,b two Strings that the user inputs to reference two propositions.
*/
public String disjunction(String a, String b) {
ArrayList<Boolean> q = new ArrayList<Boolean>(length);
ArrayList<Boolean> tmp1 = p.get(a);
ArrayList<Boolean> tmp2 = p.get(b);
for(int i = 0; i < length; i++) {
if(tmp1.get(i) || tmp2.get(i)) q.add(true);
else q.add(false);
}
if(!full) {
p.put("(" + a + "v" + b + ")",q);
return "(" + a + "v" + b + ")";
}
else {
p.put("either." + a + ".or." + b + ")",q);
return "either." + a + ".or." + b + ")";
}
}
/**
* Adds a biconditional to the list of propositions.
*
* @param a,b two Strings that the user inputs to reference two propositions.
*/
public String biconditional(String a, String b) {
ArrayList<Boolean> q = new ArrayList<Boolean>(length);
ArrayList<Boolean> tmp1 = p.get(a);
ArrayList<Boolean> tmp2 = p.get(b);
for(int i = 0; i < length; i++) {
if(tmp1.get(i).equals(tmp2.get(i))) q.add(true);
else q.add(false);
}
if(!full) {
p.put("(" + a + "<->" + b +")",q);
return "(" + a + "<->" + b +")";
}
else {
p.put(a + ".if.and.only.if." + b,q);
return a + ".if.and.only.if." + b;
}
}
/**
* Adds a negation of a proposition to the list.
*
* @param a a String that the user inputs to reference the proposition to be negated.
*/
public String negation(String a) {
ArrayList<Boolean> q = new ArrayList<Boolean>(length);
ArrayList<Boolean> tmp1 = p.get(a);
for(int i = 0; i < length; i++) q.add(!tmp1.get(i));
if(!full) {
p.put("~"+ a,q);
return "~"+ a;
}
else {
p.put("it.is.not.the.case.that." + a,q);
return("it.is.not.the.case.that." + a);
}
}
/**
* returns whether or not the given proposition is a tautology.
*/
public boolean isTautology(String a) {
ArrayList<Boolean> tmp = p.get(a);
for(int i = 0; i < tmp.size(); i++) if(!tmp.get(i)) return false;
return true;
}
/**
* returns true if the two given propositions are logicially equivalent.
*/
public boolean isLogicallyEquivalent(String a, String b) {
ArrayList<Boolean> tmp1 = p.get(a);
ArrayList<Boolean> tmp2 = p.get(b);
for(int i = 0; i < tmp1.size(); i++) {
if(!tmp1.get(i).equals(tmp2.get(i))) return false;
}
return true;
}
/**
* returns true if the argument is valid.
*/
public boolean isValidArg(String[] keys,String b) {
boolean checker = false;
ArrayList<Boolean> therefore = p.get(b);
HashMap<Integer,ArrayList<Boolean>> premise = new HashMap<Integer,ArrayList<Boolean>>();
for(int i = 0; i < keys.length; i++) premise.put(i,p.get(keys[i]));
Set<Integer> key = premise.keySet();
ArrayList<ArrayList<Boolean>> tmp = new ArrayList<ArrayList<Boolean>>();
ArrayList<Boolean> tmpo = new ArrayList<Boolean>();
for(int i = 0; i < length; i++) {
for(Integer x : key) {
ArrayList<Boolean> temp = premise.get(x);
tmpo.add(temp.get(i));
}
tmp.add(tmpo);
tmpo = new ArrayList<Boolean>();
}
for(int i = 0; i < therefore.size(); i++) {
if(!therefore.get(i)) {
for(int j = 0; j < tmp.get(i).size(); j++) {
if(!tmp.get(i).get(j)) checker = true;
}
if(!checker) return checker;
else checker = !checker;
}
}
return true;
}
/*****************************************************
* returns the truth table of the given proposition.
*/
public ArrayList<Boolean> getP(String key) {
return p.get(key);
}
/**
* returns the length of the rows.
*/
public int getLength() {
return length;
}
/**
* returns the number of propositions.
*/
public int getNumberOfPropositions() {
return p.size();
}
/**
* Allows the user to add a proposition to p.
*/
public void addP(String key) {
p.put(key,values);
}
/**
* Allows the user to print all keys in p.
*/
public Set<String> getKeys() {
return p.keySet();
}
public void changeNameP(String original, String changeTo) {
ArrayList<Boolean> tmp = p.get(original);
p.remove(original);
p.put(changeTo,tmp);
}
public ArrayList<String> findTautology() {
Set<String> tmp = p.keySet();
ArrayList<String> ret = new ArrayList<String>();
for(String x : tmp) {
if(isTautology(x)) ret.add(x);
}
return ret;
}
public ArrayList<ArrayList<String>> findLogicallyEquivalent() {
Set<String> tmp1 = p.keySet();
Set<String> tmp2 = tmp1;
HashSet<HashSet<String>> ret = new HashSet<HashSet<String>>();
for(String i : tmp1) {
HashSet<String> tmp3 = new HashSet<String>();
tmp3.add(i);
for(String j : tmp2) if(isLogicallyEquivalent(i,j)) {
tmp3.add(j);
}
ret.add(tmp3);
}
ArrayList<ArrayList<String>> tmp4 =
import java.util.*;
/**
* Write a description of class Main here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Main {
private static Scanner sc;
/**
* Constructor for objects of class Main
*/
public Main() {
}
/**********************************************************
*
*/
public static void main(String[] args) {
Logic logic = new Logic();
Interface iF = new Interface();
System.out.println("Welcome to the Logic calculator.");
System.out.println("Please enter the initial number of propositions.");
System.out.println("You can either just type the number or the number and the names of the propositions");
sc = new Scanner(System.in);
System.out.print("> ");
StringTokenizer full = new StringTokenizer(sc.nextLine());
String number = full.nextToken();
try {
if(!full.hasMoreTokens()) logic.start(Integer.parseInt(number));
else {
String[] names = new String[Integer.parseInt(number)];
for(int i = 0; full.hasMoreTokens(); i++) names[i] = full.nextToken();
logic.start(Integer.parseInt(number),names);
}
}
catch(Exception e) {
System.out.println("Error! " + e + ". Either type in a number and the calculator will");
System.out.println("give the propositions default names or type in the number followed by the names.");
System.out.println("(Note: the amount of names cannot exceed the number)");
iF.callMain();
}
iF.printMethods();
System.out.print("> ");
for(boolean done = false; !done;) {
try {
String tmp = sc.nextLine();
boolean ident = false;
if(!tmp.trim().toLowerCase().contains("done")) {
StringTokenizer tmpST = new StringTokenizer(tmp);
String identifier = tmpST.nextToken();
String alpha = identifier;
String operator = "";
if(tmpST.hasMoreTokens()) operator = tmpST.nextToken();
String beta = "";
if(iF.propSearcher(logic,alpha) || alpha.trim().toLowerCase().equals("~")) {
if(tmpST.hasMoreTokens()) beta = tmpST.nextToken();
if(operator.equals("v")) {
alpha = logic.disjunction(alpha,beta);
}
else if(operator.equals("&")) {
alpha = logic.conjunction(alpha,beta);
}
else if(operator.equals("<->")) {
alpha = logic.biconditional(alpha,beta);
}
else if(operator.equals(">")) {
alpha = logic.conditional(alpha,beta);
}
else if(operator.equals("~")) {
alpha = logic.negation(alpha);
}
else if(alpha.equals("~")) {
ident = true;
alpha = logic.negation(beta);
iF.printSpecific(logic, alpha);
alpha = logic.negation(operator);
iF.printSpecific(logic, alpha);
while(tmpST.hasMoreTokens()) {
String x = tmpST.nextToken();
alpha = logic.negation(x);
iF.printSpecific(logic, alpha);
}
}
else System.out.println("Error! Please enter an operator (& v > <->). Thank You.");
if(!ident) iF.printSpecific(logic,alpha);
}
else if(identifier.equals("PRINT")) {
String method = operator;
if(method.equals("print")) iF.print(logic);
else if(method.equals("length")) System.out.println("The number of rows is " + logic.getLength());
else if(method.equals("keys")) System.out.println("The propositions are " + logic.getKeys());
else if(method.equals("number")) System.out.println("The number of proposisitions is " + logic.getNumberOfPropositions());
else {
System.out.println("Error! Please type either 'print', 'printkv', 'length', 'keys', 'number of propositions'");
System.out.println("or 'specific' followed by the specific proposition. Thank You.");
}
}
else if(identifier.equals("EDIT")) {
String edit = operator;
if(edit.equals("change")) {
String change = tmpST.nextToken().trim().toLowerCase();
if(change.equals("name")) {
String initial = tmpST.nextToken();
for(String temp = tmpST.nextToken(); !temp.equals("to"); temp = tmpST.nextToken()) initial += "." + temp;
String fiNal = tmpST.nextToken();
while(tmpST.hasMoreTokens()) fiNal += "." + tmpST.nextToken();
logic.changeNameP(initial,fiNal);
}
}
}
else if(identifier.equals("CHECK")) {
String method = operator;
if(method.equals("tautology")) {
if(logic.isTautology(tmpST.nextToken())) System.out.println("Is a Tautology");
else System.out.println("Is Not a Tautology");
}
else if(method.contains("logic")) {
if(logic.isLogicallyEquivalent(tmpST.nextToken(), tmpST.nextToken())) System.out.println("Is Logically Equivalent");
else System.out.println("Is Not Logically Equivalent");
}
else if(method.equals("valid")) {
String[] temp = new String[tmpST.countTokens() - 1];
for(int i = 0; i < temp.length; i++) temp[i] = tmpST.nextToken();
if(logic.isValidArg(temp,tmpST.nextToken())) System.out.println("Is a Valid Argument");
else System.out.println("Is Not a Valid Argument");
}
}
else if(identifier.equals("FIND")) {
if(operator.equals("tautology")) {
ArrayList<String> finder = logic.findTautology();
for(int i = 0; i < finder.size(); i++) iF.printSpecific(logic,finder.get(i));
}
else if(operator.contains("logic")) {
boolean retTmp = false;
int print = 1;
ArrayList<ArrayList<String>> tmp1 = logic.findLogicallyEquivalent();
for(int i = 0; i < tmp1.size(); i++) {
if(tmp1.get(i).size() > 1) {
retTmp = true;
System.out.println("Logically Equivalent Set #" + print++ + ":");
for(int j = 0; j < tmp1.get(i).size(); j++) iF.printSpecific(logic,tmp1.get(i).get(j));
}
}
if(!retTmp) System.out.println("No Logically Equivalent Propositions.");
}
}
else if(identifier.trim().toLowerCase().equals("help")) iF.printMethods();
System.out.print("> ");
}
else done = true;
}
catch(Exception e) {
System.out.println("Error! " + e + ". If you continue to experience problems please contact");
System.out.println("tech support at yitzchokpinkesz@yahoo.com.");
System.out.print("> ");
}
}
}
}
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster
You need to provise more information. How was the jar file created, how do you run it, and do you get any error messages?
isaac4luck 0 Newbie Poster
You need to provise more information. How was the jar file created, how do you run it, and do you get any error messages?
BlueJ has an export option that creates the JAR file for you (if there is another way to do it, that would also be helpful). When I run (double-click) it nothing happens, as opposed to a terminal opening which allows me to interact with the program (that is what is supposed to happen when I run the main(String[] args) method, it works on blueJ).
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster
Just because it creates a jar file, doesn't mean it is executable. It only contains all the classes of your project. Then you can take that jar and you can import it to another project and call the classes that are inside.
If that jar file has a class that has a main method inside then you can run the class from command prompt from the jar file.
So my answer is that I don't know how BlueJ creates a jar file. I read somewhere that the jar file has a MANIFEST file inside where you declare if the jar is executable and what class contains the main method.
I would suggest to search the internet on how to create an executable jar file.
jwenting 1,889 duckman Team Colleague
Read the documentation of the jar tool (which is part of the documentation set of the JDK, as the jar tool is part of the JDK) for instructions on how to create executable jar files.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.