View Question
 Question
 Subject: Java Programming Category: Computers > Programming Asked by: math01-ga List Price: \$80.00 Posted: 27 Feb 2004 12:30 PST Expires: 28 Mar 2004 12:30 PST Question ID: 311492
 ```Solve the following six problems: 1. Write a Java class description that will represent a Fraction. A Fraction can be formed as the ratio of two integer values, the numerator and the denominator. Your class should keep these two values protected from modification. Your class definition should include three constructors. When no arguments are provided, construct the number 0/1. When one integer is provided, construct the fraction using the argument as numerator, and one as a denominator. When two integer arguments are provided, use then as numerator and denominator. Define public accessor methods for the numerator and denominator. Define a method named multiply. This method should take as argument another fraction, and return the fraction that represents the multiplication of the receiver fraction and the argument. 2. Consider various types of buildings (house, store, factory). Show two different types of hierarchy. One hierarchy should illustrate the is-a relationship between various levels, and another hierarchy should illustrate has-a relationships. 3. Write a Java class hierarchy for the three concepts Shape, Square, and Circle. The class Shape is a parent class, with Square and Circle as child classes. A Square will take a height and width as argument to the constructor, while a Circle will take a radius. Define a method named Area what will return the area of a shape. This method should be deferred in the parent class, and defined in the child classes. 4. Present a high-level design, described in English and diagrams, of the software that will support a vending machine. The vending machine provides coffee, tea, and soup. You can have cream or sugar with coffee or tea, but not with soup. Coffee costs 45 cents; tea costs 35 cents, and soup 65 cents. Your machine accepts coins, and will make change. There is a coin return button, which will cancel the current transaction and return the users money. Provide CRC description of the major components of your system. Provide interaction diagrams for two scenarios. In the first, a patron enters three quarters (75 cents) and selects soup. In the second, the patron enters three quarters, and wants to have soup, but discovers that there is no soup, so the patron requests their money back and leaves. 5. Describe how your design would need to be changed to support each of the following: The price of soup is raised to 40 cents A second type of soup is added. (Ignore the hardware modifications, only consider the software) A bill reader added 6. The following three classes and methods illustrate a variety of forms of polymorphism. Point to the places in the code that illustrate overloading based on classes, overloading based on method signatures, overriding, using replacement, overriding using refinement, and a polymorphic variable. Class One { public int fred (int a) { return a + 1; } public int fred (int a, int b) { return a + b + 1; } public int freeda (int a) { Two c = new Three(17); Return c.barney(7) + a.fred(7); } } class Two { public Two ( int w) { v = w; } public int fred (int a) { return a + 3; } public int barney (int b) { return v * b; } public int ginger (int b) { return v * b + 1; } private int v; } class Three extends Two { public Three (int w) { super(w+2); } public int barney (int b) { return v + b; } public int ginger (int b) { return super(2+b); } }``` Request for Question Clarification by answerguru-ga on 27 Feb 2004 13:36 PST ```Hi again math01-ga, While these probems are not overly difficult for a seasoned Java developer, the time required is fairly substantial. As such, you may consider adjusting your list price appropriately. Though I cannot speak for my fellow researchers, I would personally not take this task on for less than \$120. Thanks, answerguru-ga``` Request for Question Clarification by answerguru-ga on 29 Feb 2004 21:10 PST ```Hi math01-ga, I've completed the first three questions, however I just noticed for #4 that diagrams are required. Since this forum doesn't allow for file uploads I will not be able to do this part. Since #5 depends on the response from #4 that also cannot be done here. I can do #6, however, and provide #1,2,3, and 6 as an answer. Please let me know if this is acceptable. Thanks, answerguru-ga``` Clarification of Question by math01-ga on 01 Mar 2004 10:08 PST ```Hi answerguru-ga, You can send me number 4 and 5 as an attachment to my e-mail address. Is that ok if I provide you with that address at your email address?``` Request for Question Clarification by answerguru-ga on 01 Mar 2004 12:38 PST ```Hi math01-ga, Unfortunately researchers are not permitted to contact clients directly - only through this board. I would be happy to post the questions I have along with #6 (which I can complete) for you. If you feel you want to lower the price slightly to reflect the omitted questions that would be fine. Alternatively, I could give you some resources on CRC and a brief strategy of how to tackle #4 and #5 instead of answering them completely. Please let me know how you would like to proceed. answerguru-ga``` Clarification of Question by math01-ga on 01 Mar 2004 22:51 PST ```Hi answerguru-ga, Ok; you can go ahead and post 1,2,3 and 6. Regards```
 Subject: Re: Java Programming Answered By: answerguru-ga on 02 Mar 2004 19:56 PST
 ```Hi math01-ga, Thanks for your patience - here are the questions we agreed upon. I have little control over the formatting so my apologies in advance if any skewing occurs. QUESTION #1: /* 1. Write a Java class description that will represent a Fraction. A Fraction can be formed as the ratio of two integer values, the numerator and the denominator. Your class should keep these two values protected from modification. Your class definition should include three constructors. When no arguments are provided, construct the number 0/1. When one integer is provided, construct the fraction using the argument as numerator, and one as a denominator. When two integer arguments are provided, use then as numerator and denominator. Define public accessor methods for the numerator and denominator. Define a method named multiply. This method should take as argument another fraction, and return the fraction that represents the multiplication of the receiver fraction and the argument. */ public class Fraction { protected int numerator; protected int denominator; public Fraction() { numerator = 0; denominator = 1; } public Fraction(int num) { numerator = num; denominator = 1; } public Fraction(int num, int den) { numerator = num; denominator = den; } public int getNumerator() { return numerator; } public int getDenominator() { return denominator; } public Fraction multiply(Fraction myFraction) { int newNumerator = myFraction.getNumerator()*this.getNumerator(); int newDenominator = myFraction.getDenominator()*this.getDenominator(); return new Fraction(newNumerator, newDenominator); } } QUESTION #2: /* 2. Consider various types of buildings (house, store, factory). Show two different types of hierarchy. One hierarchy should illustrate the is-a relationship between various levels, and another hierarchy should illustrate has-a relationships. */ // this is the base class, a building class Building { int squareFeet; String address; public Building(int sqFt, String addr) { squareFeet = sqFt; address = addr; } } class Kitchen { boolean marbleCounters; int numberOfAppliances; int windows; public Kitchen() { //default kitchen settings marbleCounters = false; numberOfAppliances = 3; windows = 0; } public Kitchen(boolean marble, int appliances, int win) { marbleCounters = marble; numberOfAppliances = appliances; windows = win; } } // a house is-a building (extends keyword) // a house has-a kitchen class House extends Building { int totalRooms; int bedrooms; int bathrooms; boolean basement; Kitchen kitchenData; public House(int sqFt, String addr, int rooms, int bed, int bath, boolean base) { super(sqFt, addr); totalRooms = rooms; bedrooms = bed; bathrooms = bath; basement = base; kitchenData = new Kitchen(); // kitchen object data member of House } } QUESTION #3: /* 3. Write a Java class hierarchy for the three concepts Shape, Square, and Circle. The class Shape is a parent class, with Square and Circle as child classes. A Square will take a height and width as argument to the constructor, while a Circle will take a radius. Define a method named Area what will return the area of a shape. This method should be deferred in the parent class, and defined in the child classes. */ // both version of the area() method are defined in the superclass abstract class Shape { abstract double area(double height, double width); abstract double area(double radius); } // only the height*width area method is defined for squares class Square extends Shape { public double area(double h, double w) { return h*w; } public double area(double radius) { return -1; } } // only the radius area method is defined for circles class Circle extends Shape { public double area(double radius) { return Math.PI*radius*radius; } public double area(double h, double w) { return -1; } } QUESTION #6 Class One { public int fred (int a) { return a + 1; } public int fred (int a, int b) { return a + b + 1; } public int freeda (int a) { Two c = new Three(17); Return c.barney(7) + a.fred(7); } } class Two { public Two ( int w) { v = w; } public int fred (int a) { return a + 3; } public int barney (int b) { return v * b; } public int ginger (int b) { return v * b + 1; } private int v; } class Three extends Two { public Three (int w) { super(w+2); } public int barney (int b) { return v + b; } public int ginger (int b) { return super(2+b); } } Point to the places in the code that illustrate overloading based on classes: Overloading based on method signatures: This occurs in class One when the method name "fred" is used in two methods that have different parameter requirements. Overriding: This occurs in class Three - both methods barney and ginger in this class are overriding the respective methods in the superclass since the corresponding method signatures are identical. It should be noted that the actual content of the method differs. Using replacement: This occurs in the case of the freeda method in class One. Clearly the goal here was to ensure to create the ability to combine the results of other methods to produce a new result. In this case it is a good idea since class One is not within the same hierarchy as classes Two and Three (other than the fact that they both reside under the Object class by definition). Overriding using refinement: This occurs twice in class Three - in both instances the keyword "super" is used to request access to an overridden method in the superclass, and then adjusting the parameter as desired. Polymorphic variable: Class Two has a polymorphic variable "v" in the sense that its subclass, Three, will inherit this variable directly, thus making it polynorphic. Hopefully you understand the material above - if you have any problems with the content please let me know and I will respond promptly. Cheers! answerguru-ga```
 Subject: Re: Java Programming From: nvyseal77-ga on 29 Feb 2004 09:17 PST
 ```You might be better off posting this at www.rentacoder.com More software coders look there for freelance source writing jobs.```
 Subject: Re: Java Programming From: finiteloop-ga on 01 Mar 2004 20:41 PST
 ```Doesn't anyone else care about the fact that this is clearly an assignment for a class, and answering it would support this blatant cheating. I am sure this person's boss just came in and said "Write a Java class hierarchy for the three concepts Shape, Square, and Circle." :) I personally don't think any self-respecting software engineer would answer a "question" like this.```
 Subject: Re: Java Programming From: math01-ga on 02 Mar 2004 10:55 PST
 ```Hi finiteloop-ga, If you have problems answering questions then you are in the wrong business and you should find yourself another job.```
 Subject: Re: Java Programming From: math01-ga on 03 Mar 2004 10:48 PST
 ```Hi answerguru-ga, Well done. Look forward to working with you again.```
 Subject: Re: Java Programming From: smcinmass-ga on 10 Mar 2004 16:54 PST
 ```The answer to #3 is incorrect. Not only is it not object oriented, in two cases it returns an incorrect answer! Since you did not do any of your homework, you should at least spend some time thinking about why this is so.```