Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

n this task you will extend your program from PP Task 6.1 (or CR Task 6.2 if you wish) to manage an array of objects

n this task you will extend your program from PP Task 6.1 (or CR Task 6.2 if you wish) to manage an array of objects of the class you created. The program can then add values to the array, output these values, and output some additional summary information.

Create and open in VS Code a new folder for your work and place a copy of your 6.1PP (or 6.2CR) program in it. Also open your 5.2PP program as you will copy and adapt some of its code.

You will need to make the following changes in your main program:

  • a method to enter records in bulk from user input via the console. This method will have parameters for a Scanner (to read user input), the array of objects, and the number of records to be added in bulk. It will use a loop to allow the user to enter details for that many entries. It can assume that the array is initially empty. This will be used as the first step in the program, after which the user will interact via a menu.
  • /adapt a displayAll method to print all of the objects in an array, one per line.
  • adapt a method to add a new object of your custom type to your array. This method will follow the naming convention of addRecordName used by the readRecordName method of 6.1PP.
  • Create two methods to calculate summary information from your objects.
  • Adjust main() to store an array of your objects, with a maximum capacity of 50.
    1. Read in the number of values the user wants to enter in bulk initially (store the number in numMeasurements, numExpenses, etc., whichever suits your chosen option, then...
    2. ...call your bulkAdd method to handle that, and then...
    3. ...show the user a menu.

Program: ContactsExample Constants: int CAPACITY, maximum number of objects, initialised to 50 Variables: Scanner in, to read user input Contact[] addressBook, contact list, initialised to hold CAPACITY elements int numContacts, number of contacts in the collection int choice, user's menu selection String searchFor, a contact name to search for (just part of this example) Steps: 1 Display "Contact Manager" 2 Prompt for "Enter initial number of entries (< " + CAPACITY + "): " and store in numContacts 3 Assign numContacts result of calling Math.min with numContacts, CAPACITY (That is, don't trust the user. Steps 2 & 3 may be combined) 4 Call bulkAdd with in, addressBook, numContacts (bulkAdd will allow the user to enter numContacts entires, one after another) 5 Do 5-1 | Display a menu "1. Add another contact | 2. Display contacts | 3. Find a contact | 4. Quit" 5-2 | Prompt user to "Enter option: " 5-3 | Assign choice value of next int from in (the Scanner) 5-4 | Switch based on choice 5-4-1 | | In the case that choice is 1: | | | Assign numContacts result of calling addContact with in, addressBook, numContacts 5-4-2 | | In the case that choice is 2: | | | Call displayAll with addressBook, numContacts 5-4-3 | | In the case that choice is 3: | | | Prompt for "Which contact name? " and store in searchFor | | | Display "Has contact: " + result of calling hasContact | | | with addressBook, numContacts, searchFor 6 While choice is not 4

Tip: If you've completed CR Task 4.3 User Input Functions then consider copying askForInt into your main program in order to display the menu and get the user's selection. (You can also copy the source file into your project and call askForInt via the UserInput class.) A related tip: Use in a String to represent a newline character.

Notes on the following pages indicate the summary data you should calculate for your program (i.e., instead of a search function) based on the different options you may have chosen in PP Task 6.1.

Adding a new complex value to the array

In PP Task 5.2 you read a String from the user and called the add method to append it to the array: add's role was managing whether and where a simple value was added to the array, but not to interact with the user. In PP task 6.1 you implemented a readTypeName method to interact with the user and create (via its constructor) a more complex object. In the present task we suggest you copy (from your WordManager.java source file) and adapt add() to be similar to the following (but with a name to suit your chosen option):

Method: int addContact(Scanner in, Contact[] addressBook, int numContacts) Returns: int, the numContacts of items in the address book; numContacts+1 if there was space to add Parameters: Scanner in, to read user input Contact[] addressBook, the list of Contacts int numContacts, the number of filled positions in the array Steps: 1 If there is space in the addressBook (numContacts < addressBook.length) then 1-1 | Assign addressBook[numContacts] the value of calling readContact with in 1-2 | Increment numContacts by 1 Else 1-3 | Display "No more room in the address book" 2 Return numContacts

Note how this differs from your add() method in WordManager: instead of being given the value to add it accepts a Scanner to interact with the user. However, it then delegates that task to readContact(), which interacts with the user and creates the Contact object to store.

We would call addContact from main() as follows:

numContacts = addContact(in, addressBook, numContacts);

which allows it to update the recorded numContacts of items if it actually added a value to the array.

Note: If your readTypeName method needs to use Scanner.nextLine() then, because it will always be reached after a call to nextInt() or similar, you will need an additional call to nextLine() to progress the Scanner past the end of the current line, ready to read the user's actual input. This work around looks like:

String someText; //will be a line of text //Assume aScanner is some Scanner object, previously used for nextInt() aScanner.nextLine(); //consume the after the user's last input someText = aScanner.nextLine(); 

Allow users to add Actors to the program, and use the following function to calculate the most valuable actor.

Method: Actor mostValuableActor(Actor[] actors, int numActors) Parameters: Actor[] actors, array of Actor objects int numActors, current number of items in the array Returns (result): Actor, the actor with the highest revenue potential Variables: Actor highest, the most valuable actor Steps: 1 Assign highest the revenue for the first element of actors 2 For int i is 1 to numActors - 1 (i.e., i < numActors) 2-1 | If the ith actor's revenue is larger than highest's revenue then 2-1-1 | | Assign highest the revenue of the ith element of actors 3 Return highest

Add a second method named averageActorRevenue to find the average value of the recorded actors.

Create a menu that allows the user to:

  1. Add another actor
  2. Display all actors, one per line
  3. Display most valuable actor
  4. Display average actor revenue
  5. Quit

The program should loop until the user chooses to quit.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Java Programming, Comprehensive Version

Authors: Y. Daniel Liang

10th Edition

133761312, 978-0133761313

More Books

Students also viewed these Programming questions

Question

Cite ways to overcome fear of failure.

Answered: 1 week ago

Question

Establish identity. cos( + k) = (-1)k cos , k any integer

Answered: 1 week ago