Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7622121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:22:20+00:00 2026-05-31T04:22:20+00:00

Currently, I’m working on a project where a user can enter in custom values

  • 0

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 :/

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

}


    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T04:22:21+00:00Added an answer on May 31, 2026 at 4:22 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently working on database part of android project. The main aim of the project
Currently i m working on a project where there are users with four roles
Currently, Tapping on the same Tab (in which user is working), The App moves
Currently I'm prototyping search with Lucene.Net-2.0-004 on a web application. It's working very well,
currently we have a column with only integer values declared as NUMBER. At the
Currently, most of the popular websites, like google, yahoo detect if the user connection
Currently, Enum.Parse supports only the comma as the value separator, so that MemberOne,MemberThree will
Currently, I use an abstract factory to allow the specification of a custom class
Currently we have a project with a standard subversion repository layout of: ./trunk ./branches
Currently, working with RC0 Denali, having some issues I am attempting to review data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.