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 11.9, Problem 11.9.4CP
What is wrong in the following code?
1 public class Test {
2 public static void main(String[] args) {
3 Object fruit = new Fruit();
4 Object apple = (Apple)fruit;
5 }
6 }
7
7 class Apple extends Fruit {
8 }
10
9 class Fruit {
10 }
11
12
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
The following is in Java:
public class MyClass
{
private int x;
private double y;
public static void setValues(int a, double b)
{
x = a;
y = b;
}
}
1) What is the problem?
2) In a few sentences how can the issue be fixed?
What is the output of the following code?
1: class Arthropod2: public void printName(double input) { System.out.print("Arthropod"); }3: }4: public class Spider extends Arthropod {5: public void printName(int input) { System.out.print("Spider"); }6: public static void main(String[] args) {7: Spider spider = new Spider();8: spider.printName(4);9: spider.printName(9.0);10: }11: }
A. SpiderArthropodB. ArthropodSpiderC. SpiderSpiderD. ArthropodArthropodE. The code will not compile because of line 5.F. The code will not compile because of line 9.
Please explain how this code works each line.
Answer the given question with a proper explanation and step-by-step solution.
public class MainClass{ public static void main(String[] args) { SomeClass myObject = new SomeClass(4, 5);
int fred = SomeClass.SOME_VALUE; int barney = myObject.method1(); int wilma = SomeClass.method2(4); }}
Which of the following is a static variable
a
method1
b
method2
c
SomeClass
d
SOME_VALUE
e
This cannot be determined by examining the above code
Chapter 11 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Ch. 11.2 - True or false? A subclass is subset of a...Ch. 11.2 - What keyword do you use to define a subclass?Ch. 11.2 - What is single inheritance? What is multiple...Ch. 11.3 - What is the output of running the class C in (a)?...Ch. 11.3 - How does a subclass invoke its superclasss...Ch. 11.3 - True or false? When invoking a constructor from a...Ch. 11.4 - True or false? You can override a private method...Ch. 11.4 - True or false? You can override a static method...Ch. 11.4 - How do you explicitly invoke a superclasss...Ch. 11.4 - How do you invoke an overridden superclass method...
Ch. 11.5 - Identify the problems in the following code:...Ch. 11.5 - Prob. 11.5.2CPCh. 11.5 - If a method in a subclass has the same signature...Ch. 11.5 - If a method in a subclass has the same signature...Ch. 11.5 - If a method in a subclass has the same name as a...Ch. 11.5 - Prob. 11.5.6CPCh. 11.7 - Prob. 11.7.1CPCh. 11.8 - Prob. 11.8.1CPCh. 11.8 - Prob. 11.8.2CPCh. 11.8 - Can you assign new int[50], new Integer [50], new...Ch. 11.8 - Prob. 11.8.4CPCh. 11.8 - Show the output of the following code:Ch. 11.8 - Show the output of following program: 1public...Ch. 11.8 - Show the output of following program: public class...Ch. 11.9 - Indicate true or false for the following...Ch. 11.9 - For the GeometricObject and Circle classes in...Ch. 11.9 - Suppose Fruit, Apple, Orange, GoldenDelicious, and...Ch. 11.9 - What is wrong in the following code? 1public class...Ch. 11.10 - Prob. 11.10.1CPCh. 11.11 - Prob. 11.11.1CPCh. 11.11 - Prob. 11.11.2CPCh. 11.11 - Prob. 11.11.3CPCh. 11.11 - Prob. 11.11.4CPCh. 11.11 - Prob. 11.11.5CPCh. 11.12 - Correct errors in the following statements: int[]...Ch. 11.12 - Correct errors in the following statements: int[]...Ch. 11.13 - Prob. 11.13.1CPCh. 11.14 - What modifier should you use on a class so a class...Ch. 11.14 - Prob. 11.14.2CPCh. 11.14 - In the following code, the classes A and B are in...Ch. 11.14 - In the following code, the classes A and B are in...Ch. 11.15 - Prob. 11.15.1CPCh. 11.15 - Indicate true or false for the following...Ch. 11 - Sections 11.211.4 11.1(The Triangle class) Design...Ch. 11 - (Subclasses of Account) In Programming Exercise...Ch. 11 - (Maximum element in ArrayList) Write the following...Ch. 11 - Prob. 11.5PECh. 11 - (Use ArrayList) Write a program that creates an...Ch. 11 - (Shuffle ArrayList) Write the following method...Ch. 11 - (New Account class) An Account class was specified...Ch. 11 - (Largest rows and columns) Write a program that...Ch. 11 - Prob. 11.10PECh. 11 - (Sort ArrayList) Write the following method that...Ch. 11 - (Sum ArrayList) Write the following method that...Ch. 11 - (Remove duplicates) Write a method that removes...Ch. 11 - (Combine two lists) Write a method that returns...Ch. 11 - (Area of a convex polygon) A polygon is convex if...Ch. 11 - Prob. 11.16PECh. 11 - (Algebra: perfect square) Write a program that...Ch. 11 - (ArrayList of Character) Write a method that...Ch. 11 - (Bin packing using first fit) The bin packing...
Additional Engineering Textbook Solutions
Find more solutions based on key concepts
Rewrite the temperature conversion program of Section 1.2 to use a function for conversion.
C Programming Language
T F: A named constants value can be changed by a programming statement while the program is running.
Starting Out With Visual Basic (7th Edition)
When describing a system, explain why you may have to start the design of the system architecture before the re...
Software Engineering (10th Edition)
When displaying a Java applet, the browser invokes the _____ to interpret the bytecode into the appropriate mac...
Web Development and Design Foundations with HTML5 (9th Edition) (What's New in Computer Science)
On the planet Gzyx, a ball dropped from a height of 20 ft hits the ground in 2 s. If a ball is dropped from the...
Differential Equations: Computing and Modeling (5th Edition), Edwards, Penney & Calvis
Population Data If you have downloaded this books source code (the companion Web site is available at www.pears...
Starting Out with Java: From Control Structures through Objects (6th 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 output of the following code? Include a description of what happens. class Main { public static void main(String[] args) { SecondClass myObject = new SecondClass(93,7); System.out.println(myObject); } } class FirstClass { private int num; public FirstClass(int num) { this.num = num; System.out.println("This is a statement in the FirstClass constructor"); } public String toString() { return String.format("In an object of type FirstClass: num = %d", num); } } class SecondClass extends FirstClass { private int num; public SecondClass(int a, int b) { super(a); this.num = b; System.out.println("This is a statement in the SecondClass constructor."); } public String toString() { return String.format("%s%nIn an object of type SecondClass: num = %d", super.toString(), num); } }arrow_forwardI have the following code in java, replit: //First class public class Main { public static void main(String[] args) { Scanner console = new Scanner(System.in); Random random = new Random(); Game game = new Game(console, random); game.startGame(); console.close(); }} //Second class import java.util.Scanner; public class Player { private String name; private int totalGames; private int totalGuesses; private int bestGame; public Player() { this.name = null; this.totalGames = 0; this.totalGuesses = 0; this.bestGame = Integer.MAX_VALUE; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getTotalGames() { return totalGames; } public void setTotalGames(int totalGames) { this.totalGames = totalGames; } public int getTotalGuesses() { return totalGuesses; } public void setTotalGuesses(int totalGuesses) {…arrow_forwardConsider the following class. 1 public class Duck { 2 private String name; 3 private int age; 4 5 public Duck(String name, int age) { 6 this.name = name; 7 this.age = age; 8 } 9 10 public Duck(String name) { 11 this.name = name; 12 this.age = 10; 13 } 14 15 public void setNewName(String name) { 16 this.name = name; 17 } 18 19 ... 20 } Write the values of the name and age fields of both duck1 and duck2 objects after having executed the following code snippet: 1 Duck duck1 = new Duck("Oncle Scrooge"); 2 Duck duck2 = new Duck("Huey", 20); 3 duck1.setNewName("Dewey");arrow_forward
- What is the output of the following code? Explain each line of the console output in yourown words. (HINT: if there are errors, please explain why the errors occur)import java.util.ArrayList;public class Pet {public void goToVet(){System.out.println("My Pet needs to go to Vet ...");}public static void main(String args[]){Hamster pug = new Hamster();Pet sally = new Pet();Pet loki = new Hamster();Object sunny = new Object();Object joy = new Pet();Object mini = new Hamster();ArrayList<Object> myList = new ArrayList<>(); myList.add(pug); myList.add(sally);myList.add(loki);myList.add(sunny);myList.add(joy);myList.add(mini);myList.forEach(i -> {if (i instanceof Hamster) {System.out.println("My Pet is cute: so " + (((Hamster) i).cute())+ "! ");((Hamster) i).goToVet();}else if (i instanceof Pet)((Pet) i).goToVet();elseSystem.out.println("This is not a Pet");});}}class Hamster extends Pet {public void goToVet(){System.out.println("My Hamster needs to go to Vet…arrow_forwardWhat is the output of the following code? Explain each line of the console output in yourown words. (HINT: if there are errors, please explain why the errors occur) import java.util.ArrayList;public class Pet {public void goToVet(){System.out.println("My Pet needs to go to Vet ...");}public static void main(String args[]){Hamster pug = new Hamster();Pet sally = new Pet();Pet loki = new Hamster();Object sunny = new Object();Object joy = new Pet();Object mini = new Hamster();ArrayList<Object> myList = new ArrayList<>(); myList.add(pug); myList.add(sally);myList.add(loki);myList.add(sunny);myList.add(joy);myList.add(mini);myList.forEach(i -> {if (i instanceof Hamster) {System.out.println("My Pet is cute: so " + (((Hamster) i).cute())+ "! ");((Hamster) i).goToVet();}else if (i instanceof Pet)((Pet) i).goToVet();elseSystem.out.println("This is not a Pet");});}}class Hamster extends Pet {public void goToVet(){System.out.println("My Hamster needs to go to Vet…arrow_forwardThis is my question and this is the code in lab 2The 1's are replaced with an E public class 1xam { private int day; private int month; private int year; private int hour; private int minutes; private double maxMarks; private final String type; private final int order; private static int lab1xamCounter = 0; private static int midterm1xamCounter = 0; public static final String LAB_1XAM = "Lab 1xam"; public static final String MIDTERM_1XAM = "Midterm 1xam"; public static final String FINAL_1XAM = "Final 1xam"; public 1xam(String type) { this (type, 0, 0, 0, 0, 0, 0); } public 1xam(String type, double maxMarks) { this (type, maxMarks, 0, 0, 0, 0, 0); } public 1xam(String type, double maxMarks, int day, int month, int year) { this (type, maxMarks, day, month, year, 0, 0); } public 1xam(1xam other) { this (other.getType(), other.getMaxMarks(), other.getDay(), other.getMonth(), other.getYear(), other.getHour(), other.getMinutes()); } public 1xam(String type, double maxMarks,…arrow_forward
- 4- Consider the following program:public class ClassA{public void dothat(ClassB c1, ClassC c2){//method content goes here}}public class ClassB extends ClassA{}public class ClassC extends ClassB{}public class Main{public static void main(String[]args){ClassA c1 = new ClassA();ClassB c2 = new ClassB();ClassC c3 = new ClassC();}}Which of the following is a correct call to dothat? Why?a. c1.dothat(c1,c1);b. c2.dothat(c2,c2);c. c3.dothat(c1,c1);d. c2.dothat(c3,c2);e. c3.dothat(c3,c3);arrow_forwardbox1 and box2 are instances of the Box class. Attributes length, width, and height of both box1 and box2 are read from input. Then, two strings are read from input. In the Box class, define instance method print_measurements() with self and one string as parameters to output the string parameter in one line, and the following in the next line: The value of attribute length ' by ' The value of attribute width ' by ' The value of attribute heightarrow_forwardPlease help debug this: Debugging Exercise 4-3, Farrell, Joyce, Java Programming, 9th edition, Cengage Learning public class DebugBox { private int width; private int length; private int height; private FixDebugBox() { length = 1; width = 1; height = 1; } public DebugBox(int width, int length, int height) { width = width; length = length; height = height; } public void showData() { System.out.println("Width: " + width + " Length: " + length + " Height: " + height); } public double getVolume() { double vol = length - width - height; return vol; } }arrow_forward
- How can I write the repeating code just once in Python class subclass inheritance? For example there is a code that the same self._date = date is repeating for every class, please write down the possible ways to not repeat the same line of code that influences for all classes. class Item(): def __init__(self, date):self._date = date class Book(Item):def __init__(self, date):self._date = date class Pen(Item): def __init__(self, date):self._date = datearrow_forward(this is question1) public abstract class Shape { public Shape() { } public abstract double area() ; public String who() { return "Shape" ; } } public class Circle extends Shape { private int radius ; public Circle( int r ) { radius = r ; } @Override public double area() { return Math.PI * radius * radius ; } } public class Square extends Shape { private int _sideLength ; public Square( int sideLength ) { /* TODO question 45 */ } @Override public double area() { return _sideLength * _sideLength ; } } What is the output of the following code (approximate answer ok if needed)? Assume your constructor from question 1 above has been implemented and the following code is contained within a driver class. public static void main(String[] args) { report(new Circle(4)); report(new Square(5)); } public static void…arrow_forwardWhat is the out put of the following code ? explain your answer ? class access { public int x; static int y; void cal(int a, int b) { x += a ; y += b; } } public class A { public static void main(String args[]) { access obj1 = new access(); access obj2 = new access(); obj1.x = 0; obj1.y = 0; obj1.cal(1, 2); obj2.x = 0; obj2.cal(2, 3); System.out.println(obj1.x + " " + obj2.y); } }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
6 Stages of UI Design; Author: DesignerUp;https://www.youtube.com/watch?v=_6Tl2_eM0DE;License: Standard Youtube License