Assignment 1 Expressions Data Types Formatted Printing Variables Scanning CSC 123 Fall 2018 Answer Sheet Short Answers 1. Every complete statement ends with a c. a. period b. parenthesis c. semicolon d. ending brace 2. The following data 72 'A' "Hello World" 2.8712 are all examples of b. a. variables b. constants (also called literals) c. strings d. none of these 3. A group of statements, such as the contents of a class or a method, are enclosed in a. a. braces {} b. parentheses () c. brackets [] d. any of these will do 4. Which of the following are not valid assignment statements? (Indicate all that apply. (Assume that variables have been declared) b, c a. total = 9; b. 72 = amount; c. profit = 129 d. letter = 'W'; 5. The negation operator is a. a. unary b. binary c. ternary d. none of these 1
6. This key word is used to declare a named constant. c a. constant b. namedconstant c. final d. concrete 7. Which Scanner class method would you use to read a string as input? b a. nextstring b. nextline c. readstring d. getline 8. Which Scanner class method would you use to read a double as input? a a. nextdouble b. getdouble c. readdouble d. None of these; you cannot read a double with the Scanner class 9. True or False: A left brace in a Java program is always followed by a right brace later in the program. 10. True or False: A variable must be declared before it can be used. 11. True or False: Variable names may begin with a number. 12. True or False: You cannot change the value of a variable whose declaration uses the final key word. 13. True or False: If one of an operator s operands is a double, and the other operand is an int, Java will automatically convert the value of the double to an int. 14. What s the Output? Determine the output of the following code segments or point out the error. a. int num = 5; num = num++; System.out.println(num); //Ans.: 5 b. int num = 5; num = ++num; System.out.println(num); //Ans.: 6 c. int num = 5; num = num++ = ++num; System.out.println(num); //Ans.: ill-formed (can t assign to an expression 2
d. int num = 5; num /= 3; System.out.println(num); //Ans.: 1 e. int num = 5; System.out.println(num += 5); //Ans.: 10 f. int num = 5; System.out.println(++num + num++); //Ans.: 12 Programs. Use printf for all output. 1. ASCII Write a program that prints the letters of your name followed by the ASCII value of each letter. For example: K 75 m 109 e 101 This is with my name, you should use your own d 100 Another way d 100 With printf 3
d 100 Another way d 100 2. SalesTax Write a program that will ask the user to enter the amount of a purchase. The program should then compute the state and county sales tax. Assume the state sales tax is 4 percent and the county sales tax is 2 percent. The program should display the amount of the purchase, the state sales tax, the county sales tax, the total sales tax, and the total of the sale (which is the sum of the amount of purchase plus the total sales tax). Hint: Use the value 0.02 to represent 2 percent, and 0.04 to represent 4 percent. Please enter the Purchase Price: 42599.71 Purchase Price: 42599.71 State Sales Tax: 1703.99 County Sales Tax: 851.99 Total Sales Tax: 2555.98 Total Cost: 45155.69 Please enter the Purchase Price: 1.99 Purchase Price: 1.99 State Sales Tax: 0.08 County Sales Tax: 0.04 Total Sales Tax: 0.12 Total Cost: 2.11 3. RestaurantBill Write a program that computes the tax and tip on a restaurant bill. The program should ask the user to enter the charge for the meal. The tax should be 6.75 percent of the meal charge. The tip should be 20 percent of the total after adding the tax. Display the meal charge, tax amount, tip amount, and total bill on the screen. Please enter the cost of the meal: 75.00 Meal Cost: 75.00 Tax Amount: 5.06 Tip Amount: 16.01 Total Bill: 96.08 4
Please enter the cost of the meal: 193.57 Meal Cost: 193.57 Tax Amount: 13.07 Tip Amount: 41.33 Total Bill: 247.96 4. StockTransaction Last month Joe purchased some stock in Acme Software, Inc. Here are the details of the purchase: The number of shares that Joe purchased was 1,000. When Joe purchased the stock, he paid $32.87 per share. Joe paid his stockbroker a commission that amounted to 2% of the amount he paid for the stock. Two weeks later Joe sold the stock. Here are the details of the sale: The number of shares that Joe sold was 1,000. He sold the stock for $33.92 per share. He paid his stockbroker another commission that amounted to 2% of the amount he received for the stock. Write a program that displays the following information: The amount of money Joe paid for the stock. The amount of commission Joe paid his broker when he bought the stock. The amount that Joe sold the stock for. The amount of commission Joe paid his broker when he sold the stock. Display the amount of profit that Joe made after selling his stock and paying the two commissions to his broker. (If the amount of profit that your program displays is a negative number, then Joe lost money on the transaction.) Purchase Price: 32870.00 Commission When Bought: 657.40 Sale Price: 33920.00 Commission When Sold: 678.40 Joe's Profit: -285.80 5. Extract Digits Write a program that requests a 5-digit integer and displays the digits one at a time. For example, given 38145, you program should print: First digit: 3. Second digit: 8. Third digit: 1. Fourth digit: 4. Fifth digit: 5. Hint: Use the % and / operations to extract the digits. Enter a five digit integer number 53210 First digit: 5 Second digit: 3 5
Third digit: 2 Fourth digit: 1 Fifth digit: 0 Enter a five digit integer number 10802 First digit: 1 Second digit: 0 Third digit: 8 Fourth digit: 0 Fifth digit: 2 Enter a five digit integer number 321 (even works with less than 5 digits) First digit: 0 Second digit: 0 Third digit: 3 Fourth digit: 2 Fifth digit: 1 6. ExtractLength Write a program that reads in a length in inches. Then extract the number of yards, feet and remaining inches from the input. Print out the result. For instance, if the input is 50 then the output would look like this; 50 inches converts to 1 yard, 1 feet and 2 inches. Remember 36 in a yard, 12 in a foot. Please enter the number of inches: 36 36 inches converts to 1 yards, 0 feet and 0 inches Please enter the number of inches: 57 57 inches converts to 1 yards, 1 feet and 9 inches Please enter the number of inches: 21 21 inches converts to 0 yards, 1 feet and 9 inches 6