GeeCON Prague 2014*
The moose likes Java in General and the fly likes child classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "child classes" Watch "child classes" New topic
Author

child classes

Wesley David
Greenhorn

Joined: Dec 17, 2003
Posts: 6
I am having difficulty with the following problem and I can't get pass the first part.
Create an abstract class named Bike with a string field for the brand and a double field for the price. The bikes brand must get two seperate methods, one that returns the brand and one the price and also include an abstract method called setPrice(). I think I done this, see following:
public abstract class Bike
{
protected String bike;
protected int price;
public Bike() throws Exception
{
setBrand();
setPrice();
}
public String getBrand()
{
return bike;
}
public double getPrice()
{
return price;
}
public void setBrand() throws Exception
{
String inputString = new String();
char newChar;
System.out.print("Enter Corresponding to the brand of the bike: A 'Left Behind', B 'Biography of John Wayne' ");
newChar = (char)System.in.read();
while(newChar>= 'A' && newChar<='z'||newChar==' '||
newChar>='0' && newChar<='9')
{
inputString = inputString + newChar;
newChar = (char)System.in.read();
}
System.in.read();
bike = inputString;
}
public abstract void setPrice();
}
I don't know if this is correct, but I was able to compile it. The real question comes next:
Create 2 child classes of Bike: Motor and NonMotor. Within the constructors for these call setPrice so all Motor Bikes are 90.00 and all NonMotor are 20.00.
Not sure if this cover the requirement, because I could not get it to compile. Here it is.
public class Motor extends Bike
{
char BikeBrand;

public void printBikeBrand()
{
if ( BikeBrand == 'f' )
{
System.out.println("Motor");
}
else
{
System.out.println("Invalid Entry");
}
}

public void printHeader()
{
System.out.println("Bike Brand: ");
}

public void setBikeBrand() throws Exception
{
System.out.println("Enter Bike Brand");
System.out.print("f for Motor, n for nonMotor ");
BikeBrand = (char)System.in.read();
}

public void setBikePrice()
{
if (BikeBrand == 'f' )
{
System.out.println("24.99");
}
else
{
System.out.println("Invalid Entry");
}
}
}
Beyond that I need to create a program that demonstrates that I can create bothe a Motor and a NonMotor Bike and display their fields.
Any help I can get on this is much appreciated. Thanks
Antoine Waugh
Ranch Hand

Joined: Jan 24, 2004
Posts: 66
okies, well the first step you have to do it create a basic abstract class which contains the requirements, getPrice methods etc.
an abstract class CANNOT be initialized (meaning you cannot make an object of that class)
therefore you must create two subclasses, Motor and Non-motor which CAN have objects created as they are NOT abstract.
You had some if statements etc which did not make sense to me, so i removed them, but feel free to use the code below as a starter, and manipulate as you feel necessary.
Also, you did not understand the setting of the prices correctly, what they want is two classes; Motor and nonmotor, which have a set price, irrespective of how many objects are created of that type:
please reply with any quieries
-twans
****************************************
Bike Class
****************************************
public abstract class Bike
{
protected String bike;
protected double price;
public Bike()
{

}
public String getBrand()
{
return bike;
}
public double getPrice()
{
return price;
}

public abstract void setPrice();
public static void main(String [] args)
{
Motor Honda = new Motor();
NonMotor PushBike = new NonMotor();

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() );
}
}
****************************************
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;
}
}


B.C.S.T, SCJP, Hero
Wesley David
Greenhorn

Joined: Dec 17, 2003
Posts: 6
Thanks for your help on this, I think my communication skills in explaining it hava a lot left to be desired. I tried to use what you had given me, but could not figure out how to write a program to display everything. Let me try again, maybe I can explain it better.
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. 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().
Then I need to create two child classes of Bike: Motor and NonMotor. 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.
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.
Antoine Waugh
Ranch Hand

Joined: Jan 24, 2004
Posts: 66
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
 
GeeCON Prague 2014
 
subject: child classes