Hey Everyone,

I'm new to this form, and I seem to like it so far. Anyways, I have this Project that i'm nearly done with but in the sub-class I'm getting 0.0 as an output instead of the actual calculation. Below is the project and the code:

You are requested to design the class Package with instance variables representing package number, weight, and shipping method. The shipping method is a character: A for Air, T for Truck, and M for Mail. The class contains a constructor that requires arguments for weight and shipping method. The constructor should create automatic unique package numbers. The class defines a calculateCost()method that determines the shipping cost of a package based on the following:

Weight (in kg) Shipping method

-----------------------Air---Truck---Mail
1 - to - 8 ------------2.00---1.50---0.5
9 - to - 16 -----------3.00---2.35---1.50
17-to-over ------------4.50---3.25---2.15

The package class also contains a method display()that displays the values of a package object.

Create a subclass InsuredPackage that adds an insurance cost to the shipping cost based on the following:

Shipping cost before insurance (in $)-----Additional cost

10.00 to 100.00 --------------------------15.00
10.001 to 300.00--------------------------25.50
301.00 to over --------------------------34.00

Create a class Owner that represents the owner of a package. The class should contain an array reference that references the packages belonging to the owner, in addition to the name, mailing address, and owner ID (created automatically and uniquely by the constructor).

Write a test program that interacts with the user to create a number of owners and packages belonging to them. The program must allow the user to request a report showing information about owners and their packages.

My code:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package test2;

/**
 *
 * @author moe
 */

import java.util.*;
public class TestMain {

    public static void main (String[] args){


		Scanner in = new Scanner(System.in);
                Package pf = new Package ();
                Owner of = new Owner ();
                InsuredPackage ip = new InsuredPackage ();
                

                
                int userChoice;
                boolean quit = false;

                System.out.println ("Please Enter Your Name:" );
                Owner ob1 = new Owner ();
                String name = in.nextLine();

                System.out.println ("Please Enter Your Address:" );
                Owner ob2 = new Owner ();
                String address = in.nextLine();

                System.out.println ("Owner Name is:" + name
                        + "\nOwner Address is:" + address
                        + "\nUnique User ID is:" + of.getOwnerID()
                        + "\nPackage number " + pf.getPackageID() + " belongs to you.");

                


                do {
                  System.out.println("1. Air");
                  System.out.println("2. Truck");
                  System.out.println("3. Mail");
                  System.out.println("0. Quit");
                  System.out.print("Your choice: ");
                  userChoice = in.nextInt();
                  
                  
                  
                  switch (userChoice) {
                  case 1:
                        System.out.println ("You have choosen to Ship by Air.");
                        System.out.println ("Please enter the package weight");
                        
                        
                        double weight = in.nextDouble();
                        pf.weight = weight;

                        System.out.println("Your Shipment Will Cost:" + pf.getAir());
                        System.out.println("Insurance will cost" + ip.getRange());
                        
                                               

                        
                        
                        break;

                  case 2:
                      System.out.println ("You have choosen to Ship by Truck.");

                        System.out.print("Please enter the package weight: ");

                        pf.weight = in.nextDouble();

                        System.out.println(pf.getTruck());
                        System.out.println("Insurance will cost" + ip.getRange());
                        
                        break;

                  case 3:
                       System.out.println ("You have choosen to Ship by Mail.");

                        System.out.print("Please enter the package weight: ");

                        pf.weight = in.nextDouble();

                        System.out.println(pf.getMail());
                        System.out.println("Insurance will cost" + ip.getRange());
                        
                        break;

                  case 0:
                        quit = true;
                        break;
                        
                  default:
                        System.out.println("Wrong choice.");
                        break;
                  }
                  System.out.println();
            } while (!quit);
            System.out.println("Thank you!");


    }

   
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package test2;

/**
 *
 * @author moe
 */
import java.util.*;
public class Package  {

    
    private double pnumber;
    public double weight;
    private double Air;
    private double Truck;
    private double Mail;
    private double cost;

    public Package (double cost, double pnumber, double weight, double Air, double Truck, double Mail){
        this.cost = cost;
        this.pnumber = pnumber;
        this.weight = weight;
        this.Air = Air;
        this.Truck = Truck;
        this.Mail = Mail;
    }

    /**
     *
     */
    public Package(){

    }

    
    
    public void setPackageID (double pnumber){

                this.pnumber = pnumber;
                
    }

    public double getPackageID (){

        Random rand = new Random();
        int pick = rand.nextInt(40)+1;

        return pick;

    }




    public void setAir (double Air){

        this.Air = Air;

    }

    public double getAir (){

        if (weight <= 1) {
            System.out.println("Please enter the correct amount of weight.");
        }
                        else {
                             if (weight <= 8) {
                return weight * 2;
            }
                        }
                        if ((weight >= 9) && (weight <= 16)) {
            return weight * 3;
        }
                        else {
                            if (weight >= 17) {
                return weight * 4.50;
            }
                        }
        return cost;

    }

    public void setTruck (double Truck){

        this.Truck = Truck;

    }

    public double getTruck (){

        if (weight <= 8) {
            System.out.println("The Shipment of package will cost $" + weight * 1.50);
        }
                        else {
                             if ((weight >= 9) && (weight <= 16)) {
                System.out.println("The Shipment of package will cost $" + weight * 2.35);
            }
                        else {
                            if (weight >= 17) {
                    System.out.println("The Shipment of package will cost $" + weight * 3.25);
                }
                            }
                        }

        return getCost();

    }

    public void setMail (double Mail){

        this.Mail = Mail;

    }

    public double getMail (){

        if (weight <= 8) {
            System.out.println("The Shipment of package will cost $" + weight * 0.50);
        }
                        else {
                             if ((weight >= 9) && (weight <= 16)) {
                System.out.println("The Shipment of package will cost $" + weight * 1.50);
            }
                        else {
                            if (weight >= 17) {
                    System.out.println("The Shipment of package will cost $" + weight * 2.15);
                }
                            }
                        }

        return getCost();

    }

    /**
     * @return the cost
     */
    public double getCost() {
        return cost;
    }

    /**
     * @param cost the cost to set
     */
    public void setCost(double cost) {
        this.cost = cost;
    }



}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package test2;

/**
 *
 * @author moe
 */
public class InsuredPackage extends Package {

    private double range;

    public InsuredPackage (double cost, double pnumber, double weight, double Air, double Truck, double Mail){
        super (cost, pnumber, weight, Air, Truck, Mail);
    }

    public InsuredPackage (){
        super ();
    }




    public void setRange (double range){
        this.range = range;
    }

    public double getRange(){

        if (getCost() == 10 && getCost() <= 100) {
            return getCost() + 15;
        }

        if (getCost() == 100.01 && getCost() <= 300) {
            return getCost() + 25.50;
        }

        if (getCost() >= 300.01) {
            return getCost() + 34;
        }

        
        return range;

        
    }

    
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package test2;

/**
 *
 * @author moe
 */
import java.util.*;

public class Owner extends Package {
    
    private double onumber;
    private String name;
    private String address;

    public void setOwnerID (double onumber){

        this.onumber = onumber;

    }

    public double getOwnerID (){

        Random rand = new Random();
        int opick = rand.nextInt(40)+1;
        return opick;
    }

    public void setName (String name){
        this.name = name;
    }

    public String getName (){
        return name;
    }

    public void setAddress (String address){
        this.address = address;
    }

    public String getAddress (){
        return address;
    }

}

My Output is correct but its showing the Insurance as 0.0

Please help, any would be appreciated !!

Hi,

In the calculation of Insurance cost, you want to calculate this cost based on resulting shipping cost. So, you wrote like this in your Switch-Case method of TestMain :

switch (userChoice)
{ case 1:
System.out.println ("You have choosen to Ship by Air.");
System.out.println ("Please enter the package weight");
double weight = in.nextDouble() // you request shipping weight from user
pf.weight = weight; // you insert user input weight into weight of Package obj ' pf '
System.out.println("Your Shipment Will Cost:" + pf.getAir()); // calculate shipping cost based on 'weight'
System.out.println("Insurance will cost" + ip.getRange()); // calculate Insurance cost based on 'cost'
}

Well, Right now, i will explain you for your problem. This is very simple problem. You have already known Shipping cost must calculate based on user input weight. So, you request this from user. So, Shipping cost is appeared correctly because you inserted the weight value in Shipping cost calculation. Howerver, However, However........ Why didn't you insert the resulting shipping cost into the obj ' ip ' of InsuredPackage. You also know Insurance cost must calculate based on the resulting shipping cost. But you miss to insert it into ' ip '. Why ?!!!!!! You should write as the following .......


switch (userChoice)
{
case 1:
System.out.println ("You have choosen to Ship by Air.");
System.out.println ("Please enter the package weight");
double weight = in.nextDouble();
pf.weight = weight;
System.out.println("Your Shipment Will Cost:" + pf.getAir());
ip.setCost(pf.getCost()); // repair
System.out.println("Insurance will cost" + ip.getRange());
break;
.........
.........
.........
}

And then you should write the following instead of your 2 function .....

public double getAir ()     // function of Class Package
{
 if (weight <= 1) 
 {
   System.out.println("Please enter the correct amount of weight.");
 }
 else 
 {
  if (weight <= 8)
 {
   cost =  weight * 2;				
 }		
 else if ((weight >= 9) && (weight <= 16)) 
 {
   cost = weight * 3;
}
else if (weight >= 17) 
{
cost = weight * 4.50;
}
}
return cost;
}
public double getRange()    // function of InsuredPackage
{
 if (getCost() >= 10 && getCost() <= 100)   // you should write like this 
 {
 return getCost() + 15;
  }
  if (getCost() >= 100.01 && getCost() <= 300) 
 {
 return getCost() + 25.50;
  }
 if (getCost() >= 300.01) 
  {
  return getCost() + 34;
 }
  return range;      
  }

Here is complete coding... you must change your TestMain, Package and InsuredPackage Classes....

import java.awt.*;
import java.util.Scanner;

import javax.swing.*;


public class TestMain
{
	public TestMain()
	{


	}

	public static void main (String[] args)
	{


		Scanner in = new Scanner(System.in);
		Package pf = new Package ();
		Owner of = new Owner ();
		InsuredPackage ip = new InsuredPackage ();



		int userChoice;
		boolean quit = false;

		System.out.println ("Please Enter Your Name:" );
		Owner ob1 = new Owner ();
		String name = in.nextLine();

		System.out.println ("Please Enter Your Address:" );
		Owner ob2 = new Owner ();
		String address = in.nextLine();

		System.out.println ("Owner Name is:" + name
				+ "\nOwner Address is:" + address
				+ "\nUnique User ID is:" + of.getOwnerID()
				+ "\nPackage number " + pf.getPackageID() + " belongs to you.");




		do {
			System.out.println("1. Air");
			System.out.println("2. Truck");
			System.out.println("3. Mail");
			System.out.println("0. Quit");
			System.out.print("Your choice: ");
			userChoice = in.nextInt();



			switch (userChoice) {
			case 1:
				System.out.println ("You have choosen to Ship by Air.");
				System.out.println ("Please enter the package weight");


				double weight = in.nextDouble();
				pf.weight = weight;

				System.out.println("Your Shipment Will Cost:" + pf.getAir());							
				ip.setCost(pf.getCost());            // repair
				System.out.println("Insurance will cost" + ip.getRange());





				break;

			case 2:
				System.out.println ("You have choosen to Ship by Truck.");

				System.out.print("Please enter the package weight: ");

				pf.weight = in.nextDouble();

				System.out.println("Your Shipment Will Cost:" + pf.getTruck());							
				ip.setCost(pf.getCost());            // repair
				System.out.println("Insurance will cost" + ip.getRange());

				break;

			case 3:
				System.out.println ("You have choosen to Ship by Mail.");

				System.out.print("Please enter the package weight: ");

				pf.weight = in.nextDouble();

				System.out.println("Your Shipment Will Cost:" + pf.getMail());
				ip.setCost(pf.getCost());  
				System.out.println("Insurance will cost" + ip.getRange());

				break;

			case 0:
				quit = true;
				break;

			default:
				System.out.println("Wrong choice.");
			break;
			}
			System.out.println();
		} while (!quit);
		System.out.println("Thank you!");



	}

}
import java.util.Random;


public class Package 
{

	private double pnumber;
	public double weight;
	private double Air;
	private double Truck;
	private double Mail;
	private double cost;

	public Package (double cost, double pnumber, double weight, double Air, double Truck, double Mail){
		this.cost = cost;
		this.pnumber = pnumber;
		this.weight = weight;
		this.Air = Air;
		this.Truck = Truck;
		this.Mail = Mail;
	}

	/**
	 *
	 */
	public Package(){

	}



	public void setPackageID (double pnumber){

		this.pnumber = pnumber;

	}

	public double getPackageID (){

		Random rand = new Random();
		int pick = rand.nextInt(40)+1;

		return pick;

	}




	public void setAir (double Air){

		this.Air = Air;

	}

	public double getAir ()
	{

		if (weight <= 1) 
		{
			System.out.println("Please enter the correct amount of weight.");
		}
		else    // repair
		{
			if (weight <= 8)
			{
				cost =  weight * 2;				
			}		
			else if ((weight >= 9) && (weight <= 16)) 
			{
				cost = weight * 3;
			}
			else if (weight >= 17) 
			{
				cost = weight * 4.50;
			}
		}
		return cost;

	}

	public void setTruck (double Truck){

		this.Truck = Truck;

	}

	public double getTruck (){

		if (weight <= 8)
		{
			System.out.println("The Shipment of package will cost $" + weight * 1.50);
		}
		else 
		{
			if ((weight >= 9) && (weight <= 16)) 
			{
				System.out.println("The Shipment of package will cost $" + weight * 2.35);
			}
			else {
				if (weight >= 17) {
					System.out.println("The Shipment of package will cost $" + weight * 3.25);
				}
			}
		}

		return getCost();

	}

	public void setMail (double Mail){

		this.Mail = Mail;

	}

	public double getMail (){

		if (weight <= 8) {
			System.out.println("The Shipment of package will cost $" + weight * 0.50);
		}
		else {
			if ((weight >= 9) && (weight <= 16)) {
				System.out.println("The Shipment of package will cost $" + weight * 1.50);
			}
			else {
				if (weight >= 17) {
					System.out.println("The Shipment of package will cost $" + weight * 2.15);
				}
			}
		}

		return getCost();

	}

	/**
	 * @return the cost
	 */
	public double getCost() {
		return cost;
	}

	/**
	 * @param cost the cost to set
	 */
	public void setCost(double cost) {
		this.cost = cost;
	}



}
public class InsuredPackage extends Package
{

	   private double range;

	    public InsuredPackage (double cost, double pnumber, double weight, double Air, double Truck, double Mail){
	        super (cost, pnumber, weight, Air, Truck, Mail);
	    }

	    public InsuredPackage (){
	        super ();
	    }




	    public void setRange (double range){
	        this.range = range;
	    }

	    public double getRange()   // repair
	    {

	        if (getCost() >= 10 && getCost() <= 100)   // you should write like this 
	        {
	            return getCost() + 15;
	        }

	        if (getCost() >= 100.01 && getCost() <= 300) 
	        {
	            return getCost() + 25.50;
	        }

	        if (getCost() >= 300.01) 
	        {
	            return getCost() + 34;
	        }

	        
	        return range;

	        
	    }
}

I only changed to be correct for Air. See and Read my changed coding and also try for Truck and Mail yourself.And Then, if i have to tell the truth, your program is loosely constructed. You declared so many member functions. However, you didn't use the most of your declared function. And then, i think you are confusing about If-else and return statements. You should do to be sure. These are basic of programming.

*** Searching Error is one of the ability of programmers. When you see unexpected results from your program, don't depress. Trace the program and Search the errors. This tracing and Searching will help you to be better programmer ****

ps : If my help doesn't meet your problem, come back. I am waiting for you....

Best Wishes...........

Hey Tunlinaung,

First of all, I really thank you for your reply and help.

I spent two days trying to figure out whats the problem of the Insuredpackage class and I didn't notice its that simple and its in the if-else statement, thinking it was right, so I should take a better look next time.

Second, to honest I didn't know a clue about the ip.setCost(pf.getCost()); and didn't know I could do that or at least didn't cross my mind.

So again,
Thank You so much for your help and time... appreciate it..

Hi, my friend.......

So... I assumed your problem is successfully passed over. I also feel pleasure when someone releases from troubles because of me. So, The word "Thank you" is not needed between our relationship. That is because we are programmers... Java Programmers. The thing we need between programmers is just knowledge sharing and helping. And then, I encourage you to be a great programmer. Because your country is better place than our country(Myanmar). The salary of a programmer in my country is only 50 dollars for a month. So, you are living in a nice place and you should do better. I do really believe you are also trying hard.

My letter is rather long :$:$:$ For your future questions, you can contact me at tunlinaung.tla7@gmail.com (or) this site.

ps : Please click 'Mark as solved Thread' and vote me if you satisfy my post. ;););)

I want to meet you again and I hope your bright future .......

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.