Note that there are some explanatory texts on larger screens.

plurals
  1. POWriting a .class file?
    primarykey
    data
    text
    <p>Currently, I'm working on a project where a user can enter in custom values in a GUI then those values will be translated into a .class file for the runtime to read when the program starts up. I realize that writing a .txt file would be much easier, but that is not what I want to do. The new .class file I will be making will extend from an abstract class called "Problem" also. Can someone point me in the right direction for writing the aforementioned file? Thanks in advance for helpers! By the way, even if I have to construct a .java file then compile that somehow, that could be a solution also. But still, I don't know how to do that :/</p> <p>More code:</p> <pre><code>package resources; import java.awt.Image; import java.io.File; import java.io.Serializable; public abstract class Problem implements Comparable&lt;Problem&gt;, Serializable{ private static final long serialVersionUID = 42L; private File locatedAt; public static final int EASY = 0; public static final int MEDIUM = 1; public static final int HARD = 2; public abstract String getTitle(); public abstract String getQuestion(); public abstract Image getQuestionImage(); public abstract int getDifficulty(); public abstract Topic getTopic(); public abstract String getAuthor(); public abstract boolean isCorrect(String answer); public final int compareTo(Problem p){ return this.getTitle().compareTo(p.getTitle()); } public final String toString(){ return getTitle(); } public final void setLocatedAt(File file){ locatedAt = file; } } package resources; import java.util.StringTokenizer; public abstract class NumericProblem extends Problem{ /** * You must specify the number of significant digits the answer should contain. * If you don't want to check for significant digits, simply return 0 * * @return the number of significant digits the answer should have * * @since V 1.0 */ public abstract boolean checkSigfigs(); /** * You must specify the amount of error from the answer the user can be within * to remain correct. Your number should be represented as X% and not the decimal * format. * * @return the amount of error the submitted answer can deviate from the specified answer * * @since V 1.0 */ public abstract double getErrorPercentage(); /** * You must specify the type of units the problem should contain. * If the answer doesn't have any units return "". Also if the units shouldn't * be checked, return null. * * @return the unit type the answer should contain * * @since V 1.0 */ public abstract String getUnits(); /** * You must specify the answer for the problem being asked. The number is * represented as a String because of significant digits. * * @return the answer for the given problem * * @since V 1.0 */ public abstract String getAnswer(); public final boolean isCorrect(String userAnswer){ String answer = getAnswer().trim(); userAnswer = userAnswer.trim(); StringTokenizer tokener = new StringTokenizer(userAnswer, " "); if(tokener.countTokens() != 2){ System.err.println("Failed at formatting"); return false; } userAnswer = tokener.nextToken(); String userUnits = tokener.nextToken(); System.out.println(sigfigsIn(answer)); System.out.println(sigfigsIn(userAnswer)); // Checks sigificant digits if(checkSigfigs()){ if(!(sigfigsIn(userAnswer) == sigfigsIn(answer))){ System.err.println("Failed at sig figs"); return false; } } // Checks numeric if(!checkNumeric(userAnswer, answer)){ System.err.println("Failed at numeric"); return false; } //Checks units if(getUnits() != null){ if(!userUnits.equals(getUnits())){ System.err.println("Failed at units"); return false; } } System.out.println("Passed!"); return true; } private int sigfigsIn(String aNumber){ // Removes all unnecessary zeroes before answer boolean done = false; boolean periodHappened = false; while(!done) { if(aNumber.charAt(0) == '0'){ aNumber = aNumber.replaceFirst("0", ""); }else if (aNumber.charAt(0) == '.'){ aNumber = aNumber.replaceFirst(".", ""); periodHappened = true; }else{ done = true; } } // If it's a number like 300 with only one sig fig, do dis if(!periodHappened){ if(!aNumber.contains(".")){ done = false; while(!done){ if(aNumber.charAt(aNumber.length() - 1) == '0'){ aNumber = aNumber.substring(0, aNumber.length() - 1); }else{ done = true; } } } } return aNumber.replaceAll("\\.", "").length(); } private boolean checkNumeric(String Answer, String UserAnswer){ double answer = Double.parseDouble(Answer); double userAnswer = Double.parseDouble(UserAnswer); double ep = getErrorPercentage() / 100; if((answer * (1+ep) &gt;= userAnswer) &amp;&amp; (userAnswer &gt;= answer * (1-ep))) return true; return false; } package problems; import java.awt.Image; import resources.NumericProblem; import resources.Problem; import resources.Topic; import resources.Formula; public class ANumericProblem extends NumericProblem{ private final Formula formula; public ANumericProblem(){ formula = Formula.createRandomFormula(); } @Override public boolean checkSigfigs() { return true; } @Override public double getErrorPercentage() { return 200; } @Override public String getUnits() { return "mols"; } @Override public String getAnswer() { return Formula.getMols(); } @Override public String getTitle() { return "Formula"; } @Override public String getQuestion() { return "How many moles are in 4.9g of " + formula.getFormula(); } @Override public Image getQuestionImage() { return null; } @Override public int getDifficulty() { return Problem.EASY; } @Override public Topic getTopic() { return new Topic("Grams to Moles"); } @Override public String getAuthor() { return "Shawn"; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload