All Matches
Solution Library
Expert Answer
Textbooks
Search Textbook questions, tutors and Books
Oops, something went wrong!
Change your search query and then try again
Toggle navigation
FREE Trial
S
Books
FREE
Tutors
Study Help
Expert Questions
Accounting
General Management
Mathematics
Finance
Organizational Behaviour
Law
Physics
Operating System
Management Leadership
Sociology
Programming
Marketing
Database
Computer Network
Economics
Textbooks Solutions
Accounting
Managerial Accounting
Management Leadership
Cost Accounting
Statistics
Business Law
Corporate Finance
Finance
Economics
Auditing
Hire a Tutor
AI Study Help
New
Search
Search
Sign In
Register
study help
computer science
introduction java program
Questions and Answers of
Introduction Java Program
Suppose you define a function that takes a reference to a base-class object as an argument. Why can this function also use a derived-class object as an argument?
Suppose you define a function that takes a base-class object as an argument (that is, the function passes a base-class object by value).Why can this function also use a derived-class object as an
Why is it usually better to pass objects by reference than by value?
Suppose Corporation is a base class and PublicCorporation is a derived class. Also suppose that each class defines a head() member function, that ph is a pointer to the Corporation type, and that ph
What’s wrong, if anything, with the following code?class Kitchen{private:double kit_sq_ft;public:Kitchen() {kit_sq_ft = 0.0; }virtual double area() const { return kit_sq_ft * kit_sq_ft; }};class
For each of the following sets of classes, indicate whether public or private derivation is more appropriate for Column B: A class Bear class Kitchen class Person class Person. class Person, class
The Wine class has a string class object member (see Chapter 4) that holds the name of a wine and a Pair object (as discussed in this chapter) of valarray objects (as discussed in this chapter).The
Suppose you have the following definitions:class Frabjous {private:char fab[20];public:Frabjous(const char * s = "C++") : fab(s) { }virtual void tell() { cout << fab; }};class Gloam
This exercise is the same as Programming Exercise 1, except that you should use private inheritance instead of containment.Again, a few typedefs might prove handy.Also you might contemplate the
Suppose you have the following definitions:class Frabjous {private:char fab[20];public:Frabjous(const char * s = "C++") : fab(s) { }virtual void tell() { cout << fab; }};class Gloam : private
Define a QueueTp template.Test it by creating a queue of pointers-to-Worker (as defined in Listing 14.10) and using the queue in a program similar to that in Listing 14.12.Here is a sample run of the
Suppose you have the following definition, based on the Stack template of Listing 14.13 and the Worker class of Listing 14.10:Stack sw;Write out the class declaration that will be generated. Just do
A Person class holds the first name and the last name of a person. In addition to its constructors, it has a Show() method that displays both names.A Gunslinger class derives virtually from the
Use the template definitions in this chapter to define the following:An array of string objectsA stack of arrays of doubleAn array of stacks of pointers to Worker objectsHow many template class
Here are some class declarations:// emp.h -- header file for abstr_emp class and children#include#includeclass abstr_emp{private:std::string fname; // abstr_emp's first namestd::string lname; //
Describe the differences between virtual and nonvirtual base classes.
What’s wrong with the following attempts at establishing friends?a. class snap {friend clasp;...};class clasp { ... };b. class cuff {public:void snip(muff &) { ... }...};class muff {friend void
Modify the Tv and Remote classes as follows:a. Make them mutual friends.b. Add a state variable member to the Remote class that describes whether the remote control is in normal or interactive
You’ve seen how to create mutual class friends. Can you create a more restricted form of friendship in which only some members of Class B are friends to Class A and some members of A are friends to
Modify Listing 15.11 so that the two exception types are classes derived from the logic_error class provided by the header file. Have each what() method report the function name and the nature of the
This exercise is the same as Programming Exercise 2, except that the exceptions should be derived from a base class (itself derived from logic_error) that stores the two argument values, the
What problems might the following nested class declaration have?class Ribs{private:class Sauce{int soy;int sugar;public:Sauce(int s1, int s2) : soy(s1), sugar(s2) { }};...};
How does throw differ from return?
Listing 15.16 uses two catch blocks after each try block so that the nbad_index exception leads to the label_val() method being invoked. Modify the program so that it uses a single catch block after
Suppose you have a hierarchy of exception classes that are derived from a base exception class. In what order should you place catch blocks?
Consider the Grand, Superb, and Magnificent classes defined in this chapter. Suppose pg is a type Grand * pointer that is assigned the address of an object of one of these three classes and that ps
How is the static_cast operator different from the dynamic_cast operator?
A palindrome is a string that is the same backward as it is forward. For example,“tot” and “otto” are rather short palindromes. Write a program that lets a user enter a string and that passes
Consider the following class declaration:class RQ1{private:char * st; // points to C-style stringpublic:RQ1() { st = new char [1]; strcpy(st,""); }RQ1(const char * s){st = new char [strlen(s) + 1];
Do the same problem as given in Programming Exercise 1 but do worry about complications such as capitalization, spaces, and punctuation.That is,“Madam, I’m Adam” should test as a palindrome.
Redo Listing 16.3 so that it gets it words from a file. One approach is to use a vector object instead of an array of string.Then you can use push_back() to copy how ever many words are in your data
Name at least two advantages string objects have over C-style strings in terms of ease-of-use.
Write a function that takes a reference to a string object as an argument and that converts the string object to all uppercase.
Write a function with an old-style interface that has this prototype: int reduce(long ar[], int n);The actual arguments should be the name of an array and the number of elements in the array.The
Which of the following are not examples of correct usage (conceptually or syntactically) of auto_ptr? (Assume that the needed header files have been included.)auto_ptr pia(new int[20]);auto_ptr (new
Do the same problem as described in Programming Exercise 4, except make it a template function:templateint reduce(T ar[], int n);Test the function in a short program, using both a long instantiation
Redo the example shown in Listing 12.12, using the STL queue template class instead of the Queue class described in Chapter 12. Listing 12.12 bank.cpp // bank.cpp -- using the Queue interface //
If you could make the mechanical equivalent of a stack that held golf clubs instead of numbers, why would it (conceptually) be a bad golf bag?
Why would a set container be a poor choice for storing a hole-by-hole record of your golf scores?
A common game is the lottery card.The card has numbered spots of which a certain number are selected at random.Write a Lotto() function that takes two arguments. The first should be the number of
Because a pointer is an iterator, why didn’t the STL designers simply use pointers instead of iterators?
Compared to an array, a linked list features easier addition and removal of elements but is slower to sort.This raises a possibility: Perhaps it might be faster to copy a list to an array, sort the
Mat and Pat want to invite their friends to a party.They ask you to write a program that does the following:Allows Mat to enter a list of his friends’ names.The names are stored in a container and
Why didn’t the STL designers simply define a base iterator class, use inheritance to derive classes for the other iterator types, and express the algorithms in terms of those iterator classes?
If Listing 16.9 were implemented with list instead of vector, what parts of the program would become invalid? Could the invalid part be fixed easily? If so, how? Table 16.9 Some list Member
Give at least three examples of convenience advantages that a vector object has over an ordinary array.
Modify Listing 16.9 (vect3.cpp) as follows:Here’s a sample run of the program in Listing 16.9:Enter book title (quit to quit): The Cat Who Can Teach You Weight LossEnter book rating: 8Enter book
Consider the TooBig functor in Listing 16.15.What does the following code do, and what values get assigned to bo? bool bo = TooBig(10)(15);One functor (f100) is a declared object, and the
What role does the iostream file play in C++ I/O?
Write a program that counts the number of characters up to the first $ in input and that leaves the $ in the input stream.
Why does typing a number such as 121 as input require a program to make a conversion?
Write a program that copies your keyboard input (up to the simulated end-of-file) to a file named on the command line.
What’s the difference between the standard output and the standard error?
Write a program that copies one file to another. Have the program take the filenames from the command line. Have the program report if it cannot open a file.
Why is cout able to display various C++ types without being provided explicit instructions for each type?
Write a program that opens two text files for input and one for output. The program should concatenate the corresponding lines of the input files, use a space as a separator, and write the results to
What feature of the output method definitions allows you to concatenate output?
Mat and Pat want to invite their friends to a party, much as they did in Programming Exercise 8 in Chapter 16, except now they want a program that uses files. They have asked you to write a program
Write a program that requests an integer and then displays it in decimal, octal, and hexadecimal forms. Display each form on the same line, in fields that are 15 characters wide, and use the C++
Consider the class definitions of Programming Exercise 5 in Chapter 14,“Reusing Code in C++”. If you haven’t yet done that exercise, do so now.Then do the following:Write a program that uses
Write a program that requests the following information and that formats it as shown:Enter your name: Billy GruffEnter your hourly wages: 12Enter number of hours worked: 7.5First format:Billy Gruff:
Here is part of a program that reads keyboard input into a vector of string objects, stores the string contents (not the objects) in a file, and then copies the file contents back into a vector of
Consider the following program://rq17-8.cpp#includeint main(){using namespace std;char ch;int ct1 = 0;cin >> ch;while (ch != 'q'){ct1++;cin >> ch;}int ct2 = 0;cin.get(ch);while (ch !=
Both of the following statements read and discard characters up to and including the end of a line. In what way does the behavior of one differ from that of the other?while (cin.get() !=
In Listing 6.10, what advantage would there be in using character labels, such as a and c, instead of numbers for the menu choices and switch cases?Here is a sample run of the executive menu program
What are the three steps in using a function?
Write a program that repeatedly asks the user to enter pairs of numbers until at least one of the pair is 0. For each pair, the program should use a function to calculate the harmonic mean of the
Construct function prototypes that match the following descriptions:a. igor() takes no arguments and has no return value.b. tofu() takes an int argument and returns a float.c. mpg() takes two type
Write a program that asks the user to enter up to 10 golf scores, which are to be stored in an array. You should provide a means for the user to terminate input prior to entering 10 scores. The
Here is a structure declaration:struct box{char maker[40];float height;float width;float length;float volume;};a. Write a function that passes a box structure by value and that displays the value of
Many state lotteries use a variation of the simple lottery portrayed by Listing 7.4. In these variations you choose several numbers from one set and call them the field numbers. For example, you
Write a function that takes three arguments: a pointer to the first element of a range in an array, a pointer to the element following the end of a range in an array, and an int value. Have the
Write a function that takes a double array name and an array size as arguments and returns the largest value in that array. Note that this function shouldn’t alter the contents of the array.
Define a recursive function that takes an integer argument and returns the factorial of that argument. Recall that 3 factorial, written 3!, equals 3 × 2!, and so on, with 0! defined as 1. In
Why don’t you use the const qualifier for function arguments that are one of the fundamental types?
Write a program that uses the following functions:Fill_array() takes as arguments the name of an array of double values and an array size. It prompts the user to enter double values to be entered in
Redo Listing 7.7, modifying the three array-handling functions to each use two pointer parameters to represent a range. The fill_array() function, instead of returning the actual number of items
What are the three forms a C-style string can take in a C++ program?
Redo Listing 7.15 without using the array class. Do two versions:a. Use an ordinary array of const char * for the strings representing the season names, and use an ordinary array of double for the
Write a function that has this prototype:int replace(char * str, char c1, char c2);Have the function replace every occurrence of c1 in the string str with c2, and have the function return the number
What does the expression *"pizza" mean? What about "taco"[2]?
This exercise provides practice in writing functions dealing with arrays and structures. The following is a program skeleton. Complete it by providing the described functions:#includeusing namespace
C++ enables you to pass a structure by value, and it lets you pass the address of a structure. If glitz is a structure variable, how would you pass it by value? How would you pass its address? What
Design a function calculate() that takes two type double values and a pointer to a function that takes two double arguments and returns a double. The calculate() function should also be type double,
The function judge() has a type int return value. As an argument, it takes the address of a function. The function whose address is passed, in turn, takes a pointer to a const char as an argument and
Suppose we have the following structure declaration:struct applicant {char name[30];int credit_ratings[3];};a. Write a function that takes an applicant structure as an argument and displays its
Suppose the functions f1() and f2() have the following prototypes:void f1(applicant * a);const char * f2(const applicant * a1, const applicant * a2);Declare p1 as a pointer that points to f1 and p2
What kinds of functions are good candidates for inline status?
Write a function that normally takes one argument, the address of a string, and prints that string once. However, if a second, type int, argument is provided and is nonzero, the function should print
Suppose the song() function has this prototype:void song(const char * name, int times);a. How would you modify the prototype so that the default value for times is 1?b. What changes would you make in
Write a function that takes a reference to a string object as its parameter and that converts the contents of the string to uppercase. Use the toupper() function described in Table 6.4 of Chapter
The CandyBar structure contains three members. The first member holds the brand name of a candy bar. The second member holds the weight (which may have a fractional part) of the candy bar, and the
Write overloaded versions of iquote(), a function that displays its argument enclosed in double quotation marks. Write three versions: one for an int argument, one for a double argument, and one for
The following is a structure template:struct box{char maker[40];float height;float width;float length;float volume;};a. Write a function that has a reference to a box structure as its formal argument
What changes would need be made to Listing 7.15 so that the functions fill() and show() use reference parameters?Here’s a sample run:Enter Spring expenses: 212Enter Summer expenses: 256Enter Fall
The following is a program skeleton:#includeusing namespace std;#include // for strlen(), strcpy()struct stringy {char * str; // points to a stringint ct; // length of string (not counting '\0')};//
Write a template function max5() that takes as its argument an array of five items of type T and returns the largest item in the array. (Because the size is fixed, it can be hard-coded into the loop
The following are some desired effects. Indicate whether each can be accomplished with default arguments, function overloading, both, or neither. Provide appropriate prototypes.a. mass(density,
Write a template function maxn() that takes as its arguments an array of items of type T and an integer representing the number of elements in the array and that returns the largest item in the
Write a function template that returns the larger of its two arguments.
Showing 500 - 600
of 741
1
2
3
4
5
6
7
8