I\'m working in chapter 10 question 7 in the Java Programming Eighth Edition by
ID: 3855674 • Letter: I
Question
I'm working in chapter 10 question 7 in the Java Programming Eighth Edition by Joyce Farrell and I am getting the following errors and can't figure out what is missing. I am trying to run the UsePackage and it is giving me 2 errors on the InsuredPackage program.........
Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. All rights reserved.
C:UsersBrent>cd desktop
C:UsersBrentDesktop>javac UsePackage.java
.InsuredPackage.java:3: error: missing method body, or declare abstract
public InsuredPackage(int weight, char shipping);
^
.InsuredPackage.java:5: error: call to super must be first statement in constructor
super(weight, shipping);
^
2 errors
Here is the InsuredPackage program I have.............
public class InsuredPackage extends Package
{
public InsuredPackage(int weight, char shipping);
{
super(weight, shipping);
if (cost > 0 && cost <=1)
{
cost = cost + 2.45;
}
else if (cost > 1 && cost <= 3)
{
cost = cost + 3.95;
}
else if (cost > 3)
{
cost = cost + 5.55;
}
}
}
Explanation / Answer
Hi Let me know if you need more information/implementation:-
=================================================
Reason Error:-
1. your super class must have constructor with two parameters
2. you are not supposed to give semi colon(;) at the end of method signature.
=================================================
class Package {
int weight;
char shipping;
public Package(int weight, char shipping) {//Constructor for Package class which we can call from sub //class [InsuredPackage] using super(,);
super();
this.weight = weight;
this.shipping = shipping;
}
}
public class InsuredPackage extends Package
{
//public InsuredPackage(int weight, char shipping); <------ Before
public InsuredPackage(int weight, char shipping) <------ After
{
super(weight, shipping);// Package Class Must have two paramenter
// constructor
if (cost > 0 && cost <= 1)
{
cost = cost + 2.45;
}
else if (cost > 1 && cost <= 3)
{
cost = cost + 3.95;
}
else if (cost > 3)
{
cost = cost + 5.55;
}
}
}
==============
Thanks