如何在我的自定义类中设置构造函数?

时间:2013-11-04 18:36:24

标签: java

您好我是新手创建自己的类。我已经创建了主程序,但是不知道从哪里开始运行程序所需的自定义类。

这是我的主要内容:

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

非常感谢任何反馈!

1 个答案:

答案 0 :(得分:1)

使用

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

}

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