Rundong Huang huang784purdueedu description Program to cal

/** * @Rundong Huang huang784@purdue.edu *@description: Program to calculate the square of a given number */import java.util.scanner; public class SquareCalculator {public int squareIt(int input){int square = input * input; return square;} public static void main(String[] args) {this.input = input; SquareCalculator obj = new SquareCalculator(); Scanner in = new Scanner(System.in); System.out.println(\"Enter your input:\"); int input = in.nextInt(); int square = obj.squareIt(input); System.out.println(\"Squared Input is \" + square);}}

Solution

Note:

I just Deleted the this.input=input; statement.This is wrong.This always refers to the current class instance variables,constructors,methods

Here no need of using this keyword.

_________________________

SquareCalculator.java

import java.util.Scanner;

//SquareCalculator class
public class SquareCalculator {

   //Method which calculates the square of a number
   public int squareIt(int input)
   {
       //Calculating the square
       int square=input*input;
       return square;
   }
   public static void main(String[] args) {
      
       //Creating the object for the SquareCalculator class
       SquareCalculator obj=new SquareCalculator();
      
       /* Scanner class object is used to read
       * the inputs entered by the user
       */
       Scanner in=new Scanner(System.in);
      
       //Getting the input entered by the user
       System.out.println(\"Enter your input :\");
       int input=in.nextInt();
      
       /* Calling the method by passing
       * the user entered number as argument
       */
       int square=obj.squareIt(input);
      
       //Displaying the output
       System.out.println(\"Squared input is \"+square);
      

   }

}

______________________

Output:

Enter your input :
9
Squared input is 81

___________

Output2:

Enter your input :
12
Squared input is 144

_______________Thank You

 /** * @Rundong Huang huang784@purdue.edu *@description: Program to calculate the square of a given number */import java.util.scanner; public class SquareCalcul
 /** * @Rundong Huang huang784@purdue.edu *@description: Program to calculate the square of a given number */import java.util.scanner; public class SquareCalcul

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site