New Semester
Started
Get
50% OFF
Study Help!
--h --m --s
Claim Now
Question Answers
Textbooks
Find textbooks, questions and answers
Oops, something went wrong!
Change your search query and then try again
S
Books
FREE
Study Help
Expert Questions
Accounting
General Management
Mathematics
Finance
Organizational Behaviour
Law
Physics
Operating System
Management Leadership
Sociology
Programming
Marketing
Database
Computer Network
Economics
Textbooks Solutions
Accounting
Managerial Accounting
Management Leadership
Cost Accounting
Statistics
Business Law
Corporate Finance
Finance
Economics
Auditing
Tutors
Online Tutors
Find a Tutor
Hire a Tutor
Become a Tutor
AI Tutor
AI Study Planner
NEW
Sell Books
Search
Search
Sign In
Register
study help
computer science
java concepts late objects
Java Concepts Late Objects 3rd Edition Cay S. Horstmann - Solutions
Rewrite the following for loop into a while loop.int s = 0;for (int i = 1; i <= 10; i++){s = s + i;}
Write a program that first asks the user to type in today’s price of one dollar in Japanese yen, then reads U.S. dollar values and converts each to Japanese yen. Use 0 as the sentinel value to denote the end of dollar inputs. Then the program reads a sequence of yen amounts and converts them to
Write a program that reads a number and prints all of its binary digits: Print the remainder number % 2, then replace the number with number / 2. Keep going until the number is 0. For example, if the user provides the input 13, the output should be1011
Write pseudocode for a program that reads a sequence of student records and prints the total score for each student. Each record has the student’s first and last name, followed by a sequence of test scores and a sentinel of –1. The sequence is terminated by the word END. Here is a sample
Currency conversion. Write a program that first asks the user to type today’s price for one dollar in Japanese yen, then reads U.S. dollar values and converts each to yen. Use 0 as a sentinel.
Write a program that prints all powers of 2 from 20 up to 220.
Write pseudocode for a program that reads a student record, consisting of the student’s first and last name, followed by a sequence of test scores and a sentinel of –1. The program should print the student’s average score. Then provide a trace table for this sample input: Harry Morgan 94 71
Write a program that reads an initial investment balance and an interest rate, then prints the number of years it takes for the investment to reach one million dollars.
Write a program that reads a sequence of words and then prints them in a box, with each word centered, like this: Hello I Java Iprogrammer|
Write a program that reads an integer n and a digit d between 0 and 9. Use one or more loops to count how many of the integers between 1 and n• start with the digit d.• end with the digit d.• contain the digit d.
Write a program that reads a string and prints the longest sequence of vowels. If there are multiple sequences of the same length, print them all. For example, if the word is oiseau, print eau, and if the word is teakwood, print ea and oo.
Write pseudocode for a program that prints a calendar such as the following: Su M T W Th F Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
In the 17th century, the discipline of probability theory got its start when a gambler asked a mathematician friend to explain some observations about dice games. Why did he, on average, win a bet that at least one six would appear when rolling a die four times? And why did he seem to lose a
Write a program that reads a string and prints the most frequently occurring letter. If there are multiple letters that occur with the same maximum frequency, print them all. For example, if the word is mississippi, print is because i and s occur four times each, and no letter occurs more
How many iterations do the following loops carry out? Assume that i is not changed in the loop body.a. for (int i = 1; i <= 10; i++) . . .b. for (int i = 0; i < 10; i++) . . .c. for (int i = 10; i > 0; i--) . . .d. for (int i = -10; i <= 10; i++) . . .e. for (int i = 10; i >= 0; i++)
The Buffon Needle Experiment. The following experiment was devised by Comte Georges-Louis Leclerc de Buffon (1707–1788), a French naturalist. A needle of length 1 inch is dropped onto paper that is ruled with lines 2 inches apart. If the needle drops onto a line, we count it as a hit. (See Figure
Write a program that reads a word and prints all substrings, sorted by length. For example, if the user provides the input "rum", the program printsrumruumrum
Which loop statements does Java support? Give simple rules for when to use each loop type.
A simple random generator is obtained by the formula rnew = (a . rold + b)%mand then setting rold to rnew. If m is chosen as 232, then you can compute rnew = a . rold + bbecause the truncation of an overflowing result to the int type is equivalent to computing the remainder. Write a program
Write a program that reads a word and prints the number of syllables in the word. For this exercise, assume that syllables are determined as follows: Each sequence of adjacent vowels a e i o u y, except for the last e in a word, is a syllable. However, if that algorithm yields a count of 0, change
What is a sentinel value? Give a simple rule when it is appropriate to use a numeric sentinel value.
The Monty Hall Paradox. Marilyn vos Savant described the following problem (loosely based on a game show hosted by Monty Hall) in a popular magazine: “Suppose you’re on a game show, and you’re given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a
Write a program that reads a word and prints the number of vowels in the word. For this exercise, assume that a e i o u y are vowels. For example, if the user provides the input "Harry", the program prints 2 vowels.
What is an “off-by-one” error? Give an example from your own programming experience.
The Drunkard’s Walk. A drunkard in a grid of streets randomly picks one of four directions and stumbles to the next intersection, then again randomly picks one of four directions, and so on. You might think that on average the drunkard doesn’t move very far because the choices cancel each other
Write a program that reads a word and prints the word in reverse. For example, if the user provides the input "Harry", the program prints yrraH
Write a program trace for the pseudocode in Exercise E4.6, assuming the input values are 4 7 –2 –5 0.
Write a program that reads a word and prints each character of the word on a separate line. For example, if the user provides the input "Harry", the program printsHarry
What is an infinite loop? On your computer, how can you terminate a program that executes an infinite loop?
Following Section 4.9, develop a program that reads a string and removes all duplicates. For example, if the input is Mississippi, print Misp. Start small and just print the first letter. Then print the first letter and true if the letter is not duplicated elsewhere, false otherwise. (Look for it
Translate the following pseudocode for randomly permuting the characters in a string into a Java program.Read a word.Repeat word.length() timesPick a random position i in the word, but not the last position.Pick a random position j > i in the word.Swap the letters at positions j and i.Print the
What do these loops print?a. for (int i = 1; i < 10; i++) { System.out.print(i + " "); }b. for (int i = 1; i < 10; i += 2) { System.out.print(i + " "); }c. for (int i = 10; i > 1; i--) { System.out.print(i + " "); }d. for (int i = 0; i < 10; i++) { System.out.print(i + " "); }e. for
Translate the following pseudocode for finding the minimum value from a set of inputs into a Java program.Set a Boolean variable "first" to true. If the scanner has more numbers Read the next value. If first is true Set the minimum to the value. Set first to false. Else if the value is less than
Provide trace tables for these loops.a. int i = 0; int j = 10; int n = 0;while (i < j) { i++; j--; n++; }b. int i = 0; int j = 0; int n = 0;while (i < 10) { i++; n = n + i + j; j++; }c. int i = 10; int j = 0; int n = 0;while (i > 0) { i--; j++; n = n + i - j; }d. int i = 0; int j = 10; int
Write a loop that computesa. The sum of all even numbers between 2 and 100 (inclusive).b. The sum of all squares between 1 and 100 (inclusive).c. The sum of all odd numbers between a and b (inclusive).d. The sum of all odd digits of n. (For example, if n is 32677, the sum would be 3 + 7 + 7 = 17.)
Prime numbers. Write a program that prompts the user for an integer and then prints out all prime numbers up to that integer. For example, when the user enters 20, the program should print235711131719Recall that a number is a prime number if it is not divisible by any number except 1 and itself.
Write a program that reads a set of floating-point values. Ask the user to enter the values, then print• the average of the values.• the smallest of the values.• the largest of the values.• the range, that is the difference between the smallest and largest.Of course, you may only prompt for
Write a while loop that printsa. All squares less than n. For example, if n is 100, print 0 1 4 9 16 25 36 49 64 81.b. All positive numbers that are divisible by 10 and less than n. For example, if n is 100, print 10 20 30 40 50 60 70 80 90c. All powers of two less than n. For example, if n is 100,
Factoring of integers. Write a program that asks the user for an integer and then prints out all its factors. For example, when the user enters 150, the program should print2355
What do these code snippets print?a. int result = 0;for (int i = 1; i <= 10; i++) { result = result + i; }System.out.println(result);b. int result = 1;for (int i = 1; i <= 10; i++) { result = i - result; }System.out.println(result);c. int result = 1;for (int i = 5; i > 0; i--) { result =
The Fibonacci numbers are defined by the sequenceReformulate that asfold1 = 1;fold2 = 1;fnew = fold1 + fold2;After that, discard fold2, which is no longer needed, and set fold2 to fold1 and fold1 to fnew. Repeat an appropriate number of times. Implement a program that prompts the user for an
Write a program that prompts the user to provide a single character from the alphabet. Print Vowel or Consonant, depending on the user input. If the user input is not a letter (between a and z or A and Z), or is a string of length > 1, print an error message.
Write programs that read a line of input as a string and printa. Only the uppercase letters in the string.b. Every second letter of the string.c. The string, with all vowels replaced by an underscore.d. The number of vowels in the string.e. The positions of all vowels in the string.
What do these loops print?a. int i = 0; int j = 10;while (i < j) { System.out.println(i + " " + j); i++; j--; }b. int i = 0; int j = 10;while (i < j) { System.out.println(i + j); i++; j++; }
Mean and standard deviation. Write a program that reads a set of floating-point data values. Choose an appropriate mechanism for prompting for the end of the data set. When all values have been read, print out the count of the values, the average, and the standard deviation. The average of a data
Write programs that read a sequence of integer inputs and printa. The smallest and largest of the inputs.b. The number of even and odd inputs.c. Cumulative totals. For example, if the input is 1 7 2 9, the program should print 1 8 10 19.d. All adjacent duplicates. For example, if the input is 1 3 3
Given the variablesString stars = "*****";String stripes = "=====";what do these loops print?a. int i = 0;while (i < 5){System.out.println(stars.substring(0, i));i++;}b. int i = 0;while (i < 5){System.out.print(stars.substring(0, i));System.out.println(stripes.substring(i, 5));i++;}c. int i =
Write programs with loops that computea. The sum of all even numbers between 2 and 100 (inclusive).b. The sum of all squares between 1 and 100 (inclusive).c. All powers of 2 from 20 up to 220.d. The sum of all odd numbers between a and b (inclusive), where a and b are inputs.e. The sum of all odd
What is wrong with the following program?System.out.print("Enter the number of quarters: ");int quarters = in.nextInt();if (in.hasNextInt()){total = total + quarters * 0.25;System.out.println("Total: " + total);}else{System.out.println("Input error.");}
Simplify the following statements. Here, b is a variable of type boolean and n is a variable of type int.a. if (n == 0) { b = true; } else { b = false; }b. if (n == 0) { b = false; } else { b = true; }c. b = false; if (n > 1) { if (n < 2) { b = true; } }d. if (n < 1) { b = true; } else { b
Simplify the following expressions. Here, b is a variable of type boolean.a. b == trueb. b == falsec. b != trued. b != false
Suppose the value of b is false and the value of x is 0. What is the value of each of the following expressions?a. b && x == 0b. b || x == 0c. !b && x == 0d. !b || x == 0e. b && x != 0f. b || x != 0g. !b && x != 0h. !b || x != 0
The average person can jump off the ground with a velocity of 7 mph without fear of leaving the planet. However, if an astronaut jumps with this velocity while standing on Halley’s Comet, will the astronaut ever come back down? Create a program that allows the user to input a launch velocity (in
The “advanced search” feature of many search engines allows you to use Boolean operators for complex queries, such as “(cats OR dogs) AND NOT pets”. Contrast these search operators with the Boolean operators in Java.
A mass m = 2 kilograms is attached to the end of a rope of length r = 3 meters. The mass is whirled around at high speed. The rope can withstand a maximum tension of T = 60 Newtons. Write a program that accepts a rotation speed v and determines whether such a speed will cause the rope to break.
True or false? A && B is the same as B && A for any Boolean conditions A and B.
Crop damage due to frost is one of the many risks confronting farmers. The figure below shows a simple alarm circuit designed to warn of frost. The alarm circuit uses a device called a thermistor to sound a buzzer when the temperature drops below freezing. Thermistors are semiconductor devices that
Complete the following truth table by finding the truth values of the Boolean expressions for all combinations of the Boolean inputs p, q, and r. (p && q) || !r !(p && (q || !r)) false false false false false true false true false 5 more combinations
The electric circuit shown below is designed to measure the temperature of the gas in a chamber.The resistor R represents a temperature sensor enclosed in the chamber. The resistance R, in Ω, is related to the temperature T, in °C, by the equation R = R0 + kT In this device, assume R0 = 100
Make up a Java code example that shows the dangling else problem using the following statement: A student with a GPA of at least 1.5, but less than 2, is on probation. With less than 1.5, the student is failing.
Sound level L in units of decibel (dB) is determined by L = 20 log10(p/p0) where p is the sound pressure of the sound (in Pascals, abbreviated Pa), and p0 is a reference sound pressure equal to 20 × 10–6 Pa (where L is 0 dB). The following table gives descriptions for certain sound levels.
A minivan has two sliding doors. Each door can be opened by either a dashboard switch, its inside handle, or its outside handle. However, the inside handles do not work if a child lock switch is activated. In order for the sliding doors to open, the gear shift must be in park, and the master unlock
Repeat Exercise P3.21, modifying the program so that it first asks the user whether the input will be a wavelength or a frequency.Data from Exercise P3.21,Write a program that prompts the user for a wavelength value and prints a description of the corresponding part of the electromagnetic spectrum,
Give an example of an if/else if/else sequence where the order of the tests does not matter. Give an example where the order of the tests matters.
Repeat Exercise P3.21, modifying the program so that it prompts for the frequency instead.Data from Exercise P3.21,Write a program that prompts the user for a wavelength value and prints a description of the corresponding part of the electromagnetic spectrum, as given in the following table.
Explain the difference between an if/else if/else sequence and nested if statements. Give an example of each.
Write a program that prompts the user for a wavelength value and prints a description of the corresponding part of the electromagnetic spectrum, as given in the following table. Electromagnetic Spectrum Туре Wavelength (m) Frequency (Hz) > 101 10- to 10 7x 107 to 103 4x 107 to 7x107 4x10* to
Of the following pairs of strings, which comes first in lexicographic order?a. "Tom", "Jerry"b. "Tom", "Tomato"c. "church", "Churchill"d. "car manufacturer", "carburetor"e. "Harry", "hairy"f. "Java", " Car"g. "Tom", "Tom"h. "Car", "Carl"i. "car", "bar"
A supermarket awards coupons depending on how much a customer spends on groceries. For example, if you spend $50, you will get a coupon worth eight percent of that amount. The following table shows the percent used to calculate the coupon awarded for different amounts spent. Write a program that
Explain how the lexicographic ordering of strings in Java differs from the ordering of words in a dictionary or telephone book. Hint: Consider strings such as IBM, wiley.com, Century 21, and While-U-Wait.
Calculating the tip when you go to a restaurant is not difficult, but your restaurant wants to suggest a tip according to the service diners receive. Write a program that calculates a tip according to the diner’s satisfaction as follows:• Ask for the diners’ satisfaction level using these
Write pseudocode for a program that assigns letter grades for a quiz, according to the following table:Score Grade90-100 ....... A80-89 ......... B70-79 ......... C60-69 ......... D....< 60 ....... F
When you use an automated teller machine (ATM) with your bank card, you need to use a personal identification number (PIN) to access your account. If a user fails more than three times when entering the PIN, the machine will block the card. Assume that the user’s PIN is “1234” and write a
Write a program that asks the user to enter a month (1 for January, 2 for February, and so on) and then prints the number of days in the month. For February, print “28 or 29 days”. Enter a month: 5 30 days Do not use a separate if/else branch for each month. Use Boolean operators.
Write pseudocode for a program that prompts the user for a month and day and prints out whether it is one of the following four holidays:• New Year’s Day (January 1)• Independence Day (July 4)• Veterans Day (November 11)• Christmas Day (December 25)
Write a program that prompts the user to provide a single character from the alphabet. Print Vowel or Consonant, depending on the user input. If the user input is not a letter (between a and z or A and Z), or is a string of length > 1, print an error message.
Develop a set of test cases for the algorithm in Exercise E3.14.Data from Exercise E3.14.Draw a flowchart for the algorithm in Exercise E3.13. Data from Exercise E3.13.Draw a flowchart for the algorithm in Exercise R3.12.Data from Exercise R3.12.In a scheduling program, we want to check whether two
Write a unit conversion program that asks the users from which unit they want to convert (fl. oz, gal, oz, lb, in, ft, mi) and to which unit they want to convert (ml, l, g, kg, mm, cm, m, km). Reject incompatible conversions (such as gal → km). Ask for the value to be converted, then display the
Develop a set of test cases for the algorithm in Exercise R3.12.Data from Exercise R3.12.In a scheduling program, we want to check whether two appointments overlap. For simplicity, appointments start at a full hour, and we use military time (with hours 0–24). The following pseudocode describes an
French country names are feminine when they end with the letter e, masculine otherwise, except for the following which are masculine even though they end with e:• le Belize• le Cambodge• le Mexique• le Mozambique• le Zaïre• le ZimbabweWrite a program that reads the French name of a
Write a program that reads in two floating-point numbers and tests whether they are the same up to two decimal places. Here are two sample runs. Enter two floating-point numbers: 2.0 1.99998 They are the same up to two decimal places. Enter two floating-point numbers: 2.0 1.98999 They are different.
Draw a flowchart for the algorithm in Exercise E3.14.Data from Exercise E3.14.Draw a flowchart for the algorithm in Exercise E3.13.Data from Exercise E3.13.Draw a flowchart for the algorithm in Exercise R3.12.Data from Exercise R3.12.In a scheduling program, we want to check whether two
A year with 366 days is called a leap year. Leap years are necessary to keep the calendar synchronized with the sun because the earth revolves around the sun once every 365.25 days. Actually, that figure is not entirely precise, and for all dates after 1582 the Gregorian correction applies. Usually
The following algorithm yields the season (Spring, Summer, Fall, or Winter) for a given month and day.If month is 1, 2, or 3, season = "Winter"Else if month is 4, 5, or 6, season = "Spring"Else if month is 7, 8, or 9, season = "Summer"Else if month is 10, 11, or 12, season = "Fall"If month is
Draw a flowchart for the algorithm in Exercise E3.13.Data from Exercise E3.13.Draw a flowchart for the algorithm in Exercise R3.12.Data from Exercise R3.12.In a scheduling program, we want to check whether two appointments overlap. For simplicity, appointments start at a full hour, and we use
Roman numbers. Write a program that converts a positive integer into the Roman number system. The Roman number system has digitsI ........1V ........5X ........10L ........50C ........100D ........500M ........1,000Numbers are formed according to the following rules:a. Only numbers up to 3,999 are
Write a program that reads in three floating-point numbers and prints the largest of the three inputs. For example: Please enter three numbers: 4 9 2.5 The largest number is 9.
When two points in time are compared, each given as hours (in military time, ranging from 0 and 23) and minutes, the following pseudocode determines which comes first.If hour1 < hour2time1 comes first.Else if hour1 and hour2 are the sameIf minute1 < minute2time1 comes first.Else if minute1
Draw a flowchart for the algorithm in Exercise R3.12.Data from Exercise R3.12.In a scheduling program, we want to check whether two appointments overlap. For simplicity, appointments start at a full hour, and we use military time (with hours 0–24). The following pseudocode describes an algorithm
Write a program that reads in the x- and y-coordinates of four corner points of a quadrilateral and prints out whether it is a square, a rectangle, a trapezoid, a rhombus, or none of those shapes.
Add error handling to Exercise E3.11. If the user does not enter a number when expected, or provides an invalid unit for the altitude, print an error message and end the program.Data from Exercise E3.11. The boiling point of water drops by about one degree centigrade for every 300 meters (or
In a scheduling program, we want to check whether two appointments overlap. For simplicity, appointments start at a full hour, and we use military time (with hours 0–24). The following pseudocode describes an algorithm that determines whether the appointment with start time start1 and end time
Write a program that reads in the x- and y-coordinates of three corner points of a triangle and prints out whether it has an obtuse angle, a right angle, or only acute angles.
The boiling point of water drops by about one degree centigrade for every 300 meters (or 1,000 feet) of altitude. Improve the program of Exercise E3.10 to allow the user to supply the altitude in meters or feet.
Write a program that reads in the x- and y-coordinates of two corner points of a rectangle and then prints out whether the rectangle is a square, or is in “portrait” or “landscape” orientation.
Write a program that reads a temperature value and the letter C for Celsius or F for Fahrenheit. Print whether water is liquid, solid, or gaseous at the given temperature at sea level.
Each square on a chess board can be described by a letter and number, such as g5 in this example:The following pseudocode describes an algorithm that determines whether a square with a given letter and number is dark (black) or light (white).If the letter is an a, c, e, or gIf the number is
The TaxCalculator.java program uses a simplified version of the 2008 U.S. income tax schedule. Look up the tax brackets and rates for the current year, for both single and married filers, and implement a program that computes the actual income tax.
A compass needle points a given number of degrees away from North, measured clockwise. Write a program that reads the angle and prints out the nearest compass direction; one of N, NE, E, SE, S, SW, W, NW. In the case of a tie, prefer the nearest principal direction (N, E, S, or W).
It is easy to confuse the = and == operators. Write a test program containing the statementif (floor = 13)What error message do you get? Write another test program containing the statementcount == 0;What does your compiler do when you compile the program?
Showing 600 - 700
of 835
1
2
3
4
5
6
7
8
9
Step by Step Answers