We are learning about parent and child classes and I think I am confuisng what n
ID: 3672468 • Letter: W
Question
We are learning about parent and child classes and I think I am confuisng what needs to go in the parent class and what needs to go in the child class it must also generate a random id for each order
// The package ID number is automatically assigned.
//In Package class, the base delivery price is determined using the following criteria:
// - Packages with weight of 32 ounces or less have a delivery fee of $5.00.
// - Packages with weight of more than 32 ounces incur an extra $0.12 fee per ounce.
//In InsuredPackage class, the insurance fee is calculated using the following criteria:
// - Packages with value of $20.00 or less have an insurance fee of $1.00.
// - Packages with a value of more than $20.00 have an insurance fee of $2.50.
You must have a person name you are shipping it to and it's in a C# program
this is what I have it's not working correctly
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PackageDemo
{
public class Package
{
static void Main(string[] args)
//Parent class
/*Parent class package that has methods to set ounces
and calculates the delivery charges */
private int packageID;
private int ounces;
private double deliveryCharges;
private string name;
//Constructor that sets the number of ounces
public Package(int ounces)
{
//generage package id a random number
Random idnumber = new Random();
packageID = idnumber.Next(1, 999);
}
//Constructor to delivery charges
public Package(double deliveryCharges)
{
this.deliveryCharges = deliveryCharges;
}
public int getID()
{
return packageID;
}
//Private method that set ounces and call
//setDeliveryCharges
private void setOunces(int ounces)
{
this.ounces = ounces;
setDeliveryCharges(ounces);
}
//Set delivery charges of package of weight ounces
private void setDeliveryCharges(int ounces)
{
if (ounces <= 32)
//set charge
deliveryCharges = 5;
else
{
//set ounces grater than 32
int remainingOunces = (ounces - 5);
deliveryCharges = 5 + remainingOunces * 0.12;
}
}
//return ounces
public int getOunces()
{
return ounces;
}
//Return delivery charges
public double getDeliveryCharges()
{
return deliveryCharges;
}
}
//Child class
/**The class InsuredPackage that extends the Package
* class that pass the deliveryCharges to the
* base class package and get the deliverycharge
* fee from base class in getInsuranceFee class*/
public class InsuredPackage
{
private int getDeliveryCharges;
//constructor to set delivery charges for insured package
public InsuredPackage(double deliveryCharges)
{
}
/**Returns the insurance fee based on the delivery charges
* fee*/
public double getInsuranceFee()
{
if (getDeliveryCharges < 20)
return 1;
else
return 2.50;
}
}
class PackageDemo
static void main()
{
//Create an instance of Package with ounces as input
Package packagee = new Package(25);
System.Console.WriteLine("name");
System.Console.WriteLine("Package ID : " + name.getID());
System.Console.WriteLine("ounces : " + name.getOunces());
System.Console.WriteLine("Charges : " + name.getDeliveryCharges());
//Create an instance of InsuredPackage with ounces as charges
InsuredPackage insuredPackage = new InsuredPackage(50);
System.Console.WriteLine("name");
System.Console.WriteLine("Package ID : " + insuredPackage.getID());
System.Console.WriteLine("Insureance fee : "
+ insuredPackage.getInsuranceFee());
}
}
Explanation / Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PackageDemo
{
public class Package
{
static void Main(string[] args)
//Parent class
/*Parent class package that has methods to set ounces
and calculates the delivery charges */
private int packageID;
private int ounces;
private double deliveryCharges;
private string name;
//Constructor that sets the number of ounces
public Package(int ounces)
{
//generage package id a random number
Random idnumber = new Random();
packageID = idnumber.Next(1, 999);
}
//method to delivery charges
public double deliverycharges(double charges)
{
this.charges= charges;
return charges;
}
pubic string getName()
{
System.Console.WriteLine("Enter name");
name= str = Console.In.ReadLine();
return name;
}
public int getID()
{
return packageID;
}
//Private method that set ounces and call
//setDeliveryCharges
private void setOunces(int ounces)
{
this.ounces = ounces;
setDeliveryCharges(ounces);
}
//Set delivery charges of package of weight ounces
private void setDeliveryCharges(int ounces)
{
if (ounces <= 32)
//set charge
deliveryCharges = 5;
else
{
//set ounces grater than 32
int remainingOunces = (ounces - 5);
deliveryCharges = 5 + remainingOunces * 0.12;
}
}
//return ounces
public int getOunces()
{
return ounces;
}
//Return delivery charges
public double getDeliveryCharges()
{
return deliveryCharges;
}
}
//Child class
/**The class InsuredPackage that extends the Package
* class that pass the deliveryCharges to the
* base class package and get the deliverycharge
* fee from base class in getInsuranceFee class*/
public class InsuredPackage : Package
{
private double packagevalue;
//constructor to set delivery charges for insured package
public InsuredPackage( double packagevalue )
{
this.packagevalue = packagevalue;
}
/**Returns the insurance fee based on the delivery charges
* fee*/
public double getInsuranceFee()
{
if (packagevalue < 20)
return 1;
else
return 2.50;
}
}
class PackageDemo
{
static void main()
{
//Create an instance of Package with ounces as input
Package package1 = new Package(25);
System.Console.WriteLine("name"+ package1.getName());
System.Console.WriteLine("Package ID : " + package1.getID());
System.Console.WriteLine("ounces : " + package1.getOunces());
System.Console.WriteLine("Charges : " + package1.getDeliveryCharges());
//Create an instance of InsuredPackage with package value
InsuredPackage insuredPackage = new InsuredPackage(50);
//here using all the pmethos of parent calss without creating again in child class
//CALLING parent method setName() using child class object
System.Console.WriteLine("name"+ insuredPackage.getName());
//calling parent methods using child class object
System.Console.WriteLine("Package ID : " + insuredPackage.getID());
System.Console.WriteLine("ounces : " + insuredPackage.getOunces());
System.Console.WriteLine("Charges : " + insuredPackage.getDeliveryCharges());
//CALLING same class method
System.Console.WriteLine("Insureance fee : "
+ insuredPackage.getInsuranceFee());
}
}
Inheritance is one of the primary characteristics of object-oriented programming. Inheritance enables you to create new classes that reuse, extend, and modify the behavior that is defined in other classes. The class whose members are inherited is called the base class or parent class, and the class that inherits those members is called the derived class or child class. A child class can have only one direct parent class. However, inheritance is transitive. If ClassC is derived from ClassB, and ClassB is derived from ClassA, ClassC inherits the members declared in ClassB and ClassA.
In the above program we two class those are 1) Package
2)Insurancedpackage.
Here Package is Base class or Parent class
Insurancedpackage derived class or child class.
In C# the child class uses their parent class methods except for parent class constructors and destructors.
The Insurancedpackage child class uses only Package class getName(),getID(),setOunces(int ounces), getOunces(),getDeliveryCharges() methods.
,
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.