Code a for loop that adds the integers that are multiples


1. Code a statement that declares a class named BookOrderApp that has public access.

2. After the if statement that follows is executed, what will the value of discountAmount be?

double discountAmount = 0.0;
double orderTotal = 200.0;
if (orderTotal > 200)
discountAmount = orderTotal * .3;
else if (orderTotal > 100)
discountAmount = orderTotal * .2;
else
discountAmount = orderTotal * .1;

a) 0.0

b) 40.0

c) 20.0

d) 60.0

3. Given a double variable named total and an integer variable named quantity, code a statement that divides the total by the quantity, assigns the value to another variable named price, and retains the decimal places in the result.

4. How many times will the while loop that follows be executed if months has a value of 5?

int i = 1;
while (i
{
futureValue = futureValue * (1 + monthlyInterestRate);
i = i+1;
}

a) 0

b) 5

c) 4

d) 6

5. Code a statement that creates an instance of a class named BookOrder and assigns it to a variable named bookOrder. Pass arguments named code, price, and quantity to the constructor of this class.

6. Code a statement that returns a random number as an integer that's greater than or equal to 0 and less than or equal to 10. Store the result in an int variable named random.

7. If the double variables x = 2.5 and y = 4.0, what is the value of z after this statement is
executed?
int z = (int) x + (int) y;

a) 6

b) 6.5

c) 6.0

d) 7

8. Code a statement that converts a double variable named subtotal to a String object named subtotalString.

9. Which of the values below will not be printed to the console when this code is executed?

for (int i = 0; i
{
for (int j = 0; j
{
if (i == j)
continue;
System.out.println("i = " + i + " j = " + j);
}
}

a) i = 0 j = 1

b) i = 2 j = 1

c) i = 1 j = 0

d) i = 1 j = 1

10. If the instance variables of the Employee class are declared as follows, which of the following statements is most likely to be in the constructor of this class?

private String name;
private Address address;
private long employeeNumber;

a) address = 0;

b) address = new Address();

c) address = " ";

d) employeeNumber = "11233444";

11. What happens in the method that follows when s is "two"?

public double parseInterval(String s)
{
double interval = 0.0;
try
{
interval = Double.parseDouble(s);
}
catch(NumberFormatException e)
{
}
return interval;
}

a) 2.0 is returned

b) a compile-time error occurs since the catch block isn't properly coded

c) no value is returned since the catch block doesn't return a value

d) 0.0 is returned

11. Code a private static method named printMessage that accepts a String parameter that represents the message. The method should simply print the message followed by a blank line at the console.

VARIABLE DECLARATION & INITIALIZATION, CONSOLE OUTPUT

12. a) Write a valid statement for declaring and initializing a double variable named length to a starting value of 120?

b) Assume userName equals "Tom" and userAge equals 22. What is printed on the console when the following statement is executed?

System.out.println(userAge + " \nis " + userName + "'s age.");
WHILE STATEMENT

13. a) Code a while statement that will continue executing while a variable named multiplier is not equal to 1. The code within the while loop should start by subtracting 1 from the multiplier variable. Then, it should multiply a numeric variable named number by the multiplier variable and store the result in the number variable. Assume that both variables have already been declared and initialized.

b) Code a statement that creates an object for Scanner class.

IMPORT STATEMENTS

14. a) Code a statement that declares an integer named quantity and assigns an initial value of zero to it.

b)Code a statement that imports all of the classes in the java.io package.

c) Code a statement that imports the Scanner class.

d) Code a statement that imports the NumberFormat class.

STRINGS

15. a) Code a statement that declares a string variable named cityStateZip and then assigns a value that joins three string variables named city, state, and zip and two string literals that separate the city and state by a comma and a space and the state and zip by a space.

b) Code a string that, when displayed on the console, will look like this:

Fresno

CA

93720

GETTING INPUT VALUES & DISPLAYING OUTPUT

16. a) Code a statement that prints a variable named total to the console, followed by a new line character.

b) Given a Scanner variable named sc, code a statement that returns the value the user enters at the console as a String object and stores it in a variable named city.

c) Given a Scanner variable named sc, code a statement that returns the value the user enters at the console as a double object and stores it in a variable named price.

TYPE CASTING, NUMBERFORMAT CLASS

17. a) Given two integers named thisYTDQty and lastYTDQty, code a statement that divides the first value by the second value so that any decimal places in the result are retained. Store the result in a double variable named percentChange.

b) Code the statements needed to format the double variable named percentChange as a percent that always has two decimal places. Store the result in a string named percentString.

c) Code a single statement that formats a double value named total as currency with two decimal places and stores it in a string named totalString.

FOR LOOP

18. a) Code a for loop that adds the integers that are multiples of 5 starting with 50 and ending with 5. Each time the sum is increased, print it on a line at the console. Be sure to declare and initialize any required variables.

b) Code nested for loops that calculate the area of a rectangle that can range in size from a length and width of 1 to a length and width of 10. Print the area for each width for a given length on a single line at the console followed by a space. Be sure to declare and initialize any required variables.

19. Given an String object named shippingType, code an if statement that sets a double variable named shippingBase to 4.95 if shippingType is equal to "FEDEX" and to 5.95 if shippingType contains any other value. Include the code necessary to prevent a NullPointerException.

20. Code a statement that uses a static constant named SALES_TAX_PERCENT that's stored in a class named Sales. This statement should use this constant to calculate the sales tax for a double variable named subtotal and store it in another double variable named salesTax.

Request for Solution File

Ask an Expert for Answer!!
Basic Computer Science: Code a for loop that adds the integers that are multiples
Reference No:- TGS02146444

Expected delivery within 24 Hours