hey david,
>>I need to create an abstract class named Bike. Include a String field for the bikes name and a double field for the bikes price.
public abstract class Bike
{
protected String bikeName;
protected double price;
>>Within the class, I need to include a constructor that requires the bike name and two get methods � one that returns the name and one that returns the price. I also need to include an abstract method named setPrice().
This still is not clear to me. A constructor is called when you create a new object of that class. Therefore, the constructor will NOT hold 'two get methods', but rather have a parameter which accepts the name, then two seperate methods, one for the name and one returning the price..
the decloration for the method setPrice in Bike is:
public abstract void setPrice();
/* This is the constructor, it will be called every time you create a new object of type Motor or NonMotor and pass a parameter */
public Bike(String tempName)
{
bikeName = tempName;
}
//This method returns the string 'bikeName'
public String getBrand()
{
return bikeName;
}
//this method returns the double price (ie the cost of the bike)
public double getPrice()
{
return price;
}
>>Then I need to create two child classes of Bike: Motor and NonMotor.
****************************************
Motor SubClass
****************************************
public class Motor extends Bike
{
public Motor()
{
setPrice();
}
public void setPrice()
{
price = 90.00;
}
}
****************************************
Non-Motor SubClass
****************************************
public class NonMotor extends Bike
{
public NonMotor()
{
setPrice();
}
public void setPrice()
{
price = 24.00;
}
}
>>Within the constructors for the Motor and NonMotor classes, call setPrice so all Motor Bikes cost $90.00 and all NonMotor Bikes cost $24.00.
look under both constructors, the setPrice method is called, and as you can see it allocates: 'price = 24.00;' for Nonmotor and 90.00 for motor
>>After that I have to write a program that demonstrates that I can create both a Motor and a NonMotor Bike and display the fields.
the main method is what is executed when you call the program:
c:\java\ javac Bike.java
c:\java\
java Bike
public static void main(String [] args)
{
/*Creates an object of type Motor called 'Honda'
note 'honda' will be stored as 'bikeName' as
this is the parameter passed when calling the constructor
with the keyword 'new'
*/
Motor Honda = new Motor(honda);
//Similarly, this creates the nonmotor object
NonMotor PushBike = new NonMotor(petalBike);
/*This next line is an example of printing out a detail of an object
the System.out.println() method prints to the screen. The '+' symbol
concatinates strings and 'Honda.getPrice()' will return and print the value of 'price'
*/
System.out.println("The Price of the Motor Vehcle is: " + Honda.getPrice() );
System.out.println("The Price of the non-motor vehcle is: " + PushBike.getPrice() );
}
}
This should answer your specifications. As a final example to print the name, you would simply call
NameOfMotor . NameOfMethod() ;
ie. Honda . getBrand();
hope that answers your question
-twans