Method calculateprice

Asked by At

I have an academic submission to make, but my code doesn't seem to answer the question. I am a beginner.

Question: Assume the cost of a laptop is 2000 and a person wants to buy 3 laptops. Write a program that must call a method/function, named calculatePrice, which takes as input two numbers and returns the cost (e.g. 6000). The program should then print the cost as output. No printing or reading of input should be done from inside the calculatePricemethod/function.

I must use a method function.

    import java.util.Scanner;

    public class Question4
    {
    public static void main(String args[])
       {
          int x, y, z;

          System.out.println("Enter two inputs");
          Scanner in = new Scanner(System.in);

          x = in.nextInt();
          y = in.nextInt();
          z = x + y;

          System.out.println("total cost of laptop = " + z);
       }
    }

1 Answers

-1
Community On

You didn't call any method.

     public static int calculatePrice(int a, int b) {
        return a * b;
     }
     public static void main(String args[])
     {
         int x, y;

         System.out.println("Enter two inputs");
         Scanner in = new Scanner(System.in);

         x = in.nextInt();
         y = in.nextInt();

         System.out.println("total cost of laptop = " + calculatePrice(x, y));
         in.close();
     }