Write Java statements that accomplish the following.
1. Declare int variables x and y.
2. Initialize an int variable x to 10 and a char variable ch to 'B'.
3. Update the value of an int variable x by adding 5 to it.
4. Declare and initialize a double variable payRate to 12.50.
5. Copy the value of an int variable firstNum into an int variable tempNum.
6. Swap the contents of the int variables x and y. (Declare additional variables, if
necessary.)
7. Suppose x and y are double variables. Output the contents of x, y, and the expression
x + 12 / y – 18.
8. Declare a char variable grade and set the value of grade to 'A'.
9. Declare int variables to store four integers.
10. Copy the value of a double variable z to the nearest integer into an int variable x
public class Operations {
// 8. Declare a char class variable and set the class value to "A".
public char ch1 = 'A';
public static void main(String[] args) {
// 1. Declare variables of type int x and y.
int x, y;
x = 10;
y = 10;
// 2. Initialize variable x with value 10 and variable ch with value 'B'.
char ch = 'B';
// 3. Update the value of the int variable x with 5 added to it.
x += 5;
// 4. Declare and initialize the double payRate variable with a value of 12.50.
double payRate = 12.50;
// 5. Copy the value of int firstNum to int tempNum.
int firstNum = 7;
int tempNum = firstNum;
// 6. Swap the contents of the int x and y variables. (Declare additional variables if necessary.)
int u;
u = x;
x = y;
y = u;
// 7. Assume that x and y are double variables. Print the contents of x, y and the expression x + 12 / y - 18.
double c = (x + (12 / y))-18;
System.out.printf("x = %d, y = %d, expression x + 12 / y – 18 = %.2f", x, y, c);
System.out.println("");
// 9. Declare variables of type int to store four integers.
int[] arr = new int[4];
// 10. Copy the value of a variable of type double z up to the nearest integer into a variable of type int x.
double z = 16.48;
x = (int) z;
System.out.print(x);
}
}
Comments
Leave a comment