Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java - object oriented programming Part A Consider the Loops class. Compile and run the program. Each of the methods in the Loops class has

Java - object oriented programming image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Part A Consider the Loops class. Compile and run the program. Each of the methods in the Loops class has a comment explaining what the method is intended to do. None of the methods work as intended. Please complete the following task: Directly under each of the methods loop2(), loop30), loop40), loop5() and loop() write three comments: The first comment should start with "PROBLEM" and it should detail what the method is doing that it should not be doing, or conversely, what the method is not doing that it should be doing. The second comment should start "DIAGNOSIS," and should explain why the method has the problem described in the first comment The third comment should start "CORRECTION" and should explain how the method can be corrected so that it works as intended. You may find that for most methods you will only need to write a sentence or two for each comment, some may take more work to document, but do not write more than five sentences for each comment. Note that the first method in the Loops class, loop10), has been completed for you as an example, [30 marks] The questions [48 marks] This question asks that you write six methods. Part() describes one method. part (II) three methods, part (1) one method, and part (V) one method. Note that the fifth statement in the create() method in the Receipt Generator class invokes a method written to make sure that the name entered by the user is capitalised appropriately stateName - capitalise (stateName). The capitalise(String) method takes a string parameter and returns a string. The method makes sure that the name entered by the user starts with a capital letter, with the rest of the name in lower case. Note that the capitalise(String) method uses String methods, such as substring and to UpperCase() that you will find documented in the String API. There is a TODO comment in the create() method that reads: // TODO client wants the item description in title case Different authorities have slightly different rules for title case, but title case means, in general, that every word is capitalised (meaning the word starts with a capital letter, but the rest of the word is in lower case), except for articles (a, an, the). prepositions (e.g. in, on) and conjunctions (eg, and on. In this case the client wants every word capitalised except for the words: a: an, and the, of with This means that if the user were to type: REPLACE BATTTERY WITH A NEW BATTERY for the item description, what would be printed on the receipt would be: Replace Battery with a New Battery Write a method that will apply the above rules to a Bring, and invoke it such that the item Description variable will be put into title case before it is added to the receipt. Make sure that each word in the item Description String is separated by exactly one space, even when the user accidentally enters extra spaces between words. Also make sure to get rid of any leading spaces accidentally entered by the user before the tem description starts You may find it helpful to read the Soring API the link is given in the reading below, in order to find useful String methods. If you have a loop in your method, it should be a while do/while or Eor loop is a regular expression You may find it useful to know that that means one or more spaces. e scaces AR There are three more TODO comments in the user interaction loop in the create() method. These three comments reference turning each one of the three blocks of code in the loop (separated from each other by blank lines) into methods. Write these three methods. You should write a method to get user input for each item the customer is to pay for, a method to update the receipt with the user information about each item, and a method to ask the user if they wish to enter another item (goods or services). Once you have written your three methods, change the user interaction loop to have just three statements, each invoking one of the new methods that you have written. Once you have done this, your user interaction loop should be as follows: statement to invoke a method to ask the user for an item description and charging information for the item statement to invoke a method to update the receipt with the information just entered by the user about an item keepGoing = an invocation of a method that asks the user if there is another item to be charged for records their response and returns an appropriate value Note that some of the variables in the user interaction loop are local to the create() method. This means that they are only in scope inside the create() method. In fact, since they are declared inside the user interaction loop, they are only in scope inside the loop. Attempting to use them in another method will give a compilation error. Class variables, however, are in scope anywhere in the class. (in) After the user interaction loop, there are two fine TODO comments in the created method. The first one provides detailed description of a new method to ask the user to enter the deposit, and to enforce that the deposit is neither too smalt should be at least 20 per cent of the amount held in the total variable) nor too large (no more than the amount held in the total variable). The minimum deposit amount should be rounded to an int before being shown to the user. Normal rounding applies which means that the value after the decimal place is 5 or above the number is rounded up, and below 5 rounded down Write the method to ask for validate and return the deposit amountIf the user enters an amount that is too low they shoud see a message telling them that the amount they have entered is too low the number they enter is too high, they should see a message telling them the the number they entered was too high, in each case they should then be asked to re-enter an amount. The method will ask repeatedly for a valid amount until it receves one Your method will need to have a loop and it should be a while do/whale or Forloop When you have written your method change the statement paid get Double FromDeramount paidata deposit by the customer paid - invocation of your new method to ask for validase and turn the deposit Before you write this method, make sure that you understand the comments written in the genomser String and ge!Double From UserString methods (iv) The final TODO comment in the create() method is about validating the payment method type: 7/TODO validate this input. See PAYMENT TYPES PAYMENT_TYPES is an Array of allowed payment types. It is a constant, meaning that the Array cannot be updated by a method. Write the method to validate payment types. Your method should check the payment type entered by the user to see if it is an entry in the PAYMENT_TYPES Array. If it is in the Array it is accepted, if not it is rejected and the user is asked again for a valid entry. Your method will need a loop and it should be a while, do/while or for loop Please make sure that your method does the following: I Accepts any valid String. A valid String is a String that is spelt correctly, and is a member of the PAYMENT_TYPES Array II. Prompts the user with a list of all acceptable types if their first entry is not valid. III. Works correctly even if the developer should add some entries to the PAYMENT_TYPES Array IV Returns valid entries entirely in lower case with no leading or trailing spaces Page 10 of 22 Notes on points EN Make sure comparisons do not fall for trivial reasons. It the user enters their payment type with leading or traming spaces, the method should remove them before validating the word. If the String entered by the user is apelt correctly and is an item in the Array, it should be accepted. This means that however the user capitalises their entry, if the String can be transformed to all lower case, and if this new String corresponds to an item in the Array, then the user entry should be accepted. For example, the user might enter cash, CASH, Cash or even CASH - the method should validate each of these entries. See the example run and output of the program in Appendix C for how asking the user for a valid entry for their second and subsequent entries should look. Your method should be written in general terms, such that whatever the length of the PAYMENT_TYPES Array the method will still work as it should Recall that this is not the final version of the class, hence the developer may add to, or delete from the PAYMENT_TYPES Array in a later version of the Receipt Generator class Changes to the length of the Array should not mean that the method needs any changes Working as it should means a valid entry will be accepted, an invalid entry rejected, and if the user is asked to enter a second or subsequent payment type then they are shown a complete list of valid entries The client wants the payment type to appear on the receipt in lower case for example PAID taasti Once you have written your method, invoke it such that the statement Payment Methodet Fronter deposit payment method becomes Payment Methodnotation HER H ent to ou neethod When writing your new methods, make sure to follow the advice glven about readability in appendix A, sections 1.2-1.6. In particular try to give your mothods names that describe their actions, and please format your work in a way that is both consistent and readable. The Receipt Generator class that you have boon given is formatted in a readable way, including Indenting method codo, loops and if/else statements to show the control structure plus making it easy to pick out the start and end of loops, methods and the class itself. Good names are partly subjective, so the examiners will accept any reasonable attempt but clearly method names such as method 10 or method20) will lose readability marks since they tell the reader nothing about the function of the method. In addition any student submitting a Receipt Generator class with poorly formatted code will lose marks for this question. [6 marks) Note that poorly formatted code will be considered to be code that is formatted in such a way that the control structure is unclear. For example: 1/Good formatting private static String getShopLocationFromUser() System.out.println("SHOP LOCATIONS:"); for (int i 0; i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions