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 |