简体   繁体   English

编写 .class 文件?

[英]Writing a .class file?

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.目前,我正在开发一个项目,用户可以在 GUI 中输入自定义值,然后这些值将被转换为 .class 文件,供运行时在程序启动时读取。 I realize that writing a .txt file would be much easier, but that is not what I want to do.我意识到编写 .txt 文件会容易得多,但这不是我想要做的。 The new .class file I will be making will extend from an abstract class called "Problem" also.我将制作的新 .class 文件也将扩展自一个名为“Problem”的抽象类。 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.顺便说一句,即使我必须构造一个 .java 文件然后以某种方式编译它,这也可能是一个解决方案。 But still, I don't know how to do that :/但是,我仍然不知道该怎么做:/

More code:更多代码:

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";
    }

}


    }

It's not really what you asked for, but this problem sounds like you want to build an object with a bunch of values, then save the result for later.这不是您真正要求的,但是这个问题听起来像您想用一堆值构建一个对象,然后保存结果以备后用。 If this is the case, then you would probably be interested in object serialization , which allows you to basically save an object as a byte stream, and then load the object at a later time.如果是这种情况,那么您可能会对对象序列化感兴趣,它允许您基本上将对象保存为字节流,然后在以后加载该对象。

As Ken Wayne suggested, you need object serialization.正如 Ken Wayne 所建议的,您需要对象序列化。

A few good libraries for object serialization are一些很好的对象序列化库是

JAXB (XML Serialization) : http://jaxb.java.net/ JAXB(XML 序列化):http: //jaxb.java.net/

Java normal serialization : http://java.sun.com/developer/technicalArticles/Programming/serialization/ Java 正常序列化:http: //java.sun.com/developer/technicalArticles/Programming/serialization/

And as suggested by everyone else, .class file is probably not the best way to go through this.正如其他人所建议的那样,.class 文件可能不是解决这个问题的最佳方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM