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
building java programs a back to basics approach
Building Java Programs A Back To Basics Approach 5th Edition Stuart Reges, Marty Stepp - Solutions
Write a method called coinFlip that accepts a Scanner representing an input file of coin flips that are heads (H) or tails (T). Consider each line to be a separate set of coin flips and output the number and percentage of heads in that line. If it is more than 50%, print “You win!”. Consider
Given the following file contents, what will be the output from each of the following code fragments?a.b. Scanner input new Scanner (new File ("brownfox.txt")); while (input.hasNextLine ()) { String line = input.nextLine (): System.out.println (line);
Write a method called mostCommonNames that accepts a Scanner representing an input file with names on each line separated by spaces. Some names appear multiple times in a row on the same line. For each line, print the most commonly occurring name. If there’s a tie, use the first name that had
Write a program that prints itself to the console as output. For example, if the program is stored in Example.java, it will open the file Example.java and print its contents to the console.
Write a method called inputStats that accepts a Scanner representing an input file and reports the number of lines, the longest line, the number of tokens on each line, and the length of the longest token on each line. If the file contains the following text:Beware the Jabberwock, my son,the jaws
Write code that prompts the user for a file name and prints the contents of that file to the console as output. Assume that the file exists. You may wish to place this code into a method called printEntireFile.
Write a method called plusScores that accepts a Scanner representing an input file containing a series of lines that represent student records. Each student record takes up two lines of input. The first line has the student’s name and the second line has a series of plus and minus characters.
Write a program that takes as input lines of text like the following:This is sometext here.The program should produce as output the same text inside a box, as in the following:Your program will have to assume some maximum line length (e.g., 12 in this case). -+ This is some | | text here. +- -+
Write a method called leetSpeak that accepts two parameters: a Scanner representing an input file, and a PrintStream representing an output file. Convert the input file’s text to “leet speak,” where various letters are replaced by other letters/numbers, and output the new text to the given
What object is used to write output to a file? What methods does this object have available for you to use?
Write a method called pigLatin that accepts as a parameter a Scanner representing an input file. Your method should, preserving line breaks, print out the input file’s text in a simplified version of Pig Latin, a silly English variant where the first letter of each word is moved to the end. Our
Write code to print the following four lines of text into a file named message.txt:Testing,1, 2, 3.This is my output file.
Write code that repeatedly prompts the user for a file name until the user types the name of a file that exists on the system. You may wish to place this code into a method called getFileName, which will return that file name as a String.
In Problem 16, you wrote a piece of code that prompted the user for a file name and printed that file’s contents to the console. Modify your code so that it will repeatedly prompt the user for the file name until the user types the name of a file that exists on the system.Data from Problem
Java’s type int has a limit on how large an integer it can store. This limit can be circumvented by representing an integer as an array of digits. Write an interactive program that adds two integers of up to 50 digits each.
Write a method called lastIndexOf that accepts an array of integers and an integer value as its parameters and returns the last index at which the value occurs in the array. The method should return –1 if the value is not found. For example, in the array [74, 85, 102, 99, 101, 85, 56], the last
Which of the following is the correct syntax to declare an array of ten integers?a. int a[10] = new int[10];b. int[10] a = new int[10];c. []int a = [10]int;d. int a[10];e. int[] a = new int[10];
Write a game of Hangman using arrays. Allow the user to guess letters and represent which letters have been guessed in an array.
Write a method called range that returns the range of values in an array of integers. The range is defined as 1 more than the difference between the maximum and minimum values in the array. For example, if an array called list contains the values [ 36, 12, 25, 19, 46, 31, 22 ] , the call of
What expression should be used to access the first element of an array of integers called numbers? What expression should be used to access the last element of numbers, assuming it contains 10 elements? What expression can be used to access its last element, regardless of its length?
Write a program that plays a variation of the game of Mastermind with a user. For example, the program can use pseudorandom numbers to generate a four-digit number. The user should be allowed to make guesses until she gets the number correct. Clues should be given to the user indicating how many
Write a method called countInRange that accepts an array of integers, a minimum value, and a maximum value as parameters and returns the count of how many elements from the array fall between the minimum and maximum (inclusive). For example, in the array [ 14, 1, 22, 17, 36, 7, –43, 5 ], for
Write a method called isSorted that accepts an array of real numbers as a parameter and returns true if the list is in sorted (nondecreasing) order and false otherwise. For example, if arrays named list1 and list2 store [16.1, 12.3, 22.2, 14.4] and [1.5, 4.3, 7.0, 19.5, 25.1, 46.2] respectively,
Write code that stores all odd numbers between -6 and 38 into an array using a loop. Make the array’s size exactly large enough to store the numbers.Then, try generalizing your code so that it will work for any minimum and maximum values, not just -6 and 38.
What elements does the array numbers contain after the following code is executed?int[] numbers = new int[8];numbers[1] = 4;numbers[4] = 99;numbers[7] = 2;int x = numbers[1];numbers[x] = 44;numbers[numbers[7]] = 11; // uses numbers[7] as index
Use a two-dimensional array to write a game of Tic-Tac-Toe that represents the board.
Write a method called mode that returns the most frequently occurring element of an array of integers. Assume that the array has at least one element and that every element in the array has a value between 0 and 100 inclusive. Break ties by choosing the lower value. For example, if the array passed
What elements does the array data contain after the following code is executed?int[] data = new int[8];data[0] = 3;data[7] = -18;data[4] = 5;data[1] = data[0];int x = data[4];data[4] = 6;data[x] = data[0] * data[1];
Write a program that reads a file of DNA data and searches for protein sequences. DNA data consists of long Strings of the letters A, C, G, and T, corresponding to chemical nucleotides called adenine, cytosine, guanine, and thymine. Proteins can be identified by looking for special triplet
Write a basic Photoshop or Instagram-inspired program with a menu of available image manipulation algorithms similar to those described in the exercises in this chapter. The user can load an image from a file and then select which manipulation to perform, such as grayscale, zoom, rotate, or blur.
Write a method called kthLargest that accepts an integer k and an array a as its parameters and returns the element such that k elements have greater or equal value. If k = 0, return the largest element; if k = 1, return the second-largest element, and so on. For example, if the array passed
Which of the following is the correct syntax to declare an array of the given six integer values?a. int[] a = {17, -3, 42, 5, 9, 28};b. int a {17, -3, 42, 5, 9, 28};c. int[] a = new int[6] {17, -3, 42, 5, 9, 28};d. int[6] a = {17, -3, 42, 5, 9, 28};e. int[] a = int [17, -3, 42, 5, 9, 28] {6};
Write a method called median that accepts an array of integers as its parameter and returns the median of the numbers in the array. The median is the number that appears in the middle of the list if you arrange the elements in order. Assume that the array is of odd size (so that one sole element
Write a piece of code that declares an array called data with the elements 7, -1, 13, 24, and 6. Use only one statement to initialize the array.
Write a method called minGap that accepts an integer array as a parameter and returns the minimum difference or gap between adjacent values in the array, where the gap is defined as the later value minus the earlier value. For example, in the array [ 1, 3, 6, 7, 12 ], the first gap is 2 (3 − 1),
Write a piece of code that examines an array of integers and reports the maximum value in the array. Consider putting your code into a method called max that accepts the array as a parameter and returns the maximum value. Assume that the array contains at least one element.
Write a method called percentEven that accepts an array of integers as a parameter and returns the percentage of even numbers in the array as a real number. For example, if the array stores the elements [ 6, 2, 9, 11, 3 ], then your method should return 40.0. If the array contains no even elements
Write a method called average that computes the average (arithmetic mean) of all elements in an array of integers and returns the answer as a double . For example, if the array passed contains the values [1, – 2, 4, –4, 9, –6, 16, –8, 25, –10], the calculated average should be 2.5. Your
Write a method called isUnique that accepts an array of integers as a parameter and returns a boolean value indicating whether or not the values in the array are unique ( true for yes, false for no). The values in the list are considered unique if there is no pair of values that are equal. For
What is an array traversal? Give an example of a problem that can be solved by traversing an array.
Write a method called priceIsRight that mimics the guessing rules from the game show The Price Is Right. The method accepts as parameters an array of integers representing the contestants’ bids and an integer representing a correct price. The method returns the element in the bids array that is
Write code that uses a for loop to print each element of an array named data that contains five integers:element [0] is 14element [1] is 5element [2] is 27element [3] is −3element [4] is 2598Consider generalizing your code so that it will work on an array of any size.
Write a method called longestSortedSequence that accepts an array of integers as a parameter and returns the length of the longest sorted (nondecreasing) sequence of integers in the array. For example, in the array [ 3, 8, 10, 1, 9, 14, –3, 0, 14, 207, 56, 98, 12 ], the longest sorted sequence in
What elements does the array list contain after the following code is executed? int [] list {2, 18, 6, -4, 5, 1}; %3D for (int i = 0; i < list.length; i++) { list[i] list[i] + (list[i] / list [0]);
Write a method called contains that accepts two arrays of integers a1 and a2 as parameters and that returns a boolean value indicating whether or not the sequence of elements in a2 appears in a1 ( true for yes, false for no). The sequence must appear consecutively and in the same order. For
Write a piece of code that prints an array of integers in reverse order, in the same format as the print method from Section 7.2. Consider putting your code into a method called printBackwards that accepts the array as a parameter.
Write a method called collapse that accepts an array of integers as a parameter and returns a new array containing the result of replacing each pair of integers with the sum of that pair. For example, if an array called list stores the values [7, 2, 8, 9, 4, 13, 7, 1, 9, 10], then the call of
Describe the modifications that would be necessary to change the count and equals methods developed in Section 7.2 to process arrays of String s instead of arrays of integers.
Write a method called append that accepts two integer arrays as parameters and returns a new array that contains the result of appending the second array’s values at the end of the first array. For example, if arrays list1 and list2 store [ 2, 4, 6 ] and [ 1, 2, 3, 4, 5 ] respectively, the call
Write a method called allLess that accepts two arrays of integers and returns true if each element in the first array is less than the element at the same index in the second array. Your method should return false if the arrays are not the same length.
Write a method called vowelCount that accepts a String as a parameter and produces and returns an array of integers representing the counts of each vowel in the string. The array returned by your method should hold five elements: the first is the count of As, the second is the count of Es, the
Why does a method to swap two array elements work correctly when a method to swap two integer values does not?
Write a method called evenBeforeOdd that accepts an array of integers and rearranges its elements so that all even values appear before all odds. For example, if the array is [5, 4, 2, 11, 9, 10, 4, 7, 3], then after the method has been called, one acceptable ordering of the elements would be [4,
Write a method called wordLengths that accepts a Scanner for an input file as its parameter. Your method should open the given file, count the number of letters in each token in the file, and output a result diagram of how many words contain each number of letters. For example, consider a file
Write a method called matrixAdd that accepts a pair of twodimensional arrays of integers as parameters, treats the arrays as twodimensional matrixes, and returns their sum. The sum of two matrixes A and B is a matrix C, where for every row i and column j, Cij = Aij + Bij. You may assume that the
Write a method called swapPairs that accepts an array of integers and swaps the elements at adjacent indexes. That is, elements 0 and 1 are swapped, elements 2 and 3 are swapped, and so on. If the array has an odd length, the final element should be left unmodified. For example, the list [10, 20,
Write a method called isMagicSquare that accepts a two-dimensional array of integers as a parameter and returns true if it is a magic square. A square matrix is a magic square if all of its row, column, and diagonal sums are equal. For example, [[2, 7, 6], [9, 5, 1], [4, 3, 8]] is a square matrix
What are the values of the elements in the array numbers after the following code is executed? int [] numbers = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; for (int i = 0; i < 9; i++) { numbers [i] = numbers [i + 1];
What are the values of the elements in the array numbers after the following code is executed? int [] numbers = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100); for (int i = 1; i < 10; i++) { numbers [i] numbers [i 1]; %3D }
Write a method transpose that accepts a DrawingPanel as a parameter and inverts the image about both the and axes. You may assume that the image is square, that is, that its width and height are equal. Drawing Panel Drawing Panel Eile View Help Eile View Help ↑
Consider the following method, mystery:What are the values of the elements in array a1 after the following code executes?int[] a1 = {1, 3, 5, 7, 9};int[] a2 = {1, 4, 9, 16, 25};mystery(a1, a2); public static void mystery (int [] a, int [] b) { for (int i = 0; i < a.length; i++) { a[i] += b[b.length
Write a method zoomIn that accepts a DrawingPanel as a parameter and converts it into an image twice as large in both dimensions. Each pixel from the original image becomes a cluster of 4 pixels (2 rows and 2 columns) in the new zoomed image. Drawing Panel Eile Vew Help Drawing Panel Ele Vew Help
Consider the following method, mystery2:What are the values of the elements in array a1 after the following code executes?int[] a1 = {2, 4, 6, 8, 10, 12, 14, 16};int[] a2 = {1, 1, 2, 3, 5, 8, 13, 21};mystery2(a1, a2); public static void mystery2 (int [] a, int[] b) { for (int i = 0; i < a.length;
Write methods rotateLeft and rotateRight that rotate the pixels of an image counter-clockwise or clockwise by 90 degrees respectively. You should not assume that the image is square in shape; its width and height might be different. Drawing Panel - + x Drawing Panel - + X Drawing Panel Fle View
Consider the following method, mystery3:What are the values of the elements in the array numbers after the following code executes?int[] numbers = {3, 7, 1, 0, 25, 4, 18, −1, 5};mystery3(numbers, 3, 1);mystery3(numbers, 5, 6);mystery3(numbers, 8, 4); public static void mystery3 (int [] data, int
Write a method blur that makes an image look “blurry” using the following specific algorithm. Set each pixel to be the average of itself and the 8 pixels around it. That is, for the pixel at position (x, y), set its RGB value to be the average of the RGB values at positions (x − 1, y − 1)
Consider the following method:What value does the method return when passed each of the following arrays?a. {5}b. {3, 12}c. {4, 2, 10, 8}d. {1, 9, 3, 5, 7}e. {8, 2, 10, 4, 10, 9} public static int mystery4 (int [] list) { int x = 0; for (int i = 1; i < list.length; i++) { int y = list[i] list[0];
Consider the following method:What are the final contents of each of the following arrays if each is passed to the above method?a. {8}b. {14, 7}c. {7, 1, 3, 2, 0, 4}d. {10, 8, 9, 5, 5}e. {12, 11, 10, 10, 8, 7} public static void mystery5 (int[] nums) { for (int i = 0; i < nums.length 1; i++) { if
Write a piece of code that computes the average String length of the elements of an array of Strings . For example, if the array contains {"belt", "hat", "jelly", "bubble gum"}, the average length is 5.5.
Write code that accepts an array of String s as its parameter and indicates whether that array is a palindrome—that is, whether it reads the same forward as backward. For example, the array {"alpha", "beta", "gamma", "delta", "gamma", "beta", "alpha"} is a palindrome.
What elements does the array numbers contain after the following code is executed? int[] [] numbers = new int[3] [4]; for (int r = 0; r < numbers.length; r++) { for (int c = 0; c < numbers [0].length; c++) { numbers [r] [c] = r + c;
Assume that a two-dimensional rectangular array of integers called data has been declared with four rows and seven columns. Write a loop to initialize the third row of data to store the numbers 1 through 7.
Write a piece of code that constructs a two-dimensional array of integers with 5 rows and 10 columns. Fill the array with a multiplication table, so that array element [i][j] contains the value i * j. Use nested for loops to build the array.
Assume that a two-dimensional rectangular array of integers called matrix has been declared with six rows and eight columns. Write a loop to copy the contents of the second column into the fifth column.
Write a piece of code that constructs a jagged two-dimensional array of integers with five rows and an increasing number of columns in each row, such that the first row has one column, the second row has two, the third has three, and so on. The array elements should have increasing values in
When examining a 2D array of pixels, how could you figure out the width and height of the image even if you don’t have access to the DrawingPanel object?
Finish the following code for a method that converts an image into its red channel; that is, removing any green or blue from each pixel and keeping only the red component. public static void toRedChannel (DrawingPanel panel) { Color[] [] pixels panel.getPixels (); for (int row = 0; row <
What is the result of the following code? What will the image look like? public static void pixelMystery (DrawingPanel panel) { Color[][] pixels panel.getPixels (); for (int row = 0; row < pixels.length; row++) { for (int col = 0; col < pixels [0].length; col++) { int n = Math.min (row + col, 255);
Write a program that uses the DrawingPanel to draw Figure 3G.21 .The window is 220 pixels wide and 150 pixels tall. The background is yellow. There are two blue ovals of size 40 × 40 pixels. They are 80 pixels apart, and the left oval’s top-left corner is located at position (50, 25). There is a
Translate each of the following English statements into logical tests that could be used in an if/else statement. Write the appropriate if statement with your logical test. Assume that three int variables, x, y , and z , have been declared.a. z is odd.b. z is not greater than y’ s square root.c.
Describe a problem with the following code: Scanner console = new Scanner (System.in); System.out.print ("What is your favorite color?"); String name = console.next (); if (name == "blue") { System.out.println ("Mine, too!"):
Consider the following Java method, which is written incorrectly:Under what cases will the method print the correct answer, and when will it print an incorrect answer? What should be changed to fix the code? Can you think of a way to write the code correctly without any if/else statements? // This
One of the exercises in Chapter 3 asked you to write a method that would find the roots of a quadratic equation of the form ax2 + bx + c = 0. The quadratic method was passed a , b , and c and then applied the following quadratic formula:Under what conditions would this formula fail? Modify the
Write a program that produces images of Christmas trees as output. It should have a method with two parameters: one for the number of segments in the tree and one for the height of each segment. For example, the tree shown here on the left has three segments of height 4 and the one on the right has
Which of the following is the correct syntax for a method header with parameters?a. public static void example(x, y) {b. public static (int x, int y) example() {c. public static void example(int x, y) {d. public static void example(x: int, y: int) {e. public static void example(int x, int y) {
Which of the following is the correct syntax to draw a rectangle?a. Graphics g.drawRect(10, 20, 50, 30);b. G.drawRect(10, 20, 50, 30);c. G.draw.rectangle(10, 20, 50, 30);d. Graphics.drawRect(10, 20, 50, 30);e. G.drawRect(x = 10, y = 20, width = 50, height = 30);
Write a method called printNumbers that accepts a maximum number as an argument and prints each number from 1 up to that maximum, inclusive, boxed by square brackets. For example, consider the following calls:printNumbers(15);printNumbers(5);These calls should produce the following output:[1] [2]
Write a program that draws the patterns shown in Figure 3G.35 onto a DrawingPanel.The DrawingPanel ’s size is 400 × 400 pixels and its background color is cyan. It contains four figures of concentric yellow circles with black outlines, all surrounded by a green rectangle with a black outline.
A certain bank offers 6.5% interest on savings accounts, compounded annually. Create a table that shows how much money a person will accumulate over a period of 25 years, assuming that the person makes an initial investment of $1000 and deposits $100 each year after the first. Your table should
What output is produced by the following program?
There are two mistakes in the following code, which attempts to draw a line from coordinates (50, 86) to (20, 35). What are they?DrawingPanel panel = new DrawingPanel(200, 200);panel.drawLine(50, 20, 86, 35);
Modify your program from the previous exercise to draw the figure by a method called drawFigure . The method should accept three parameters: the Graphics g of the DrawingPanel on which to draw, and a pair of (x, y) coordinates specifying the location of the top-left corner of the figure. Use the
Write a method called printPowersOf2 that accepts a maximum number as an argument and prints each power of 2 from 20 (1) up to that maximum power, inclusive. For example, consider the following calls:printPowersOf2(3);printPowersOf2(10);These calls should produce the following output:1 2 4 81 2 4 8
Write a program that draws the image shown in Figure 3G.36 onto a DrawingPanel of size 200 × 200. Each stamp is 50 × 50 pixels in size. Drawing Panel 00 Love Java Love Java Love Java
Write a program that shows the total number of presents that the person in the song “The Twelve Days of Christmas” received on each day, as indicated in Table 3.5.Table 3.5 Twelve Days of Christmas
The following program has 9 mistakes. What are they? public class Oops3 { public static void main () { double bubble = 867.5309; %3D double x = 10.01; printer (double x, double y); 6. printer (x); printer ("barack", "obama"); 8. System.out.println ("z = " + z); 9. } 10 11 public static void printer
The following code attempts to draw a black-filled outer rectangle with a white-filled inner circle inside it:DrawingPanel panel = new DrawingPanel(200, 100);Graphics g = panel.getGraphics();g.setColor(Color.WHITE);g.fillOval(10, 10, 50, 50);g.setColor(Color.BLACK);g.fillRect(10, 10, 50,
Suppose you have the following existing program called Face that uses the DrawingPanel to draw the face figure shown in Figure 3G.23 . Modify the program to draw the modified output shown in Figure 3G.24 . Do so by writing a parameterized method that draws a face at different positions. The window
Write a program that draws checkerboards like these shown in Figure 3G.37 onto a DrawingPanel of size 420 × 300. Drawing Panel
Write a method called printPowersOfN that accepts a base and an exponent as arguments and prints each power of the base from base0 (1) up to that maximum power, inclusive. For example, consider the following calls:printPowersOfN(4, 3);printPowersOfN(5, 6);printPowersOfN(–2, 8);These calls should
Showing 700 - 800
of 1041
1
2
3
4
5
6
7
8
9
10
11
Step by Step Answers