Introduction to Java Programming and Data Structures Comprehensive Version (11th Edition)
11th Edition
ISBN: 9780134700144
Author: Liang
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Expert Solution & Answer
Chapter 6.6, Problem 6.6.1CP
Explanation of Solution
//Class test
public class Test
{
public static void main(String args[])
{
int x= gcd(4,6);
System.out.println(x);
}
public static int gcd(int n1, int n2)
{
//Declare the variable "gcd" to 1
int gcd=1;
//Declare the variable "k" to 1
int k=2;
/*Check the condition that the specified value lies between "n1" and "n2"*/
while (k<=n1 && k<=n2)
{
/*Check the condition for n1 and n2 mod k equals to 0 */
if(n1%k==0 && n2%k==0)
//Assign "gcd" to "k"
gcd=k;
//Increment the "k"
k++;
}
//Return the gcd
return gcd;
}
}
Explanation:
Definition for Main method:
- Method “gcd()” is called by passing the value (4,6)...
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Please explain why this is the correct answer.
Execute the following codes:
import numpy as np
mean = [0,0]
cov = [[1,-0.6],[-0.6,7]]
x,y = np.random.multivariate_normal(mean,cov,500).T
Save x and y using save method to your computer. Read them and calculate the correlation coefficient matrix. Make a 2-dimensional scatter plot of x and y. Do the straight line fit to y vs x.
Can you pls answer the second method?
Chapter 6 Solutions
Introduction to Java Programming and Data Structures Comprehensive Version (11th Edition)
Ch. 6.4 - What are the benefits of using a method?Ch. 6.4 - Prob. 6.4.2CPCh. 6.4 - How do you simplify the max method in Listing 6.1...Ch. 6.4 - Prob. 6.4.4CPCh. 6.4 - Prob. 6.4.5CPCh. 6.4 - Prob. 6.4.6CPCh. 6.4 - Prob. 6.4.7CPCh. 6.4 - Write method headers (not the bodies) for the...Ch. 6.4 - Identify and correct the errors in the following...Ch. 6.4 - Prob. 6.4.10CP
Ch. 6.5 - Prob. 6.5.1CPCh. 6.5 - Identify and correct the errors in the following...Ch. 6.5 - Prob. 6.5.3CPCh. 6.5 - Prob. 6.5.4CPCh. 6.6 - Prob. 6.6.1CPCh. 6.6 - Prob. 6.6.2CPCh. 6.7 - What is hexCharToDecimal ( B)) ? What is...Ch. 6.8 - What is method overloading? Is it permissible to...Ch. 6.8 - What is wrong in the following program? public...Ch. 6.8 - Given two method definitions, public static double...Ch. 6.9 - Prob. 6.9.1CPCh. 6.9 - What is the scope of a local variable?Ch. 6 - (Math: pentagonal numbers) A pentagonal number is...Ch. 6 - (Sum the digits in an integer) Write a method that...Ch. 6 - (Palindrome integer) Write the methods with the...Ch. 6 - (Display an integer reversed) Write a method with...Ch. 6 - (Sort three numbers) Write a method with the...Ch. 6 - (Display patterns) Write a method to display a...Ch. 6 - (Financial application: compute the future...Ch. 6 - (Conversions between Celsius and Fahrenheit) Write...Ch. 6 - Prob. 6.9PECh. 6 - (Use the isPrime Method) Listing 6.7,...Ch. 6 - (Financial application: compute commissions) Write...Ch. 6 - (Display characters) Write a method that prints...Ch. 6 - (Sum series) Write a method to compute the...Ch. 6 - (Estimate ) can be computed using the following...Ch. 6 - (Financial application: print a tax table) Listing...Ch. 6 - Prob. 6.16PECh. 6 - Sections 6.10 and 6.11 6.17 (Display matrix of 0s...Ch. 6 - (Check password) Some Websites impose certain...Ch. 6 - (Triangles) Implement the following two methods: /...Ch. 6 - (Count the letters in a string) Write a method...Ch. 6 - (Phone keypads) The international standard...Ch. 6 - (Math: approximate the square root) There are...Ch. 6 - (Occurrences of a specified character) Write a...Ch. 6 - (Display current date and time) Listing 2.7,...Ch. 6 - Prob. 6.25PECh. 6 - (Palindromic prime) A palindromic prime is a prime...Ch. 6 - (Emirp) An emirp (prime spelled backward) is a...Ch. 6 - (Mersenne prime) A prime number is called a...Ch. 6 - (Twin primes) Twin primes are a pair of prime...Ch. 6 - (Game: craps) Craps is a popular dice game played...Ch. 6 - (Financial: credit card number validation) Credit...Ch. 6 - (Game: chance of winning at craps) Revise...Ch. 6 - (Current date and time) Invoking System....Ch. 6 - (Print calendar) Programming Exercise 3.21 uses...Ch. 6 - (Geometry: area of a pentagon) The area of a...Ch. 6 - (Geometry: area of a regular polygon) A regular...Ch. 6 - (Format an integer) Write a method with the...Ch. 6 - (Generate random characters) Use the methods in...Ch. 6 - (Geomentry: point position) Programming Exercise...
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- How do you obtain the seconds from the returned value for time.time()?arrow_forwardDon't change variable names and please don't use the answer on another site. thank youarrow_forwardPlease provide an explaation and comments and check image for formula . Write approxPI(), a static method using a simple formula to return an approximate value of π. The method has a single int parameter n, the number of terms used to calculate π, and returns a double value. The simple formula (see notation), is expressed as: Step 1: a simple loop (i=0; i<=n) for the summation (Σ), stored to the variable pSum, of the terms: (-1)i / (2i + 1)pSum = 1/1 + -1/3 + 1/5 + -1/7 + 1/9 + -1/11 + 1/13 … (up to, and include, number of terms: n) i=0 i=1 i=2 i=3 i=4 i=5 i=6 … i <= n Step 2: after the summation, multiple by 4 to obtain the final approximation, piApprox = (4 * pSum) Hint: use Math.pow(x,y) (xy) to calculate (-1)iExample calls to the method:displayln ( "n=1: " + approxPI(1) ); // 2.666666666666667displayln ( "n=10: " + approxPI(10) ); // 3.232315809405594displayln ( "n=50: " + approxPI(50) ); // 3.1611986129870506 Test: - test with values of n: 10, 100, 500, 100000arrow_forward
- Find the running time T(n) of the calculateSum method: Note that you are finding the running time, not the order of growth. We know the order of growth is O(n). List the steps leading to your calculation in case your answer is wrong but could get partial credit. Count all operations, including loop controls, declarations, updates, return, etc. i++ should be considered two statements (an addition and assignment). Accessing a value from an array does not need to be counted as a step. For example, numbers[i] = x is 1 statement (an assignment), not 2 statements. public static int calculateSum(int[] numbers) { int sum = 0; for(int i=0; i<numbers.length; i++) { sum += numbers[i]; } return sum; }arrow_forwardWrite a code to show the multiplication table by two methods? (1 ate the main difference between the matlah nooram and than of loquare plant Prope using sw weile a code to estar des ngle)arrow_forwardHere is the code so far:(THE PROBLEM WITH THE CODE IS THAT THE IM SUPPOSE TO USE FIND() TO REPEATEDLY CHANGE THE OCCURRENCE OF THE WORD "MASK" TO THE WORD "HAT" IN THE FILE BUT AM KIND OF LOST HAVE BEEN WORKING ON THIS FOR A WEEK AND A HALF) ALSO I CAN NOT use the replace method, count method, split method, range() function, or lists in your solution. HAVE BEEN TRYING TO FIND SUM WAY TO USE FIND() BUT AM COMING UP SHORT CODE: #Get input file name fileOne = input("Enter the input file name :") #Get output file name fileTwo = input("Enter the output file name :") '''Get a string that will be searched in the source file to locate all occurrences of the string''' targetString = input("Enter the target string :") '''Get a string that will replace each occurrence of the target string in the source file''' replacementString = input("Enter the replacement string :") files =input("Enter a file with the word 'mask':") #Open the input file in read mode f1 = open(fileOne,"r+") #Read…arrow_forward
- Ok so im going to list my question and then the work so far. Here is the code so far:(THE PROBLEM WITH THE CODE IS THAT THE IM SUPPOSE TO USE FIND() TO REPEATEDLY CHANGE THE OCCURRENCE OF THE WORD "MASK" TO THE WORD "HAT" IN THE FILE BUT AM KIND OF LOST HAVE BEEN WORKING ON THIS FOR A WEEK AND A HALF) ALSO I CAN NOT use the replace method, count method, split method, range() function, or lists in your solution. HAVE BEEN TRYING TO FIND SUM WAY TO USE FIND() BUT AM COMING UP SHORT here's the code i have so far: inputfile = input('Enter input file name: ')outputfile = input('Enter output file name: ')input_data = open(inputfile, 'r') # this opens the file to read it.output_data = open(outputfile, 'w') # this opens a file to write to.userStr = (input('Enter target string:')) # this prompts the user for a worduserReplace = input('Enter replacement string: ') # this prompts the user for the replacement wordfor line in input_data:if userStr in…arrow_forwardOk so im going to list my question and then the work so far. Here is the code so far:(THE PROBLEM WITH THE CODE IS THAT THE IM SUPPOSE TO USE FIND() TO REPEATEDLY CHANGE THE OCCURRENCE OF THE WORD "MASK" TO THE WORD "HAT" IN THE FILE BUT AM KIND OF LOST HAVE BEEN WORKING ON THIS FOR A WEEK AND A HALF) ALSO I CAN NOT use the replace method, count method, split method, range() function, or lists in your solution. HAVE BEEN TRYING TO FIND SUM WAY TO USE FIND() BUT AM COMING UP SHORT I HAVE TO USE FIND() TO GET EVERY OCURRANCE BUT AM HAVING TROUBLE WITH THAT ** I CAN NOT use the replace method, count method, split method, range() function, or lists ** fileOne = input("Enter the input file name:") fileTWo = input("Enter the output file name:") f = open('infile.txt', 'r') f.read() targetString = input("Enter the target string:") replacementString = input("Enter the replacement string :") Fhand = open(“inFile.txt”) for line in fhand: line = line.rstrip() If line.find(“mask”) == -1:…arrow_forwardOk so im going to list my question and then the work so far. Here is the code so far:(THE PROBLEM WITH THE CODE IS THAT THE IM SUPPOSE TO USE FIND() TO REPEATEDLY CHANGE THE OCCURRENCE OF THE WORD "MASK" TO THE WORD "HAT" IN THE FILE BUT AM KIND OF LOST HAVE BEEN WORKING ON THIS FOR A WEEK AND A HALF) ALSO I CAN NOT use the replace method, count method, split method, range() function, or lists in your solution. HAVE BEEN TRYING TO FIND SUM WAY TO USE FIND() BUT AM COMING UP SHORT CODE: #Get input file name fileOne = input("Enter the input file name :") #Get output file name fileTwo = input("Enter the output file name :") '''Get a string that will be searched in the source file to locate all occurrences of the string''' targetString = input("Enter the target string :") '''Get a string that will replace each occurrence of the target string in the source file''' replacementString = input("Enter the replacement string :") files =input("Enter a file with the word 'mask':") #Open…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
Binary Numbers and Base Systems as Fast as Possible; Author: Techquikie;https://www.youtube.com/watch?v=LpuPe81bc2w;License: Standard YouTube License, CC-BY
Binary Number System; Author: Neso Academy;https://www.youtube.com/watch?v=w7ZLvYAi6pY;License: Standard Youtube License