2013-11-04 53 views
-2

你好我是新来创建我自己的类。我创建了主程序,但不知道从我的自定义类开始运行程序所需的位置。如何在我的自定义类中设置构造函数?

这里是我的主:

import java.util.Scanner; 

public class MathQuiz { 
    public static final char symbol_MULTIPLY = '*'; 
    public static final char symbol_ADD = '+'; 

    public static void main(String[] args) { 
    int choice; 

    ShowExampleProblems(); 

    Scanner inputReader = new Scanner(System.in); 

    do { 
     System.out.println("[1] Addition Quiz"); 
     System.out.println("[2] Multiplication Quiz"); 
     System.out.println("[3] Mixed Problem Quiz"); 
     System.out.println("[4] Quit"); 
     System.out.print("\nSelect ==>"); 

     choice = inputReader.nextInt(); 
     System.out.println(); 

     switch (choice) { 
     case 1: 
      AdditionQuiz(); 
      break; 
     case 2: 
      MultiplicationQuiz(); 
      break; 
     case 3: 
      MixedQuiz(); 
      break; 
     case 4: 
      break; 
     default: 
      System.out.println("Invalid selection."); 
      System.out.println(); 
     } 

    } 
    while (choice != 4); 

    System.out.println("Thanks for using the Math Quiz Program."); 
    System.out.println(); 

    } 

    /** 
    * *****************Static Methods******************** 
    */ 
    public static void ShowExampleProblems() { 
    System.out.println("Math Quiz - Sample Problems"); 
    System.out.println("---------------------------\n"); 

    MathProblem p_add = new MathProblem(symbol_ADD); 
    MathProblem p_multiply = new MathProblem(symbol_MULTIPLY); 
    System.out.println("Sample Addition Problem:\t" + p_add); 
    System.out.println("Sample Multiplication Problem:\t" + p_multiply); 
    System.out.println("\n"); 
    } 

    public static void AdditionQuiz() { 
    MathProblem p_add = new MathProblem(symbol_ADD); 
    DoQuiz(p_add); 
    } 

    public static void MultiplicationQuiz() { 
    MathProblem p_multiply = new MathProblem(symbol_MULTIPLY); 
    DoQuiz(p_multiply); 
    } 

    public static void MixedQuiz() { 
    MathProblem p_mixed = new MathProblem(); 
    DoQuiz(p_mixed); 
    } 

    public static void DoQuiz(MathProblem problem) { 
    Scanner keyboard = new Scanner(System.in); 

    int correct = 0; 
    int answer; 

    for (int i = 1; i <= 10; i++) { 
     problem.setNewValues(); 
     System.out.println(i + ") " + problem); 
     System.out.print("\tAnswer ==> "); 
     answer = keyboard.nextInt(); 
     if (problem.isCorrect(answer)) { 
     System.out.println("\tCorrect!\n"); 
     correct++; 
     } 
     else { 
     System.out.print("\tSorry, the correct answer is "); 
     System.out.println(problem.getAnswer() + "\n"); 
     } 
    } 

    System.out.println("You got " + correct + " out of 10 items correct.\n"); 
    } 
} 

这里是我的自定义类:

import java.util.Random; 

public class MathProblem { 
    //************************************** 
    //***********MEMBER VARIABLES*********** 
    //************************************** 
    private int number1, number2; //The 2 numbers in the math problem 
    private char symbol;   //The operation: '+' for adding, '*' for multiplying 
    private boolean mixOperations; //Flag: can problem be either addition or multiplication? 

    //********************************** 
    //***********CONSTRUCTORS*********** 
    //********************************** 
    // One of these methods is called when the object is created 
    //--------------------------------------------------------------------------- 
    //Default Constructor 
    //Sets up a MathProblem object that can be either addition or multiplication 
    //Turns "mix" flag ON 
    //Generates initial values for problem 
    //--------------------------------------------------------------------------- 
    public MathProblem() { 
    mixOperations = true; 
    Random generator = new Random(); 
    number1 = generator.nextInt(10) + 1; 
    number2 = generator.nextInt(10) + 1; 
    } 

    //--------------------------------------------------------------------------- 
    //Alternate Constructor 
    //If a specific operation is designated, set up MathProblem object so that 
    // only that operation will be used 
    //Turns "mix" flag OFF 
    //Sets symbol to the operation specified (+ or *) 
    //Generates initial values for problem 
    //--------------------------------------------------------------------------- 
    public MathProblem(char operation) { 
    mixOperations = false; 
    Random generator = new Random(); 
    number1 = generator.nextInt(10) + 1; 
    number2 = generator.nextInt(10) + 1; 
    } 

    //*********************************** 
    //***********CLASS METHODS*********** 
    //*********************************** 
    //***********Methods to access information about the MathProblem object*************** 
    //--------------------------------------------------------------------------- 
    //Method: getAnswer 
    //Parameters: none 
    //Returns: the answer to the current math problem (an integer) 
    //--------------------------------------------------------------------------- 
    public int getAnswer() { 
    return 0; 
    } 

    //--------------------------------------------------------------------------- 
    //Method: isCorrect 
    //Parameters: answer to be checked (an integer) 
    //Returns: TRUE if answer is correct, FALSE if it is incorrect 
    //--------------------------------------------------------------------------- 
    public boolean isCorrect(int answer) { 
    return false; 
    } 

    //**********Method to update data in the MathProblem object********** 
    //--------------------------------------------------------------------------- 
    //Method: setNewValues() 
    //Parameters: none 
    //Returns: nothing (void) 
    //Task: Generate 2 new random numbers for the MathProblem object 
    //  Store these in the number1 and number2 data members 
    //  If problem allows mixed operations, randomly set symbol to + or * 
    //--------------------------------------------------------------------------- 
    public void setNewValues() { 
    } 

    //**********Method to create printable string representing MathProblem object********** 
    //--------------------------------------------------------------------------- 
    //Method: toString() 
    //Parameters: none 
    //Returns: A String representation of this object 
    //   The String returned is in one of the following formats, 
    //   depending on whether it is an addition or multiplication problem: 
    //    7 + 4 = ? 
    //    7 * 4 = ? 
    //--------------------------------------------------------------------------- 
    public String toString() { 
    return "?"; 
    } 
} 

任何反馈意见是非常感谢!

+0

你的问题是什么? – buzzsawddog

+1

这看起来像一个家庭作业问题。 –

+0

如何在我的自定义类中设置构造函数?我很迷茫,我只需要指出正确的方向。 – user2953452

回答

1

使用

public MathQuiz(pars // if you need parameters) 
{ 

} 

把所有的类在他们自己的文件(ClassName.java),因为它使代码更容易阅读和理解。

+0

这个,并把它放在它自己的文件(MathQuiz.java) – MadConan

+0

真的,我忘了我会添加 –

相关问题