EchoSquare.java

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

Answer:

Not without first converting them to a numeric type.

EchoSquare.java

Here is the program shown the the previous picture. It computes the square of an integer entered (as characters) by the user.

import java.util.Scanner;

class EchoSquare
{
  public static void main (String[] args)
  { 
    Scanner scan = new Scanner( System.in );
    int num, square;      // declare two int variables 

    System.out.println("Enter an integer:");
    num = scan.nextInt();
    square = num * num ;  // compute the square 

    System.out.println("The square of " + num + " is " + square);
  }
}

Here is a picture of it running:

run of the program

 

QUESTION 13:

Do you think that the following input would work with this program?

twelve hundred