java assignment due this sunday

profilesehj
assignment4.html1.zip

Content/assignments/assignment4.html

COMP 1020

Material covered: Please ensure that you: Part A: Order up Part B: Order up, continued

Material covered:

  • Create a hierarchy of classes in Part A
  • Implement some simple sorts in Part B

top

Please ensure that you:

If you submit a solution that does not meet these requirements (for example, if the program does not run), you will lose some or all marks.

top

Part A: Order up  [10 marks]

Write a program that will be used to keep track of orders placed at a donut shop. There are two types of items that can be ordered: coffee or donuts. Your program will have a class for each order type, and an abstract superclass.

Coffee orders are constructed with the following information:

  • quantity (int) - how many coffees are being ordered
  • size (String) - the size of the coffees (all coffees in the order have the same size)

Donut orders are constructed with following information:

  • quantity (int) - how many donuts are being ordered
  • price (double) - price per donut (all donuts in the order have the same price)
  • flavour (String) - the flavour of donut (all donuts in the order have the same flavour)

The two order types need constructors and toString methods. The toString should include in its results the type of order ("Coffee" or "Donut"), plus the value of all instance variables.

The size of a coffee determines its price. There are three sizes, with the following prices:

  • small is $1.39
  • medium is $1.69
  • large is $1.99

You can assume that when you construct a coffee order, one of those three strings will be passed as the size of the coffees in the order.

Your classes should have a method called totalPrice(), which returns the total price of the order. Total price is generally equal to price per unit times the quantity; however, orders of donuts with a quantity of less than 6 should have a 7% tax added to the total price. There is no tax on coffee orders, or donut orders with 6 or more donuts.

Use inheritance to prevent duplicate code. Make all your instance variables private, and write getter/setter methods only when necessary. Do not store total price as an instance variable, as it can be calculated when needed.

Your program will read in orders from a text file, create objects, and store them in a single ArrayList of objects of your classes. Each line of the file starts with the word "Coffee" or "Donut", and then provides values for the instance variables for an order of that type, separated by commas, in the same order given above. There will not be any errors in the file.

Coffee,3,medium
Donut,7,0.89,chocolate

Once your program has read in the values and stored them in the ArrayList, it should do the following:

  • Print the complete contents of the list (i.e. all the orders). Show both the results of the toString() and the total price of that order.
  • Print the total quantity of donuts in all the orders, and the total quantity of coffees in all the orders.
  • Print the total of all the prices of all the orders.

Use the data file a4a.txt to test your program.

top

Part B: Order up, continued  [10 marks]

For this question, you will continue to refine your "Order up" program from the previous question.

The first change will be the addition of two new types of products that can be ordered: sandwiches and pop. This gives you a total of four products. These two new products will need new classes, plus additional modifications to the class hierarchy with one or more new abstract class(es).

Sandwich orders are constructed with following information:

  • quantity (int) - how many sandwiches are being ordered
  • price (double) - price per sandwich (all sandwiches in the order have the same price)
  • filling (String) - what's going in the sandwich (for all in the order)
  • bread (String) - the type of bread to use for the sandwich (for all in the order)

Pop orders are constructed with the following information:

  • quantity (int) - how many drinks are being ordered
  • size (String) - the size of the drinks (all drinks in the order have the same size)
  • brand (String) - the brand of pop being ordered

The new classes will need constructors and toString methods. The toString should include in its results the type of order, plus the value of all instance variables.

The size of a pop order determines its price. There are three sizes, with the following prices:

  • small is $1.79
  • medium is $2.09
  • large is $2.49

You can assume that when you construct a pop order, one of those three strings will be passed as the size of the drinks in the order.

Your classes should have a method called totalPrice(), which returns the total price of the order, calculated as before. All sandwich orders have 7% tax applied to their price, regardless of the order size. The drinks do not, and the donuts have the same rule as before (7% tax if there are fewer than 6 donuts in the order).

Use inheritance to prevent duplicate code. Make all your instance variables private, and write getter/setter methods only when necessary. Do not store total price as an instance variable, as it can be calculated when needed. The principles of good object-oriented programming will be enforced strictly in this assignment.

