Hi, I am new here. Java is my first programming class. How do you do the muffler program for Java project? I need more help on this.
Project Requirements:
1. Develop the beginnings of a simple invoicing program for a Muffler Shop. We will have a Muffler class representing an individual muffler ordered by a customer. We will also have a test class (driver program) for the Muffler class.
2. Design and build a Muffler class. This will have three instance variables. There will be a customer variable that will hold the customers last name. We will have a priceCode variable that will identify the size and warranty class of the muffler. We will also have a cost variable that will hold the cost of the muffler given the pricing code. This class should have a constructor, accessors for the instance variables, a mutator for the customer, a method to compute the cost, a method to update the price code, and a method to convert the state of the object to a string. Using the UML, the class diagram looks like this:
Muffler
customer : String
priceCode : String
cost : double
Muffler()
Muffler(String, String)
getCustomer() : String
getPriceCode() : String
getCost() : double
setCustomer(String newCustomer)
computeCost()
updatePriceCode(String)
toString(): String
1. The default constructor will set the instance field to a default value.
2. The parameterized constructor will assign the first parameter to the variable customer and the second to the priceCode variable. The constructor will call computeCost() to convert the price code to the numeric cost and store it in cost.
3. The class will have three accessor methods getCustomer(), getPriceCode(), and getCost(). Each will return the value of the respective instance variable.
4. The class will have an accessor method for customer.
5. The method computeCost() handles the conversion of the price code to cost and saves it in the instance variable cost. Price codes consist of a size letter (A through F) and an optional warranty modifier. Base price of a muffler is $39.95 installed and as the size increases the cost changes according to the table below
Size Code Price Factor
A 1.00
B 1.07
C 1.15
D 1.22
E 1.38
F 1.50
6. The warranty modifier indicates that there is a either a three-year warranty (3) or a lifetime warranty (L). A three-year warranty adds $2.00 to the base price, while a lifetime warranty adds $5.00 to the base price. Note that the smallest muffler cannot have a lifetime warranty and the largest must have either a three-year or lifetime warranty. The cost should be rounded to the nearest penny. cost = (baseprice + warranty) * price factor.
7. There will be 16 possible price codes: A, A3, B, B3, BL, C, C3, CL, D, D3, DL, E, E3, EL, F3, FL. Remember AL and F are invalid.
8. Use the switch statement to determine the proper price factor. Use the if/else construction to handle the warranty modifiers.
9. The class will have a method updatePriceCode(String) that takes a new price code and updates the priceCode and cost variables accordingly. Use the computeCost() method to update the cost. Note that we can call methods of a class from within the class; in this case, we do not use the dot (.) operator. Methods which are useful to other methods are called utility methods, and do not need to be public if they will only be called from within the class. The computeCost() method is a utility method.
10. The method toString() allows us to access the state of the object in a printable or readable form. It converts the variables to a single string that is neatly formatted. Look at pages 152 and 153 of the text for a discussion of escape sequences. These are characters that you can insert into your strings and, when printed, will format the display neatly. You can insert an escape sequence for the tab character and get a tabular form when printing. This tab character is '\t'. Your class will have a toString() method that concatenates customer, priceCode, and cost separated by tab characters and returns this new string. When you try to print an object, the toString() method will be implicitly called. For example, if Jones bought a D3 muffler, the output would be:
Jones D3 $51.18
3. Build a class MufflerTest that will test the constructor and all the public methods and constructors of the class Muffler. This will run as an application. Generate objects that have each of the possible values for a price code and print each object by implicitly calling the toString() method on each object. Remember to test for invalid price codes. Invalid price codes should display an error message and set the cost to $9999.99. No user input, just create objects with the price code in the constructor call
Muffler cust=new Muffler("David","C3");
Implementation Notes:
• You should use both the switch and the if/else constructions in this program. The switch construction is good when you are checking a single variable against a number of different discrete values. A character variable (char) is discrete (you have one of a set of characters only) as are integral types (int). Strings and floating-point numbers are not discrete and will not work with the switch statement. if statements can be used anytime you have a relational operation such as equality, less than, etc. But you need to be careful when using a long series of if {...} else {...} constructs because of the confusion factor (Which if does this else go with? Where am I in this sequence of tests?).
• Strings have many behaviors available. These behaviors include finding an individual character in the string, telling how many characters are in the string and converting the contents of the string to either upper or lower case. You will need to use the charAt(int i) method to extract the size code and warranty modifier from the priceCode string to work with. For example, suppose you have a Java statement:
String x = "This is a string.";
You can extract the period at the end of the statement by using the Java statement:
char period = x.charAt(16);
This will extract the 17th character in the string and place it in the variable period. Remember that string numbering starts at 0, not 1. A better way to do this would be to use the method length(), which tells us how many characters are in the string. So we can use it to tell us where the period is since we know it is the last character (in this specific case) in the string. So the Java statement:
int howLong = x.length();
tells us the number of characters. But the numbering starts at zero if we want to access them, so we have to reduce howLong by 1 to get to the last character. Then we could do:
char period = x.charAt(howLong - 1);
to get the same result as in statement b above. Experienced programmers would probably simplify the construction slightly more by eliminating the variable howLong if this is the only use of it. We could then re-write it as:
char period = x.charAt(x.length() - 1);
Note how we are nesting method calls inside of other method calls. This can be a very powerful programming technique.
Submission Requirements:
Your project must be submitted using the instructions below. Any submissions that do not follow the stated requirements will not be graded.
1. Follow the submission requirements of your instructor.
2. You should have three files for this assignment:
a. Muffler.java - The Muffler class,
b. Muffler.html - The documentation file generated from your class (you do not have to turn this file in),
c. MufflerTest.java - A driver program for your Muffler class.
d. Self graded Grade Sheet (+ 5 pts)
3. Remember to compile and run your program one last time before you submit it. If your program will not compile, the graders will not be responsible for trying to test it.