Java: An Introduction to Problem Solving and Programming (8th Edition)
8th Edition
ISBN: 9780134462035
Author: Walter Savitch
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Textbook Question
Chapter 5.3, Problem 28STQ
Given the class Species as defined in Listing 5.19, why does the following
public class SpeciesEqualsDemo
{
public static void main(String [] args)
{
Species s1, s2; s1;
setSpecies(“Klingon ox”, 10, 15);
s2.setSpecies(“Klingon ox”, 10, 15);
if (s1 == s2)
System.out.println(“Match with ==,”);
else
sysyem.out.println(“Do Not match with ==.”)
}
}
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Consider the following piece of code:
public static void main(String[] args)
{ BankAccount a = new BankAccount(500);
BankAccount b = new BankAccount(a.getBalance());
BankAccount c = a;
Identify all of the expressions that are true:
a)(a.equals(500)) b)(a.equals(b)) c)(a == 500) d)(a == b) e)(a == c) f)(a.getBalance() == 500) g)(a.getBalance() == b.getBalance())
For the class Employee example provided in tutorial 3.1.7, what code segment could be added to keep track of the number of job titles ever held by an employee in a variable called numJobTitles?
1- public String setJobtitle(String title){
if (title.length() > 0){
this.jobtitle = title;
this.numJobTitles += 1;
}
}
public int getNumJobtitles(){
return this.numJobTitles;
}
2- public void setJobtitle(String title){
if (title.length() > 0){
this.jobtitle = title;
this.numJobTitles += 1;
}
}
public int getNumJobtitles(){
return this.numJobTitles;
}
3- public void setJobtitle(title){
if (title.length() > 0){
this.jobtitle = title;
this.numJobTitles += 1;
}
}
public int getNumJobtitles(numJobTitles){
return this.numJobTitles;
}
4- public void setJobtitle(String title){
if (title.length() > 0){
this.jobtitle = title;
this.numJobTitles += 1;
}
}
public void getNumJobtitles(){
return this.numJobTitles;
}
1/ 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…
Chapter 5 Solutions
Java: An Introduction to Problem Solving and Programming (8th Edition)
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 - Rewrite the method drawFaceSansMouth in Listing...Ch. 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
Additional Engineering Textbook Solutions
Find more solutions based on key concepts
A negative offset causes the files read or write position to be moved ______ in the file from the position spec...
Starting Out with C++: Early Objects (9th Edition)
Let Ly=y+py+qy. Suppose that y1 and y2 are two functions such that Ly1=f(x)andLy2=g(x). Show that their sum y=y...
Differential Equations: Computing and Modeling (5th Edition), Edwards, Penney & Calvis
Write your own version of malloc and free, and compare its running time and space utilization to the version of...
Computer Systems: A Programmer's Perspective (3rd Edition)
A sentence is in disjunctive normal form (DNF) if it is the disjunction of conjunctions of literals. For exampl...
Artificial Intelligence: A Modern Approach
The following if statement determines whether choice is equal to 'Y' or 'y': if (choice == 'Y' || choice == 'y'...
Starting Out with C++ from Control Structures to Objects (9th 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? 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.arrow_forwardThe 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 class:1 public class Student {2 private int studentNumber;3 private Student labPartner;4 private static Student classPres = null;5 private static int nextStudentNumber = 100;67 public Student () {8 this.studentNumber = Student.nextStudentNumber;9 Student.nextStudentNumber ++;10 this.labPartner = null;11 }1213 public void setLabPartner (Student labPartner) {14 this.labPartner = labPartner;15 }1617 public static void setClassPres (Student pres) {18 Student.classPres = pres;19 }20 }(a) Write code that is able to construct an array of 1000 student objects and loads them in increasingstudent number order into that array. The lab partners of these students should all be null. TheclassPres should also be null.arrow_forward
- Consider 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_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_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_forward
- Consider the following class. 1 public class Example 2 { 3 private int a; 4 private String x; 5 6 public Example() 7 { 8 a = 0; 9 x = "1"; 10 } 11 12 public Example(int a, String x) 13 { 14 a = a + 10; 15 this.a = a; 16 this.x = x; 17 } 18 19 public void b() 20 { 21 a = a + 1; 22 x = x + "0"; 23 } 24 25 public void c() 26 { 27 b(); 28 a = a / 2; 29 x = x + "1"; 30 b(); 31 } 32 33 public int d() 34 { 35 return a; 36 } 37 38 public String e() 39 { 40 return x; 41 } 42 43 public int f(int a) 44 { 45 return a; 46 } 47 48 public void g() 49 { 50 System.out.println(x + " | " + a); 51 } 52 } State which lines (begin and end of line) declare 1) constructors, 2) accessors, and 3) mutators.arrow_forwardConsider the following class. 1 public class Example 2 { 3 private int a; 4 private String x; 5 6 public Example() 7 { 8 a = 0; 9 x = "1"; 10 } 11 12 public Example(int a, String x) 13 { 14 a = a + 10; 15 this.a = a; 16 this.x = x; 17 } 18 19 public void b() 20 { 21 a = a + 1; 22 x = x + "0"; 23 } 24 25 public void c() 26 { 27 b(); 28 a = a / 2; 29 x = x + "1"; 30 b(); 31 } 32 33 public int d() 34 { 35 return a; 36 } 37 38 public String e() 39 { 40 return x; 41 } 42 43 public int f(int a) 44 { 45 return a; 46 } 47 48 public void g() 49 { 50 System.out.println(x + " | " + a); 51 } 52 } State which lines (begin and end of line) declare 1) constructors, 2) Consider class Example from the previous exercise. For each object first and second, write down the value of the fields a and x or indicate that the object does not exist (e.g. using a forward slash “/”), after the execution of each of the following lines of code. 1 Example first = new Example(); 2 Example second = new Example(2, "0");…arrow_forward(Java) Q 4,5 COMPLETE BOTH STEPS Explain the answer step-by-step and include verbal explanation. Thank you! Given the below class: public class Dog { private String name; private double weight; private static int numDogs = 0; public Dog(String name, double weight) { this.name = name; this.weight = weight; numDogs++; } public String getName() { return name; } public double getWeight() { return weight; } public static int getNumDogs() { return numDogs; }public static void increaseNumDogs() {numDogs++;}public void printGreeting() { System.out.println("Woof!"); } @Override public String toString() { return "Name: " + name + "\nWeight: " + weight; }} Add a method call to getNumDogs to the below starter code: public class DogTest {public static void main(String[] args) { Dog fluffy = new Dog("Fluffy", 10); int num = //call getNumDogs method here }} Then, add a method call…arrow_forward
- 4.2.3: Method definition: Volume of a pyramid. Define a method pyramidVolume with double parameters baseLength, baseWidth, and pyramidHeight, that returns as a double the volume of a pyramid with a rectangular base. Relevant geometry equations: Volume = base area x height x 1/3Base area = base length x base width.(Watch out for integer division). import java.util.Scanner; public class CalcPyramidVolume { //answer goes here// public static void main (String [] args) { Scanner scnr = new Scanner(System.in); double userLength; double userWidth; double userHeight; userLength = scnr.nextDouble(); userWidth = scnr.nextDouble(); userHeight = scnr.nextDouble(); System.out.println("Volume: " + pyramidVolume(userLength, userWidth, userHeight)); }}arrow_forwardGiven 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_forwardWrite a java (Java 7 ) program for this :-- A new deadly virus has infected large population of a planet. A brilliant scientist has discovered a new strain of virus which can cure this disease. Vaccine produced from this virus has various strength depending on midichlorians count. A person is cured only if midichlorians count in vaccine batch is more than midichlorians count of person. A doctor receives a new set of report which contains midichlorians count of each infected patient, Practo stores all vaccine doctor has and their midichlorians count. You need to determine if doctor can save all patients with the vaccines he has. The number of vaccines and patients are equal. Input Format First line contains the number of vaccines - N. Second line contains N integers, which are strength of vaccines. Third line contains N integers, which are midichlorians count of patients. Output Format Print a single line containing ′Yes′ or ′No′. Input Constraint 1<N<10 Strength of vaccines…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