The second change will be the use of a collection class to store the data. The collection will still contain an ArrayList, but it should also include instance methods that ensure only your order subclasses can ever be inserted into the collection. You may also wish to put some of the processing in your methods for that class.

The third change will be the addition of a sort() method, which will be an instance method in your collection class. It should sort the items in the collection by total price, in ascending order (smallest to largest price).

Your program will read in orders from a text file, create objects, and store them in the collection. Each line of the file starts with the word "Coffee", "Donut", "Pop", or "Sandwich", and then provides values for the instance variables for an order of that type, separated by commas, in the same order given above. There will not be any errors in the file.

Coffee,3,medium
Donut,7,0.89,chocolate
Pop,5,large,Splat! Cola
Sandwich,1,3.89,mystery meat,37-grain whole wheat

Once your program has read in the values and stored them in the collection, it should do the following:

  • Print the complete contents of the list (i.e. all the orders). Show both the results of the toString() and the total price of that order.
  • Sort the order by calling the sort method.
  • Print the items again, with total price, as you did in the first step.
  • Print the total quantity of donuts in all the orders, all the coffees, all the pops, and all the sandwiches
  • Print the total of all the prices of all the orders.

Use the data file a4b.txt to test your program.

top

Content/css/comp_1020_new.css

@charset "utf-8"; /* CSS Document */ body {height: auto;margin-top: 10px; margin-left: 25px; margin-bottom: 10px;} #container { width: 780px; height:468px; padding-left: 47px; padding-top: 50px; padding-right: 35px; margin: auto; background-image: url(unit1.jpg); left: 38px; } #containersmall { width: 780px; height:468px; padding-left: 47px; padding-top: 0px; padding-right: 35px; margin: auto; background-image: url(unit1.jpg); left: 38px; } #banner { width:800px; height:100px; } .h1 { font-family:Arial, Helvetica, sans-serif; font-size:18pt; font-style:normal; font-weight:bold; color:#7eae37; margin-bottom:-10pt; } .h1b { font-family:Arial, Helvetica, sans-serif; font-size:18pt; font-style:normal; font-weight:bold; font-variant:small-caps; color:#000000; } .h1w { font-family:Arial, Helvetica, sans-serif; font-size:18pt; font-style:normal; font-weight:bold; font-variant:small-caps; color:#006699; } .h2 { font-family:Arial, Helvetica, sans-serif; font-size:13pt; font-weight:bold; color:#244062; font-style:normal; position: relative; background-color: #FFFFFF; } .h2b { font-family:Arial, Helvetica, sans-serif; font-size:12pt; font-weight:bold; font-variant:small-caps; color:#999966; font-style:normal; } .h3 { font-family:Arial, Helvetica, sans-serif; font-size:11.5pt; font-style:normal; font-weight:bold; color:#244062; margin-bottom:-7pt; } .h3b { font-family:Arial, Helvetica, sans-serif; font-size:16pt; font-style:normal; font-weight:bold; font-variant:small-caps; color:#336699 } #page { width:780px; height:994px; left: 7px; top: 166px; padding-left: 10px; padding-right: 10px; } .body { font-family:Arial, Helvetica, sans-serif; font-size:10.5pt; font-style:normal; font-weight:normal; color:#000000; line-height:14pt; } .body_box { font-family:Arial, Helvetica, sans-serif; font-size:10.5pt; font-style:normal; font-weight:normal; color:#000000; line-height:14pt; } .body_bold { font-family:Arial, Helvetica, sans-serif; font-size:10.5pt; font-style:normal; font-weight:bold; color:#006699 } .body_bold_box { font-family:Arial, Helvetica, sans-serif; font-size:10.5pt; font-style:normal; font-weight:bold; color:##006699 } .body_dr_green { font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-style:normal; font-weight:normal; color:#336600 } .definition { font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-style:italic; font-weight:normal; border-width:.25pt; border-style:solid; border-color: #CCCCCC; border-collapse:separate; empty-cells:hide; } .toc { font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-style:normal; font-weight:normal; color:#669933 } a:link {color:#0000FF; text-decoration:underline; font-family:Arial, Helvetica, sans-serif;} a:visited {color:#0000FF; text-decoration:underline;font-family:Arial, Helvetica, sans-serif;} .top { font-family:Arial, Helvetica, sans-serif; font-size:8pt; font-style:normal; font-weight:normal; } p.ind { text-indent:-20px; margin-left:20px; font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-style:normal; font-weight:normal; color:#000000 } .quote { margin-left:40px; margin-right:40px; font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-style:normal; font-weight:normal; color:#000000 } .blackBox { font-family:Arial, Helvetica, sans-serif; font-size:10.5pt; background-color:#FFFFFF; border: 1.5px solid #000; padding-top: 10px; padding-left: 15px; padding-right: 5px; padding-bottom: 10px; margin-right: 200px; margin-left: 3px; } .blueBox { background-color: #daeef4; border: 1.5px solid #FFF; padding-top: 10px; padding-left: 15px; padding-right: 5px; padding-bottom: 10px; margin-left: -10px; margin-right: -15px; } .yellowBox { font-family:Arial, Helvetica, sans-serif; font-size:10.5pt; background-color: #FFFF99; border: 1.5px solid #FFF; padding-top: 10px; padding-left: 15px; padding-right: 5px; padding-bottom: 10px; margin-left: -10px; margin-right: -15px; } .blackBorder { font-family:Arial, Helvetica, sans-serif; font-size:10.5pt; background-color: #FFFFFF; border: 1.5px solid #00000; padding-top: 10px; padding-right: -17px; padding-bottom: 14px; margin-right: 200px; margin-left: 3px; } .blackBorderwy { font-family:Arial, Helvetica, sans-serif; font-size:10.5pt; background-color: #FFFFCC; border: 1.5px solid #00000; padding-top: 10px; padding-right: -17px; padding-bottom: 14px; margin-right: 200px; margin-left: 3px; } .blueBorder { background-color: #FFFFFF; border: 1.5px solid #669966; padding-top: 10px; padding-right: -17px; padding-bottom: 14px; margin-right: 200px; margin-left: 3px; } .biol_head { font-family:Arial, Helvetica, sans-serif; font-size:12pt; font-style:normal; font-weight:bold; color:#FFFFFF; } .body_white { font-family:Arial, Helvetica, sans-serif; font-size:9pt; font-style:normal; font-weight:normal; color:#FFFFFF } .body_white_bold { font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-style:normal; font-weight:bold; color:#FFFFFF }

