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
Question
Chapter 5, Problem 3P
Program Plan Intro
Magic 8 ball game using class
Program Plan:
“MagicBall.java”:
- • Define “MagicBall” class.
- ○ Define method “displayAnswer()” which is used to returns an answer at random number.
- ■ Generate a random number by using “(int)(Math.random() * 8) + 1” and then store that value in the variable “num”.
- ■ Performs “Switch” case for “num”.
- • If the generated number is “1”, then print “It is certain”. Break the statement after this case.
- • If the generated number is “2”, then print “It is decidedly so”. Break the statement after this case.
- • If the generated number is “3”, then print “Most likely”. Break the statement after this case.
- • If the generated number is “4”, then print “Signs point to yes”. Break the statement after this case.
- • If the generated number is “5”, then print “Reply hazy, try again”. Break the statement after this case.
- • If the generated number is “6”, then print “Ask again later”. Break the statement after this case.
- • If the generated number is “7”, then print “Don’t count on it”. Break the statement after this case.
- • If the generated number is “8”, then print “My sources say no”. Break the statement after this case.
- ○ Define method “displayAnswer()” which is used to returns an answer at random number.
“MagicBallTest.java”:
- • Import required package.
- • Define “MagicBallTest” class.
- ○ Define main function.
- ■ Create an object for scanner class.
- ■ Create an object “b” from “MagicBall” class.
- ■ Declare a variable “str” in "String" type.
- ■ Performs “do-while” loop.
- • Call method “displayAnswer” for object “b”.
- • Prompt statement for ask whether the user wants to continue or not.
- • Read user choice.
- • If the string “str” is “n”, then the loop will exits.
- ○ Define main function.
Expert Solution & Answer
Trending nowThis is a popular solution!
Students have asked these similar questions
https://youtu.be/4hAoK54Pfdc Here is a video regarding the kit component.
Number 11
In a game of chess, the following strategy is employed: canMoveTo(int x, int y), boolean. The Piece class includes a method that returns if the piece can move to location (x, y). Describe how you'd test this approach.
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
Knowledge Booster
Similar questions
- Modify this class by: 1. Suppose the bank wants to keep track of how many accounts exist. A. Do you need a static variable or a regular variable to save how many accounts have been created? Declare it and name the variable as numAccounts. It should be initialized to 0 (since no account exists initially). B. Update this variable in the constructor. (Ask the instructor if you do not know how) C. Add a method getNumAccounts that returns the total number of accounts. Decide if this method should be static – is the information related to a single account or is it for the entire class? Does this method manipulate a static variable? Complete this method. 2. Use the driver class TestAccounts1.java to test your modified Account class. Complete the code as instructed. 3. Add a method void close() to your Account class. This method should close the current account by appending “CLOSED” to the account name and setting the balance to 0. Also, decrement the total number of accounts. Should this…arrow_forwardUse the scenario below for help answering 1(A), (B) and (C). This is using Java. The Scenario: You have a class named HeartsPlayer A round of Hearts starts with every player having 13 cards Players then choose 3 cards to “trade” with a player (1st you pass left, 2nd you pass right, 3rd you pass across, 4th you keep) Players then strategically play cards in order to have the lowest score At the end of the round, points are cumulatively totaled for each player. If one player’s total is greater than 100, the game ends and the player with the lowest score wins. Guide: Static & Not Final Field: Accessed by every object, Changing Non-Static & Final Field: Accessed by object itself, Non-Changing Static & Final: Accessed by every object, Non-Changing Non-Static & Not Final Field: Accessed by object itself, Changing 1. How should the following data fields be defined (with respect to final and static)? (a) playerPosition (These have values of North, South, East, or West)- Is…arrow_forwardPlease help me with this Java Labarrow_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_forward1) Create a "Car" class that keeps car ids and prices. And create a "Galleries" class that holds the car list for a particular gallery. In this class there should be methods for get / set and print for car name, car number and car list. Adding / Removing Cars to the List in This Class should have methods. And create another method to find and print the IDs of Cars with Car Segment equal to X. (print (X)). Car Prices are as follows according to the segments. 0-19999 -> Z20000-29999 -> Y30000-44999 -> T45000-100000 -> P Apply the Car list using "Single Linked List"(Node, newNode, head). The program should be written in JAVA.arrow_forwardI would like you to create a program that keeps track of the inventory (by make - e.g., Toyota) of cars on a used car lot. you will have to create two classes. The first is a simple Car class that consists of an inventory, make, and model. The second class you need to create for this challenge is the Lot. A Lot can contain many cars. Part 1 Write the Car constructor, get methods, and toString method. Part 2 Write the Car's increase and decrease inventory methods. Part 3 Edit the Car so it implements Comparable and compares Car instances based on their inventory in descending order. Part 4 Write the getCars and getCarsSorted methods of the Lot class. Part 5 Write the getInventory method of the Lot class. Part 6 Use a try/catch block in the Car's constructor so that it sets the inventory to 0 if the inventory passed to the constructor is not a valid integer. this is the output that should be produced by main: Make: [Toyota Camry (10), Toyota Prius (4), Toyota Rav4 (16)] [Hyundai…arrow_forward
- The following is a class definition for a simple Ebook. Two instance variables and one parameterless constructor are provided. Part 1: 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 current page number to zero. Part 2: Write a getter and a setter method for the 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_forwardImplement the main method below in a class called Grade. The program should read integer points (use the Scanner class) and print the grade (“A”, “B”, “C”, “D”, or “F”) based on the final score associated with a student. Grades are classified as follows: Number of Points Final Grade Greater than or equal to 90 A Between 80 (inclusive) and 90 (exclusive) B Between 70 (inclusive) and 80 (exclusive) C Between 60 (inclusive) and 70 (exclusive) D Less than 60 F Restrictions/Assumptions Use the message “Enter final score” to prompt the user to enter the number of points. Use the Scanner class to read the number of points, i.e., Scanner sc = new Scanner(System.in); Use System.out.println(...) to print the final grade. Assume that the user correctly enters an integer score. Start with this code: import java.util.Scanner; public class Grade { public static void main(String[] args) { For the toolbar, press ALT+F10 (PC) or ALT+FN+F10 (Mac)arrow_forwardDesign a class called Stopwatch. The job of this class is to simulate a stopwatch. It should provide two methods: Start and Stop. We call the start method first, and the stop method next. Then we ask the stopwatch about the duration between start and stop. Duration should be a value in TimeSpan. Display the duration on the console. We should also be able to use a stopwatch multiple times. So we may start and stop it and then start and stop it again. Make sure the duration value each time is calculated properly. We should not be able to start a stopwatch twice in a row (because that may overwrite the initial start time). So the class should throw an InvalidOperationException if its started twice. 1arrow_forward
- Given the enum below, write Kotlin code that will either output "Primary Color", or "Secondary Color". You don't need a full program/method, just a code snippet. For this problem, Primary Colors are Red, Green, and Blue. Anything else is a Secondary Color. dnum class Color { Red, Green, Blue, Yellow, Purple, Orangearrow_forwardCreate a 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. 1. Write a mutator method that takes four parameters-A String and three doubles. Use these parameters to initialize the instance variables declared earlier. 2. Write a getAverage method that calculates and returns the average of the three exam grades. 3. 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 assignment, you will need to design and implement a class Country (Country.java) that stores the name of the country, its population, and its area. Then write a program (LargestCountry.java) that reads countries from a “coutrydata.txt” and prints • The country with the largest area. • The country with the largest population. • The country with the largest population density (people per square mile). To make it concrete, the followings are the suggested tasks you need to perform: 1.1 Country Class In the first file Country.java, create a class named Country that models a country. It should contain at least the followings (However, you can add any other methods if you like): • Private instance variables name, population, and area; • A constructor takes all three inputs (the name, population and area); • Three getter methods to return each of three instance variables (accessor method); • One getter method to return country population density (accessor method); • Three setter…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