Methods in java (grade calculator)

后端 未结 6 1787
醉酒成梦
醉酒成梦 2021-01-29 06:49

We\'ve been learning about methods in java (using netbeans) in class and I\'m still a bit confused about using methods. One homework question basically asks to design a grade ca

6条回答
  •  醉话见心
    2021-01-29 06:56

    As opposed to other answers I will start with one other thing.

    You forgot about the most important method - and that is the Constructor.

    You have to create a grade calculator, so you create a class(type) that represents objects of grade calculators. Using the Java convention this class should be named GradeCalculator, don't use abbreviations like Calc so that the name is not ambiguous.

    So back to the constructor - You have not created your Calculator object. It may not be needed and you may achieve your goal not using it, but it's not a good practice.

    So use this method as well - this way, you'll create actual Calculator object.

    It can be achieved like that:

    public static void main(String[] args)
    {
        GradeCalculator myCalculator = new GradeCalculator();
    }
    

    And now you can imagine you have your calculator in front of you. Whan can you do with it? Getting a mark would be a good start - so, what you can do is:

    myCalculator.getMark()
    

    Now you'll have to define an method getMark():

    private void getMark() { }
    

    In which you would prompt the user for the input. You can also do:

    myCalculator.getMaxMark() { }
    

    and that way get max mark (after defining a method).

    The same way you can call method myCalculator.getWeighting(), myCalculator.calculateFinalResult(), myCalculator.printResult().

    This way you'll have actual object with the following mehtods (things that it can do):

    public GradeCalculator() { } //constructor
    private void getMark() { } //prompts user for mark
    private void getMaxMark() { } //prompts user for max mark
    private void getWeighting() { } //prompts user for weighting factor
    private void calculateFinalResult() // calculates the final result
    private void printResult() // prints the result.
    

    And that I would call creating a calculator using methods - and that I would grade highly. Try to think of the classes you are creating as a real objects and create methods representing the behaviour that objects really have. The sooner you'll start to do that the better for you. Writing whole code in one method is not a good practice and in bigger applications can lead to various problems. So even when doing small projects try to do them using best practices, so that you don't develop bad habbits.

    Edit: So that your whole program can look like this:

    import java.util.Scanner;
    
    public class GradeCalculator 
    {
        //here you define instance fields. Those will be visible in all of your classes methods.
        private Scanner userInput;  //this is the userInput the calculator keypad if you will.
    
        private int mark;  //this is the mark the user will enter.
        private int maxMark;  //this is the mark the user will enter.
        private int weightingFactor;  //this is the weighting factor the user will enter.
        private int result;  //this is the result that will be calculated.
    
        public static void main(final String args[])
        {
            Scanner userInput = new Scanner(System.in);  //create the input(keypad).
            GradeCalculator calculator = new GradeCalculator(userInput); //create the calculator providing it with an input(keypad)
            calculator.getMark();
            calculator.getMaxMark();
            calculator.getWeightingFactor();
            calculator.printResult();
        }
    
        private GradeCalculator(final Scanner userInput)
        {
            this.userInput = userInput;  //from now the provided userInput will be this calculators userInput. 'this' means that it's this specific calculators field (defined above). Some other calculator may have some other input.
        }
    
        private void getMark() { }  //here some work for you to do.
    
        private void getMaxMark() { } //here some work for you to do.
    
        private void getWeightingFactor() { } //here some work for you to do.
    
        private void printResult() { } //here some work for you to do.
    }
    

    Please mind the fact that after constructing the Calculator object you don't have to use methods that are static.

提交回复
热议问题