Data Structures and Algorithms in Java
6th Edition
ISBN: 9781118771334
Author: Michael T. Goodrich
Publisher: WILEY
expand_more
expand_more
format_list_bulleted
Concept explainers
Textbook Question
Chapter 1, Problem 25C
Modify the CreditCard class to add a toString() method that returns a String representation of the card (rather than printing it to the console, as done by printSummary). Modify the main method from Code Fragment 1.6 accordingly to use the standard println command.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Create the compareTo method for a class that represents an individual whose first and last names are stored as two Strings. In an alphabetical list of people with last names first and then first names, one individual is "less than" another if they come before the other.(as is typical).
Can you implement the Student class using the concepts of encapsulation? A solution is placed in the "solution" section to help you, but we would suggest you try to solve it on your own first.
You are given a Student class in the editor. Your task is to add two fields:
● String name
● String rollNumber
and provide getter/setters for these fields:
● getName
● setName
● getRollNumber ● setRollNumber
Implement this class according to the rules of encapsulation.
Input #
Checking all fields and getters/setters
Output #
Expecting perfectly defined fields and getter/setters.
There is no need to add constructors in this class.
Add a toString() method to Fraction class that returns the fraction as aString in the form "x / y", where x and y are numerator and denominatorrespectively. As the method does not do any display itself, the output can be done by a client program that calls the method in an output statement. Use client program to test this functionality; i.e. provide an output statement to display a fraction as its String representation.
class Fraction2{private int n, d;public Fraction(){this.n = this.d = 0; //Initialize the values}public Fraction(int n, int d){this.n = n; //Initialize the variablesthis.d = d;}//Define the getter function getNum() that returns the numeratorpublic int getNum(){//Returns numeratorreturn n;}
//Define the getter function getDen() that returns the denominatorpublic int getDen(){//Returns denominatorreturn d;}//Define the boolean function isZero() that returns 0 if numerator is 0 and denominator is not equals to zeropublic boolean isZero(){return(getNum() == 0 &&…
Chapter 1 Solutions
Data Structures and Algorithms in Java
Ch. 1 - Prob. 1RCh. 1 - Suppose that we create an array A of GameEntry...Ch. 1 - Write a short Java method, isMultiple, that takes...Ch. 1 - Write a short Java method, isEven, that takes an...Ch. 1 - Write a short Java method that takes an integer n...Ch. 1 - Write a short Java method that takes an integer n...Ch. 1 - Write a short Java method that takes an integer n...Ch. 1 - Write a short Java method that counts the number...Ch. 1 - Prob. 9RCh. 1 - Prob. 10R
Ch. 1 - Modify the CreditCard class from Code Fragment 1.5...Ch. 1 - Prob. 12RCh. 1 - Modify the declaration of the first for loop in...Ch. 1 - Prob. 14CCh. 1 - Write a pseudocode description of a method for...Ch. 1 - Write a short program that takes as input three...Ch. 1 - Write a short Java method that takes an array of...Ch. 1 - Prob. 18CCh. 1 - Write a Java program that can take a positive...Ch. 1 - Write a Java method that takes an array of float...Ch. 1 - Write a Java method that takes an array containing...Ch. 1 - Prob. 22CCh. 1 - Write a short Java program that takes two arrays a...Ch. 1 - Modify the CreditCard class from Code Fragment 1.5...Ch. 1 - Modify the CreditCard class to add a toString()...Ch. 1 - Write a short Java program that takes all the...Ch. 1 - Write a Java program that can simulate a simple...Ch. 1 - A common punishment for school children is to...Ch. 1 - The birthday paradox says that the probability...Ch. 1 - (For those who know Java graphical user interface...
Additional Engineering Textbook Solutions
Find more solutions based on key concepts
(Multiples of 2 with an Infinite Loop) Write an application that keeps displaying in the command window the mul...
Java How To Program (Early Objects)
True or False: A static member method may refer to non-static member variables of the same class, but only afte...
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Explain the important differences between application security engineering and infrastructure security engineer...
Software Engineering (10th Edition)
E-Mail Address Book Create an application with a class named PersonEntry. The PersonEntry class should have pro...
Starting out with Visual C# (4th Edition)
A common memory matching game played by young children is to start with a deck of cards that contains identical...
Problem Solving with C++ (9th Edition)
In the following exercises, write a program to carry out the task. The program should use variables for each of...
Introduction to Programming Using Visual Basic (10th 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
- Create a class MapTester (either in your current project or in a new project). In it, use a HashMap to implement a contacts list similar to the one in the example above. (Remember that you must import java.util.HashMap.) In this class, implement two methods: public void enterNumber(String name, String number) and public String lookupNumber(String name) The methods should use the put and get methods of the HashMap class to implement their functionality.arrow_forwardThis is Java project plz solve it thanks!arrow_forwardalso need help Write a second constructor that takes a String array of pages as input and sets the String array instance variable equal to the input. Continue to default the page number to zero. Part 2: Write a getter and a setter method for the current page number variable. The setter should check to make sure that the input is a valid page number and only update the variable if the new value is valid. Part 3: Write a getCurrentPage method that returns the String of the current page indexed by current_page. public class Ebook{ private String[] pages; private int current_page; //constructor public Ebook() { this.pages = {"See Spot.", "See Spot run.", "Run, Spot, run."}; this.current_page = 0; }}arrow_forward
- . Download the program from the instructor’s GitHub account (Ch3HW) then update it tocreate a class Student that includes four properties: an id (type Integer), a name (type String), a Major (type String) and a Grade (type Double). Use class Student to create objects (using buttonAdd) that will be read from the TextFields then save it intoan ArrayList. Perform the following queries on the ArrayList of Student objects and show the results on the listViewStudents (Hint: add buttons as needed):a) Use lambdas and streams to sort the Student objects by name, then show the results.b) Use lambdas and streams to map each Student to its name and grade, sort the results by grade (descending), then display the results.c) Use lambdas and streams to map each Student to its name and grade, to select the Students who have grade values in the range 80 to 90. Sort the results by grade value(descending), then show the results.d) Use lambdas and streams to calculate the total average of all Students…arrow_forwardImplement a method called removeDuplicates and write the method signature (header) and the method body. Determine the proper signature by output below. You do not need to declare the containing class. You can assume it is contained in a class called MyMethods. For this method, you can only use the length°, charAtO, substring. and concat° methods of the String class. The removeDuplicates method creates and returns a new string that contains all unique letters from the string that is passed in (inString). Output is attached in the image.arrow_forwardWrite a method that takes two string parameters, and tells whether the first is a substring of the second. You can't use framework methods that do this for you, such as indexOf(). In other words, you have to write the loops yourself. But, you can use the primitive methods such as charAt(). Also analyze the program's performance and state the big-O complexity of your method. Provide a screenshot of the code working.arrow_forward
- Redefine the equals method of the class Date (Display 4.13) so that it hasa parameter of type Object and follows the other guidelines we gave for anequals method. (Remember, you should use getClass(), not instanceof.)arrow_forwardWrite a class that would match a string. The class has method match with String as a return type. It accepts two inputs: the phrase/sentence string (text) and the pattern string (word). This method finds the first (or all) instances of the pattern in the text and changes that word in all uppercase and return the new phrase. Method countOccurence accepts a phrase/sentence and a pattern string and returns its number of occurrences. Add a main method that will allow the user to input the phrase/sentence and pattern. Call method match and countOccurence. See test case below. Sample Input: Text string: You will always have my love, my love, for the love I love is as lovely as love itself. Pattern string: love Output: New text: You will always have my LOVE, my LOVE, for the LOVE I LOVE is as lovely as LOVE itself. Number of occurrence: 5 For example: Input Result You will always have my love, my love, for the love I love is as lovely as love itself. love New text: You will…arrow_forwardAn obvious continuation is to use random numbers to create namesfor the stars and planets. Instead of creating random strings of characters, names usually follow certain rules. Select a set of real-world names (e.g. from J.R.R. Tolkien’sworld) and invent a set of rules that they follow. Design and implement a methodthat creates new names based on the set of rules and random numbers.arrow_forward
- Write the method stringSplosion().* * The method takes one parameter, non-empty String str (such as "Code") and* returns a String in the form "CCoCodCode". Notice that this includes the* first character of the original String, followed by the first two characters,* and so on until the whole String is used.* * Examples: stringSplosion("Code") returns "CCoCodCode" stringSplosion("abc")* returns "aababc" stringSplosion("x") returns "x"* * @param str the input String to process.*, @return a new String as described above. Use for loopsarrow_forwardPlease help me create a cave class for a Hunt the Wumpus game (in java). You can read the rules in it's entirety of the Hunt the Wumpus game online to get a better idea of the specifications. It's an actual game. INFORMATION: The object of this game is to find and kill the Wumpus within a minimum number of moves, before you get exhausted or run out of arrows. There is only one way to win: you must discover which room the Wumpus is hiding in and kill it by shooting an arrow into that room from an adjacent room. The Cave The Wumpus lives in a cave of 30 rooms. The rooms are hexagonal. Each room has up to 3 tunnels, allowing access to 1, 2 or 3 (out of 6) adjacent rooms. The attached diagram shows the layout of rooms in the cave. The map wraps around such that rooms on the edges (white cells) have neighbors from the opposite edge (blue cells). E.g., the neighbors of room 1 are rooms 25, 26, 2, 7, 6, and 30, and you could choose to connect room 1 to any of these rooms. Observe how…arrow_forwardYou can just provide code as you don't have required utility class, Please provide explanation and comments for code : Write a static method asciiGraph(), to display a “graph” of ASCII values of a String. 2.The method has a single String parameter str, and returns nothing (void).3.The method loops through all characters in str, and for each displays a line of *s on a row equal to the ASCII/Unicode integer value of the character being examined (see example).Hints:1. recall the String methods .charAt() and .length(), and casting a char with (int) directly returns the int ASCII/Unicode value2. to produce the line of *s, call the method starLine() from the Exam utility class3. in the output capture document, consider reducing the font size to 7 or 8 point to have each row of *s on a single line Consider the strings “Ab C” and “TED 99”, that are composed of:A displays 65 stars (*), since Unicode 65 = 'A'b 98 stars (*), since Unicode 98 = 'b'space 32 * , since Unicode 32 = ' ' (the space)C…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 Math Library; Author: Alex Lee;https://www.youtube.com/watch?v=ufegX5o8uc4;License: Standard YouTube License, CC-BY