Concept explainers
Consider the variable allCents in the method writePositive in Listing 6.14. It holds an amount of money as if it were all cents. So for the amount $12.95, the int variable allCents would be set to 1295. Using an int variable is somewhat limiting. The largest value of type int is 2147483647, meaning that the largest amount the method can handle a $21,474,836.47. That is more than $21 million, a nice large sum, but we often need to consider larger amounts, such as the national budget or even the salary for the CEO of a large company. How can you easily change the definitions of the methods in the class DollarFormat so that they handle larger amounts of money?
Want to see the full answer?
Check out a sample textbook solutionChapter 6 Solutions
Java: An Introduction to Problem Solving and Programming (8th Edition)
Additional Engineering Textbook Solutions
Programming in C
Starting Out with C++: Early Objects (9th Edition)
Problem Solving with C++ (9th Edition)
Starting Out with Java: From Control Structures through Objects (6th Edition)
C++ How to Program (10th Edition)
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
- Write a Java program that contains two static methods:Method 1 called FtoC( ) that converts a Fahrenheit degree to its equivalent Celsius degree using the formula C=5/9(F-32) where F is Fahrenheit degree and C represents Celsius degree. For example: F=70 is C=5/9(70-32)=21.1. Your program asks the user to enter temperature in Fahrenheit degree and then displays its equivalent in Celsius.arrow_forwardDefine a Static method 'max' with return type as 'int' in class 'ProblemSolution' that take two numbers with type 'int'. Inside the 'max' method, compare the two numbers and return the maximum of the two numbers. Then, inside the 'main' method. Define two 'int' type numbers; the first number (num1) is equal to 5 and the second number (num2) is equal to 2. At the next line, call the 'max' method where its parameter input are the two numbers above, and assign the result of the method to the variable num3 (type 'int'). Finally, on the next line, print out -> "The maximum of " + num1 + " and " + num2 + " is " + num3 Output The maximum of 5 and 2 is 5 Where, There must be space between all the words and numbers. There should not any space after the last numberarrow_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).arrow_forward
- Write a Java program that will 1. Ask the user to type in a sentence, using a JOptionPane.showInputDialog(). 2. The program will examine each letter in the string and count how many time the upper-case letter 'E' appears, and how many times the lower-case letter 'e' appears. The key here is to use the charAt method in class String. 3. Using a JOptionPane.showMessageDialog(), tell the user how many upper and lower case e's were in the string. 4. Repeat this process until the user types the word "Stop". (Check out the method equalsIgnoreCase in class String to cover all upper/lower case possibilities of the word "STOP"arrow_forwardFor this problem you will write a method that computes the number of boxes in a pyramid of boxes based on the number of boxes in the base. The boxes in each layer of the pyramid are stacked directly on top of one another (i.e. not offset between two boxes below). For example, below are shown two pyramids, one with base 5 and one with base 6. [] [][] [][][] [][][][] [][][][][] [][][][][][] Implement and test the computePyramidBoxes() method in the PyramidBoxCount class. When testing the computePyramidBoxes() method you should be sure to have at least one test method for each base case and at least one test method for each recursive case in your program.arrow_forwardDefine a method findAnnualMembership() that takes two integer parameters as a person's number of museum visits and the person's age, and returns the person's museum membership fee as an integer. The membership fee is returned as follows: Visits < 7 years 7 - 70 years > 70 years < 6 75 80 70 6 - 12 45 55 40 > 12 30 35 25 Ex: If the input is 5 6, then the output is: 75 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import java.util.Scanner; public class MembershipFinder { /* Your code goes here */ publicstaticvoidmain(String[] args) { Scannerscnr=newScanner(System.in); intmuseumVisits; intuserAge; museumVisits=scnr.nextInt(); userAge=scnr.nextInt(); System.out.println(findAnnualMembership(museumVisits, userAge)); } }arrow_forward
- Program is in Java. Using methods as a base point, write a password generator that will generate a password of a given length. The password generated must contain least one uppercase letter, one lowercase letter, and one digit, all random. In addition, add a method that will make the generated password of at least a set minimum length and no longer then a maximum length. Have another method to ensure that the indicated required characters show up in random positions of the generated password as well.arrow_forwardFor this problem you will write a method that prints out a pyramid. The boxes in each layer of the pyramid are stacked directly on top of one another (i.e. not offset between two boxes below). For example, below are shown two pyramids, one with base 5 and one with base 6. [] [][] [][][] [][][][] [][][][][] [][][][][][]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_forward
- 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_forward. Embed your method in a complete program that requests for three variables all of the types int and displays the conditions explained in “b” above.arrow_forwardAssume that we want to swap the values of two integer -variables, a andb. To do this, we write a method public void swap(int i1, int i2){int tmp = i1; i1 = i2; i2 = tmp;}Then we call this method with our a and b variables:swap(a, b);Are a and b swapped after this call? If you test it, you will notice that they are not! Why does this not work? Explain in detail.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