Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
11th Edition
ISBN: 9780134670942
Author: Y. Daniel Liang
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Textbook Question
Chapter 9.5, Problem 9.5.6CP
What is wrong in the following code?
- 1. class Test {
- 2. public static void main (String[] args) {
- 3. A a= new A();
- 4. a. print();
- 5. }
- 6. }
- 7.
- 8. class A {
- 9. String s;
- 10.
- 11. A(String newS)
- 12. s = newS;
- 13. }
- 14.
- 15. public void print() {
- 16. System.out.print(s);
- 17. }
- 18. }
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
4. What will be the output of following Java program?
class Test{ public static void main (String[] args) { int arr1[] = {1, 2, 3}; int arr2[] = {1, 2, 3}; if (arr1.equals(arr2)) System.out.println("Same"); else System.out.println("Not same"); }}
What will happen when you compile and run the following code? #1. public class Test{
public static void main(String[] args){
int a = 10, b = 0;
while( a + 2 < 10 || b - 2 > 0){
System.out.println(a);
}
}
}
#2. public class Test{
public static void main(String[] args){
int a = 0, b = 10;
while(b - 40 > 0){
b -= 2;
while(a + 12 < 10){
a += 2;
if(a == b)
continue;
System.out.print(a + " " + b + ", ");
}
}
}
}
show the output and solution, its java language
3. What will be the output of the following Java Program?
class Test { public static void main(String args[]) { int arr[] = new int[2]; System.out.println(arr[0]); System.out.println(arr[1]); }}
Chapter 9 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Ch. 9.3 - Prob. 9.3.1CPCh. 9.3 - How do you define a class?Ch. 9.3 - How do you declare an objects reference variable?Ch. 9.3 - How do you create an object?Ch. 9.4 - Prob. 9.4.5CPCh. 9.4 - When will a class have a default constructor?Ch. 9.5 - Which operator is used to access a data field or...Ch. 9.5 - What is an anonymous object?Ch. 9.5 - Prob. 9.5.3CPCh. 9.5 - Is an array an object or a primitive-type value?...
Ch. 9.5 - Prob. 9.5.5CPCh. 9.5 - What is wrong in the following code? 1. class Test...Ch. 9.5 - Prob. 9.5.7CPCh. 9.6 - Prob. 9.6.1CPCh. 9.6 - Prob. 9.6.2CPCh. 9.6 - Which packages contain the classes Date, Random,...Ch. 9.7 - Suppose the class F is defined in (a). Let f be an...Ch. 9.7 - Prob. 9.7.2CPCh. 9.7 - Can you invoke an instance method or reference an...Ch. 9.9 - Prob. 9.9.1CPCh. 9.9 - Prob. 9.9.2CPCh. 9.9 - In the following code, radius is private in the...Ch. 9.10 - Prob. 9.10.1CPCh. 9.10 - Show the output of the following program: public...Ch. 9.10 - Show the output of the following code:Ch. 9.10 - Prob. 9.10.4CPCh. 9.11 - What is wrong in the following code? 1public class...Ch. 9.12 - If a class contains only private data fields and...Ch. 9.12 - If all the data fields in a class are private and...Ch. 9.12 - Is the following class immutable? public class A {...Ch. 9.13 - What is the output of the following program?...Ch. 9.14 - Prob. 9.14.2CPCh. 9.14 - Prob. 9.14.3CPCh. 9 - (The Rectangle class) Following the example of the...Ch. 9 - (The Stock class) Following the example of the...Ch. 9 - (Use the Date class) Write a program that creates...Ch. 9 - Prob. 9.4PECh. 9 - (Use the GregorianCa1endar class) Java API has the...Ch. 9 - (Stopwatch) Design a class named StopWatch. The...Ch. 9 - (The Account class) Design a class named Account...Ch. 9 - (The Fan class) Design a class named Fan to...Ch. 9 - (Geometry: n-sided regular polygon) In an n-sided...Ch. 9 - Prob. 9.10PECh. 9 - (Algebra: 2 2linear equations) Design a class...Ch. 9 - (Geometry: intersecting point) Suppose two line...Ch. 9 - (The Location class) Design a class named Location...
Additional Engineering Textbook Solutions
Find more solutions based on key concepts
If a class has this method, it is called automatically just before an instance of the class is destroyed by the...
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
What is the purpose of an abstract class?
Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
Given that y=ax3+7, which of the following are correct Java statements for this equations? int y = a x x (x +...
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
What will be the final value of dblResult in the following sequence? Dim dblResult As Double = 3.5 dblResult +=...
Starting Out With Visual Basic (7th Edition)
Write an if statement that displays the word digit if the char variable ch contains a numeric digit. Otherwise,...
Starting Out with Java: From Control Structures through Objects (6th Edition)
Suppose the class SportsCar is a derived class of a class Automobile. Suppose also that the class Automobile ha...
Java: An Introduction to Problem Solving and Programming (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
- What is the printout of the following code? public class Test { public static void main(string[] args) { int[][] matrix = {{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}, {12, 13, 14, 15}}; for (int i = 0; i < 4; i++) System.out.print(matrix[1][i] + " "); }}arrow_forwardQuestion.13. What will be the output of the following Java program? class box { int width; int height; int length; } class mainclass { public static void main(String args[]) { box obj = new box(); obj.width = 10; obj.height = 5; obj.length = 10; int y = obj.width * obj.height * obj.length; System.out.print(y); } }arrow_forward1. What will be the output of the following Java Program? // file name: Main.java public class Main { public static void main(String args[]) { int arr[] = {10, 20, 30, 40, 50}; for(int i=0; i < arr.length; i++) { System.out.print(" " + arr[i]); } }}arrow_forward
- Complete the sumOfDiagonals method in SumOfDiagonals.java to do the following public class SumOfDiagonals { public static void main(String[] args) { String[][] s1 = {{"2", "3", "4"}, {"5", "6", "7"}, {"8", "9", "10"}}; String[][] s2 = {{"1", "2", "3"}, {"5", "6", "7"}, {"9", "10", "11"}, {"13", "14", "15"}}; String[][] s3 = {{"1", "2", "3", "4"}, {"5", "6", "7", "8"}, {"9", "10", "11", "12"}, {"13", "14", "15", "16"}}; try { sumOfDiagonals(s1); sumOfDiagonals(s2); sumOfDiagonals(s3); } catch(NumberFormatException ex) { System.out.println("Not a valid integer"); } catch(IllegalArgumentException ex) { System.out.println("Array not a square"); } } public static void…arrow_forwardPROGRAM SIMULATION. Understand the given JAVA program and write the output. b. public class ChangeIt { public void doIt( int[] z ) { z[0] = 0; } } public class TestIt { public static void main ( String[] args ) { int[] myArray = {1, 2, 3, 4, 5} ; ChangeIt.doIt(myArray ); for (int j=0; j<myArray.length; j++ ) System.out.print( myArray[j] + " " ) ; } }arrow_forwardChange Java code below so that it outputs a 9x9 board and not a 3x3 board. import java.util.*; public class Tic_Tac_Toe{ static String[] board;static String turn;static String checkWinner(){for (int a = 0; a < 8; a++) {String line = null; switch (a) {case 0:line = board[0] + board[1] + board[2];break;case 1:line = board[3] + board[4] + board[5];break;case 2:line = board[6] + board[7] + board[8];break;case 3:line = board[0] + board[3] + board[6];break;case 4:line = board[1] + board[4] + board[7];break;case 5:line = board[2] + board[5] + board[8];break;case 6:line = board[0] + board[4] + board[8];break;case 7:line = board[2] + board[4] + board[6];break;}//For X winnerif (line.equals("XXX")) {return "X";} // For O winnerelse if (line.equals("OOO")) {return "O";}} for (int a = 0; a < 9; a++) {if (Arrays.asList(board).contains(String.valueOf(a + 1))) {break;}else if (a == 8) {return "draw";}} // To enter the X Or O at the exact place on board.System.out.println(turn + "'s turn; enter…arrow_forward
- Write the Java program: Make sure to have proper code indentation. Code: class Main { public static void main(String[] args) { Tile t1, t2, t3, t4, t5; int totalValue; t1 = new Tile('H', 4); t2 = new Tile('E', 1); t3 = new Tile('L', 1); t4 = new Tile('L', 1); t5 = new Tile('O', 1); totalValue = t1.getValue() + t2.getValue() + t3.getValue() + t4.getValue() + t5.getValue(); System.out.print("The word "); System.out.print(t1.getLetter()); System.out.print(t2.getLetter()); System.out.print(t3.getLetter()); System.out.print(t4.getLetter()); System.out.print(t5.getLetter()); System.out.println(" is worth " + totalValue + " points"); if(t4.equals(t3)) { System.out.print("It has two of these: "); System.out.println(t4); } else { System.out.print("Error: " + t3.toString() + " and ");System.out.println(t4.toString() + " aren't equal."); } }} /* SAMPLE OUTPUT The word HELLO is worth 8 pointsIt…arrow_forwardFill with appropriate java keywords/syntaxes in the following method overloading program. package analyze; class { method (double X, double Y, double Z){ return X * Y * Z; } double method (double M, double ) { return M / N; } public class Analyze { public static void main(String[] args) { Find fi = Find(); System.out.println( Method 1 : "+fi.method(2, 4, 6)); System.out.println("Method 2 : "+fi.method(3, 6)); } }arrow_forwardWhy the given Java code is not getting compiled and giving error? Remove the errors as well. class Main { public static void main(String args[]) { int A[2]; System.out.println(A[0]); System.out.println(A1]); }}arrow_forward
- Getting a error message and I dont understand why public class DebugTwo2 { public static void main(String args[]) { { int a, b; a = 7; b = 4; System.out.println("The sum is " + (a + b); System.out.println("The difference is " + (a - b); System.out.println("The product is " + (a * b); }arrow_forwardShow the output of the following programs. (Hint: Draw a table and list the variables in the columns to trace these programs.) (java language)(a)public class Test {public static void main(String[] args) {for (int i = 1; i < 5; i++) {int j = 0;while (j < i) {System.out.print(j + " ");j++;}}}} (b)public class Test {public static void main(String[] args) {int i = 0;while (i < 5) {for (int j = i; j > 1; j--)System.out.print(j + " ");System.out.println("****");i++;}}} (c)public class Test {public static void main(String[] args) {int i = 5;while (i >= 1) {int num = 1;for (int j = 1; j <= i; j++) {System.out.print(num + "xxx");num *= 2;} System.out.println();i--;}}} (d)public class Test {public static void main(String[] args) {int i = 1;do {int num = 1;for (int j = 1; j <= i; j++) {System.out.print(num + "G");num += 2;} System.out.println();i++;} while (i <= 5);}}arrow_forwardAnalyze the following code.public class Test { public static void main(String[] args) { System.out.println(m(2)); } public static int m(int num) { return num; } public static void m(int num) { System.out.println(num); }} What can you say about the above code? Does it run? Does it compile? Any output?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
Java random numbers; Author: Bro code;https://www.youtube.com/watch?v=VMZLPl16P5c;License: Standard YouTube License, CC-BY