Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
7th Edition
ISBN: 9780134802213
Author: Tony Gaddis
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Expert Solution & Answer
Chapter 5, Problem 4FTE
Explanation of Solution
Given
//Line 1- Define a method half()
public static int half(double num)
{
//Line 2 - Compute value of result
double result = num/2.0;
//Line 3- Return
return result;
}
Methods:
- The methods could be used to break a complex program into small pieces.
- The “void” method simply executes a statements group and then terminates.
- A value returning method would return a value to statement that has called it.
- While calling a method, program would branch to that method.
- It would then execute statements in its body.
- The values that are sent into a method are termed as “arguments”.
- A “parameter” denotes a special variable that holds a value that is being passed into a method.
- The “passed by value” means that only copy of a value of argument is been passed into parameter variable...
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Find the error of the following method definition:
public static void timesTwo( int x, int y)
{
int sum=x + y;
return sum;
}
List the name of this method and the name and type of its parameter:public void setCredits(int creditValue){ credits = creditValue;}
Examine the following method header; then write an example call to the method. private void ResetValue(ref int value)
Chapter 5 Solutions
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
Ch. 5.1 - What is the difference between a void method and a...Ch. 5.1 - Prob. 5.2CPCh. 5.1 - Prob. 5.3CPCh. 5.1 - What message will the following program display if...Ch. 5.1 - Prob. 5.5CPCh. 5.2 - What is the difference between an argument and a...Ch. 5.2 - Prob. 5.7CPCh. 5.2 - Prob. 5.8CPCh. 5.2 - Prob. 5.9CPCh. 5.2 - What will the following program display? public...
Ch. 5.4 - Prob. 5.11CPCh. 5.4 - Prob. 5.12CPCh. 5.4 - Prob. 5.13CPCh. 5.4 - Prob. 5.14CPCh. 5 - This type of method does not return a value. a....Ch. 5 - Prob. 2MCCh. 5 - Prob. 3MCCh. 5 - Prob. 4MCCh. 5 - A value that is passed into a method when it is...Ch. 5 - Prob. 6MCCh. 5 - Prob. 7MCCh. 5 - Prob. 8MCCh. 5 - Prob. 9MCCh. 5 - True or False: You terminate a method header with...Ch. 5 - Prob. 11TFCh. 5 - Prob. 12TFCh. 5 - Prob. 13TFCh. 5 - Prob. 14TFCh. 5 - Prob. 15TFCh. 5 - Prob. 16TFCh. 5 - Prob. 17TFCh. 5 - True or False: No two methods in the same program...Ch. 5 - True or False: It is possible for one method to...Ch. 5 - True or False: You must have a return statement in...Ch. 5 - Prob. 1FTECh. 5 - Look at the following method header: public static...Ch. 5 - Prob. 3FTECh. 5 - Prob. 4FTECh. 5 - Prob. 1AWCh. 5 - Here is the code for the displayValue method,...Ch. 5 - Prob. 3AWCh. 5 - What will the following program display? public...Ch. 5 - A program contains the following method...Ch. 5 - Prob. 6AWCh. 5 - Prob. 7AWCh. 5 - Write a method named square that accepts an...Ch. 5 - Write a method named getName that prompts the user...Ch. 5 - Write a method named quartersToDol1ars. The method...Ch. 5 - Prob. 1SACh. 5 - Prob. 2SACh. 5 - What is the difference between an argument and a...Ch. 5 - Where do you declare a parameter variable?Ch. 5 - Prob. 5SACh. 5 - Prob. 6SACh. 5 - Prob. 1PCCh. 5 - Retail Price Calculator Write a program that asks...Ch. 5 - Rectangle AreaComplete the Program If you have...Ch. 5 - Paint Job Estimator A painting company has...Ch. 5 - Prob. 5PCCh. 5 - Celsius Temperature Table The formula for...Ch. 5 - Test Average and Grade Write a program that asks...Ch. 5 - Conversion Program Write a program that asks the...Ch. 5 - Distance TraveLed Modification The distance a...Ch. 5 - Stock Profit The profit from the sale of a stock...Ch. 5 - Multiple Stock Sales Use the method that you wrote...Ch. 5 - Kinetic Energy In physics, an object that is in...Ch. 5 - isPrime Method A prime number is a number that is...Ch. 5 - Prime Number List Use the isPrime method that you...Ch. 5 - Even/Odd Counter You can use the following logic...Ch. 5 - Present Value Suppose you want to deposit a...Ch. 5 - Rock, Paper, Scissors Game Write a program that...Ch. 5 - ESP Game Write a program that tests your ESP...
Knowledge Booster
Similar questions
- Free Response Question: 28. Write a method that will return true if a randomly generated number between 1 and 10 is less than or equal to 4. public static boolean FortyPercentOrLess() { }arrow_forwardValue-returning methods must pass parameters. -True or -Falsearrow_forwardYou can declare a void method as follow; public void int calculateSalary() { int num1=89; int num 2= 71; int sum= num1+num2; return sum; } a) True b) Falsearrow_forward
- 2) Two integers are said to be relatively prime if their greatest common divisor (GCD) is one. For example 12 and 13 are relatively prime, but 12 and 14 are not. a. Write a method called relativelyPrime that accepts two integer parameters A and B. The method should output if A, and B are relatively prime or nat. Use the following method header: public static void relativelyPrime(int A, int B) b. Write a test program (main) to test the method by asking the user to input two positive integers X and Y. Then the program will call the method relativelyPrime to output if X and Y are relatively prime or not. Sample Run 1: Enter Two positive integers X and Y: 12 13 12 and 13 are relatively prime Sample Run 2: Enter Two positive integers X and Y: 12 14 12 and 14 are not relatively primearrow_forward1- question Given the following method; public static void mystery(int x) { int z = 10; if (Z <= x) Z--; z++; System.out.println(z); } What is the output of mystery(30)? a. 9 b. 10 C. 29 d. 30arrow_forwardMethod Overloading is a feature that allows a class to have two or more methods having same name, if their argument lists are different. Argument lists could differ in: 1. Number of parameters. 2. Data type of parameters. 3. Sequence of Data type of parameters. Write different version of method sum() that display the sum of the values received as parameter according to the following main method content: public static void main(String[] args) { sum ( 10, 10 ); sum ( 10, 10, 10 ); sum ( 10.0, 10.0 ); sum ( 10, 10.0 ); sum ( 10.0, 10); } You have to define five functions with the specified types. Then, demonstrate the Argument Promotion concept by reducing the number of method to two.arrow_forward
- Using METHOD SAMPLE Problem: Create a method that will compute the value of PI, if pi is 3.14. Given formula is : value = pi x square of radius Solution: float area(int radius) final float pi = 3.14f; float val; val = pi * (float) (radius * radius); return(val); Direction: Analyze the given problem and create a java method. Problem: Create a method that will compute the Mass (m). Acceleration(a) is 9.80. if Force (F) is equal to acceleration multiplied by mass.arrow_forwarduse java as programming languagearrow_forwardJAVA PROGRAM SEE ATTACHED PHOTO FOR THE PROBLEMarrow_forward
- Correct- need the question.arrow_forwardProblem: Construct a program that simulates the major Bank Transactions: Deposit, Withdraw and Inquire. Your program should use methods for the 3 transactions and another method for the login transaction. Below is the initial structure of the your code: public class BankTransaction { static void DisplayMenu() { /*this method displays the menu of the transactions*/ } static void withdraw(float amount) { /*this method should deduct the amount withdrawn from the current balance*/ } static void deposit(float amount) { /*this method should add the amount deposited to the current balance*/ } static float inquire() { /*this method should return the current balance*/ } static boolean CorrectPinNUmber() { /*this method should ask user to input the pin number. returns TRUE if the pin number is valid*/ It } public static void main(String a[]) { } } Design your main program to test these methods. You can design your own user interface.arrow_forwardDescription: The program prompts the user to enter three numbers and invokes the method: public static void displayIncreasingNumbers(int num1, double num2, long num3) to display them in increasing order. Below is my code: package threeincreasingnumbers; import java.util.Scanner; public class Threeincreasingnumbers { public static void main(String[] args) {// Create Scanner Scanner scan = new Scanner(System.in); // Prompt the user to enter three numbersSystem.out.print("Enter three numbers: ");int num1 = scan.nextInt();double num2 = scan.nextDouble();long num3 = scan.nextLong(); // Display numbers in increasing orderdisplayIncreasingNumbers(num1, num2, num3);}public static void displayIncreasingNumbers(int num1, double num2, long num3) {double temp; if (num2 < num1 && num2 < num3){temp = num1;num1 = (int) (double) num2;num2 = temp;}else if (num3 < num1 && num3 < num2) {temp = num1; num1 = (int) num3;num3 = (long) temp;}if (num3 < num2) {temp = num2;num2 =…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning