Extending abstract methods

Extending abstract methods

Page 1 of 1

3 Replies - 651 Views - Last Post: 04 October 2009 - 11:31 AM Rate Topic: -----

#1 putha-nee  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 168
  • Joined: 04-October 09

Extending abstract methods

Post icon  Posted 04 October 2009 - 10:52 AM

[code]public class ServiceZone1 extends ServiceZone

{

public double getShippingCost(int w)
{
return 0;
}

}[code]

When i try to run that, it tells me it can't find the ServiceZone construction but its a ServiceZone1 class!
I am trying to write a code in ServiceZone1 which uses methods created in the abstract method ServiceZone so some help in that regard would be helpful.

Roughly, there is a base rate of $9.25 applied to all packages and then a rate of $0.18 for each pound between 11-20. i was considering using for loops to implement this in the ServiceZone1 class but any other method or way of looking at this would be appreciated

Is This A Good Question/Topic? 0
  • +

Replies To: Extending abstract methods

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Extending abstract methods

Posted 04 October 2009 - 11:05 AM

alrighty, first of all methods are not extended, they are inherited. the only thing that can be extended are classes or interfaces extending other interfaces. to help you with your problem, we will have to see more of the code coz i hardly understood what is the problem you are facing
Was This Post Helpful? 0
  • +
  • -

#3 putha-nee  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 168
  • Joined: 04-October 09

Re: Extending abstract methods

Posted 04 October 2009 - 11:15 AM

View Postmostyfriedman, on 4 Oct, 2009 - 10:05 AM, said:

alrighty, first of all methods are not extended, they are inherited. the only thing that can be extended are classes or interfaces extending other interfaces. to help you with your problem, we will have to see more of the code coz i hardly understood what is the problem you are facing


/**
 * 
 * This class is the general class which provide all the implementation
 * of the general methods to get the shipping cost. In the different 
 * zones, the shipping costs are calculated differently. Therefore, the 
 * getShippingCost() method needs to be defined specificly for each 
 * zone.
 */
public abstract class ServiceZone 
{
	
	final int FREE_WEIGHT = 10;
	final int WEIGHT_LIMIT = 20;
	final int WEIGHT_BEYOND_SERVICE = 75;
	protected double baseRate; 
	protected double rate20; //rate per extra pound between free weight and 20lb
	protected double rate75; //rate per extra pound between 20lb and 75lb
	protected double fuelSurcharge;
	
	/**
	 * @param baseRate
	 * @param rate20
	 * @param rate75
	 * @param fuelSurcharge
	 */
	public ServiceZone(double baseRate, double rate20, double rate75, double fuelSurcharge)
	{
		this.baseRate = baseRate;
		this.rate20 = rate20;
		this.rate75 = rate75;
		this.fuelSurcharge = fuelSurcharge;
	}
	
	/**
	 * @return the shipping cost within the free weight
	 */
	public double getMinimumCost()
	{
		 //Provide code to implement this method

	}

	/**
	 * @return the base rate
	 */
	public double getBaseRate() {
		return baseRate;
	}

	/**
	 * @return rate per extra pound between free weight and 20lb
	 */
	public double getRate20() 
	{ 
		return rate20; 
		}



	/**
	 * @return rate per extra pound between 20lb to 75lb
	 */
	public double getRate75() {
		return rate75;
	}

	/**
	 * @return fuel surcharge
	 */
	public double getFuelSurcharge() {
		return fuelSurcharge;
	}


	/**
	 * @param weight
	 * @return shipping cost at the weight
	 */
	public abstract double getShippingCost(int weight);

}



There is the original code. Now for
 public double getMinimumCost()
	{
		 //Provide code to implement this method

	}


I added return (baseRate + fuelSurcharge); which hopefully makes sense.

Now, I am extending this ServiceZone class into ServiceZone1 which I started with this code

public class ServiceZone1 extends ServiceZone

{

public double getShippingCost(int weight)
{
return 0;
}

}


Problem is when i try to compile ServiceZone1, it keeps saying it cannot find symbol contructor ServiceZone()

The assignment is to write a subclass ServiceZone1 which calculates the shippingcost when the baseRate is 9.25 for 10 pound packages, $0.18 /pound for 11-20 pounds, and 0.17/pound for 21-75 pounds plus $11 for fuel.

What i was thinking about doing was using a if statement in the getShippingCost(int weight) such as

public class ServiceZone1 extends ServiceZone

{
double cost1, cost2;

public double getShippingCost(int weight)
{
if (weight<= FREE-WEIGHT)
{
baserate = 9.25;
}
if (weight> FREE_WEIGHT)
{
for(weight = 11; weight<21; weight++)
{
cost1 = weight * 0.18;
}
for(weight = 21; weight,75; weight++)
{
cost2 = weight * 0.17;
}
return (baserate + cost1 + cost2);
}


}


This may not be the most efficient way and any direction in cleaning this up or an easier way of doing this would be very much appreciated

This post has been edited by putha-nee: 04 October 2009 - 11:28 AM

Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Extending abstract methods

Posted 04 October 2009 - 11:31 AM

you are getting that error because you haven't written a constructor for you're ServiceZone1 class, so the JVM automatically provides a default no argument constructor, the first thing this constructor will do is call the superclass's no argument constructor which you dont have in your ServiceZone class. so to solve this problem either write a no argument constructor in your superclass or write a constructor in your subclass which calls the superclass's constructor with the appropriate arguments, hope this helps :)

This post has been edited by mostyfriedman: 04 October 2009 - 11:32 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1