Content/css/COMP1020.css

@charset "utf-8"; /* CSS Document */ body {margin-top: 10px; margin-left: 25px; margin-bottom: 10px;} #container { width: 780px; height:591px; padding-left: 47px; padding-top: 50px; padding-right: 35px; margin: auto; } #banner { left:25px; top: 10px; width:800px; height:100px; padding-bottom: 30px; } .heading_banner { margin: 0px 0px 0px 0px; padding: 20px 0px 0px 30px; font-size: 34px; font-weight: 400; font-family: Arial, Helvetica, sans-serif; color: #FFF; height: 100px; width: 90%; line-height: 1.1em; opacity: 0.75; letter-spacing: 0.05em; position:absolute; } .h1 { font-family:Arial, Helvetica, sans-serif; font-size:18pt; font-style:normal; font-weight:bold; font-variant:small-caps; color:#336600; margin-bottom:-9pt; } .h1b { font-family:Arial, Helvetica, sans-serif; font-size:18pt; font-style:normal; font-weight:bold; font-variant:small-caps; color:#000000; } .h1w { font-family:Arial, Helvetica, sans-serif; font-size:18pt; font-style:normal; font-weight:bold; font-variant:small-caps; color:#FFFFFF; } .h2 { font-family:Arial, Helvetica, sans-serif; font-size:12pt; font-weight:bold; font-variant:small-caps; color:#336600; font-style:normal; position: relative; margin-bottom:3pt; } .h2b { font-family:Arial, Helvetica, sans-serif; font-size:12pt; font-weight:bold; font-variant:small-caps; color:#336699; font-style:normal; } .h3 { font-family:Arial, Helvetica, sans-serif; font-size:16pt; font-style:normal; font-weight:bold; font-variant:small-caps; color:#336600; margin-bottom:-7pt; } .h3b { font-family:Arial, Helvetica, sans-serif; font-size:16pt; font-style:normal; font-weight:bold; font-variant:small-caps; color:#336699 } .body { font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-style:normal; font-weight:normal; color:#000000; } .body_bold { font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-style:normal; font-weight:bold; color:#000000 } .definition { font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-style:italic; font-weight:normal; border-width:.25pt; border-style:solid; border-color: #CCCCCC; border-collapse:separate; empty-cells:hide; } a:link {color:#336699; text-decoration:underline; font-family:Arial, Helvetica, sans-serif;} a:visited {color:#669933; text-decoration:underline;font-family:Arial, Helvetica, sans-serif;} .top { font-family:Arial, Helvetica, sans-serif; font-size:8pt; font-style:normal; font-weight:normal; } p.ind { text-indent:-20px; margin-left:20px; font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-style:normal; font-weight:normal; color:#000000 } .quote { margin-left:40px; margin-right:40px; font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-style:normal; font-weight:normal; color:#000000 } .blueBox { background-color: #336699; border: 1px solid #336699; padding-top: 10px; padding-right: 5px; padding-bottom: 10px; padding-left: 10px; } .blueBorder { background-color: #FFFFFF; border: 1px solid #336699; padding-top: 10px; padding-right: 5px; padding-bottom: 10px; padding-left: 10px; } .biol_head { font-family:Arial, Helvetica, sans-serif; font-size:12pt; font-style:normal; font-weight:bold; color:#FFFFFF; } .body_white { font-family:Arial, Helvetica, sans-serif; font-size:9pt; font-style:normal; font-weight:normal; color:#FFFFFF } .body_white_bold { font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-style:normal; font-weight:bold; color:#FFFFFF } .Highlight { background-color: #FFFF00; border: solid thin black; margin-top: .5em; margin-bottom: .5em; margin-right: 20px; margin-left: 20px; padding-top: .5em; padding-bottom: .5em; padding-left: 1em; padding-right: 1em; } .Aside { background-color: lightGrey; border: solid thin black; margin-top: .5em; margin-bottom: .5em; margin-right: 20px; margin-left: 20px; padding-top: .5em; padding-bottom: .5em; padding-left: 1em; padding-right: 1em; } .Action { background-color: #FFFFC0; border: solid thin black; margin-top: .5em; margin-bottom: .5em; margin-right: 20px; margin-left: 20px; padding-top: .5em; padding-bottom: .5em; padding-left: 1em; padding-right: 1em; } .key { background-color: #a9a9a9; color: white; border: solid thin black; font-style: italic; } .menu { background-color: #a9a9a9; color: white; border: solid thin black; } .version { color: olive; font-style: italic; } code { font-family: "Lucida Sans Typewriter", Courier, monospace; color: #222200; background-color: #FFFFE0; } .code { font-family: "Lucida Sans Typewriter", Courier, monospace; background-color: #FFFFB0; border-top: dotted thin black; border-bottom: dotted thin black; padding-top: 1em; padding-bottom: 1em; padding-left: .5em; padding-right: .5em; margin-right: 20px; margin-left: 20px; } .Aside .code { margin-right: 10px; margin-left: 10px; } pre.code { white-space: -moz-pre-wrap; /* Mozilla, supported since 1999 */ white-space: -pre-wrap; /* Opera 4 - 6 */ white-space: -o-pre-wrap; /* Opera 7 */ white-space: pre-wrap; /* CSS3 - Text module (Candidate Recommendation) http://www.w3.org/TR/css3-text/#white-space */ word-wrap: break-word; /* IE 5.5+ */ } dfn { font-weight: bold; color: brown; } .Example { background-color: aqua; border: solid thin black; margin-top: .5em; margin-bottom: .5em; margin-right: 20px; margin-left: 20px; padding-left: .5em; padding-right: .5em; } .ref { color: red; font-style: italic; } .filename { font-family: Arial, Helvetica, sans-serif; color: green; text-decoration: underline; } .output { font-family: "Lucida Sans Typewriter", Courier, monospace; color: purple; font-style: italic; } table.bordered { border-width: thin; border-style: solid; border-spacing: 0; } table.bordered td { border-width: thin; border-style: solid; border-spacing: 0; padding-left: 4px; padding-right: 4px; } table.bordered th { border-width: thin; border-style: solid; border-spacing: 0; padding-left: 8px; padding-right: 8px; } th.side { font-weight: bold; color: black; background-color: #ffd0d0; } th.right { font-weight: bold; color: black; background-color: #ffffd0; } th.top { font-weight: bold; color: black; background-color: #ffd0d0; text-alignment: center; } th.side code { background-color: inherit; } th.top code { background-color: inherit; } .InlineTitle { font-weight: bold; } .view { background-color: black; color: white; border: solid thin black; padding: 0.25em 1em; text-decoration: none; } a.view { background-color: black; color: white; border: solid thin black; } a.view:hover { background-color: blue; color: yellow; text-decoration: none; } /*added for IE as above (hover) does not work in IE*/ a.viewOn { background-color: blue; color: yellow; border: solid thin black; padding: 0.25em 1em; text-decoration: none; } ol li { margin-top: 0.3em; margin-bottom: 0.3em; } #footer {margin: 0px 0px 0px 0px; border: thin solid red;}

