Methods in java (grade calculator)

后端 未结 6 1783
醉酒成梦
醉酒成梦 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:50

    As you can see scoreCalc method needs a set of parameters, but you call it without parameters.

    The second: there is no need in Scanner in = new Scanner(System.in); into your main(String[] args) method. You are calling it into scoreCalc method.

    Third: you are calling scoreCalc twice. The first call is before System.out.println, the second is into System.out.println. And your application will ask user twice to enter values.

    store result value in a variable and show it later:

    double result = scoreCalc(.... required params .....);
    System.out.println("Your score is: " + result);
    
    0 讨论(0)
  • 2021-01-29 06:53

    You are calling your method scoreCalc() without passing the parameters you defined. When you are calling it, it was defined as having 3 parameters.

    scoreCalc(7, 10, 3.0, 8.0);
    

    Also, when creating a class, start it with upper case, GradeCalc

    0 讨论(0)
  • 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.

    0 讨论(0)
  • To start :

    1) Follow coding conventions. (Class name should start with a capital letter).

    2) In your context, you don't need Scanner in = new Scanner(System.in); in main() because you are not using it.

    3) You are a calling the method scoreCalc() without parameters. Whereas, the method needs to be called with parameters.

    4) A method,is a module. It as block of code which increases re-usability. So I suggest that accept the values from user in main() method and then pass them to the method for calculation.

    0 讨论(0)
  • 2021-01-29 07:10

    Some mistakes/errors to point out are:-

    1) You do not need this statement Scanner in = new Scanner(System.in); in your main() , as you are not taking input from user through that function.

    2) Your function scoreCalc (int score1, int maxMark, double weighting, double finalScore) takes parameters, for example its call should look like scoreCalc(15, 50, 1.5, 2.7), but you are calling it as scoreCalc(), that is without paramters from main().

    Edit:- There is one more serious flaw in your program, which might work , but is not good coding. I wont provide code for it , and will leave implementation to you. Take input from user in the main() (using scanner) , assign the result to a temp variable there, and then pass that variable as parameter to the function scoreCalc()

    //pseudocode in main()
    Scanner in = new Scanner(System.in);
    int score= in.nextInt();
    .
    .
    .
    scoreCalc(score,...);
    

    Or you can make your scoreCalc function without parameters, and take user input in it (like present), and finally return just the result to main().

    Both approaches seem appropriate and you are free to choose :)

    0 讨论(0)
  • 2021-01-29 07:11

    A couple of things spring to mind:

    1. You execute scoreCalc() twice. Probably you want to execute it once and save the result in a double variable like: double score = scoreCalc().

    2. Speaking of scoreCalc(): Your definition takes 4 parameters that your don't have as input for that method. You should remove those from your method definition, and instead add score1, maxMark, weighting and finalScorevariable declarations in the method-body.

    3. In your main function, you declare and instantiate a Scanner object you don't use.

    4. Be careful with arithmetic that mixes int and double.

    0 讨论(0)
提交回复
热议问题