CS variables

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Suppose you have an int variable called number. What Java expression produces the second-to-last digit of the number (the 10s place)? What expression produces the third-to-last digit of the number (the 100s place)? (Note: Our code checker won't match every possible expression; please come up with the simplest expression using division and modulus.) 10s place 100s place

(number%100) / 10 (number%1000) / 100

What are the values of first and second at the end of the following code? int first = 8; int second = 19; first = first + second; second = first - second; first = first - second; first, second

19 8

int i = 2; int j = 3; int k = 4; int x = i + j + k; i = x - i - j; j = x - j - k; k = x - i - k; i,j,k

4 2 1

What is the output from the following code? int max; int min = 10; max = 17 - 4 / 10; max = max + 6; min = max - min; System.out.println(max * 2); System.out.println(max + min); System.out.println(max); System.out.println(min); output

46 36 23 13

What are the values of a, b, and c after the following code statements? (It may help you to write down their values after each line.) int a = 5; int b = 10; int c = b; a = a + 1; b = b - 1; c = c + a; a,b,c

6 9 16

In physics, a common useful equation for finding the position s of a body in linear motion at a given time t, based on its initial position s0, initial velocity v0, and rate of acceleration a, is the following: s = s0 + v0 t + ½ at2 Write code to declare variables for s0 with a value of 12.0, v0 with a value of 3.5, a with a value of 9.8, and t with a value of 10, and then write the code to compute s on the basis of these values. At the end of your code, print the value of your variable s to the console.

double s0=12.0; double v0=3.5; double a=9.8; int t=10; double s= s0+v0*t+.5*a*t*t; System.out.println(s);

Suppose you have a real number variable x. Write a single Java statement that declares and computes a variable y storing the result of the following computation, but modify the expression to use the * operator only four times while computing an equivalent value. y = 12.3x4 - 9.1x3 + 19.3x2 - 4.6x + 34.2

double y = x * (x * (x * (x * 12.3 - 9.1) + 19.3) - 4.6) + 34.2;

Imagine you are writing a personal fitness program that stores the user's age, gender, height (in feet or meters), and weight (to the nearest pound or kilogram). Declare variables with the appropriate names and types to hold this information. Write a complete variable declaration statement with the type, the variable name, and a semicolon. age, gender, height, weight

int age; boolean gender; double height; int weight;

int first = 8; int second = 19; first = first + second; second = first - second; first = first - second; shortened code

int first = 8, second = 19; first += second; second = first - second; first -= second;

Imagine you are writing a program that stores a student's year (Freshman, Sophomore, Junior, or Senior), the number of courses the student is taking, and his or her GPA on a 4.0 scale. Declare variables with the appropriate names and types to hold this information. Write a complete variable declaration statement with the type, the variable name, and a semicolon. year, courses taken, GPA

int year; int courses taken; double GPA;

Suppose you have an int variable called number. What Java expression produces the last digit of the number (the 1s place)? (Our code checker won't match every possible expression; come up with the simplest expression using division and modulus.) the 1s place of number

number % 10

The following program redundantly repeats the same expressions many times. Modify the program to remove all redundant expressions using variables of appropriate types. (Your variables should be used to help solve the overall problem. You must use a sufficient number of variables to receive credit for solving this problem. Declare each variable in its own statement on its own line.) public class ComputePay { public static void main(String[] args) { // Calculate pay at work based on hours worked each day System.out.println("My total hours worked:"); System.out.println(4 + 5 + 8 + 4); System.out.println("My hourly salary:"); System.out.println("$8.75"); System.out.println("My total pay:"); System.out.println((4 + 5 + 8 + 4) * 8.75); System.out.println("My taxes owed:"); // 20% tax System.out.println((4 + 5 + 8 + 4) * 8.75 * 0.20); } }

public class ComputePay { public static void main(String[] args) { // Calculate pay at work based on hours worked each day int totalHours= 4+5+8+4; double salary =8.75; double totalPay =totalHours* salary; double taxRate= 0.20; double taxOwed=totalPay*taxRate; System.out.println("My total hours worked:"); System.out.println(totalHours); System.out.println("My hourly salary:"); System.out.println("$" + salary); System.out.println("My total pay:"); System.out.println(totalPay); System.out.println("My taxes owed:"); // 20% tax System.out.println(totalPay*taxRate); } }

The following program contains 9 errors. Correct the errors and submit a working version of the program. The corrected version of the program should produce the following output: x is 0 x is now 15 x and y are 15 and 16 Complete program: For this problem, you are supposed to submit a complete Java program as a class with a main method. (You do not need to write any import statements.) public class Oops2 { public static void main(String[] args) { int x; System.out.println("x is " x); int x = 15.2; // set x to 15.2 System.out.println("x is now + x"); int y; // set y to 1 more than x y = int x + 1; System.out.println("x and y are " + x + and + y); } }

public class Oops2 { public static void main(String[] args) { int x=0; System.out.println("x is " +x); x = 15; // set x to 15.2 System.out.println("x is now " + x); int y; // set y to 1 more than x y = x + 1; System.out.println("x and y are " + x + " and " + y); } }


Set pelajaran terkait

AP World History - Chapter 14 - The Spread of Chinese Civilization: Japan, Korea, and Vietnam

View Set

Ultimate Vocabulary (Class of 2026) through 4th Quarter List II

View Set

Intro to Business: Strategic Management

View Set

Myer's AP Psychology: Unit 10 Review

View Set

Exam 3 questions. (ch. 11, 12, 14, 15, 18)

View Set