Java: An Introduction to Problem Solving and Programming (8th Edition)
8th Edition
ISBN: 9780134462035
Author: Walter Savitch
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Expert Solution & Answer
Chapter 6.4, Problem 34STQ
Explanation of Solution
Program:
Given code:
//Define the method "convertValue()"
public static int convertValue(double number)
{
//Initialize the variable
int result = 0;
//Check the condition
if(number > 0.0)
/*True, assign the value to the variable "result"*/
result = (int)number;
//Return the value
return result;
}
//Define the method "convertValue()"
public static double convertValue(int number)
{
//Initialize the variable
double result = 0;
//Check the condition
if(number > 0...
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
mport java.util.Scanner;
public class ParkingCharges {
// function to calculate the basic charge using the asked hours static double getBasicCharge(int hours) { if (hours >= 7 && hours <= 8) return 5.50; else if (hours >= 5 && hours <= 6) return 4.50; else if (hours >= 2 && hours <= 4) return 4.00; return 3.00; }
// function to return the amount to subtract based on local living and OAP static double getDiscount(String isLocal, String isOAP) { if (isOAP.equals("Yes") && isLocal.equals("Yes")) return 2.0 + 1.0; else if (isOAP.equals("Yes")) return 2.0; else if (isLocal.equals("Yes")) return 1.0; return 0; }
public static void main(String[] args) { // create a new Scanner object Scanner sc = new Scanner(System.in);
// prompt the user to ask if they are disabled…
7 public static int test1(int x1, int x2){ 8 9 if(x1==0){ 10 x1=10; 11 } 12 if(x2==0){ 13 x2=10 14 } 15 16 return x1*x2; 17 } 18 19 public static int test2(int x2, int x1){ 20 return x2-x1; 21 } 22 public static int input(){ //Don’t need to trace through
//this method to show how it works. Just assume it pulls
//the value for the user. 23 Scanner keyboard = new Scanner(new BufferedReader(new InputStreamReader(System.in))); 24 int counter=0; 25 System.out.print("Please enter an integer: "); 26 while(counter<3 && !keyboard.hasNextInt()) 27 { 28 System.out.println(); 29 String temp=keyboard.next(); 30 System.out.println(temp+" is not an integer. Please reenter an integer."); 31 counter++; 32 33 } 34 return keyboard.nextInt(); 35 } 36 37 38…
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 prime
Chapter 6 Solutions
Java: An Introduction to Problem Solving and Programming (8th Edition)
Ch. 6.1 - If a class is named Student, what name can you use...Ch. 6.1 - When defining a constructor, what do you specify...Ch. 6.1 - What is a default constructor?Ch. 6.1 - Does every class in Java automatically have a...Ch. 6.1 - In the program PetDemo shown in Listing 6 2, you...Ch. 6.2 - Prob. 6STQCh. 6.2 - Can a class contain both instance variables and...Ch. 6.2 - Can you reference a static variable by name within...Ch. 6.2 - Can you reference an instance variable by name...Ch. 6.2 - Can you reference a static variable by name within...
Ch. 6.2 - Can you reference an instance variable by name...Ch. 6.2 - Is the following valid, given the class...Ch. 6.2 - Prob. 13STQCh. 6.2 - Prob. 14STQCh. 6.2 - Prob. 15STQCh. 6.2 - Is the following valid, given the class...Ch. 6.2 - What values are returned by each of the following?...Ch. 6.2 - Suppose that speed is a variable of type double...Ch. 6.2 - Repeat the previous question, but instead assign...Ch. 6.2 - Suppose that nl is of type int and n2 is of type...Ch. 6.2 - Define a class CircleCalculator that hat only two...Ch. 6.2 - Which of the following statements are legal?...Ch. 6.2 - Write a Java expression to convert the number in...Ch. 6.2 - Consider the variable 5 of type String that...Ch. 6.2 - Repeat the previous question, but accommodate a...Ch. 6.2 - Write Java code to display the largest and...Ch. 6.3 - Prob. 27STQCh. 6.3 - Consider the variable allCents in the method...Ch. 6.3 - What is wrong with a program that starts as...Ch. 6.3 - Prob. 30STQCh. 6.3 - In your definition of the class OutputFormat. In...Ch. 6.4 - Prob. 32STQCh. 6.4 - Prob. 33STQCh. 6.4 - Prob. 34STQCh. 6.4 - Consider the class Species in Listing 5.19 of...Ch. 6.4 - Repeat the previous question for a method...Ch. 6.4 - Still considering the class Species in Listing...Ch. 6.4 - Rewrite the method add in Listing 6.16 so that it...Ch. 6.4 - In Listing 6.16, the set method that has a String...Ch. 6.5 - Give the definitions of three accessor methods...Ch. 6.6 - If cardSuit is an instance of Suit and is assigned...Ch. 6.7 - Suppose you want to use classes in the package...Ch. 6.7 - Prob. 43STQCh. 6.7 - Can a package have any name you might want, or are...Ch. 6.7 - On your system, place the class Pet (Listing 6.1)...Ch. 6.8 - The previous section showed you how to change the...Ch. 6 - Prob. 1ECh. 6 - Prob. 2ECh. 6 - Write a default constructor and a second...Ch. 6 - Write a constructor for the class...Ch. 6 - Consider a class characteristic that will be used...Ch. 6 - Create a class RoomOccupancy that can be used to...Ch. 6 - Write a program that tests the class RoomOccupancy...Ch. 6 - Sometimes we would like a class that has just a...Ch. 6 - Create a program that tests the class Merlin...Ch. 6 - In the previous chapter, Self-Test Question 16...Ch. 6 - Create a class Android whose objects have unique...Ch. 6 - Prob. 12ECh. 6 - Modify the definition of the class Species in...Ch. 6 - Prob. 2PCh. 6 - Using the class Pet from Listing 6.1, write a...Ch. 6 - Do Practice Program 4 from Chapter 5 except define...Ch. 6 - The following class displays a disclaimer every...Ch. 6 - Do Practice Program 5 from Chapter 5 but add a...Ch. 6 - We can improve the Beer class from the previous...Ch. 6 - Define a utility class for displaying values of...Ch. 6 - Write a new class TruncatedDollarFormat that is...Ch. 6 - Complete and fully test the class Time that...Ch. 6 - Complete and fully test the class Characteristic...Ch. 6 - Write a Java enumeration LetterGrade that...Ch. 6 - Complete and fully test the class Per n that...Ch. 6 - Write a Temperature class that represents...Ch. 6 - Repeat Programming Project 8 of the previous...Ch. 6 - Write and fully test a class that represents...Ch. 6 - Write a program that will record the votes for one...Ch. 6 - Repeat Programming Project 10 from Chapter 5, but...Ch. 6 - Create a JavaFX application that displays a button...
Knowledge Booster
Similar questions
- 42) Look at the following method: public static int test2 (int x, int y) if (x < y) { return -5; } else { return (test2 (x - y, y + 5) + 6) ; } } What is returned for test2 (10, 20)? A) -5 B) 1 C) 6 D) 10 43) This term is used for methods that directly call themselves. A) Simple recursion C) Absolute recursion B) Direct recursion D) Native recursion 44) If the base case in a recursive method is never reached: A) The result will always be off by one. C) The method will call itself indefinitely. B) The method will call itself only once. D) The method will never call itself. 45) A(n) is an object that is generated in memory as the result of an error or an unexpected event. A) exception C) default exception handler B) exception handler D) error message 46) All of the exceptions that you will handle are instances of classes that extend this class. A) Exception C) IOException B) RunTimeException D) Error 47) All exceptions are instances of dasses that extend this class. A) Throwable C)…arrow_forward(3) public static void test_b(int n) { if (n>0) test_b(n-2); System.out.println(n + " "); Consider the following method: What is printed by the call test_b(4)? A. 0 2 4 B. 0 2 C. 2 4 D. 4 2 E. 4 20 3 (4) What is the efficacy class of +? n 3 A: ©(1) B: O (log n) C: O (n) D: O (n log n) E: Θ n)arrow_forwardbasic java please Write a void method named emptyBox()that accepts two parameters: the first parameter should be the height of a box to be drawn, and the second parameter will be the width. The method should then draw an empty box of the correct size. For example, the call emptyBox(8, 5) should produce the following output: ***** * * * * * * * * * * * * ***** The width of the box is 5. The middle lines have 3 spaces in the middle. The height of the box is 8. The middle columns have 6 spacesarrow_forward
- use only C# programming Write a method call CubeIt(int x, ref int cube) that takes two arguments and does not return a value. The method will cube the first argument and assign it to the second argument.In your main, call this method twice and print the value of the parameters after each method call. Write a method with the following header: static void CalculateTuitionFee(int numberOfCourses, double costPerCourse, ref double fees). This method will calculate and assign the required fees amount to the third argument. [Fees = number of courses * cost per course + 15.25].From your program Main() method, call the CalculateTuitionFee () method four times supplying different arguments each time and display the value of the third argument after each method call. Write a method that takes four parameter of type int. The method will assign the sum of the first two arguments to the third and the difference of the first two to the fourth. This method should be coded so that the calling…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_forward//Todo write test cases for SimpleCalculator Class // No need to implement the actual Calculator class just write Test cases as per TDD. // you need to just write test cases no mocking // test should cover all methods from calculator and all scenarios, so a minimum of 5 test // 1 for add, 1 for subtract, 1 for multiply, 2 for divide (1 for normal division, 1 for division by 0) // make sure all these test cases fail public class CalculatorTest { //Declare variable here private Calculator calculator; //Add before each here //write test cases here }arrow_forward
- Method 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_forwardT/F 5) The following two methods will both compute the same thing when invoked with the same value of x. That is,method1(x) = = method2(x).public int method1(int x){if(x > 0) return method1(x – 1) + 1;else return 0;}public int method2(int x){if(x > 0) return 1 + method2(x – 1);else return 0;}arrow_forwardIn C++ class rectangleType { public: void setLengthWidth(double x, double y); //Sets the length = x; width = y; void print() const; //Output length and width double area(); //Calculate and return the area of the rectangle (length*width) double perimeter(); //Calculate and return the perimeter (length of outside boundary of the rectangle) private: double length; double width; }; Declare an instance of rectangleType.arrow_forward
- In C++ class rectangleType { public: void setLengthWidth(double x, double y); //Sets the length = x; width = y; void print() const; //Output length and width double area(); //Calculate and return the area of the rectangle (length*width) double perimeter(); //Calculate and return the perimeter (length of outside boundary of the rectangle) private: double length; double width; }; Set the length of the rectangle to 24 and the width to 30.arrow_forwarda) Write a boolean method called sumDivisibleByFive that returns true if the sum of the digits of its integer parameter is divisible by 5 and false otherwise. For example, if the parameter passed to the method is 73 then the sum of its digits is 10 (7+3) The method should return true since 10 is divisible by 5. b) Write a main method that calls the method sumDivisibleByFive for the values between 10 and 100 inclusive 10 per line. This should output the following numbers: 14 19 23 28 32 3741 46 50 55 64 69 73 78 82 87 91 96arrow_forwardJAVA PROGRAM SEE ATTACHED PHOTO FOR THE PROBLEMarrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage