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 10.2, Problem 10.4CP
Explanation of Solution
Output of the given
// Class definition
public class Sample {
// Main method
public static void main(String[] args)
{
// Create object for subclass
Sky object= new Sky();
}
// Superclass definition
public class Ground
{
// Constructor
public Ground()
{
// Display the statement
System.out.println("You are on the ground.");
}
}
// Subclass definition
public class Sky extends Ground
{
// Constructor
public Sky()
{
// Display the statement
System.out.println("You are in the sky.");
}
}
}
In the above program,
- There are two classes,
- “Ground” is the superclass and “Sky” is the subclass.
- In “Ground” superclass,
- The constructor “Ground” is created and the statement which is to be printed is given...
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Consider the following three Java classes:
public abstract class Toy {public void makeNoise() { System.out.println("DEFAULT SOUND"); }} public class Doll extends Toy {public void makeNoise() { System.out.println("Mama!"); }}public class ToyRobot extends Toy {private int volume; public ToyRobot() { this.volume = 5; } public void makeNoise() { System.out.println("BEEP"); } public int getVolume() { return volume; } public void setVolume(int volume) { this.volume = volume; }}
Given these classes, determine what will be printed when each of the code snippets below are copied into the following main method at the indicated location.
public static void main(String[] args) { Toy toy; Doll doll; ToyRobot robot1; ToyRobot robot2; doll = new Doll(); robot1 = new ToyRobot(); robot2 = robot1; toy = robot1; // INSERT CODE HERE. }
A)
doll.makeNoise();
["", "", "", ""]
B)
toy.makeNoise();
["", "", "", ""]
C)…
There is a class hierarchy that includes three classes:
class Animal { protected int age; public int getAge() { return age; } public void setAge(int age) { this.age = age; }}class Pet extends Animal { protected String name; public String getName() { return name; } public void setName(String name) { this.name = name; }}class Cat extends Pet { protected String color; public String getColor() { return color; } public void setColor(String color) { this.color = color; }}
Given the following object:
Pet cat = new Cat();
Select all invalid method invocations.
**Python code**
Write a class definition line and a one line docstring for the class Dog. Write an __init__ method for the class Dog that gives each dog its own name and breed. Test this on a successful creation of a Dog object.>>> import dog>>> sugar = dog.Dog('Sugar', 'border collie')>>> sugar.name'Sugar'>>> sugar.breed'border collie'
Chapter 10 Solutions
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
Ch. 10.1 - Here is the first line of a class declaration....Ch. 10.1 - Look at the following class declarations and...Ch. 10.1 - Class B extends class A. (Class A is the...Ch. 10.2 - Prob. 10.4CPCh. 10.2 - Look at the following classes: public class Ground...Ch. 10.3 - Under what circumstances would a subclass need to...Ch. 10.3 - How can a subclass method call an overridden...Ch. 10.3 - If a method in a subclass has the same signature...Ch. 10.3 - If a method in a subclass has the same name as a...Ch. 10.3 - Prob. 10.10CP
Ch. 10.4 - When a class member is declared as protected, what...Ch. 10.4 - What is the difference between private members and...Ch. 10.4 - Why should you avoid making class members...Ch. 10.4 - Prob. 10.14CPCh. 10.4 - Why is it easy to give package access to a class...Ch. 10.6 - Look at the following class definition: public...Ch. 10.6 - When you create a class, it automatically has a...Ch. 10.7 - Recall the Rectangle and Cube classes discussed...Ch. 10.8 - Prob. 10.19CPCh. 10.8 - If a subclass extends a superclass with an...Ch. 10.8 - What is the purpose of an abstract class?Ch. 10.8 - If a class is defined as abstract, what can you...Ch. 10.9 - Prob. 10.23CPCh. 10.9 - Prob. 10.24CPCh. 10.9 - Prob. 10.25CPCh. 10.9 - Prob. 10.26CPCh. 10.9 - Prob. 10.27CPCh. 10.9 - Prob. 10.28CPCh. 10 - In an inheritance relationship, this is the...Ch. 10 - In an inheritance relationship, this is the...Ch. 10 - This key word indicates that a class inherits from...Ch. 10 - A subclass does not have access to these...Ch. 10 - This key word refers to an objects superclass. a....Ch. 10 - In a subclass constructor, a call to the...Ch. 10 - The following is an explicit call to the...Ch. 10 - A method in a subclass that has the same signature...Ch. 10 - A method in a subclass having the same name as a...Ch. 10 - These superclass members are accessible to...Ch. 10 - Prob. 11MCCh. 10 - With this type of binding, the Java Virtual...Ch. 10 - This operator can be used to determine whether a...Ch. 10 - When a class implements an interface, it must...Ch. 10 - Prob. 15MCCh. 10 - Prob. 16MCCh. 10 - Abstract classes cannot ___________. a. be used as...Ch. 10 - You use the __________ operator to define an...Ch. 10 - Prob. 19MCCh. 10 - Prob. 20MCCh. 10 - You can use a lambda expression to instantiate an...Ch. 10 - True or False: Constructors are not inherited.Ch. 10 - True or False: in a subclass, a call to the...Ch. 10 - True or False: If a subclass constructor does not...Ch. 10 - True or False: An object of a superclass can...Ch. 10 - True or False: The superclass constructor always...Ch. 10 - True or False: When a method is declared with the...Ch. 10 - True or False: A superclass has a member with...Ch. 10 - True or False: A superclass reference variable can...Ch. 10 - True or False: A subclass reference variable can...Ch. 10 - True or False: When a class contains an abstract...Ch. 10 - True or False: A class may only implement one...Ch. 10 - True or False: By default all members of an...Ch. 10 - // Superclass public class Vehicle { (Member...Ch. 10 - // Superclass public class Vehicle { private...Ch. 10 - // Superclass public class Vehicle { private...Ch. 10 - // Superclass public class Vehicle { public...Ch. 10 - Write the first line of the definition for a...Ch. 10 - Look at the following code, which is the first...Ch. 10 - Write the declaration for class B. The classs...Ch. 10 - Write the statement that calls a superclass...Ch. 10 - A superclass has the following method: public void...Ch. 10 - A superclass has the following abstract method:...Ch. 10 - Prob. 7AWCh. 10 - Prob. 8AWCh. 10 - Look at the following interface: public interface...Ch. 10 - Prob. 1SACh. 10 - A program uses two classes: Animal and Dog. Which...Ch. 10 - What is the superclass and what is the subclass in...Ch. 10 - What is the difference between a protected class...Ch. 10 - Can a subclass ever directly access the private...Ch. 10 - Which constructor is called first, that of the...Ch. 10 - What is the difference between overriding a...Ch. 10 - Prob. 8SACh. 10 - Prob. 9SACh. 10 - Prob. 10SACh. 10 - What is an. abstract class?Ch. 10 - Prob. 12SACh. 10 - When you instantiate an anonymous inner class, the...Ch. 10 - Prob. 14SACh. 10 - Prob. 15SACh. 10 - Employee and ProductionWorker Classes Design a...Ch. 10 - ShiftSupervisor Class In a particular factory, a...Ch. 10 - TeamLeader Class In a particular factory, a team...Ch. 10 - Essay Class Design an Essay class that extends the...Ch. 10 - Course Grades In a course, a teacher gives the...Ch. 10 - Analyzable Interface Modify the CourseGrades class...Ch. 10 - Person and Customer Classes Design a class named...Ch. 10 - PreferredCustomer Class A retail store has a...Ch. 10 - BankAccount and SavingsAccount Classes Design an...Ch. 10 - Ship, CruiseShip, and CargoShip Classes Design a...
Knowledge Booster
Similar questions
- WRITE THE OUTPUT OF THE PROGRAM AND WRITE THE UML DIAGRAM OF THE PROGRAM : JAVA public class TestMain { public static void main(String[] args) { Invoice check1= new Invoice(112, "Book", 3, 125.98); Invoice check2= new Invoice(101, "Phone", 2, 456.35); Invoice check3= new Invoice(187, "Laptop", 1, 2345.68); check1.displayLine(); check2.displayLine(); check3.displayLine(); } } class Invoice { private int item_number; private String name; private int quantity; private double price; private double total_cost; public Invoice(int item_num, String nm, int quan, double pri) { item_number=item_num; name=nm; quantity=quan; price=pri; total_cost = 0; } public int getitem_number(){ return item_number; } public String getname() { return name; } public int getquantity() { return quantity; } public double getprice() { return price; } public double gettotal_cost() { return total_cost; } public void total() { total_cost=price * quantity; } public void displayLine() { System.out.println("Item…arrow_forwardpublic class worksheet3_1 { public static void main(String[] arg) { ShadowingExample example = new ShadowingExample(); example.x = 99; example.sampleMethod(); }}class ShadowingExample { int x; public void sampleMethod() { int x = 0; System.out.println("the value of local variable x = " + ………………………………………………); System.out.println("the value of instance variable x = " + …………………………………………); }} what should be written in the second print statement so that the output is: the value of instance variable x = 99 what should be written in the first print statement so that the output is: the value of local variable x = 0 options are: samplemethod.x this.x shadowingexample.x x Answer 1 Question 1arrow_forwardIN JAVA Look at the following class definition: public class Dog {private int age = 0; // age is given in years, the default value is 0private String name = "Fido"; // default name is FidoDog(String firstName, int firstAge) {name = firstName;age = firstAge;if (age < 0) {age = 0;}}public void setName(String newName) {name = newName;}public int getAge() {return age;}public String getName() {return name;}public void haveBirthdayParty() {age = age + 1;System.out.println("Happy birthday " + name + "! You are now " + age + " years old!");}public void printInfo() {System.out.println("This dog's name is " + name + " and he's " + age + " years old!");}} Make a post containing your answers to all of the questions below: Can you create a new dog without specifying its name? Why or why not? Which of the Dog class's methods are accessors? Which ones are mutators? Can you use the methods provided to make a dog younger? (In other words, can you set age to a lower value than it already was?)…arrow_forward
- Given the following class. (JAVA) public class FamilyMember { public String drink( ) {return “cup”;} public String eat( ) {return “fork”;} } Write a Baby class that is derived from the FamilyMember class. The Baby class has an instance variable for the age, a default constructor, and a constructor that accepts an integer. Include any other necessary methods. If a baby is older than 3 years, print that “it eats with its fork”; otherwise, “it eats with its hands”.arrow_forwardUsing JAVA Part A) Design a class Message that models an e-mail message. A message has a recipient, a sender, and a message text. Support the following methods: 1) A constructor that takes the sender and recipient. 2) A method append that appends a line of text to the message body A method toString that makes the message into one long string like: “From: Harry Morgan%nTo: Rudolf Reindee%n . . . “ Write a program that uses this class to make a message and print it. Part B) Design a class Mailbox that stores e-mail messages, using the Message class in part a) Implement the following methods in the Message class. public void addMessage(Message m) // Adds Message M to the Mailbox public Message getMessage(int i) // returns the ith message from the Mailbox public void removeMessage(int i) // removes the ith message from the Mailbox public int mailBoxSize () // returns the number of messages in the Mailbox Write a program to demonstrate the functionality of the Mailbox. Add messages to…arrow_forwardConsider the following class definitions: public class Thing {public void string_method(String word) {word = word.substring(word.length() - 1);System.out.print(word);} }public class Thing2 extends Thing {public void string_method(String word) {super.string_method(word);System.out.print(word.substring(0,1));}} The following code segment appears in a class other than Thing or Thing2 Thing2 thing = new Thing2();thing.string_method("Tacky"); What is printed as a result of running the code segment? Group of answer choices yT yy Index Out of Bounds Error TT TackyTackyarrow_forward
- UML for the following Code: public class ProductOrder { private int numBook = 0; private int numCD = 0; private int numDVD = 0; public ProductOrder(int numBook, int numCD, int numDVD){ this.numBook = numBook; this.numCD = numCD; this.numDVD = numDVD; } public int getBook(){ return numBook; } public void setBook(int numBook){ this.numBook = numBook; } public int getCD(){ return numCD; } public void setCD(int numCD){ this.numCD = numCD; } public int getDVD(){ return numDVD; } public void setDVD(int numDVD){ this.numDVD = numDVD; } public int getProductTotal(){ return getBook() + getCD() + getDVD(); } }arrow_forward1/ Write a BankAccountTester class whose main method constructs a bank account, deposits $1,000, withdraws $500, withdraws another $400, and then prints the remaining balance. Also print the expected result 2/ Implement a class Employee. An employee has a name (a string) and a salary (a double). Provide a constructor with two arguments public Employee(String employeeName, double currentSalary) and methods public String getName() public double getSalary() public void raiseSalary(double byPercent) These methods return the name and salary, and raise the employee's salary by a certain percentage. Sample usage: Employee harry = new Employee("Hacker, Harry", 50000); harry.raiseSalary(10); // Harry gets a 10 percent raise Supply an EmployeeTester class that tests all methods. 3/Implement a class Car with the following properties. A car has a certain fuel efficiency (measured in miles/gallon or liters/km—pick one) and a certain amount of fuel in the gas tank. The efficiency is specified in…arrow_forward*using java* Create a class AnimalCreate a class Cat, and a class Dog, and a class Bearded Dragon which extend Animal. Add to your Animal class: Member: name Methods: Public Animal(String name) //constructor public void makesSound()with the implementation printing out a generic animal sound. Next, override (add) the makesSound()method to your Cat and Dog class, with the implementation specific to each animal (i.e, cat says purr..). Do not override the makesSound() for your Dragon (bearded dragons don’t make sounds!)Note, you’ll also need a constructor in each of your subclasses which calls super(name) to initialize the common ‘name’ member of Animal. --- Next (in your test harness) create a List of different Animals ( a couple cats, a dog, a dragon... ) and add these Animals to your list. Iterate through your list & call makeSound on each. ( you should observe the implementation of the makeSound() method called will be: cat -> from Cat class, dog-> from Dog class,…arrow_forward
- GIVEN //BookMain.java//This is the client code of Book class public class BookMain{ public static void main(String[] arguments){// Small test of the Book classBook example = new Book("Angels and Demons");System.out.println("Title (should be Angels and Demons): " + example.getTitle());System.out.println("Rented? (should be false): " + example.isBorrowed());example.rented(); //marks the book as rentedSystem.out.println("Rented? (should be true): " + example.isBorrowed());example.returned();//marks the book as returnedSystem.out.println("Rented? (should be false): " + example.isBorrowed());} GIVEN //Book.java//complete the missing implementation to this class public class Book{private String title;private boolean borrowed; // Creates a new Book (constructor)public Book(String bookTitle){// Implement this method} // Marks the book as rentedpublic void rented() {// Implement this method} // Marks the book as not rentedpublic void returned() {// Implement this method} // Returns true if the…arrow_forwardJava Problem. compile and run the code below: /** A class to test the Door class.*/public class DoorTester{ /** Tests the methods of the Door class @param args not used */ public static void main(String[] args) { Door frontDoor = new Door("Front", "open"); System.out.println("The front door is " + frontDoor.getState()); System.out.println("Expected: open"); }} Create a second Door object called “backDoor” with the name property “Back” and an initial state of “closed”. Verify that the object was properly created. Use the mutator to change the state of object backDoor to “open”. Verify that the mutator is working. Create a third Door object called “sideDoor” with the name property “Side” and an initial state of “open”. Verify that the object was properly created. Use the mutator to change the name of object sideDoor to “Front”. Verify that the mutator is working.arrow_forwardJava program Make a class Employee with a name and salary. Make a class Manager inherit from Employee. Add an instance variable, named department, of type String. Supply a method toString that prints the manager’s name, department, and salary. Make a class Executive inherit from Manager. Supply appropriate toString methods for all classes. Supply a test program that tests these classes and methods.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage