Java Homework 8: your first OOP Programming
General description for homework 8:
In this homework, you need to first develop a class using OOP principles: data abstraction and encapsulation, and then create an application class to make use of the encapsulated class developed above. Therefore, after finishing this homework, you demonstrate your skill in the development and use of OOP class.
Here is the detail description for homework 8:
Create a Java project in Eclipse. This project has two java files (classes): House.java and JohnDoeHw8.java :
First, create a new java file named House.java. At the beginning of this file, add an appropriate program header similar to what you did in “homework 1--part B--step 3”. Then, right after the program header is the definition line for public class House. Then in the body of this class House, do the following:
1. define two private data member: total price of double type (for example, a value of 168000 indicates that this house is worth 168000 dollars), and total square feet of double type (for example, a value of 2398 indicates that this house has 2398 square feet in total).
2. define two public constructors, the first one is the default constructor with no parameter, and the second one has two parameters to initialize the two private data members of this class.
3. for every data member, provide a public getter and a public setter, so there are two getters and two setters in total. Refer to textbook sec 8.9 for formal definition of getter and setter. Or refer to the modified TV.java for examples of getter and setter.
4. provide a public method named calcPricePerSqf . This method returns the average price per square foot of the house by dividing the total price by the total square feet. The return type of this method is double. This method does NOT need any formal parameter, because as a public member method, this method can directly access the private data members of the same class.
5. provide a public method named calcPropertyTax. This method returns a double type value, which is the property tax of this house. This method needs only ONE formal parameter, which is a double type variable named taxPercentage. The property tax of the house is calculated by multiplying the total price of the house (a private data member of the class) with the tax percentage (the formal parameter of this method). For example, if the tax percentage is 1.25 percent for a house of 160000 dollars, then the property tax is 160000 * 1.25 * 0.01 = 2000 dollars. Notice that the 0.01 in the above formula is to convert the 1.25 percent to the real value. Attention: taxPercentage is a formal parameter of this method, and it is NOT a data member of the House class, therefore, do not make the mistake of setting taxPercentage as a private data member of the House class, and if you do that, you are in the wrong track!!!!
Secondly, create a new java file named JohnDoeHw8.java, which has the application class JohnDoeHw8 (replace JohnDoe with your first and last name). At the beginning of JohnDoeHw8.java, add an appropriate program header as what you did in “homework 1--part B -- step 3”. This class has only one method name main, and in the first line of the main method, insert a statement to output your name, course section and semester info, and refer to “homework 1 -- part B -- step 4” for the same requirement of this output line. Then do the following:
1. create two House objects using the constructor with two parameters, plug in the data below as the actual parameters of the constructor. Each line below contains the two actual parameters for one object:
, where the 1st column is total price, and the 2nd column is total square feet. Refer to the modified TestTV.java file, on how tv3 object is created.
2. create the third House object using the default constructor. Then ask the user to input the two parameters with a Scanner type variable, and no GUI input is needed. Then use the setters to set the user input values into the third object object. No data type or range checking of the input parameters is needed. Refer to the modified TestTV.java file, on how tv1 object is created.
3. ask the user to input a tax percentage. For example, a user input of 1.25 means 1.25%. This property tax value will be used in the next step to calculate the property taxes for all three house objects.
4. For each individual House object, invoke two methods: calcPricePerSqf () and calcPropertyTax() and store the results in two local variables, respectively. For method calcPropertyTax, you need to plug in the tax percentage variable obtained in step 3 above. In summary, you have 6 variables that store the results for three house objects.
5. For each house object, output its total square feet, total price, average price per square feet, and the property tax. If the output information exceeds 80 columns, you need to output them in multiple lines, with each line less than 80 columns. Use System.out.printf() method, and format the total square feet with one digits after the decimal point, and the other three output items with two digits after the decimal point (because they are money values). Assume that the total square feet in each house object is less than 10000 square feet, and the total price of each house is less than 1000000 dollars.
• No need to use array in this homework.
• File House.java contains only one class named House, and file JohnDoeHw8.java contains only one class named JohnDoeHw8. File House.java and file JohnDoeHw8.java are two different java files.
• In file House.java, you need to have two private data members, and all methods are public, including: two constructors; one setter and one getter for each private data member; method calcPricePerSqf () and method calcPropertyTax().
• When coding House.java, please refer to file “CorrectSequence.java” in folder “chap8” for the correct sequence of components in an encapsulated class. The modified TV.java follows the correct sequence strictly, which also serves as a reference.
• In file you have only one method: the main method.
• The signature and return type of the methods in these two classes have already been defined in the above homework descriptions, and you cannot change the signature and the return type of any method.
• Add suitable comments in the source code.
• Please read the above implementation details carefully BEFORE start coding.
• As a general reference, read the modified source code files: TV.java and TestTV.java on how to code class with OOP principle and how to code application class that uses OOP class.
• After finishing coding, you need to debug your program and test it multiple times with different input parameters supplied for each test run.Attachments