java programs

Programming Exercise 8.7

*8.7 (Points nearest to each other) Listing 8.3 gives a program that finds two points in a two-dimensional space nearest to each other. Revise the program so that it finds two points in a three-dimensional space nearest to each other. Use a two-dimensional array to represent the points. Test the program using the following points:…

Read More
java programs

Programming Exercise 8.6

**8.6 (Algebra: multiply two matrices) Write a method to multiply two matrices. The header of the method is: public static double[][] multiplyMatrix(double[][] a, double[][] b) To multiply matrix a by matrix b, the number of columns in a must be the same as the number of rows in b, and the two matrices must have…

Read More
805

Programming Exercise 8.5

8.5 (Algebra: add two matrices) Write a method to add two matrices. The header of the method is as follows: public static double[][] addMatrix(double[][] a, double[][] b) In order to be added, the two matrices must have the same dimensions and the same or compatible types of elements. Let c be the resulting matrix. Each…

Read More
804

Programming Exercise 8.4

**8.4 (Compute the weekly hours for each employee) Suppose the weekly hours for all employees are stored in a two-dimensional array. Each row records an employee’s seven-day work hours with seven columns. For example, the following array stores the work hours for eight employees. Write a program that displays employees and their total hours in…

Read More
803

Programming Exercise 8.3

*8.3 (Sort students on grades) Rewrite Listing 8.2, GradeExam.java, to display the students in increasing order of the number of correct answers.

Read More
802

Programming Exercise 8.2

*8.2 (Sum the major diagonal in a matrix) Write a method that sums all the numbers in the major diagonal in an n * n matrix of double values using the following header: public static double sumMajorDiagonal(double[][] m) Write a test program that reads a 4-by-4 matrix and displays the sum of all its elements…

Read More
801

Programming Exercise 8.1

*8.1 (Sum elements column by column) Write a method that returns the sum of all the elements in a specified column in a matrix using the following header: public static double sumColumn(double[][] m, int columnIndex) Write a test program that reads a 3-by-4 matrix and displays the sum of each column. Here is a sample…

Read More
735

Programming Exercise 7.35

***7.35 (Game: hangman) Write a hangman game that randomly generates a word and prompts the user to guess one letter at a time, as shown in the sample run. Each letter in the word is displayed as an asterisk. When the user makes a correct guess, the actual letter is then displayed. When the user…

Read More
734

Programming Exercise 7.34

**7.34 (Sort characters in a string) Write a method that returns a sorted string using the following header: public static String sort(String s) For example, sort("acb") returns abc. Write a test program that prompts the user to enter a string and displays the sorted string.

Read More
733

Programming Exercise 7.33

*7.33 (Culture: Chinese Zodiac) Simplify Listing 3.9 using an array of strings to store the animal names.

Read More
732

Programming Exercise 7.32

**7.32 (Partition of a list) Write the following method that partitions the list using the first element, called a pivot. public static int partition(int[] list) After the partition, the elements in the list are rearranged so that all the elements before the pivot are less than or equal to the pivot and the elements after…

Read More
731

Programming Exercise 7.31

**7.31 (Merge two sorted lists) Write the following method that merges two sorted lists into a new sorted list. public static int[] merge(int[] list1, int[] list2) Implement the method in a way that takes at most list1.length + list2. length comparisons. Write a test program that prompts the user to enter two sorted lists and…

Read More
730

Programming Exercise 7.30

*7.30 (Pattern recognition: consecutive four equal numbers) Write the following method that tests whether the array has four consecutive numbers with the same value. public static boolean isConsecutiveFour(int[] values) Write a test program that prompts the user to enter a series of integers and displays if the series contains four consecutive numbers with the same…

Read More
729

Programming Exercise 7.29

*7.29 (Game: pick four cards) Write a program that picks four cards from a deck of 52 cards and computes their sum. An Ace, King, Queen, and Jack represent 1, 13, 12, and 11, respectively. Your program should display the number of picks that yields the sum of 24.

Read More
728

Programming Exercise 7.28

*7.28 (Math: combinations) Write a program that prompts the user to enter 10 integers and displays all combinations of picking two numbers from the 10.

Read More