Java: An Introduction To Problem Solving And Programming Plus Mylab Programming With Pearson Etext -- Access Card Package (8th Edition)
8th Edition
ISBN: 9780134710754
Author: Walter Savitch
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Textbook Question
Chapter 5, Problem 5E
Consider a Java class that you could use to get an acceptable integer value from the user. An object of this class will have the attributes
- Minimum accepted value
- Maximum accepted value
- Prompt string
and the following method:
- getValue displays the prompt and reads a value using the class Scanner. If the value read is not within the allowed range, the method should display an error message and ask the user for a new value, repeating these actions until an acceptable value is entered. The method then returns the value read.
- a. Write preconditions and postconditions for the method getValue.
- b. Implement the class in Java.
- c. Write some Java statements that test the class.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Consider a Java class that you could use to get an acceptable integer value from the user. An object of this class will have the attributes
• Minimum accepted value
• Maximum accepted value
• Prompt string
and the following method:
• getValue displays the prompt and reads a value using the class Scanner. If the value read is not within the allowed range, the method should display an error message and ask the user for a new value, repeating these actions until an acceptable value is entered. The method then returns the value read.
a. Write preconditions and postconditions for the method getValue.
b. Implement the class in Java.
Write complete java code for scenario given:
Consider a Java class that you could use to get an acceptable integer value from the user. An object of this class will have the attributes:
Minimum accepted value
Maximum accepted value
• Prompt String and the following method:
getValue() displays the prompt and reads a value from the user. If the value read is not within the allowed range, the method should display an error message and ask the user for a new value, repeating these actions until an acceptable value is entered. The method then returns the value read.
** in JAVA **
Write a Java program called ThreeWords that prompts (asks) the userto enter three words, separated by a space, on the same line and readin using the .nextLine( ) method of the Scanner class. You may NOTuse the .next( ) method of the Scanner class in this Task. Using the .next( ) method of the Scanner class will result in this Task being awarded 0, regardless of correct execution.(You may assume that the user always enters 3 words, each word separated by a single space.)Once the user has entered the three words, the program displays to the screen the longest word, that is, the word with the most characters. In the event of one or more of the words having the same number of characters, the program may decide which word it displays to the screen.
Chapter 5 Solutions
Java: An Introduction To Problem Solving And Programming Plus Mylab Programming With Pearson Etext -- Access Card Package (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
Write a complete Java program that reads in a line of keyboard input containing two values of type int (separat...
Absolute Java (6th Edition)
Whats wrong with the following array declarations? int[] readings = new int[-1]; double[] measurements = new do...
Starting Out with Java: From Control Structures through Objects (6th Edition)
When working with a sequential access file, you can jump directly to any piece of data in the file without read...
Starting Out with Programming Logic and Design (5th Edition) (What's New in Computer Science)
Consider the following code (and assume that it is embedded in a complete and correct program and then run): ch...
Problem Solving with C++ (9th Edition)
Consider the following statement: A car has an engine. If this statement refers to classes, what is the aggrega...
Starting Out with Java: Early Objects (6th Edition)
(Two movable vertices and their distances) Write a program that displays two circles with radius 10 at location...
Introduction to Java Programming and Data Structures, Comprehensive Version (11th 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
- 4. Write a java program that will check the validity of a password:a. Prompt the user to enter a password (a string, use Scanner class to read the password)b. Check the validity of the password by calling a method, named passValidity()c. Return an appropriate message (i.e. password is valid or invalid) to the main() method. The main() method will print the message.The passValidity() method will:a. take the password as the input parameter,b. check the validity as per the following rule:• the password string must have at least eight characters • the password must have at least one alphabet • the password must contain at least one digit• the password must contain at least one special character from @, #, $, and &c. return an appropriate message to the main() method.Sample output (assuming the input is ab459#de0):The password is validSample output (assuming the input is abcd#de&ui):The password is invalidarrow_forwardConsider a class that keeps track of the sales of an item. An object of this class will have the attributes Number sold Total sales Total discounts Cost per item Bulk quantity Bulk discount percentage and the following methods: registerSale(n) records the sale of n items. If n is larger than the bulkquantity, the cost per item will be reduced by the bulk discount. displaySales displays the number sold, the total sales, and total discount. Implement the class in Java. Write some Java statements that test the class.arrow_forwardIn java, create a program that gathers user input and prints a paystub. This assignment requires one class per .java file, total of three classes. The first one is employee.java which gathers the user's id (fixed value), first name, last name, address, city, zip, phone, email, and hourly rate. The second one is payperiod.java, which gathers the user's hours worked (taking overtime into account), start date (fixed), end date (fixed), and paystub id (fixed). The third one is payrollmanager.java, which calculates and displays the data gathered from the first two. This class/file should also use methods such as: doubleCalculateGrossPay(Employee, PayPeriod), double CalculateRegularPay(Employee, PayPeriod), double CalculateOvertimePay(Employee, PayPeriod), and void PrintPaystub(Employee, PayPeriod).arrow_forward
- Java program requirements:- a PesoCurrency class with an amount attribute.- Define an overloaded convertTo method that will convert amount attribute to dollars and euros.- Conversion: 1 dollar = 50 pesos while 10 dollars = 2 euros.- Implement the class to ask the user an amount in peso to convert and will display the dollar and euro equivalent of the inputted peso amount.arrow_forward2. Write a java program which will input an integer value from the user (Use Scanner class). Your program will have mathOper() method in addition to the main() method.The main() method will be responsible for:a. extracting the 1's digit (ones), the 10's digit (tens), and any digits larger than the 10's digit. For instance, if the input is 3562, the 1's digit would be 2, the 10's digit would be 6, and the remaining digits would be 35.b. call the mathOper() method with these 3 values,c. print the results to the screen.The mathOper() method will make the following calculations:a. Multiply the 1's digit by 7, subtract the result by 2 and then divide that by 5.b. Add 3.5 to the 10's digit, multiply the result by 4.3 and then divide that by 2.c. Subtract 5 from the remaining digits, multiply the result by 3, find the remainder after dividing that by 7, and then add 6 to the remainder value.d. Add the 3 values obtained from ‘a’, ‘b’, and ‘c’ operations.e. Return the final value.Sample output…arrow_forwardImplement the below pseudo-code in a Java program using a while loop and aswitch-case statement. The program should be well structured, and the taskperformed under each option (at least options 'b' to 'e') should be implementedas a separate method.NOTE: The Scanner class does not have a method to input a character. Inorder to read a character from the keyboard, use one of the following methods(after declaring the Scanner object):static Scanner kb = new Scanner(System.in);1. char ch = kb.nextLine().charAt(0); OR2. char ch = kb.nextLine().toLowerCase().charAt(0);where kb is a Scanner class object.The second method above also converts the input to lowercase, which is oftenuseful. Though these methods allow the user to input more than one characteron the input line, the rest of the line (after capturing the first character withcharAt(0)) is discarded.If you also want to ignore the leading spaces before the first character thenuse:1. char ch = kb.nextLine().trim().charAt(0); OR2. char ch =…arrow_forward
- write program in java Create a Java Project With Your name as “EemanAsadProjectQNo1”, add a class to this Project, Class name should be Your Name with Your Reg No as “YourName18-arid-2860” Add a Method to Accept names and marks of nstudents from user, enter the value of nfrom Userb. Ensure that valid data is entered. If user enters an invalid data, then appropriate message should be displayed using Message Dialog Box e.g. ‘Name cannot be a number’ or ‘Marks must be an integer value’ Add another Method, to Display this data in descending order according to marks such that name of student having maximum marks should come at the top and the name of student having minimum marks should come at the bottom.arrow_forwardWrite a Java 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…arrow_forwardPlease write it in Java Write a class definition of a class named 'Value' with the following: a constructor accepting a single integer parameter a constructor with no parameters a method 'setVal' that accepts a single parameter, a boolean method, 'wasModified' that returns true if setVal was ever called for the object. a method 'getVal' that returns an integer value as follows: if setVal has ever been called, it getVal returns the last value passed to setVal. Otherwise if the "single int parameter" constructor was used to create the object, getVal returns the value passed to that constructor. Otherwise getVal returns 0. Please write it in Java. Most of the answers that can be searched using Google are not correct for this question.arrow_forward
- Create a java class called Student to represent a Student exam grades. The class has four instance variables: A String variable for the name, and three double variables to store three exams grades for the student. Write a mutator method that takes four parameters—A String and three doubles. Use these parameters to initialize the instance variables declared earlier. Write a getAverage method that calculates and returns the average of the three exam grades. Write a separate program that creates 2 Student objects to hold the following data. First Name Exam#1 Exam#2 Exam#3 Ahmed 83.0 95.5 90.0 Kareema 75.25 83.0 89.0 Output the name and average grade for each of the above students.arrow_forwardIn this exercise, you have to implement a calculator which can perform addition, subtraction, multiplication, and division. Problem Statement# Write a Java class called Calculator with ● private fields: ○ num1 and num2 (double type) And methods: ● add(), a method which returns the addition of two numbers ● subtract(), a method which returns the subtraction of num1 from num2 ● multiply(), a method which returns the multiplication of numbers ● divide(), a method which returns the division of num2 by num1 ● Define a parameterized constructor which takes two parameters num1 and num2 and assigns these variables to the respective fields in the class. Input# Pass double point numbers in the parameterized constructor Output# addition, subtraction, division, and multiplication Sample Input# Calculator obj = new Calculator(10, 94); obj.add() obj.subtract() obj.multiply() obj.divide() Sample Output# 104 84 940 9.4 Part of the Solution class Calculator { // write class fields here public…arrow_forwardWrite Java code for a Baby class. A Baby has a name of type String and an age of type integer.Supply two constructors: one will be the default constructor, that just setsdefault values for the name and age; the second constructor will take twoparameters, a string to set the name and an integer to set the age. Also, supply methods for setting the name, setting the age, getting the name and getting the age.The class should not contain I/O methods; input of values to the instancevariables must be done with a set method or constructor, output of values from the instance variables must be done with get methods. The set method for the name instance variable should ensure that the input is not empty or contain whitespaces (otherwise set a default value). The set method for the age instance variable should validate the input to be between 1 and 4 inclusive(otherwise set a default value).Give Java code for an equals method for the Baby class. Babies count asbeing the same (i.e. equal) if their…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
Memory Management Tutorial in Java | Java Stack vs Heap | Java Training | Edureka; Author: edureka!;https://www.youtube.com/watch?v=fM8yj93X80s;License: Standard YouTube License, CC-BY