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
Concept explainers
Textbook Question
Chapter 6.4, Problem 6.4.9CP
Identify and correct the errors in the following
- 1. public class Test {
- 2. public static method1(int n, m) {
- 3. n += m;
- 4. method2(3. 4 );
- 5. }
- 6.
- 7. public static int method2(int n) {
- 8. if (n > 0 ) return 1 ;
- 9. else if (n == 0 ) return 0 ;
- 10. else if (n < 0) return - 1 ;
- 11. }
- 12. }
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
C#
Method
public class Turn{public static void main(String[] args){// Different initial values will be used for testingint x = 1; int y = 1;
// Turn the arrow by 90 degrees
System.out.print("x:", y + 90);System.out.println(x);System.out.print("y: ", x - 1 );System.out.println(y);
// Turn the arrow again
System.out.print("x:", y - 1);System.out.println(x);System.out.print("y: ",x + 90);System.out.println(y);}}
public class PrintPrimes { private static boolean isDivisible (int i, int j) { if (j%i == 0) return true; else return false; } private static void printPrimes (int n) { int curPrime; // Value currently considered for primeness int numPrimes; // Number of primes found so far. boolean isPrime; // Is curPrime prime? int [] primes = new int [100]; // The list of prime numbers. // Initialize 2 into the list of primes. primes [0] = 2; numPrimes = 1; curPrime = 2; while (numPrimes < n) { curPrime++; // next number to consider ... isPrime = true; for (int i = 0; i <= numPrimes-1; i++) { // for each previous prime. if (isDivisible (primes[i], curPrime)) { // Found a divisor, curPrime is not prime. isPrime = false; break; // out of loop through primes. } } if (isPrime) { // save it! primes[numPrimes] = curPrime; numPrimes++; } } // End while // Print all the primes out. for (int i = 0; i <= numPrimes-1; i++) { System.out.println ("Prime: " + primes[i]); } } // end printPrimes public…
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...
Additional Engineering Textbook Solutions
Find more solutions based on key concepts
(Separating the Digits in an Integer) Write an application that inputs one number consisting of five digits fro...
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
The ____________ is always transparent.
Web Development and Design Foundations with HTML5 (8th Edition)
What is the difference between the names defined in an ML let construct from the variables declared in a C bloc...
Concepts Of Programming Languages
In Exercises 49 through 54, find the value of the given function.
Introduction To Programming Using Visual Basic (11th Edition)
A file that data is written to is known as a(n) a. input file b. output file c. sequential access file d. binar...
Starting Out with Programming Logic and Design (5th Edition) (What's New in Computer Science)
Code an SQL statement that creates a table with all columns from the parent and child tables in your answer to ...
Database Concepts (8th Edition)
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
- Find the error of the following method definition: public static void timesTwo( int x, int y) { int sum=x + y; return sum; }arrow_forwardpublic static void printIt(int value) { if(value 0) { } System.out.println("Play a paladin"); System.out.println("Play a white mage"); }else { } }else if (value < 15) { System.out.println("Play a black mage"); System.out.println("Play a monk"); } else { public static void main(String[] args) { printit (5); printit (10); printit (15); Play a black mage Play a white mage (nothing/they are all printed) Play a paladin Play a monkarrow_forwardAnalyze the following code. public class Test { public static void main(String[] args){ System.out.printlin( m(2) ); public static int m( int num ) { return num; } public static void m( int num ) { System.out.printin( num ); } The program has a compile error because the second m method is def method. The program runs and prints 2 once.arrow_forward
- Urgent Answer plzzarrow_forwardpublic class Errors{public static void main (String[] args){System.out.println("Welcome to my first program!\n")String ageStr = "24 years";int age = Integer.parseInt(ageStr);System.out.println("I'm " + age + " years old.");int three = "3";int answerYears = age + three;System.out.println("Total number of years: " + answerYears);int answerMonths = answerYears * 12; System.out.println("In 3 years and 6 months, I'll be " + answerMonths + " months old");//Once you've corrected all the errors, the answer should be 330.}}arrow_forwardJava true or false: The method public boolean simpleMethod() { return true; } is a valid mutator?arrow_forward
- Javaarrow_forwardFollow these steps: Open the Java file Errors.java. Attempt to compile the program. You'll notice the compilation will fail due to some errors. Fix all the compilation errors and compile the program again. Every time you fix an error, add a comment to the line you fixed and indicate which of the three types of errors it was. Now run the program. Once again, you'll encounter some errors (this time, runtime errors). Fix these errors and run the program once again to see if it works. Now run the program and notice the output. You'll notice that there are some logical errors in the code. Fix these errors and run the program once again to see if it now correctly displays what it should.arrow_forwardpublic class CallStack{ // Called by func1() void func2 (){ System.out.println("In func2 method"); int a = 0; int b; b = 10 / a; } //Called by Main void func1(){ System.out.println("In func1 method"); this.func2 (); System.out.println("Back in func1 method"); } public static void main (String args[]){ CallStack myCallStack; myCallStack = new CallStack(); System.out.println("In the main method"); try { myCallStack.func1 (); }catch (ArithmeticException e) { System.out.println("Can't divide by Zero!"); } } } Examine the code to determine what it does. Compile and execute the code. Modify the main() method to handle the exception that is propagated to it. Use a try- catch block to display a meaningful error message when the exception occurs. Test your code. Notice that, although the exception was thrown in func2, it is caught by the catch block in the main method.arrow_forward
- class Param3 { public int y; public void method1(int y) { y = y * 3; } public int getY() { return y; } } Param3 q3 = new Param3(); q3.y = 100; q3.method1(25); System.out.println(q3.getY()); what is the output for the system.out.println method and whyarrow_forwardIdentify the following from the given java program. | class test { public static int square(int num) 5. Method name: return num * num; } public static void main(String[] args) { int result; result = square(10); System.out.printIn("Squared value of 10 is: "+ result); Method call: Parameter: } Return statement:arrow_forward11arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageEBK JAVA PROGRAMMINGComputer ScienceISBN:9781305480537Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781305480537
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Java Math Library; Author: Alex Lee;https://www.youtube.com/watch?v=ufegX5o8uc4;License: Standard YouTube License, CC-BY