目前,我正在开发一个项目,用户可以在GUI中输入自定义值,然后这些值将被转换为.class文件,以便运行时在程序启动时读取。我意识到编写.txt文件会容易得多,但这不是我想要做的。我将要创建的新.class文件也将扩展为名为“Problem”的抽象类。有人能指出我正确的方向写上述文件吗?在此先感谢帮助! 顺便说一下,即使我必须构造一个.java文件,然后以某种方式编译,这也可能是一个解决方案。但是,我仍然不知道如何做到这一点:/
更多代码:
package resources;
import java.awt.Image;
import java.io.File;
import java.io.Serializable;
public abstract class Problem implements Comparable<Problem>, 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) >= userAnswer) && (userAnswer >= 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";
}
}
}
答案 0 :(得分:1)
这不是你要求的,但是这个问题听起来像你想用一堆值构建一个对象,然后保存结果以供日后使用。如果是这种情况,那么您可能会对object serialization感兴趣,它允许您基本上将对象保存为字节流,然后在以后加载对象。
答案 1 :(得分:1)
正如Ken Wayne所说,你需要对象序列化。
用于对象序列化的一些好的库是
JAXB(XML序列化):http://jaxb.java.net/
Java正常序列化:http://java.sun.com/developer/technicalArticles/Programming/serialization/
正如其他人所建议的那样,.class文件可能不是最好的方法。