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 8, Problem 2PP
Program Plan Intro
Displaying right arrow and left arrow
Program Plan:
Filename: Test.java
- • Include the required header files.
- • Define the class “Test”.
- • Define main function.
- ○ Create an object for scanner.
- ○ Declare the character variable.
- ○ The “do-while” condition is used to get the user response to do the operation once again.
- ■ Create the objects “a1” and “b1” for “LeftArrow” and “RightArrow” classes.
- ■ Call the method “writeOutput” using both the objects.
- ■ Create the objects “a2” and “b2” for “LeftArrow” and “RightArrow” classes.
- ■ Call the method “writeOutput” using both the objects.
- ■ Create the objects “a3” and “b3” for “LeftArrow” and “RightArrow” classes.
- ■ Call the method “writeOutput” using both the objects.
- ■ Change the value of offset from 0 to 1 and call the method “writeOutput” using both the objects.
- ■ Call the method “setOffset” using both the objects.
- ■ Change the value of offset from 0 to 2 and call the method “writeOutput” using both the objects.
- ■ Call the method “setTail” using both the objects.
- ■ Change the value of offset from 3 to 10 and call the method “writeOutput” using both the objects.
- ■ Call the method “setBase” using both the objects.
- ■ Test the write offset values for both arrows by calling “writeOffset” method.
- ■ Test the return offset for both arrows by calling “getOffset” method.
- ■ Test the write tail for both arrows by calling “writeTail” method.
- ■ Test the return tail for both arrows by calling “getTail” method.
- ■ Test the write base for both arrows by calling “writeBase” method.
- ■ Test the return tail for both arrows by calling “getBase” method.
- ■ Reset the values and call “set” method.
- ■ Call the method “writeOutput” using both the objects.
- ■ Call the “drawHere” method using both the objects.
- ■ Reset the values and call “set” method.
- ■ Call the method “writeOutput” using both the objects.
- ■ Call the “drawHere” method using both the objects.
- ■ Reset the values and call “set” method.
- ■ Call the method “writeOutput” using both the objects.
- ■ Call the “drawHere” method using both the objects.
- ■ Finally display the two arrows on the output screen.
Filename: LeftArrow.java
- • Define the class “LeftArrow” extends from the “ShapeBase” class.
- ○ Declare the required variables.
- ○ Define the default constructor.
- ■ Call the “super” method.
- ■ Set the values.
- ○ Define the constructor with the arguments “theTail”, “theBase”.
- ■ Call the method “super” with an offset value.
- ■ Set the value.
- ■ Check the “theBase” value is less than 3.
- • Assign “base” is equal to 3.
- ■ Check the “theBase% 2” is equal to 0.
- • Assign “base” value is equal to “theBase + 1”.
- ■ Otherwise, assign the “base” is equal to “theBase”.
- • Assign “base” is equal to 3.
- ○ Define another constructor with the arguments “theTail”, “theBase”.
- ■ Call the method “super”.
- ■ Set the value.
- ■ Check the “theBase” value is less than 3.
- • Assign “base” is equal to 3.
- ■ Check the “theBase% 2” is equal to 0.
- • Assign “base” value is equal to “theBase + 1”.
- ■ Otherwise, assign the “base” is equal to “theBase”.
- ■ Define another constructor with the parameters “theTail”, “theBase”.
- • Assign “base” is equal to 3.
- ○ Define the “set” method with the arguments “newOffset”, newTail”, and “newBase”.
- ■ Call the method “super” with an offset value.
- ■ Set the value.
- ■ Check the “newBase” value is less than 3.
- • Assign “base” is equal to 3.
- ■ Check the “newBase% 2” is equal to 0.
- • Assign “base” value is equal to “theBase + 1”.
- ■ Otherwise, assign the “base” is equal to “newBase”.
- • Assign “base” is equal to 3.
- ○ Define the “setTail” method with an argument “newTail”.
- ■ Set the value.
- ○ Define the method “setBase” with an argument “newBase”.
- ■ Check the “newBase” value is less than 3.
- • Assign “base” is equal to 3.
- ■ Check the “newBase% 2” is equal to 0.
- • Assign “base” value is equal to “theBase + 1”.
- ■ Otherwise, assign the “base” is equal to “newBase”.
- • Assign “base” is equal to 3.
- ■ Check the “newBase” value is less than 3.
- ○ Define the “writeOutput()” method.
- ■ Call the “writeOffset()”, “writeTail ()”, and “writeBase()” methods.
- ○ Define the “writeOffset()” method.
- ■ Display the offset by calling the “getOffset()” method.
- ○ Define the “writeTail()” method.
- ■ Display the offset by calling the “getTail()” method.
- ○ Define the “writeBase()” method.
- ■ Display the offset by calling the “getBase()” method.
- ○ The method “getTail()” returns the “tail” value.
- ○ The method “getBase()” returns the “base” value.
- ○ Define the method “drawHere ()”.
- ■ Call the “drawTop()”, “drawTail()”, and “drawBottom()” methods.
- ○ Define the “drawTop ()” method.
- ■ Declare the variable “linecount” and assign the value of “getBase()” value divided by 2.
- ■ Declare the variable “numberOfSpaces” and calculate it.
- ■ Call the “skipSpaces()” method with a parameter “numberOfSpaces” value.
- ■ Display the “*” character.
- ■ Declare the “count” variable.
- ■ Declare the “insideWidth” with value 1.
- ■ The “for” condition is used to display the “*” character in left arrow shape.
- • Decrement the “numberOfSpaces” value by 2.
- • Call the “skipSpaces()” method with “numberOfSpaces” value.
- • Display the “*” character.
- • Call the “skipSpaces()” method with “insideWidth” value.
- • Display the “*” character.
- • Increment the “insideWidth” value by 2.
- ○ Define the “drawTial()” method.
- ■ Call the “skipSpaces()” method with “getOffset()” value.
- ■ Display the “*” character.
- ■ Declare and calculate the “insideWidth” value.
- ■ Call the “skipSpaces()” method with “insideWidth” value.
- ■ Declare the “count” variable.
- ■ The “for” condition is used to display the “*” character in left arrow shape.
- • Display the “*” character.
- ○ Define the “drawBottom()” method.
- ■ Declare the required variables and calculate them respectively.
- ■ The “for” condition is used to display the “*” character in left arrow shape.
- • Call the “skipSpaces()” method with “startOfLine” value.
- • Display the “*” character.
- • Call the “skipSpaces()” method with “inideWidth” value.
- • Display the “*” character.
- • Increment the “startOfLine” value by 2.
- • Decrement the “insideWidth” value by 2.
- ○ Define “skipSpaces()” method.
- ■ Display the space.
Filename: RightArrow.java
- • Define the class “RightArrow” extends from the “ShapeBase” class.
- ○ Declare the required variables.
- ○ Define the default constructor.
- ■ Call the “super” method.
- ■ Set the values.
- ○ Define the constructor with the arguments “theTail”, “theBase”.
- ■ Call the method “super” with an offset value.
- ■ Set the value.
- ■ Check the “theBase” value is less than 3.
- • Assign “base” is equal to 3.
- ■ Check the “theBase% 2” is equal to 0.
- • Assign “base” value is equal to “theBase + 1”.
- ■ Otherwise, assign the “base” is equal to “theBase”.
- • Assign “base” is equal to 3.
- ○ Define another constructor with the arguments “theTail”, “theBase”.
- ■ Call the method “super”.
- ■ Set the value.
- ■ Check the “theBase” value is less than 3.
- • Assign “base” is equal to 3.
- ■ Check the “theBase% 2” is equal to 0.
- • Assign “base” value is equal to “theBase + 1”.
- ■ Otherwise, assign the “base” is equal to “theBase”.
- ■ Define another constructor with the parameters “theTail”, “theBase”.
- • Assign “base” is equal to 3.
- ○ Define the “set” method with the arguments “newOffset”, newTail”, and “newBase”.
- ■ Call the method “super” with an offset value.
- ■ Set the value.
- ■ Check the “newBase” value is less than 3.
- • Assign “base” is equal to 3.
- ■ Check the “newBase% 2” is equal to 0.
- • Assign “base” value is equal to “theBase + 1”.
- ■ Otherwise, assign the “base” is equal to “newBase”.
- • Assign “base” is equal to 3.
- ○ Define the “setTail” method with an argument “newTail”.
- ■ Set the value.
- ○ Define the method “setBase” with an argument “newBase”.
- ■ Check the “newBase” value is less than 3.
- • Assign “base” is equal to 3.
- ■ Check the “newBase% 2” is equal to 0.
- • Assign “base” value is equal to “theBase + 1”.
- ■ Otherwise, assign the “base” is equal to “newBase”.
- • Assign “base” is equal to 3.
- ■ Check the “newBase” value is less than 3.
- ○ Define the “writeOutput()” method.
- ■ Call the “writeOffset()”, “writeTail ()”, and “writeBase()” methods.
- ○ Define the “writeOffset()” method.
- ■ Display the offset by calling the “getOffset()” method.
- ○ Define the “writeTail()” method.
- ■ Display the offset by calling the “getTail()” method.
- ○ Define the “writeBase()” method.
- ■ Display the offset by calling the “getBase()” method.
- ○ The method “getTail()” returns the “tail” value.
- ○ The method “getBase()” returns the “base” value.
- ○ Define the method “drawHere ()”.
- ■ Call the “drawTop()”, “drawTail()”, and “drawBottom()” methods.
- ○ Define the “drawTop ()” method.
- ■ Declare the variable “startOfLine” and assign the value of “getOffset()” plus “getTail()”.
- ■ Call the “skipSpaces()” method with a parameter “startOfLine” value.
- ■ Display the “*” character.
- ■ Declare the “linecount” and calculate it.
- ■ Declare the “count” variable.
- ■ Declare the “insideWidth” with value 1.
- ■ The “for” condition is used to display the “*” character in right arrow shape.
- • Call the “skipSpaces()” method with “startOfLine” value.
- • Display the “*” character.
- • Call the “skipSpaces()” method with “insideWidth” value.
- • Display the “*” character.
- • Increment the “insideWidth” value by 2.
- ○ Define the “drawTial()” method.
- ■ Call the “skipSpaces()” method with “getOffset()” value.
- ■ Declare the “count” variable.
- ■ The “for” condition is used to display the “*” character in right arrow shape.
- • Display the “*” character.
- • Calculate the “insideWidth”.
- • Call the “skipSpaces()” method with “insideWidth” value.
- • Display the “*” character.
- ○ Define the “drawBottom()” method.
- ■ Declare the required variables and calculate them respectively.
- ■ The “for” condition is used to display the “*” character in right arrow shape.
- • Call the “skipSpaces()” method with “startOfLine” value.
- • Display the “*” character.
- • Call the “skipSpaces()” method with “inideWidth” value.
- • Display the “*” character.
- • Decrement the “insideWidth” value by 2.
- • Call the “skipSpaces()” method with “startOfLine” value.
- • Display the “*” character.
- ○ Define “skipSpaces()” method.
- ■ Display the space.
Filename: ShapeInterface.java
Define the interface “SpaceInterface”.
- • Declare the “setOffset()” method.
- • Declare the “getOffset()” method.
- • Declare the “drawAt()” method.
- • Declare the “drawHere()” method.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Create a class BeautyProduct with name, color, brand.
Provide Constructors, getters, setters and also write toString method.
Now create a class Lipstick which extends the class BeautyProduct as Lipstick is-a Beauty Product it has totalVolume, remainingVolume, price, texture (gloss, matt) as private data members.
Provide Constructors, getters, setters and an apply() method whenever this method is called a beauty item is applied and its volume decreases by 10.This should also check that volume must not be zero if it then u have to throw exception. Also write toString method which prints all details like name, color, brand, price, texture and volume.
Create another class foundation having totalVolume, remainingVolume, price, texture (liquid, cake) and lastingTime (number of hours foundation remain intact on face and does not crease) as private data members. This also extends the base class i.e. BeautyProduct.
Provide Constructors, getters, setters and an apply() method whenever this…
Java Programming: ConWay World
Conway’s game of life is a simple simulation of life forms in a two dimensional grid. The ConwayCell implements the standard behavior of a call in Conway’s Game of Life. You only need to implement and test two new classes. You will create two different cells, derived from AbstractCell.
1. Alternating Cell
The first class that you should create is a class named AlternatingCell. This is a cell that should alternate between being alive and dead with each generation. Add one or more of your AlternatingCells to your ConwayWorld. You can see how to add a cell in line 15 - 20 of Main.java. Give your AlternatingCell a unique display character when it is alive so that you can see it in your conway world.
2. Your own cell
For the second cell you should feel free to implement any behavior that you want. If you are having trouble thinking of ideas then post up a question.
You can add your custom cell in just a few places in the world, or you might choose to replace…
Java-
Suppose that Vehicle is a class and Car is a new class that extends Vehicle. Write a description of which kind of assignments are permitted between Car and Vehicle variables.
Chapter 8 Solutions
Java: An Introduction to Problem Solving and Programming (8th Edition)
Ch. 8.1 - Prob. 1STQCh. 8.1 - Suppose the class SportsCar is a derived class of...Ch. 8.1 - Suppose the class SportsCar is a derived class of...Ch. 8.1 - Can a derived class directly access by name a...Ch. 8.1 - Can a derived class directly invoke a private...Ch. 8.1 - Prob. 6STQCh. 8.1 - Suppose s is an object of the class Student. Base...Ch. 8.2 - Give a complete definition of a class called...Ch. 8.2 - Add a constructor to the class Student that sets...Ch. 8.2 - Rewrite the definition of the method writeoutput...
Ch. 8.2 - Rewrite the definition of the method reset for the...Ch. 8.2 - Can an object be referenced by variables of...Ch. 8.2 - What is the type or types of the variable(s) that...Ch. 8.2 - Prob. 14STQCh. 8.2 - Prob. 15STQCh. 8.2 - Consider the code below, which was discussed in...Ch. 8.2 - Prob. 17STQCh. 8.3 - Prob. 18STQCh. 8.3 - Prob. 19STQCh. 8.3 - Is overloading a method name an example of...Ch. 8.3 - In the following code, will the two invocations of...Ch. 8.3 - In the following code, which definition of...Ch. 8.4 - Prob. 23STQCh. 8.4 - Prob. 24STQCh. 8.4 - Prob. 25STQCh. 8.4 - Prob. 26STQCh. 8.4 - Prob. 27STQCh. 8.4 - Prob. 28STQCh. 8.4 - Are the two definitions of the constructors given...Ch. 8.4 - The private method skipSpaces appears in the...Ch. 8.4 - Describe the implementation of the method drawHere...Ch. 8.4 - Is the following valid if ShapeBaSe is defined as...Ch. 8.4 - Prob. 33STQCh. 8.5 - Prob. 34STQCh. 8.5 - What is an advantage of having the main...Ch. 8.5 - What Java construct allows us to define and...Ch. 8 - Consider a program that will keep track of the...Ch. 8 - Implement your base class for the hierarchy from...Ch. 8 - Draw a hierarchy for the components you might find...Ch. 8 - Suppose we want to implement a drawing program...Ch. 8 - Create a class Square derived from DrawableShape,...Ch. 8 - Create a class SchoolKid that is the base class...Ch. 8 - Derive a class ExaggeratingKid from SchoolKid, as...Ch. 8 - Create an abstract class PayCalculator that has an...Ch. 8 - Derive a class RegularPay from PayCalculator, as...Ch. 8 - Create an abstract class DiscountPolicy. It should...Ch. 8 - Derive a class BulkDiscount from DiscountPolicy,...Ch. 8 - Derive a class BuyNItemsGetOneFree from...Ch. 8 - Prob. 13ECh. 8 - Prob. 14ECh. 8 - Create an interface MessageEncoder that has a...Ch. 8 - Create a class SubstitutionCipher that implements...Ch. 8 - Create a class ShuffleCipher that implements the...Ch. 8 - Define a class named Employee whose objects are...Ch. 8 - Define a class named Doctor whose objects are...Ch. 8 - Create a base class called Vehicle that has the...Ch. 8 - Create a new class called Dog that is derived from...Ch. 8 - Define a class called Diamond that is derived from...Ch. 8 - Prob. 2PPCh. 8 - Prob. 3PPCh. 8 - Prob. 4PPCh. 8 - Create an interface MessageDecoder that has a...Ch. 8 - For this Programming Project, start with...Ch. 8 - Modify the Student class in Listing 8.2 so that it...Ch. 8 - Create a JavaFX application that uses a TextField...Ch. 8 - Prob. 10PP
Knowledge Booster
Similar questions
- Poker card in PythonLet the CarteBase class present itself in the context of this exercise. It encapsulates a game card characterized by numerical values for its face and its kind. The possible faces are integers from 1 to 13, with face 1 corresponding to the ace, and faces 11, 12 and 13 corresponding respectively to the jack, the queen and the king. The other faces are simply identified by their number from 2 to 10. The possible grades are integers from 1 to 4 corresponding respectively to the cards of spade, heart, tile and clover. Analyze this class to understand how it works. You are asked to create a class called CarteDePoker that inherits CarteBase and defines or redefines the following methods: force(self) which returns the strength of the card in the poker game. In Poker, cards have the strength of their digital face from 2 to 13, except for the ace which is stronger than the other cards. So we give it a strength of 14.__eq__(self, other) to define operator behavior…arrow_forwardA class Student can have three member variables: name, age, and an array of grades of two exams marks [5]. The grades can be integers (0-100), doubles (0.0-100.00) and characters (A-F, where F denotes grades less than 50). Write a class template to accommodate all five types of grades. The class template must have two constructors (no-arg constructor, and aconstructor that takes 7 parameters: name, age and grades of 5 exams). Write a c++ function to read an object from console, and another function to print the object to the console. Implement the class template by creating three objects with three different types (int, double and char) of grades, and printing the objects on the console.arrow_forwardCreate a Right Triangle class that has two sides. Name your class rightTraingle. Code getter and setters for the base and the height. (Remember class variables are private.) The class should include a two-argument constructor that allows the program to set the base and height. The constructor should verify that all the dimensions are greater than 0. before assigning the values to the private data members. If a side is not greater than zero, set the value to -1. The class also should include two value-returning methods. One value-returning method should calculate the area of a triangle, and the other should calculate the perimeter of a triangle. If either side is -1, these functions return a -1. The formula for calculating the area of a triangle is 1/2 * b*h, where b is the base and h is the height. The formula for calculating the perimeter of a triangle is b+h+sqrt (b*b+h*h). Be sure to include a default constructor that initializes the variables of the base, height to -1. To test…arrow_forward
- 1 - Student class Make a class student (in student.py) that stores the following information for a student: Name (name) Student number (student_nr) Points per assignment (points_per_assignment) Exam grade (exam_grade) a) Behind each point of information is the name of the parameter to the initializer method. Store this information from the parameters in the object attributes with the same name. b) Add a method course_points() which returns the number of course points the student has gotten. Example: mary = Student("Mary", 15789613, [10, 9, 8, 10, 9, 10], 9)print(mary.course_points()) > 121 The calculation of the course points is explained in the course overview, course setup slides (Links to an external site.) and the first lectureLinks to an external site.. c) Add a method grade() which returns a the final grade of the student, rounded to nearest half (upwards, 6.75 -> 7). As per regulations, a 5.5 becomes a 6. If the student did not pass both the assignments (>= 95…arrow_forward1. Create a Student class that implements the Person interface. As well as storing the students name and email, also store their course grade (e.g A, B, C) in a member variable. The grade should be accessible via a getGrade method. For the implementation of getDescription return a message along the lines of “A C grade student”, substituting the students actual grade.2. Create a Lecturer class that implements the Person interface. This class should also store the subject that the lecturer teaches. Add a getSubject method, and implement getDescription so that it returns a suitable message, e.g. “Teaches Biology”.3. Create a third class, Employee that implements the interface. This should also store the name of the department the Employee works in (available via getDepartment). Again, getDescription should return a suitable message.arrow_forwardSolve:arrow_forward
- Complete the code for the following program. You are provided with an abstract class called abst. Create a class that will be a child of abst and it will be called usesAbst. Your program will have an int variable called value, and all the appropriate methods, as well 2 constructors. The first constructor will simply set the variable value to 0. The second constructor will set the variable value to equal a passed in parameter. You do not have to comment your code. public abstract class abst{ public abst () { } //Outputs to the screen the message Hello public abstract void sayHello (); //returns the stored int value public abstract int getValue (); //sets the int value to x public abstract void setValue (int x); //Outputs to the screen the message Another method public void output () { System.out.println("Another method"); public String tostring () { return "This is an abstract class"; }arrow_forwardlanguage is c++ sample output included with user input in boldarrow_forwardJAVA-Homework Write a Java program to create Human as a class with Hpersonname and Hpersonage as members of class HUman, and can make employee and Lecturer as the subclasses of Human (a class can have any number of subclasses). In the employee class, we can define grade member and in the Lecturer class also income member will be defined. 1. The program must be able to store information about employee and Lecturer. we want to store the Hpersonname and Hpersonage with grade of employee and the Hpersonname and Hpersonage with income of lecturer.arrow_forward
- in C++arrow_forwardA. Writing an Interface In this problem you will first write an interface and then modify classes to implement the interface. Write an interface, GeometricSolid, which has one method, volume. The volume method takes no arguments and returns a double. You are provided with three classes: Cylinder, Sphere, and RightCircularCone Modify the classes so that they implement the GeometricSolid interface. Supply the appropriate method for each class. You can use Google search to find the formula for the volume. Make sure that you use Math.PI in your calculations. Notice in InterfaceRunner that the objects are added to an ArrayList of GeometricSolids. since I couldn't fit all images with two screen shots I left the link here link: https://codecheck.it/files/18040616319p6jy6gzkdzuq08nmlp81251tarrow_forwardwrite in java Write a CashRegister class that can be used with the RetailItem class that you wrote inChapter 6’s Programming Challenge 4. (in the attachments) The CashRegister class should simulate the sale ofa retail item. It should have a constructor that accepts a RetailItem object as an argument.The constructor should also accept an integer that represents the quantity of items beingpurchased. In addition, the class should have the following methods:• The getSubtotal method should return the subtotal of the sale, which is the quantitymultiplied by the price. This method must get the price from the RetailItem objectthat was passed as an argument to the constructor.• The getTax method should return the amount of sales tax on the purchase. The salestax rate is 6 percent of a retail sale.• The getTotal method should return the total of the sale, which is the subtotal plus thesales tax.Demonstrate the class in a program that asks the user for the quantity of items being purchased,and…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT