Concept explainers
Write a method called printDetails for a class that has a field of type String called name. The printDetai1s method should print out a string of the form "The name of this person is", followed by the value of the name field. For instance, if the value of the name field is "Helen", then printDetails would print:
The name of this person is Helen
Want to see the full answer?
Check out a sample textbook solutionChapter 2 Solutions
Objects First with Java: A Practical Introduction Using BlueJ (6th Edition)
Additional Engineering Textbook Solutions
Starting Out with C++ from Control Structures to Objects (8th Edition)
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Database Concepts (7th Edition)
C How to Program (8th Edition)
Problem Solving with C++ (9th Edition)
Starting Out with C++: Early Objects (9th Edition)
- In Java, Use the Phone class as a baseline, and write a method isUSA. It should return true if the country code is 1 and false otherwise. Also expanding the Phone class, write a method that determines the location of an area code. It should return the name of the area code's city. For example, if cc = 1 and area code = 510 East Bay Area, 650 SF Peninsula, 415 San Francisco, 408 South Bay Area. For this assignment, use literal strings as return values.arrow_forwardWrite a Java method named check() that checks the validity of a password received as string parameter. To be valid, a password should contain at least: 3 letters, 2 digits, and 1 character from {‘-’, ‘_’}. If the password is valid the method returns true, otherwise it returns false. To test your method, write a main() method that enables the user to input a password as a string, removes eventual extra spaces from it, sends it to check(), and displays the returned decision (the password is valid or not). Hints: To check if a character c is a digit you can use Character.isDigit(c), and to check if it is a letter you can use Character.isLetter(c). Run Example 1: Enter a password to check (at least 2 digits, 3 letters, and one char (- or _)): p5t4_i The password is valid. Run Example 2: Enter a password to check (at least 2 digits, 3 letters, and one char (- or _)): i0z#-9k The password is not valid.arrow_forwardWrite a program that simulates the Magic 8 Ball game. The class should have a method that returns an answer at random. Test your class by outputting the answer several times.arrow_forward
- Write a Java method named check() that checks the validity of a password received as string parameter. To be valid, a password should contain at least: 3 letters, 2 digits, and 1 character from {‘-’, ‘_’}. If the password is valid the method returns true, otherwise it returns false. To test your method, write a main() method that enables the user to input a password as a string, removes eventual extra spaces from it, sends it to check(), and displays the returned decision (the password is valid or not).arrow_forwardWrite a static method called larger that accepts two double parameters and returns the larger of the two values. If the values are equal, it doesn't matter which one is returned. JAVAarrow_forwardFor all methods in each class (labeled), how do I write the JUnit 4 test(s) to test all methods? Full code is shown below: PunctuationNode.java (class) public class PunctuationNode implements Sentence { private String punctuation; private Sentence rest; /** * This is a constructor for class PunctuationNode. * * @param punctuation - a String. * @param rest - a String. */ public PunctuationNode(String punctuation, Sentence rest) { this.punctuation = punctuation; this.rest = rest; } /** * A method to get number of words. * * @return - a integer. */ @Override public int getNumberOfWords() { return this.rest.getNumberOfWords(); } /** * A method to get longest word. * * @return - a integer. */ @Override public String longestWord() { return this.rest.longestWord(); } /** * Returns a String that represents a sentence. * * @return - a Sentence. */ @Override public String toString() { return this.punctuation +…arrow_forward
- Write the equals method for Circle2 which returns true when the radius and the color are the same as the parameter’s radius and color. The TestCircle2, Circle2 and GeometricObject2 classes are given as follows. public class TestCircle2 { public static void main(String[] args) { Circle2 c1 = new Circle2(2.3, "red"); Circle2 c2 = new Circle2(5.6, "white"); Circle2 c3 = new Circle2(2.3, "red"); System.out.println("c1 equals c2: " + c1.equals(c2)); System.out.println("c1 equals c3: " + c1.equals(c3)); System.out.println("c2 equals c3: " + c2.equals(c3)); } } class Circle2 extends GeometricObject2 { private double radius; public Circle2() { } public Circle2(double radius) { this.radius = radius; } public Circle2(double radius, String color) { this.radius = radius; setColor(color); } /** * Return radius */ public double getRadius() {…arrow_forwardImplement a class that simulates a traffic light. The next method advances the color in the usual way, from green to yellow to red, then again to green. Provide two constructors, as described in the documentation of the public interface. Also supply a method that yields the number of times that this traffic light has been red. /** A simulated traffic light.*/public class TrafficLight{ private String color; private int reds; /** Constructs a green traffic light. */ public TrafficLight() { /* Your code goes here */ } /** Constructs a traffic light. @param initialColor the initial color "green", "yellow", or "red" */ public TrafficLight(String initialColor) { /* Your code goes here */ } /** Moves this traffic light to the next color. */ public void next() { /* Your code goes here */ } /** Returns the current color of this traffic light. @return the current color */ public String getColor() {…arrow_forwardCreate a method named multiConcat that has two parameters: a String and an integer. Give back a String that is created by concatenating the string given with itself count times, where count is the integer parameter. For instance, the return value is "hihihihi" if the argument values are "hi" and 4. In the event that the integer parameter is less than 2, give the initial string.arrow_forward
- Write a method that turns some text into something Porky Pig would say. To do that, you just need to add "Bdap bdap bb " to before the given text. For example, if you called porkyPig("that's all folks!"), it would return "Bdap bdap bb that's all folks!" The method signature should be public String porkyPig(String something)arrow_forwardSuppose that a Scanner object contains a bunch of tokens all on a single line separated by spaces. For example: 100.5 30.4 pizza 56.8 1000.3 8.2 Also suppose that you have a particular item that you know the price of. Let’s use the following example: Kindle: 99.99 Write a static method: public static double priceIsRight(Scanner s, double price) that takes a Scanner and an item price as parameters and returns the bid that is closest to the item price but not greater than the item price. If all bids are greater than the price, return 0.0. If there are no bids (Scanner is empty) return 0.0. You can assume the Scanner is not null. In the above example. 56.8 should be returned, since it is the number that is closest to 99.99 but not over 99.99. Notice that there may be some invalid tokens in the Scanner's input. The above example has pizza as one of its tokens, which is clearly not a valid bid. Your method should throw these tokens away and not consider them as bids. It is probably…arrow_forwardSuppose that a Scanner object contains a bunch of tokens all on a single line separated by spaces. For example: 100.5 30.4 pizza 56.8 1000.3 8.2 Also suppose that you have a particular item that you know the price of. Let’s use the following example: Kindle: 99.99 Write a static method: public static double priceIsRight(Scanner s, double price) that takes a Scanner and an item price as parameters and returns the bid that is closest to the item price but not greater than the item price. If all bids are greater than the price, return 0.0. If there are no bids (Scanner is empty) return 0.0. You can assume the Scanner is not null. In the above example. 56.8 should be returned, since it is the number that is closest to 99.99 but not over 99.99. Notice that there may be some invalid tokens in the Scanner's input. The above example has pizza as one of its tokens, which is clearly not a valid bid. Your method should throw these tokens away and not consider them as bids. It is probably…arrow_forward
- 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