All Matches
Solution Library
Expert Answer
Textbooks
Search Textbook questions, tutors and Books
Oops, something went wrong!
Change your search query and then try again
Toggle navigation
FREE Trial
S
Books
FREE
Tutors
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
Hire a Tutor
AI Study Help
New
Search
Search
Sign In
Register
study help
computer science
building java programs a back to basics approach
Questions and Answers of
Building Java Programs A Back to Basics Approach
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
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
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
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
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
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
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
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,”
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
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
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
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
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
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
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
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
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
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
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
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] =
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
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] =
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
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
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
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,
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
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
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
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
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
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).
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
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
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
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 (
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
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
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
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
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
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,
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
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
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
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,
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] =
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]
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
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);
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
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,
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
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,
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
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
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,
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
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
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
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
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 *
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
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
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
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 <
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
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
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") {
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
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
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
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,
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,
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
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
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
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,
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
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
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
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);
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 =
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
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
Showing 700 - 800
of 1041
1
2
3
4
5
6
7
8
9
10
11