Content/images/comp1020_banner.jpg

Content/assignments/standards.html

COMP 1020 Programming Standards

This document lists the programming standards that you must follow for the programming questions of your assignments. Failure to follow these standards will result in the loss of marks.

Commenting files and methods

  1. Each of your program files must begin with a comment block like the following: /**  * Name of class or program (matches filename)  *  * COMP 1020 SECTION Axx  * INSTRUCTOR    Name of your instructor  * ASSIGNMENT    Assignment #, question #  * @author       your name, your student number  * @version      date of completion  *  * PURPOSE: what is the purpose of your program?  */
  2. If the purpose of a method is not self-explanatory, write a prologue comment at the beginning of the method, similar to the following: /**  * PURPOSE: Describe the purpose of the method. Clearly.  * Indicate how the parameters and return values are used.  * For instance methods, describe the effect on the object state.  */

    or:

    /**  * Describe the purpose of the method. Clearly.  * For instance methods, describe the effect on the object state.  *  * @param firstParameter  description of what firstParameter means  *                        indicate if the value is modified, and why  * @param secondParameter description of what secondParameter means (as above)  *  * @return                what is the meaning of the return value?  */

Writing readable code

  1. Group the contents of your class consistently. Always place data before behaviours. The following order is recommended: class Something {    // Instance variables    // Static variables    // Class-level constants    // Constructors    // Other instance methods    // Static methods: main comes first, if it exists }
  2. Each assignment question should be submitted in a separate Java source code file. Place the "main" (public) class first and then add any other classes after the main class, as shown below. public class A2Q1 {   public static void main(String[] args) {     ...   }      ... } class Worker {   ... }
  3. Use blank lines to separate blocks of code and declarations to improve readability. In particular, use blank lines between declarations and other code, and between methods.
  4. Comment blocks of code. Describe why you wrote the code this way, not what each line does. For example: // Summarizes taxes in each category (only federal so far) System.out.println("The taxes payable are:"); System.out.print("Federal tax = "); System.out.println(federalTax);
  5. Use meaningful but reasonable variable names. Start in lower case and capitalize only the first letter of each new word.
    • a — Very bad. Too short, not meaningful.
    • average — Good if there is only one possible average
    • averageMark — Good
    • average_of_all_the_marks_in_the_list — Bad. Too long and wordy.
  6. Use consistent indentation to clarify control structures (e.g. loops and if constructs). Levels of indentation should clearly indicate the depth of nesting. For example: class MyClass {   public static void someMethod() {     statement;     while (...) {       statement;       if (...) {         statement;         ...       }       statement;     }     statement;   }   public static int anotherMethod() {     ...   } }

    Align else with the corresponding if for readability. Common styles include:

    if (...) {     statement;   ... } else {     statement;   ... }

    or:

    if (...) {     statement;   ... } else {     statement;   ... }

Avoid long lines; where needed, continuations of a statement on a new line should be indented too. Any readable and consistent style is acceptable. The essential features are that all statements that are nested within another statement must be indented, and that the braces { } must be in predictable and consistent positions.

Writing maintainable and extendable code

  1. Avoid the use of literal constants ("magic numbers") in your program. Generally use constant (final) identifiers rather than literal constants in your program. Acceptable exceptions are strings that appear only once, and small fundamental values. For example:
    • sum = 0; — Literal constant 0 is OK
    • count = count + 1; — Literal constant 1 is OK
    • price = total * 1.07; — Magic values are not proper. Create a named constant like PST_RATE = 1.07.
    • lastDigit = accountNumber % 10; — Literal constant 10 is OK.
    • while (!command.equals("Quit")) {   ...   if (command.equals("Quit")) ... — Duplication is not proper. Create a named constant like QUIT_COMMAND = "Quit".
  2. Declare variables with as small a scope as possible. Use instance variables for object properties, local variables for method-specific data, and variables declared in for loops for counters. Use static variables only where absolutely necessary.
  3. Values of type float or double should not normally be compared with == or !=.
  4. Do not use break or continue statements (unless you are using a switch control structure) without the explicit permission of your instructor.
  5. Do not use multiple return statements in the same method without the explicit permission of your instructor.
  6. Never change the value of a for loop variable inside the loop.
  7. Use the best possible construct. For loops are only used when the number of iterations is known in advance; use a while or do/while for non-deterministic loops.
  8. Avoid unnecessary duplication of code. Use methods and inheritance wherever possible to prevent code duplication.
  9. Use appropriate language-specific naming standards for Java. Class names must start with InitialCapitalLetters, constants are ALL_UPPER_CASE, and all other identifiers use initialLowerCase.
  10. There should not be any processing performed in the main method; main should only call the methods that perform the actual processing. (This is also true for constructors.) Thus, the main method will normally consist of several calls to the processing methods. Use meaningful method and variable names. For example, printFlights(flights); instead of output(f);

Input and output

  1. All output produced on the console must have appropriate titles and headings. Output that is sent to a file typically does not require titles or headings.
  2. Print console output neatly. Use tabular output where appropriate.
  3. Print a message on the console at the end of the program that indicates whether or not the program completed successfully; e.g. End of processing. This message should be printed by the last statement in your main program.

Classes and objects

  1. Never perform any printing inside a toString() method. The toString() method always returns a String that contains information that describes an object.
  2. Perform as much processing as is possible inside objects. For example, if you have to compare the value of an object's instance variable with some other value, do not extract the value of the instance variable from the object and compare it with the other value outside of the object; instead, define a method such as compareValue(double value) inside the object. The method performs the comparison and returns the result.
  3. Make your access specifiers as restrictive as possible. Private things should be private, including all data members.

Content/assignments/a4a.txt

Coffee,3,medium Donut,7,0.89,chocolate Donut,3,1.19,eclair Coffee,1,large Coffee,2,large Coffee,7,small Donut,6,0.89,baker's choice Donut,1,1.10,lemon buster Donut,120,0.15,petite oeufs Coffee,10,medium

Content/assignments/a4b.txt

Pop,5,large,Splat! Cola Sandwich,1,3.89,mystery meat,37-grain whole wheat Sandwich,3,2.99,squeeze cheese,wonderful bread Donut,1,0.15,petite oeufs Pop,2,small,Sugar Water Coffee,3,medium Donut,7,0.89,chocolate Pop,1,medium,Splat! Cola Donut,3,1.19,eclair Coffee,1,large Sandwich,1,5.99,club,rye Coffee,2,large Coffee,7,small Sandwich,1,4.59,chicken of the sea,seaweed Donut,6,0.89,baker's choice Donut,1,1.10,lemon buster Sandwich,2,4.99,peanut butter and bacon,bagel Donut,120,0.15,petite oeufs Pop,3,medium,Fresssh Coffee,10,medium