Question

In: Computer Science

1. Declare the following variables: byteNumber, shortNumber, intNumber, longNumber, floatNumber, doubleNumber choosing the types as suggested...

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*

Solutions

Expert Solution


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)


Related Solutions

Write a c statement to declare variables of primitive data types, and assign values to the...
Write a c statement to declare variables of primitive data types, and assign values to the variables. Use common control structure in c, such as if, switch, break, for, while statements. Write a c program that takes command line parameters, and perform some computation based on the command line input. Write c statements to generate random number within a certain range. Write a c statement to declare array variables of primitive data types, and assign values to the elements of...
Complete the following assignment in C programming language. 1. Declare the following float variables: -Maximum exam...
Complete the following assignment in C programming language. 1. Declare the following float variables: -Maximum exam score, user's exam score, and percentage. 2. Ask the user to input data into your variables, such as: -"What is the max score of your exam:" -"What was your score:" 3. Use if statements to validate user inputs. For example, score received should not be more than maximum possible score. -Display an error message when the user enters invalid data. -You can restart the...
C++ Code Write a program that performs the following: a) Declare long variables value1 and value2....
C++ Code Write a program that performs the following: a) Declare long variables value1 and value2. The variable value1 has been initialized to 200000. Declare the variable longPtr to be a pointer to an object of type long. b) Assign the address of variable value1 to pointer variable longPtr. c) Display the value of the object pointed to by longPtr. d) Assign the value of the object pointed to by longPtr to variable value2. e) Display the value of value2....
7) Create the following using Java. Create a scanner Declare double variables for price and tax...
7) Create the following using Java. Create a scanner Declare double variables for price and tax Declare character variable reply Create do while loop Inside of do loop Prompt the console to display headline Product Price Check Prompt the user to enter initial price and relate to scanner Prompt the user to enter the tax rate and relate to scanner Calculate price which is equal to price multiply (1+tax/100) Display in console the cost after tax which is price Check...
Create an object called Circle. Declare the following integer variables for the object Circle, radius, diameter,...
Create an object called Circle. Declare the following integer variables for the object Circle, radius, diameter, and pi is declared as double. Create the following for the Circle object: ● Implicit constructor (default constructor) ● Void method Calculate (double pi, int radius) to calculate the area of the Circle object. The method must include a system.out statement that displays the area value. Use the following formula to calculate area of the circle: Area = pi * (r * r) Your...
For the following types of values, designate discrete variables (D) and continuous variables (C): (a) weight...
For the following types of values, designate discrete variables (D) and continuous variables (C): (a) weight of the contents of a package of cereal, (b) diameter of a bearing, (c) number of defective items produced, (d) number of individuals in a geographic area who are collecting unemployment benefits, (e) the average number of prospective customers contacted per sales representative during the past month, (f) dollar amount of sales.
Write a program in c++ to do the following : (1) Declare an array a of...
Write a program in c++ to do the following : (1) Declare an array a of size 10 and three pointer variables p, q, and v. (2) Write a loop to fill array a with values 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 (3) write following statement: p= &a[2]; q = &a[5]; i = *q - *p; cout<<“The value of i is”<< i; i = *p - *q; cout<<“The value of i is %d”<< i; 4) assign...
Rewrite your program for part 1. Do not declare the array globally, declare it in the...
Rewrite your program for part 1. Do not declare the array globally, declare it in the loop function. This now requires that you add two parameters to your fill array and print array functions. You must now pass the array name and array size as arguments, when the program calls these functions. The program has the same behavior as problem 1, but illustrates the difference between globally and locally declared variables. The program code for part 1 was: int Array[15]...
Which of the following factors is vitally important in choosing between defined contribution plan types for...
Which of the following factors is vitally important in choosing between defined contribution plan types for a small employer? Select as many items as is appropriate. The need to attract and retain employees. The use of the plan as a tax shelter for the owner(s). The desire to account for past service. Group of answer choices I only I and II only I and III only I, II, and III
C++ Program 1. Declare an integer static array a[ ] with 100 elements. 2. Declare an...
C++ Program 1. Declare an integer static array a[ ] with 100 elements. 2. Declare an integer pointer p. 3. Let p pointing to the array a[ ]. 4. Use p (you have to use p) to put 0 into the first element of this array, 2 into the second element, 4 into the 3rd element, 6 into the 4th element, ... 198 into the 100th element of this array. 5. Use a (you have to use a) to display...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT