Java: Intro. to Prob. Solv... (Looseleaf)
7th Edition
ISBN: 9780133841084
Author: SAVITCH
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Expert Solution & Answer
Chapter 5.3, Problem 31STQ
Explanation of Solution
Reasons for displaying the given output:
- • From the given code of “ExerciseProgram”
- ○ The variable “s1” is the object of “Species” class.
- ○ The variable “mysteryMaker” is the object of “ExerciseClass” class.
- ○ The statement “int n = 0;” implies initializes the variable “n” to “0”.
- ○ The statement “s1.setSpecies("Hobbit", 100, 2);” implies call the method “setSpecies” for object “s1” with arguments of “Hobbit” for name, “100” for population and “2” for growth rate.
- ■ Now “s1” contains “Hobbit” for name, “100” for population and “2” for growth rate.
- ○ The statement “mysteryMaker.mystery(s1, n)” implies call the method “mystery” for object “mysteryMaker” with arguments of “s1” for “Species” class and “n”.
- ■ In the “ExerciseClass” class, the method “mystery” implies
- • Call the method “setSpecies” for object “s” with arguments of “Klingon ox” for name, “10” for population and “15” for growth rate...
- ■ In the “ExerciseClass” class, the method “mystery” implies
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
please i need add interface to my code please
// Java Program
interface University { public void add(int id, String name); } class Person { String name; int id; Person(String name, int id) { this.name = name; this.id = id; } } class Student extends Person { String Specialization; double gpa; Student(String name, int id, String Specialization, double gpa) { super(name, id); this.Specialization = Specialization; this.gpa = gpa; } } class Employee extends Person { String jop; int salary; Employee(String name, int id, String jop, int salary) { super(name, id); this.jop = jop; this.salary = salary; } }
public class Main { public static void main(String[] args) { // creating the Objects of sub classes Student s = new Student("Ritika", 20, "CSE", 7.5); Employee e = new…
Modify the given codes to remove the compilation errors.
1. public interface Carnivore {
int pieces = 10;
public static void eatGrass();
public int chew() {
return 13;
}
}
2. Interface HasWings {
public int getNumberOfWings();
}
abstract class Insect implements HasWings {
abstract int getNumberOfLegs();
}
public class Dragonfly extends Insect {
int getNumberOfLegs() {
return 6;
}
}
what is the output of the following codes :
code 1.
class A
{
{
System.out.print(1);
}
}
class B extends A
{
{
System.out.print(2);
}
}
class C extends B
{
{
System.out.print(3);
}
}
public class MainClass
{
public static void main(String[] args)
{
C c = new C();
}
}
code 2
class M
{
int i;
public M(int i)
{
this.i = i--;
}
}
class N extends M
{
public N(int i)
{
super(++i);
System.out.println(i);
}
}
public class MainClass
{
public static void main(String[] args)
{
N n = new N(26);
}
}
code 3
class M
{
int i;
public M(int i)
{
this.i = i--;
}
}
class N extends M
{
public N(int i)
{
super(++i);
System.out.println(i);
}
}
public class MainClass
{
public static void main(String[] args)
{
N n = new N(26);
}
}
Chapter 5 Solutions
Java: Intro. to Prob. Solv... (Looseleaf)
Ch. 5.1 - Consider the program in Listing 5.4 . Suppose you...Ch. 5.1 - Prob. 2STQCh. 5.1 - Prob. 3STQCh. 5.1 - Suppose every species in the world has a peculiar...Ch. 5.1 - Revise the definition of the method writeOutput in...Ch. 5.1 - Revise the definition of the method readInput in...Ch. 5.1 - Revise the definition of the method...Ch. 5.1 - What is the meaning of (int) that appears in the...Ch. 5.1 - Prob. 9STQCh. 5.1 - Define a method called getDensity that could be...
Ch. 5.1 - Define a method called changePopulation that could...Ch. 5.1 - Define a method called changePopulation that could...Ch. 5.2 - In Listing 5.12, we set the data for the object...Ch. 5.2 - Give preconditions and postconditions for the...Ch. 5.2 - What is an accessor method? What is a mutator...Ch. 5.2 - Give the complete definition of a class called...Ch. 5.2 - Prob. 17STQCh. 5.2 - In the definition of the method in Listing 5.15,...Ch. 5.2 - What is a well-encapsulated class definition?Ch. 5.2 - When should an instance variable in a class...Ch. 5.2 - Prob. 21STQCh. 5.2 - In a class definition, is anything private ever...Ch. 5.2 - In a class definition, is the body of any method...Ch. 5.3 - What is a reference type? Are class types...Ch. 5.3 - When comparing two quantities of a class type to...Ch. 5.3 - Prob. 26STQCh. 5.3 - Write a method definition for a method called...Ch. 5.3 - Given the class Species as defined in Listing...Ch. 5.3 - After correcting the program in the previous...Ch. 5.3 - What is the biggest difference between a parameter...Ch. 5.3 - Prob. 31STQCh. 5.3 - Write an equals method for the class Person...Ch. 5.4 - Prob. 33STQCh. 5.4 - Prob. 34STQCh. 5.4 - Prob. 35STQCh. 5.4 - Prob. 36STQCh. 5.4 - Prob. 37STQCh. 5 - Design a class to represent a credit card. Think...Ch. 5 - Repeat Exercise 1 for a credit card account...Ch. 5 - Repeat Exercise 1 for a coin instead of a credit...Ch. 5 - Repeat Exercise 1 for a collection of coins...Ch. 5 - Consider a Java class that you could use to get an...Ch. 5 - Consider a class that keeps track of the sales of...Ch. 5 - Consider a class MotorBoat that represents...Ch. 5 - Prob. 8ECh. 5 - Prob. 9ECh. 5 - Prob. 10ECh. 5 - Write a program to answer questions like the...Ch. 5 - Define a class called Counter. An object of this...Ch. 5 - Prob. 3PCh. 5 - Define a Trivia class that contains information...Ch. 5 - Define a Beer class that contains the following...Ch. 5 - Write a grading program for an instructor whose...Ch. 5 - Add methods to the Person class from Self-Test...Ch. 5 - Create a class that represents a grade...Ch. 5 - Write a program that uses the Purchase class in...Ch. 5 - Write a program to answer questions like the...Ch. 5 - Consider a class that could be used to play a game...Ch. 5 - Consider a class BasketballGame that represents...Ch. 5 - Consider a class ConcertPromoter that records the...Ch. 5 - Prob. 9PPCh. 5 - Consider a class Movie that contains information...Ch. 5 - Repeat Programming Project 18 from Chapter 4, but...Ch. 5 - Prob. 12PP
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
- An arithmetic progression is a sequence of numbers such that the difference between the consecutive terms is constant. For instance, the sequence 5, 7, 9, 11, 13, 15 is an arithmetic progression with a common difference of 2.Given the class IntNumber:1 public class IntNumber2 {3 public final int value;45 public IntNumber(int number) {6 this.value = number;7 }8 }where value is an immutable field, that can be directly accessed (public), implement a class called ArithmeticProgressionthat implements the following methods:•1 public static IntNumber getArithmeticProgressionDifference(ArrayList<IntNumber> list)which returns the difference between terms of the arithmetic progression (as an object of type IntNumber) if thevalues of the IntNumber objects are an arithmetic progression. The method returns null in any other case.•1 public static void printIsArithmeticProgression(ArrayList<IntNumber> list)which prints on the console:The list is an arithmetic progression with difference =…arrow_forwardTrace and document every step of execution of the following program: public class Trace { public static void main(String[] args) { int[] num = {1, 3, 6, 9, 12 }; for (int i = 4; i >= 0; i--) { num[i] +=1; } num[0] *=num[4]; } }arrow_forwardAn arithmetic progression is a sequence of numbers such that the difference between the consecutive terms is constant. For instance, the sequence 5, 7, 9, 11, 13, 15 is an arithmetic progression with a common difference of 2. Given the class IntNumber: 1. public class IntNumber 2. { 3. public final int value; 4. 5. public IntNumber(int number) { 6. this.value = number; 7. } 8. } where value is an immutable field, that can be directly accessed (public), implement a class called ArithmeticProgression that implements the following methods: •1 public static IntNumber getArithmeticProgressionDifference(ArrayList< IntNumber> list) which returns the difference between terms of the arithmetic progression (as an object of type IntNumber) if the values of the IntNumber objects are an arithmetic progression. The method returns null in any other case. • public static void printIsArithmeticProgression(ArrayList<IntNumber> list ) which prints on the console: The list is an arithmetic…arrow_forward
- 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?arrow_forwardConsider the following code in Java 8:public interface Runnable{void run() ;}public final class Caller {public static void call (String before, String after, Runnable r) (System.out.println(before);r. run() ;System.out.println(after);} }public class MyClass{public static void main(String args[]) (Caller.call("BEFORE", "AFTER",); } }Complete the blark line with some code so that the program prints the folowing to the output: BEFOREHi there!AFTERarrow_forwardwhat is the problem with the following code. Give reason for your answer. public class MyClass\{ public static void main(String[] args) \{ int n[]={1,3,5,6} ; int b[]; b=n; \} \}arrow_forward
- Exercise 1 Consider the following definition of the class MyClass: public class MyClass { private static int count = 0; private int x; public MyClass(int i) { x = i; } public void incrementCount() { count++; } public void printX() { System.out.println("Value of x : " + x); } public static void printCount() { System.out.println("Value of count : " + count); } } public class MyClassDemo { public static void main(String[] args) { MyClass myObject1 = new MyClass(5); MyClass myObject2 = new MyClass(7); } } What is the output of the following Java code? (Assume that following statements are written inside main) myObject1.printX(); myObject1.incrementCount(); MyClass.incrementCount(); myObject1.printCount(); myObject2.printCount(); myObject2.printX(); myObject1.setX(14); myObject1.incrementCount();…arrow_forwardUse Netbeans to run the following decorator pattern code. Provide screenshot to prove that the code runs successfully. Design a UML class diagram to model the program and fully explain the class diagram. SpaceshipDetails.java: public interface SpaceshipDetails {void name(String name);void size(String size);void passengerCabin(int load, int unload);void laserGun(int c);void rangeFinder(int d);void hyperDriver(int dist);} EbonHawk.java: public class EbonHawk implements SpaceshipDetails {@Overridepublic void name(String name) {System.out.println("The name of the ship is: " + name);}@Overridepublic void size(String size) {System.out.println("The size of the ship Ebon Hawk is: " + size);}@Overridepublic void passengerCabin(int load, int unload) {System.out.println("Number of passengers to be loaded: " + load);System.out.println("Number of passengers to be unloaded: " + unload);}@Overridepublic void laserGun(int c) {if (c == 1) {System.out.println("The spaceship has a gun");}else…arrow_forwardImplement the following tasks by using concepts of object-oriented programming (Java). i. Create a generic class with a type parameter that simulates drawing an item at random out of a box. This class could be used for simulating a random drawing. For example, the box might contain Strings representing names written on a slip of paper, or the box might contain Integers representing a random drawing for a lottery based on numeric lottery picks. Create an add method that allows the user of the class to add an object of the specified type along with an isEmpty method that determines whether or not the box is empty. Finally, your class should have a drawItem method that randomly selects an object from the box and returns it. If the user attempts to drawn an item out of an empty box, return null. Write a main method that tests your class.ii. The following is a short snippet of code that simulates rolling a 6-sided dice 100 times. There is an equal chance of rolling any digit from 1 to…arrow_forward
- What is the output of the following code? public interface finalExam { int x = 10; void finalMethod(); void finalMethod1(); } public abstract class AbstractExam implements finalExam { int x = 11; String name; public AbstractExam(String aname) { this.aname = aname; } public void finalMethod1() { System.out.print("Abstract"); } public void finalMethod() { System.out.print("Abstract1"); } } public class MyClass extends AbstractExam { int x = 1; String name; public MyClass(String sname, String aname) { super(aname); this.sname = sname; } public void finalMethod1() { System.out.print("MyClass"); } } public class Test66 { public static void main(String[] args) { finalExam finalexam = new MyClass("class", "abs"); finalexam.finalMethod(); finalexam.finalMethod1(); System.out.print(finalExam.x); } }arrow_forward1/ 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. 2/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 the constructor, and the initial fuel level is 0. Supply a method drive that simulates driving the car for a certain distance, reducing the amount of gasoline in the fuel tank. Also supply methods…arrow_forwardConsider the following inheritance hierarchy: class A{ protected: int x, y; public: int z; }; class B: private A{ private: int a, b, c; public: void set(int x, int y, int z, int a, int b, int c); }; Int main(){ Aobja; B objb; } questions:a) How many data members does B have? Write their names. b) How many of B’s data members are visible in B? Write down their names. c) Which members of B are accessible in main()? How will they be accessed? d) If the protected Access specifier in A is changed to public, then how many members of B will be accessed in main() and how? e) Define the function set() without changing its signature as given above. f) Write a default constructor for the class B that does not have an empty parameter list. g) If the data members of A become private, then how they be initialized? h) Add a static data member in class B. Can we use this pointer with static members? If yes how?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