CSE Reviews

¡Supera tus tareas y exámenes ahora con Quizwiz!

Write pseudocode for a program that computes the first and last digit of a number. For example, if the input is 23456, the program should print 2 and 6. Hint: %, Math.log10.

// Input is stored in variable called num exponent = integer part of log10 of num first digit = integer part of num / 10^exponent last digit = num % 10 Print first digit. Print last digit.

In How To 1.1, you made assumptions about the price of gas and annual usage to compare cars. Ideally, you would like to know which car is the better deal without making these assumptions. Why can't a computer program solve that problem?

// Inputs are one way distance, beginning mileage, ending mileage, number of work days // Question asks only for fraction "commuting to work" thus we don't need to count the fraction commuting back home total miles traveled = ending mileage - beginning mileage miles traveled to work = one way distance * number of work days // This is what we want to know: fraction of miles for commute = miles traveled to work / total miles traveled // Personal use is whatever is left over fraction of miles for personal use = 1 - fraction of miles for commute

In order to estimate the cost of painting a house, a painter needs to know the surface area of the exterior. Develop an algorithm for computing that value. Your inputs are the width, length, and height of the house, the number of windows and doors, and their dimensions. (Assume the windows and doors have a uniform size.)

// Inputs are one way distance, fuel efficiency, train ticket price cost of gallon of gas = 4 // $4 per gallon car maintenance = .05 // $0.05 per mile cost of car = one way distance / fuel efficiency * cost of gallon of gas + one way distance * car maintenance if cost of car < train ticket price choose to drive car. else choose to take train.

How do you discover syntax errors? How do you discover logic errors?

Syntax errors are discovered by compiling your Java program; the Java compiler will report them directly. Logic errors are discovered by testing your program by running it and verifying the correct output; if the output is incorrect, you have a logic error

Which parts of a computer can store program code? Which can store user data?

Typically program code is stored on a hard disk, CD/DVD disc, or in some other central location across a network. User data is often more likely to be stored on a local hard disk, although it can also be stored on a network or even a CD/DVD for backup storage.

Find at least five compile-time errors in the following program. public class HasErrors { public static void main(); { System.out.print(Please enter two numbers:) x = in.readDouble; y = in.readDouble; System.out.printline("The sum is " + x + y); } }

1) The is a semicolon after main 2) No double quotations for string 3) No semicolon after System.out.print 4) Did not call in a scanner 5) Forgot String

Find three run-time errors in the following program. public class HasErrors { public static void main(String[] args) { int x = 0; int y = 0; Scanner in = new Scanner("System.in"); System.out.print("Please enter an integer:"); x = in.readInt(); System.out.print("Please enter another integer: "); x = in.readInt(); System.out.println("The sum is " + x + y); } }

1) The scanner should have been constructed as Scanner in = new Scanner(System.in); 2) The second integer should have been read as y = in.readInt(); 3) The sum should have been printed as "The sum is " + (x + y), so that the integer x is not concatenated with the string "The sum is".

Write three versions of the HelloPrinter.java program that have different compile-time errors. Write a version that has a run-time error.

1) This version omits a semicolon after the println statement and omits a curly brace to close the class: public class HelloPrinter1 { public static void main(String[] args) { System.out.println("Hello, World!") } 2) This version omits quotes around the Hello, World! statement and adds a semicolon after the main declaration: public class HelloPrinter2 { public static void main(String[] args); { System.out.println(Hello, World!); } } 3) This version omits parenthesis around the println argument and omits the static reserved word in the main declaration: public class HelloPrinter3 { public void main(String[] args) { System.out.println "Hello, World!"; }

Write the following mathematical expressions 1) s=s0+v0t+1/2gt^2 2) G=4pi^2(a^3/(p^2(m1+m2))) 3) FV=PV(1+INT/100)^(YRS) c=(a^2+b^2-2abcos(gamma))^1/2

1) s= s0 + v0 * t + g * Math.pow(t, 2) / 2.0; 2) G = 4.0 * Math.pow(Math.PI,2))* Math.pow(a,3) / (Math.pow(p,2) * (m1 + m2)); 3) FV = PV * Math.pow((1.0 + INT / 100.0), YRS); 4) c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2) - 2 * a * b * Math.cos(gamma));

What does this program print? public class Test { public static void main(String[] args) { System.out.println("39 + 3"); System.out.println(39 + 3); } }

39+3 42 (Java interprets the statement "39 + 3" as a string and thus prints out the literal characters 39 + 3. Java interprets the second statement 39 + 3 as an operation between two numbers, so it first calculates the value 39 + 3 = 42 then prints out the result 42.)

Explain the difference between using a computer program and programming a computer.

A well-designed computer program is easy to use without any special knowledge. For example, most people can learn to navigate webpages with only a few minutes of practice. On the other hand, programming a computer requires special knowledge about what the computer can fundamentally do and how you would communicate with it through a programming language.

Suppose you put your younger brother in charge of backing up your work. Write a set of detailed instructions for carrying out his task. Explain how often he should do it, and what files he needs to copy from which folder to which location. Explain how he should verify that the backup was carried out correctly.

Every day at 5 P.M. please do the following: 1) Insert the USB memory stick into the computer. 2) Create a new directory on the USB memory stick entitled "BACKUP - DATE" where "DATE" is replaced with the current date. 3) Copy the files from the "My Documents" folder on the computer to the folder you just created on the USB stick. 4) Double check to make sure the new directory contains the files you copied. a. If the folder is empty, something is wrong. It's possible you backed up to the wrong directory. Try it again and be careful which directory you copy into. 5) Once you verified the copy is complete, remove the USB stick and store it someplace safe. 6) Thank you!

A cocktail shaker is composed of three cone sections. The volume of a cone section with height h and top and bottom radius r1 and r2 is v=pi[((r1^2+r1r2+r2^2)*h)]/3 Compute the total volume by hand for one set of realistic values for the radii and heights. Then develop an algorithm that works for arbitrary dimensions.

First, compute the total volume by hand using the formula in Self Check 25. Let's assume the following values for the three sections: • Conic Section 1 (top of bottle) has first radius (r11) 0.5 inches, second radius (r21) 0.75 inches, and height (h1) 1.5 inches. • Conic Section 2 (middle) has first radius (r12) 0.75 inches, second radius (r22) 1.5 inches, and height (h2) 0.5 inches. • Conic Section 3 (bottom of bottle) has first radius (r13) 1.5 inches, second radius (r23) 0.75 inches, and height (h3) 6.0 inches. So, the total volume of the bottle, Vt, will equal the volume of conic section 1 (V1) plus the volume of conic section 2 (V2) plus the volume of the conic section 3 (V3): Vt = V1 + V2 + V3 Using the equation given in Self Check 25, we calculate the three volumes to be (rounded to two decimal places): V1 = 1.87 cubic inches V2 = 2.06 cubic inches V3 = 24.74 cubic inches So, the total volume for the cocktail shaker in our example is: Vt = V1 + V2 + V3 = 28.67 cubic inches The algorithm we use is very similar to the calculation we did above: volume 1 = π x (r112 + r11 x r21 + r212) x h1 / 3 volume 2 = π x (r122 + r12 x r22 + r222) x h2 / 3 volume 3 = π x (r132 + r13 x r23 + r232) x h3 / 3 total volume = volume 1 + volume 2 + volume 3

What does this program print? Pay close attention to spaces. public class Test { public static void main(String[] args) { System.out.print("Hello"); System.out.println("World"); } }

Helloworld (Because there are no spaces after the System.out.print("Hello"); the next line prints World directly after Hello is printed.)

A toaster is a single-function device, but a computer can be programmed to carry out different tasks. Is your cell phone a single-function device, or is it a programmable computer? (Your answer will depend on your cell phone model

It's very likely your cell phone is a programmable computer. If you can take pictures, send email/text messages, and/or surf the web with your phone, it is a programmable computer. If your cell phone can only send and receive phone calls, it is probably a single-function device.

What is the compile-time error in this program? public class Test { public static void main(String[] args) { System.out.println("Hello", "World!"); } }

Java interprets the comma in the println method to mean that two strings are passed to println. It's likely the programmer meant to do this: System.out.print("Hello, World!");

Explain two benefits of using Java over machine code.

One advantage of Java over machine code is that Java statements are independent of the machine (computer) they are being executed on; machine code statements differ from one type of machine to the next. Another advantage of Java is that it is much more readable and understandable (by humans) than machine code

Write pseudocode for a program that reads a name (such as Harold James Morgan) and then prints a monogram consisting of the initial letters of the first, middle, and last name (such as HJM).

Read first name into variable called first. Read middle name into variable called middle. Read last name into variable called last. Print first character in first. Print first character in middle. Print first character in last.

Write pseudocode for a program that reads a word and then prints the first character, the last character, and the characters in the middle. For example, if the input is Harry, the program prints H y arr.

Read input into a variable called word. Print first character in word followed by a space. Print character at length -1 in word followed by a space. Print substring between 1st and last character (length -1) in word.

Consider the following code segment. double purchase = 19.93; double payment = 20.00; double change = payment - purchase; System.out.println(change); The code segment prints the change as 0.07000000000000028. Explain why. Give a recommendation to improve the code so that users will not be confused.

The given output is printed in a raw format up to the range of a double data type. Users can use format specifiers (printf with %) to format the output as they require.

Which parts of a computer serve to give information to the user? Which parts take user input?

The monitor, speakers, and printer serve as the primary devices to give information to the user. The keyboard and mouse are the primary devices that take user input.

Explain the differences between 2, 2.0, '2', "2", and "2.0".

The type of 2.0 is a double; specifically the Java programming language recognizes it as the real number 2.0. On the other hand, Java treats "2.0" as a string. Its value is literally the three characters 2, ., and 0.

What is the value of mystery after this sequence of statements? int mystery = 1; mystery = 1 - 2 * mystery; mystery = mystery + 1;

The value of mystery is equal to 0 after the statements are executed. In the first statement (line 1), mystery is initialized to a value of 1. In the assignment statement on line 2, mystery is set to -1. Finally, mystery is set to 0 in line 3.

What is wrong with the following sequence of statements? int mystery = 1; mystery = mystery + 1; int mystery = 1 - 2 * mystery;

The variable mystery is being declared twice, first in line 1 and then again in line 2. A variable can only be initialized once. (If you remove the reserved word int on line 3, the statements will work just fine, and will set mystery to -3.)

Consider the question in Exercise •• R1.13. Suppose the numbers ($10,000, 6 percent, $500) were user selectable. Are there values for which the algorithm you developed would not terminate? If so, change the algorithm to make sure it always terminates.

Yes, any interest rate that causes more money to be added each month than was taken out would prevent the solution to R1.12 from terminating. We can guarantee that our algorithm terminates by checking for this condition before the calculation starts: balance = $10,000 monthly interest rate = 0.5% monthly withdrawal amount = $500 if balance * monthly interest rate > monthly withdrawal amount report that the balance will never reach zero else total months = 0 while balance is greater than $0 increase balance by 0.5% of its value decrease balance by $500 add 1 to the total number of months years to deplete = total months /12

On your own computer or on a lab computer, find the exact location (folder or directory name) of a.The sample file HelloPrinter.java, which you wrote with the editor. b.The Java program launcher java.exe or java. c.The library file rt.jar that contains the run-time library.

a) Solutions here will vary based on user and IDE preference. On a UNIX-based system using the Eclipse IDE you may see a path like /home/nancy/JFE/src While on a Microsoft Windows machine you might find a directory like: C:\Users\nancy\Documents\JFE\src b) Again, solutions can vary. On Unix using Eclipse you might see: /home/nancy/JFE/bin A Microsoft Windows machine might be: C:\Users\nancy\Documents\JFE\bin c) The answer to this question is dependent on the type of operating system and version of Java. On a Unix based system using Java 1.6 you might find rt.jar here: /usr/lib/jvm/java-6-sun-1.6.0.13/jre/lib/rt.jar While on a Microsoft Windows platform you might find it here: C:\Program Files\Java\jdk1.6.0_10\jre

Explain what each of the following program segments computes. a.x = 2; y = x + x; b.s = "2"; t = s + s;

a) This statement doubles the value of x. Given the initial value of x as 2, the result is 4. b) This statement concatenates the strings "2" together. The result is "22".

Write the following Java expressions in mathematical notation. a.dm = m * (Math.sqrt(1 + v / c) / Math.sqrt(1 - v / c) - 1); b.volume = Math.PI * r * r * h; c.volume = 4 * Math.PI * Math.pow(r, 3) / 3; d.z = Math.sqrt(x * x + y * y);

a.

What are the values of the following expressions? In each line, assume that String s = "Hello"; String t = "World"; a.s.length() + t.length() b.s.substring(1, 2) c.s.substring(s.length() / 2, s.length()) d.s + t e.t + s

a. 10 b. e c. llo d. HelloWorld e. WorldHello

What are the values of the following expressions? In each line, assume that double x = 2.5; double y = -1.5; int m = 18; int n = 4; a.x + n * y - (x + n) * y b.m / n + m % n c.5 * x - n / 5 d.1 - (1 - (1 - (1 - (1 - n)))) e.Math.sqrt(Math.sqrt(n))

a. 6.25 b. 6 c. 12.5 d. -3 e. 1.414213562

What are the values of the following expressions, assuming that n and m have type int, n is 17, and m is 18? a.n / 10 + n % 10 b.n % 2 + m % 2 c.(m + n) / 2 d.(m + n) / 2.0 e.(int) (0.5 * (m + n)) f.(int) Math.round(0.5 * (m + n))

a. 8 b. 1 c. 17 d. 17.5 e. 17 f. 18

The cafeteria offers a discount card for sale that entitles you, during a certain period, to a free meal whenever you have bought a given number of meals at the regular price. The exact details of the offer change from time to time. Describe an algorithm that lets you determine whether a particular offer is a good buy. What other inputs do you need?

balance = $10,000 total months = 0 while balance is greater than $0 increase balance by 0.5% of its value decrease balance by $500 add 1 to the total number of months years to deplete = total months / 12

Write an algorithm to settle the following question: A bank account starts out with $10,000. Interest is compounded monthly at 6 percent per year (0.5 percent per month). Every month, $500 is withdrawn to meet college expenses. After how many years is the account depleted?

balance = $10,000 total months = 0 while balance is greater than $0 increase balance by 0.5% of its value decrease balance by $500 add 1 to the total number of months years to deplete = total months / 12

Modify the pseudocode for the program in How To 2.1 so that the program gives change in quarters, dimes, and nickels. You can assume that the price is a multiple of 5 cents. To develop your pseudocode, first work with a couple of specific values.

change due = 100 x bill value - item price in pennies quarters = change due / 25 (without remainder) change due = change due % 25 dimes = change due / 10 (without remainder) amount due = change due % 10 nickels = change due / 5 (without remainder) // Change is now in quarters, dimes, and nickels


Conjuntos de estudio relacionados

Psychology Exam 5: Intro to Analysis of Variance

View Set