In: Computer Science
1. Declare the following variables: byteNumber, shortNumber, intNumber,
longNumber, floatNumber, doubleNumber choosing the types as suggested by the
identifiers.
For each of the following problems 2 through 4, (A) assign all the variables (given
in problem 1) their value according to the instructions given in problems 3 through 5.
Example: byteNumber = 127;
shortNumber = 127;
….
doubleNumber = 127;
Then, display them in the console. (This two statements can be re-used throughout.)
Example:
String output = "\n\tbyteNumber = " + byteNumber +
"\n\tshortNumber = " + shortNumber +
"\n\tintNumber = " + intNumber +
"\n\tlongNumber = " + longNumber +
"\n\tfloatNumber = " + floatNumber +
"\n\tdoubleNumber = " + doubleNumber;
System.out.println("For problem 2(A): " + output);
Then, again, for each of these problems 2 through 4, (B) assign all variables (given in
problem 1) their value by means of reading input from the keyboard, using Scanner
class and its method (pp. 84-87).
Example:
Scanner keyboardInput = new Scanner(System.in)
System.out.println(“Enter the same integer number 127 six times: “);
byteNumber = keyboardInput.nextByte();
shortNumber = keyboardInput.nextShort();
…
doubleNumber = keyboard_Input.nextDouble();
Then display again the input read from the keyboard.
String output = "\n\tbyteNumber = " + byteNumber +
"\n\tshortNumber = " + shortNumber +
"\n\tintNumber = " + intNumber +
"\n\tlongNumber = " + longNumber +
"\n\tfloatNumber = " + floatNumber +
"\n\tdoubleNumber = " + doubleNumber;
System.out.println("For problem 2(B): " + output);
Do you get any error message? If a statement has an error, correct it, otherwise use
insert // in front of the statement. For example
//intByte = 127;
As a matter of fact, there is a correct statement. Use proper cast operator, it works.
intByte = (byte)127; //This works.
Report your explanation in a comment after the assignments. Observe their
differences (A) and (B) if there are.
*For the most part I assume that its pretty cop and paste but all of my java code has errors*
import java.util.Scanner;
public class variableTypes {
public static void main(String argv[])
{
//1
//declaring and initilizing variables
byte byteNumber=127;
short shortNumber=127;
int intNumber=127;
long longNumber=127;
float floatNumber=127;
double doubleNumber=127;
String output = "\n\tbyteNumber = " + byteNumber +
"\n\tshortNumber = " + shortNumber +
"\n\tintNumber = " + intNumber +
"\n\tlongNumber = " + longNumber +
"\n\tfloatNumber = " + floatNumber +
"\n\tdoubleNumber = " + doubleNumber;
System.out.println("For problem 2(A): " + output);
//2
//all errors are fixed
Scanner keyboardInput = new Scanner(System.in);//include Scanner
module
System.out.println("Enter the same integer number 127 six times:
");
byteNumber = keyboardInput.nextByte();
shortNumber = keyboardInput.nextShort();
intNumber = keyboardInput.nextInt();
longNumber = keyboardInput.nextLong();
floatNumber = keyboardInput.nextFloat();
doubleNumber = keyboardInput.nextDouble();
output = "\n\tbyteNumber = " + byteNumber +
"\n\tshortNumber = " + shortNumber +
"\n\tintNumber = " + intNumber +
"\n\tlongNumber = " + longNumber +
"\n\tfloatNumber = " + floatNumber +
"\n\tdoubleNumber = " + doubleNumber;
System.out.println("For problem 2(A): " + output);
}
}
output:
run:
For problem 2(A):
byteNumber = 127
shortNumber = 127
intNumber = 127
longNumber = 127
floatNumber = 127.0
doubleNumber = 127.0
Enter the same integer number 127 six times:
127 127 127 127 127 127
For problem 2(A):
byteNumber = 127
shortNumber = 127
intNumber = 127
longNumber = 127
floatNumber = 127.0
doubleNumber = 127.0
BUILD SUCCESSFUL (total time: 10 seconds)