Problem with if statements and accessing private modifiers into a sub-class

Hey, i'm having way too many problems here... basicly, the user needs to input a weight in oz (of a package).. the shipping method and if they want shipping insurance. now the calculateCost() method "should" be doing the calculations for just basic shipping. the insuredPackage sub-class should be doing the calculations for if they choose to have insurance or not.. but my teacher wants the instance variables all private along with the calculateCost() method. I'm currently running in circles trying to fix it and have no idea anymore, i really don't know why the if's don't work... Maybe you guys can help. Thanks in advance to whomever can help me.

publicclass Package

{

privatedouble weightInOz;

private String shipMethod;

privatedouble totalCost;

private String askInsurance;

privatedouble insuranceTotal;

public Package(double weightInOz, String shipMethod, String askInsurance)

{

this.askInsurance = askInsurance;

this.weightInOz = weightInOz;

this.shipMethod = shipMethod;

totalCost = calculateCost(weightInOz, shipMethod);

//insuranceTotal = calculateInsurance(askInsurance);

}

privatedouble calculateCost(double weightInOz, String shipMethod)

{

if (weightInOz == 1 && weightInOz <= 8)

{

if (shipMethod =="A")

{

totalCost = 2.00;

}

elseif (shipMethod =="T")

{

totalCost = 1.50;

}

elseif (shipMethod =="M")

{

totalCost = 0.50;

}

}

elseif (weightInOz == 9 && weightInOz <= 16)

{

if (shipMethod =="A")

{

totalCost = 3.00;

}

elseif (shipMethod =="T")

{

totalCost = 2.35;

}

elseif (shipMethod =="M")

{

totalCost = 1.50;

}

}

elseif (weightInOz == 17 && weightInOz > 17)

{

if (shipMethod =="A")

{

totalCost = 4.50;

}

elseif (shipMethod =="T")

{

totalCost = 3.25;

}

elseif (shipMethod =="M")

{

totalCost = 2.15;

}

}

return totalCost;

}

/* CALCULATE INSURANCE

public double calculateInsurance(String askInsurance)

{

if (askInsurance == "yes")

{

if (totalCost == 0.00 && totalCost <= 1.00)

{

insuranceTotal = totalCost + 2.45;

}

else if (totalCost == 1.01 && totalCost <= 3.00)

{

insuranceTotal = totalCost + 3.95;

}

else if (totalCost == 3.01 && totalCost >= 3.01)

{

insuranceTotal = totalCost + 5.55;

}

}

return insuranceTotal;

}

*/

publicdouble getInsuranceTotal()

{

return totalCost;

}

publicvoid setInsuranceTotal()

{

this.insuranceTotal = insuranceTotal;

}

publicdouble getTotalCost()

{

return totalCost;

}

publicvoid setTotalCost()

{

this.totalCost = totalCost;

}

publicvoid display()

{

System.out.println("Weight in Ounces: "+weightInOz +"\n"+"Shipping Method: "+shipMethod +"\n"+"Total Cost: " +totalCost +"\n"+"Total Insurance Cost: "+insuranceTotal);

}

}

/*

class InsuredPackage extends Package

{

}

*/

Now this is the test class

import javax.swing.*;

publicclass TestPackage

{

publicstaticvoid main(String[] args)

{

double weightInOz = Double.parseDouble(JOptionPane.showInputDialog(null,

"Enter the packages weight in oz.",

"Weight In Oz.", JOptionPane.QUESTION_MESSAGE));

String shipMethod = JOptionPane.showInputDialog(null,

"Enter the shipping method (A for Air, T for Truck, M for Mail).",

"Shipping Method.", JOptionPane.QUESTION_MESSAGE);

String askInsurance = JOptionPane.showInputDialog(null,

"Would you like shipping insurance?",

"Shipping Insurance.", JOptionPane.QUESTION_MESSAGE);

Package P1 =new Package(weightInOz, shipMethod, askInsurance);

P1.display();

}

}

Message was edited by:

DenisK

[7875 byte] By [DenisKa] at [2007-11-27 5:55:36]
# 1

I'm not going to read all that, but one thing leaps out at me:

if (someString == "X") // WRONG

if (stomeString.equals("X")) // Right

jverda at 2007-7-12 16:24:38 > top of Java-index,Java Essentials,New To Java...
# 2
If weightInOz is 1, then it's <= 8...if (weightInOz == 1 && weightInOz <= 8)you probably mean this:if (weightInOz >= 1 && weightInOz <= 8)For Strings, use equals...if ("A".equals(shipMethod))
bsampieria at 2007-7-12 16:24:38 > top of Java-index,Java Essentials,New To Java...
# 3

if (totalCost == 0.00 && totalCost <= 1.00) // 1

else if (totalCost == 1.01 && totalCost <= 3.00) // 2

else if (totalCost == 3.01 && totalCost >= 3.01) // 3

In #1, the stuff after the && is pointless. If the first part is true (it equals 0), then the second part must be true.

Same thing in #2.

#3 will be true only if it's exactly equal to 3.01. If it's greater than 3.01, then the first part will be false, and so the whole && is false.

jverda at 2007-7-12 16:24:38 > top of Java-index,Java Essentials,New To Java...
# 4

Edit: i got the totalCost working.. sweet. thanks a ton guys, now .. i've been messing with insuredPackage and my teacher wants the insurance cost to be calculated in the sub-class.. but i can't take the totalCost from the super class and use it in the sub-class... says it's private and etc..

Message was edited by:

DenisK

Message was edited by:

DenisK

DenisKa at 2007-7-12 16:24:38 > top of Java-index,Java Essentials,New To Java...
# 5

I don't know what exact problem you're having.

The first thing that pops to mind is to have protected getter methods for the private variables.

Another option is that the subclass' calc method can call the super's method and then add its own additional cost into the results.

I haven't looked that closely at what you're doing, and don't really know what's required, so that's all I can tell you.

jverda at 2007-7-12 16:24:38 > top of Java-index,Java Essentials,New To Java...
# 6

"Create a sub-class named InsuredPackage that adds an insurance cost to the shipping cost based on the following: " ... im confused.. should i do the if statements in the subclass?

if the super classes calculateCost method is private... how would i put it into the sub-class? i would have to put a getter method in the sub-class?

Message was edited by:

DenisK

DenisKa at 2007-7-12 16:24:38 > top of Java-index,Java Essentials,New To Java...
# 7

> "Create a sub-class named InsuredPackage that adds an

> insurance cost to the shipping cost based on the

> following: " ... im confused.. should i do the if

> statements in the subclass?

"Do the if statements"?

It's not about where you "do the if statements." However, anything involving calculating insurance cost should be in the subclass. If that's what your ifs are doing, then yes, put them in the subclass.

jverda at 2007-7-12 16:24:38 > top of Java-index,Java Essentials,New To Java...
# 8

this is my sub-class. now it's saying:

totalCost has private access in Package

which is understood, but i'm not quite sure how to bring totalcost over. do i just make a getter method? does it work that way?

edit: it's not finished, i'm just focusing on getting the private modifier into here..

class InsuredPackage extends Package

{

public double calculateInsurance(String askInsurance)

{

if (askInsurance == "yes")

{

if (totalCost >= 0.00 && totalCost <= 1.00)

{

insuranceTotal = totalCost + 2.45;

}

else if (totalCost >= 1.01 && totalCost <= 3.00)

{

insuranceTotal = totalCost + 3.95;

}

else if (totalCost >= 3.01 && totalCost <= 3.01)

{

insuranceTotal = totalCost + 5.55;

}

}

return insuranceTotal;

}

}

Message was edited by:

DenisK

DenisKa at 2007-7-12 16:24:38 > top of Java-index,Java Essentials,New To Java...
# 9
> askInsurance == "yes"Aside: weren't your knuckles already slapped over doing that?Also: do you know about boolean?
Hippolytea at 2007-7-12 16:24:38 > top of Java-index,Java Essentials,New To Java...
# 10
yeah i know, i didn't focus on that yet. i know how to fix it. i just can't get the private modifier into the sub-classif ("yes".equals(askInsurance))Message was edited by: DenisK
DenisKa at 2007-7-12 16:24:38 > top of Java-index,Java Essentials,New To Java...
# 11
Do you have a method suggestively named getTotalCost?
Hippolytea at 2007-7-12 16:24:38 > top of Java-index,Java Essentials,New To Java...
# 12

totalCost should not even be a member variable. You should just calculate it and return it on the fly. No need to store it.

The subclass' calc method can just call the super's, and then add its own stuff onto the result, e.g.

public double calc() {

double ins = ...;

double result = super.calc() + ins;

return result;

}

jverda at 2007-7-12 16:24:38 > top of Java-index,Java Essentials,New To Java...
# 13

class InsuredPackage extends Package

{

private double insuranceTotal;

public double calculateInsurance(String askInsurance)

{

if ("yes".equals(askInsurance))

{

if (super.getTotalCost() >= 0.00 && super.getTotalCost() <= 1.00)

{

insuranceTotal = super.getTotalCost() + 2.45;

}

else if (super.getTotalCost() >= 1.01 && super.getTotalCost() <= 3.00)

{

insuranceTotal = super.getTotalCost() + 3.95;

}

else if (super.getTotalCost() >= 3.01 && super.getTotalCost() <= 3.01)

{

insuranceTotal = super.getTotalCost() + 5.55;

}

}

return insuranceTotal;

}

public void display()

{

System.out.println(super.display() +"Total Insurance Cost: "+insuranceTotal);

}

}

now.. what am i doing.. :S

Package.java:87: cannot find symbol

symbol : constructor Package()

location: class Package

class InsuredPackage extends Package

^

Package.java:113: 'void' type not allowed here

System.out.println(super.display() +"Total Insurance Cost: "+insuranceTotal);

^

2 errors

DenisKa at 2007-7-12 16:24:38 > top of Java-index,Java Essentials,New To Java...
# 14

> Package.java:87: cannot find symbol

> symbol : constructor Package()

> location: class Package

> class InsuredPackage extends Package

You didn't provide an explicit constructor in the subclass, so there's an implicit one that just calls super(). You *did* provide an explicit c'tor in the parent class, so the implicit no-arg c'tor no longer exists, unless you provide it.

See rules below.

> ^

> Package.java:113: 'void' type not allowed here

> System.out.println(super.display()

> +"Total Insurance Cost: "+insuranceTotal);

>^

Looks like you missed a semicolon or have an extra or missing brace somewhere.

Constructor rules:

1) Every class has at least one ctor.

1.1) If you do not define an explicit constructor for your class, the compiler provides a implicit constructor that takes no args and simply calls super().

1.2) If you do define one or more explicit constructors, regardless of whether they take args, then the compiler no longer provides the implicit no-arg ctor. In this case, you must explicitly define a public MyClass() {...}

if you want one.

1.3) Constructors are not inherited.

2) The first statement in the body of any ctor is either a call to a superclass ctor super(...)

or a call to another ctor of this class this(...)

2.1) If you do not explicitly put a call to super(...) or this(...) as the first statement in a ctor that you define, then the compiler implicitly inserts a call to super's no-arg ctor super()

as the first call. The implicitly called ctor is always super's no-arg ctor, regardless of whether the currently running ctor takes args.

2.2) There is always exactly one call to either super(...) or this(...) in each constructor, and it is always the first call. You can't put in more than one, and if you put one in, the compiler's implicitly provided one is removed.

jverda at 2007-7-12 16:24:38 > top of Java-index,Java Essentials,New To Java...
# 15
Okay, thanks a ton jverd! I'm still trying to figure out the display error. but you've helped enough. thanks to everyone else aswell :)
DenisKa at 2007-7-21 21:37:31 > top of Java-index,Java Essentials,New To Java...
# 16
seems like theres no syntax error there... has to be something with the void..Package.java:124: 'void' type not allowed hereSystem.out.println(super.display() +"Total Insurance Cost: "+insuranceTotal);^
DenisKa at 2007-7-21 21:37:31 > top of Java-index,Java Essentials,New To Java...
# 17

i made the display() method in the super class return a string apposed to void and made it return. the display() method in the sub-class i changed to String instead of void and it compiled.. i'm just not getting past the constructor issue now

class InsuredPackage extends Package

{

public InsuredPackage()

{

insuranceTotal = calculateInsurance(super.getAskInsurance());

}

Package.java:96: cannot find symbol

symbol : constructor Package()

location: class Package

{

^

1 error

Press any key to continue . . .

DenisKa at 2007-7-21 21:37:31 > top of Java-index,Java Essentials,New To Java...
# 18

> Package.java:96: cannot find symbol

> symbol : constructor Package()

It's looking for Package's no-argument constructor but there isn't one!

What's happening is that there is an implicit call, here:

class InsuredPackage extends Package {

public InsuredPackage() {

super(); //implicit -- a futile call to nonexistent Package()

...

To fix this syntax error, supply that constructor or explicitly call an existing one.

Hippolytea at 2007-7-21 21:37:31 > top of Java-index,Java Essentials,New To Java...
# 19

> Package.java:96: cannot find symbol

> symbol : constructor Package()

> location: class Package

> {

> ^

> 1 error

> Press any key to continue . . .

Didn't I already answer this?

You need to either call the existing super c'tor from the subclass, or provide a default (no-arg) c'tor in the super class to be called implicitly by the subclass.

I'd go for the former. If super needs those parameters in order to be in a valid state, then sub needs them too and should pass them on to super.

jverda at 2007-7-21 21:37:31 > top of Java-index,Java Essentials,New To Java...
# 20
i'm reading over your posts and it's just not coming in, i have no idea how to call it, i've never learnt anything like this... sorry guysMessage was edited by: DenisK
DenisKa at 2007-7-21 21:37:31 > top of Java-index,Java Essentials,New To Java...
# 21

If your class is studying derivation, you must have discussed writing the

constructors in the derived classes, or have a section in your textbook. Review.

In the meanwhile, here's a demo:

class A {

public A (String sampleParam) {

}

}

class B extends A {

public B() {

//super("uncomment me and recompile");

}

}

Hippolytea at 2007-7-21 21:37:31 > top of Java-index,Java Essentials,New To Java...
# 22

oh that. yeah i know that actually, just the way you guys were saying it threw me way off.. whenever i tried adding super in the subclass it gave an error too..

Package.java:97: cannot find symbol

symbol : constructor Package()

location: class Package

super();

^

1 error

DenisKa at 2007-7-21 21:37:31 > top of Java-index,Java Essentials,New To Java...
# 23

> oh that. yeah i know that actually, just the way you

> guys were saying it threw me way off.. whenever i

> tried adding super in the subclass it gave an error

> too..

>

> Package.java:97: cannot find symbol

> symbol : constructor Package()

> location: class Package

>super();

> ^

> 1 error

Because the only c'tor that exists in the superclass requires one or more arguments, and you're not passing any.

jverda at 2007-7-21 21:37:31 > top of Java-index,Java Essentials,New To Java...
# 24

If I'm doing new Package(weightInOz, shipMethod, askInsurance)

wouldn't I also want to do new InsuredPackage(weightInOz, shipMethod, askInsurance)

?

And what's askInsurance for ayway? Is it to distinguish between a plain old package and an insured one? If so, get rid of it completely. The fact that something is or is not an instance of the subclass does that for you.

jverda at 2007-7-21 21:37:31 > top of Java-index,Java Essentials,New To Java...
# 25

Well the TestPackage class(which is in another file) asks the user for weight, shipping method, and if they want shipping insurance.. and from there it goes through the calculations..

public Package(double weightInOz, String shipMethod, String askInsurance)

{

this.weightInOz = weightInOz;

this.shipMethod = shipMethod;

totalCost = calculateCost(weightInOz, shipMethod);

}

public InsuredPackage()

{

super();

insuranceTotal = calculateInsurance(super.getAskInsurance());

}

DenisKa at 2007-7-21 21:37:31 > top of Java-index,Java Essentials,New To Java...
# 26

> > public Package(double weightInOz, String shipMethod,

> , String askInsurance)

...

> public InsuredPackage()

> {

> super();

...

> }

>

So, InsuredPackages don't have a weight or shipping method?

jverda at 2007-7-21 21:37:31 > top of Java-index,Java Essentials,New To Java...
# 27
Look, you're saying you need weight and shipping method when creating a Package object. Since an InsuredPackge IS-A Package, why wouldn't you need that information when creating an InsuredPackage object?And I still think that the askInsure parameter is pointless.
jverda at 2007-7-21 21:37:31 > top of Java-index,Java Essentials,New To Java...
# 28

no, basicly, the super class is doing the calculations by grabbing the weight and shipping method.. does the calculations and puts it into totalCost, then if the user said they want insurance.. it should go into the insuredPackage class and add a cost to it.. then send it back as insuredTotal.. and print it off in display

public class Package

{

private double weightInOz;

private String shipMethod;

private String askInsurance;

private double totalCost;

public Package(double weightInOz, String shipMethod, String askInsurance)

{

this.weightInOz = weightInOz;

this.shipMethod = shipMethod;

totalCost = calculateCost(weightInOz, shipMethod);

}

private double calculateCost(double weightInOz, String shipMethod)

{

if (weightInOz >= 1 && weightInOz <= 8)

{

if ("A".equals(shipMethod))

{

totalCost = 2.00;

}

else if("T".equals(shipMethod))

{

totalCost = 1.50;

}

else if("M".equals(shipMethod))

{

totalCost = 0.50;

}

}

else if (weightInOz >= 9 && weightInOz <= 16)

{

if ("A".equals(shipMethod))

{

totalCost = 3.00;

}

else if ("T".equals(shipMethod))

{

totalCost = 2.35;

}

else if ("M".equals(shipMethod))

{

totalCost = 1.50;

}

}

else if (weightInOz >= 17 && weightInOz > 17)

{

if ("A".equals(shipMethod))

{

totalCost = 4.50;

}

else if("T".equals(shipMethod))

{

totalCost = 3.25;

}

else if("M".equals(shipMethod))

{

totalCost = 2.15;

}

}

return totalCost;

}

public double getTotalCost()

{

return totalCost;

}

public void setTotalCost()

{

this.totalCost = totalCost;

}

public String getAskInsurance()

{

return askInsurance;

}

public void setAskInsurance()

{

this.askInsurance = askInsurance;

}

public String display()

{

return("Weight in Ounces: "+weightInOz +"\n"+ "Shipping Method: "+shipMethod +"\n"+ "Total Cost: " +totalCost +"\n");

}

}

class InsuredPackage extends Package

{

public InsuredPackage()

{

super();

}

private double insuranceTotal;

public double calculateInsurance(String askInsurance)

{

if ("yes".equals(super.getAskInsurance()))

{

if (super.getTotalCost() >= 0.00 && super.getTotalCost() <= 1.00)

{

insuranceTotal = super.getTotalCost() + 2.45;

}

else if (super.getTotalCost() >= 1.01 && super.getTotalCost() <= 3.00)

{

insuranceTotal = super.getTotalCost() + 3.95;

}

else if (super.getTotalCost() >= 3.01 && super.getTotalCost() <= 3.01)

{

insuranceTotal = super.getTotalCost() + 5.55;

}

}

return insuranceTotal;

}

public String display()

{

System.out.println(super.display() +"Total Insurance Cost: "+insuranceTotal);

}

}

Message was edited by:

DenisK

DenisKa at 2007-7-21 21:37:31 > top of Java-index,Java Essentials,New To Java...
# 29

pseudo...

Package pkg;

ask user for weight

ask user for ship method

ask user if insure

if insure {

pkg = new InsuredPackage (weight, shipMeth);

}

else {

pkg = new Package(weight, shipMeth);

}

shipCost = pkg.calcShipCost(); // or whatever you call that method

jverda at 2007-7-21 21:37:31 > top of Java-index,Java Essentials,New To Java...
# 30

> no, basicly, the super class is doing the

> calculations by grabbing the weight and shipping

> method.. does the calculations and puts it into

> totalCost, then if the user said they want

> insurance.. it should go into the insuredPackage

> class and add a cost to it.. then send it back as

> insuredTotal.. and print it off in display

No.

You create one object. It's either a Package or an InsuredPackage. You call that object's public calc method. Package does the calc one way, and InsuredPackage does it another way. InsuredPackage may or may not call the super.calc() method as part of its own implementation of calc().

jverda at 2007-7-21 21:37:36 > top of Java-index,Java Essentials,New To Java...
# 31
Get rid of the askInsurance parameter and member variable. It's unnecessary and wrong.
jverda at 2007-7-21 21:37:36 > top of Java-index,Java Essentials,New To Java...
# 32
mhm ok..Message was edited by: DenisK
DenisKa at 2007-7-21 21:37:36 > top of Java-index,Java Essentials,New To Java...
# 33
the way i was meaning to do it was to print out the insuredTotal even if the person says no.. and it'll print out probably just 0.00Message was edited by: DenisK
DenisKa at 2007-7-21 21:37:36 > top of Java-index,Java Essentials,New To Java...
# 34
how about i just gut the whole askInsurance **** and add the insurance automaticcally.. would thatbe easier?
DenisKa at 2007-7-21 21:37:36 > top of Java-index,Java Essentials,New To Java...
# 35

> the way i was meaning to do it was to print out the

> insuredTotal even if the person says no.. and it'll

> print out probably just 0.00

>

> Message was edited by:

> DenisK

You can do it one of two ways.

You can print out 0.00 if it's not insured, or you can override the print or display or toString or whatever method, so that the parent prints it one way and the child prints it another.

Either way, you do NOT need the askInsured param and member variable.

You really shouldn't print 0.00 if it's not insured--the parent should not even know about insurance at all. There should be nothing in the Package class that has anything to do with insurance.

jverda at 2007-7-21 21:37:36 > top of Java-index,Java Essentials,New To Java...
# 36
ok, so i'll take th askInsurance and put it into the insuredPackage, take it out of the super constructor paramter, get rid of the set and gets for askInsurance?
DenisKa at 2007-7-21 21:37:36 > top of Java-index,Java Essentials,New To Java...
# 37
now how to i make the sub-class take the answer from the user?
DenisKa at 2007-7-21 21:37:36 > top of Java-index,Java Essentials,New To Java...
# 38

> now how to i make the sub-class take the answer from

> the user?

The Package and InsuredPackage classes do NOT get the user input. Some other class gets the input (e.g., your test class), and then based on Y or N, creates an InsuredPackage or Package, passing the values the user provided into the c'tor.

jverda at 2007-7-21 21:37:36 > top of Java-index,Java Essentials,New To Java...
# 39
ok. thats wehre this is used.Package P1 = new Package(weightInOz, shipMethod);P1.display();i'm just still confused as to how i would get the insurance in on it.
DenisKa at 2007-7-21 21:37:36 > top of Java-index,Java Essentials,New To Java...
# 40
if insure { pkg = new InsuredPackage (weight, shipMeth);}else { pkg = new Package(weight, shipMeth);}where would i put this? ^^
DenisKa at 2007-7-21 21:37:36 > top of Java-index,Java Essentials,New To Java...
# 41

> if insure {

>pkg = new InsuredPackage (weight, shipMeth);

>

> else {

>pkg = new Package(weight, shipMeth);

>

>

>

> where would i put this? ^^

Right after you get the user's input for weight, method, and insurance.

jverda at 2007-7-21 21:37:36 > top of Java-index,Java Essentials,New To Java...
# 42

public class TestPackage

{

public static void main(String[] args)

{

double weightInOz = Double.parseDouble(JOptionPane.showInputDialog(null,

"Enter the packages weight in oz.",

"Weight In Oz.", JOptionPane.QUESTION_MESSAGE));

String shipMethod = JOptionPane.showInputDialog(null,

"Enter the shipping method (A for Air, T for Truck, M for Mail).",

"Shipping Method.", JOptionPane.QUESTION_MESSAGE);

String askInsurance = JOptionPane.showInputDialog(null,

"Would you like shipping insurance?",

"Shipping Insurance.", JOptionPane.QUESTION_MESSAGE);

//Package P1 = new Package(weightInOz, shipMethod);

//P1.display();

if ("yes".equals(askInsurance))

{

InsuredPackage P2 = new InsuredPackage(weightInOz, shipMethod);

}

else {

Package P2 = new Package(weightInOz, shipMethod);

}

}

}

error:

TestPackage.java:27: cannot find symbol

symbol : constructor InsuredPackage(double,java.lang.String)

location: class InsuredPackage

InsuredPackage P2 = new InsuredPackage(weightInOz, shipMethod);

^

.\Package.java:87: cannot find symbol

symbol : constructor Package()

location: class Package

super();

^

2 errors

Press any key to continue . . .

Message was edited by:

DenisK

DenisKa at 2007-7-21 21:37:36 > top of Java-index,Java Essentials,New To Java...
# 43

once i do this:

public InsuredPackage()

{

super(1.0,"abc");

}

it compiles, but it won't print with the display() i wrote... as far as i know the display methods have to be void..

heres the super class display()

public void display()

{

System.out.println(super.display() +"Total Insurance Cost: "+insuranceTotal);

}

heres the sub-class.

public void display()

{

System.out.println(super.display() +"Total Insurance Cost: "+insuranceTotal);

}

i'm getting this.

Package.java:109: 'void' type not allowed here

System.out.println(super.display() +"Total Insurance Cost: "+insuranceTotal);

^

1 error

Press any key to continue . . .

also: i'm hoping that it overwrites those values in the super(); becuase i don't know which other way to do it

DenisKa at 2007-7-21 21:37:36 > top of Java-index,Java Essentials,New To Java...
# 44

I'm not going to read all that in detail, but you seem to be missing an important point.

Read the error messages carefully. They're your friends. They tell you what's wrong.

class Person {

private String name;

Person(String name) {

this.name = name;

}

}

class Student extends Person {

Student(String name) {

super(name);

}

}

...

new Student("Joe Cool");

jverda at 2007-7-21 21:37:36 > top of Java-index,Java Essentials,New To Java...
# 45
> once i do this:> public InsuredPackage()> {> super(1.0,"abc");> }Why would you do that?That says that all InsuredPackages have a weight of 1.0 and a shipping method of "abc".
jverda at 2007-7-21 21:37:41 > top of Java-index,Java Essentials,New To Java...
# 46

^^ yeah i know, i fixed that now. now it's just the void from the beginning..

now i just have this..

Package.java:109: 'void' type not allowed here

System.out.println(super.display() +"Total Insurance Cost: "+insuranceTotal);

^

1 error

Press any key to continue . . .

Message was edited by:

DenisK

Message was edited by:

DenisK

DenisKa at 2007-7-21 21:37:41 > top of Java-index,Java Essentials,New To Java...
# 47
something with those display methods just isn't working, no idea what though.
DenisKa at 2007-7-21 21:37:41 > top of Java-index,Java Essentials,New To Java...
# 48

> ^^ yeah i know, i fixed that now. now it's just the

> void from the beginning..

> now i just have this..

> Package.java:109: 'void' type not allowed here

> System.out.println(super.display()

> +"Total Insurance Cost: "+insuranceTotal);

>^

What does super.display() return?

How do you think you would print that "result"?

Again: Read the error message closely, think about what kind of thing it might mean, and do a little detective work.

jverda at 2007-7-21 21:37:41 > top of Java-index,Java Essentials,New To Java...
# 49
returns void. i tried making it return String but how would i print it off in a return statement?...
DenisKa at 2007-7-21 21:37:41 > top of Java-index,Java Essentials,New To Java...
# 50
Here's a suggestion: classes often override Object's toString method.
Hippolytea at 2007-7-21 21:37:41 > top of Java-index,Java Essentials,New To Java...
# 51

> returns void. i tried making it return String

Yes. That's what you have to do if you're going to use it that way.

> but how

> would i print it off in a return statement?...

EH?

What do you mean "print it off in a return statement"? You don't print anything in a return statement. You return something. Something else might then print that value.

You need to step back, take a breath, and not try to just jam everything together until it works. Think about the pieces. Think about the steps. Tackle a little bit at a time. It seems like you've almost got it, and you have all the kinds of pieces in one place or another. You just need to apply what you already know and put the pieces together correctly.

jverda at 2007-7-21 21:37:41 > top of Java-index,Java Essentials,New To Java...
# 52
the teacher wanted them to be displayed in a display method. if that clears anything up?
DenisKa at 2007-7-21 21:37:41 > top of Java-index,Java Essentials,New To Java...
# 53

> the teacher wanted them to be displayed in a display

> method. if that clears anything up?

But does that literally mean you must define a method called display? Okay:

public void display() {

System.out.println(toString());

}

This assumes you overridden toString() properly ;-)

Hippolytea at 2007-7-21 21:37:41 > top of Java-index,Java Essentials,New To Java...
# 54

> the teacher wanted them to be displayed in a display

> method. if that clears anything up?

No, it doesn't. And it doesn't have any bearing on anything.

class Parent {

void display() {

print stuff

}

}

class Child extends Parent {

void display() {

print other stuff

}

}

Oh... hmm.... Child's display wants to use some of the same stuff as Parent's display. But display() doesn't return anything. It prints. Okay, fine, so we need to have parent's display get its stuff to print from someplace else (where Child can also get it) and then print it

class Parent {

void display() {

stuff = stuffToPrint();

print stuff

}

protected String stuffToPrint() {

return some stuff

}

}

class Child extends Parent {

void display() {

stuff = stuffToPrint();

stuff += child specific stuff;

print stuff;

}

}

jverda at 2007-7-21 21:37:41 > top of Java-index,Java Essentials,New To Java...
# 55
:S all this "stuff" gets confusing..
DenisKa at 2007-7-21 21:37:41 > top of Java-index,Java Essentials,New To Java...
# 56

super class

public String toString()

{

return("Weight in Ounces: "+weightInOz +"\n"+ "Shipping Method: "+shipMethod +"\n"+ "Total Cost: " +totalCost +"\n");

}

public void display()

{

System.out.println(toString());

}

sub-class

public String toString()

{

return(super.toString() +"Total Insurance Cost: "+insuranceTotal);

}

public void display()

{

System.out.println(toString());

}

hows that looking?

works, just doesn't calculate the insurance cost for some reason..

DenisKa at 2007-7-21 21:37:41 > top of Java-index,Java Essentials,New To Java...
# 57

anybody see anything in here that is wrong? it returns 0.00 for insuranceTotal

class InsuredPackage extends Package

{

public InsuredPackage(double weightInOz, String shipMethod)

{

super(weightInOz, shipMethod);

}

private double insuranceTotal;

public double calculateInsurance()

{

if (super.getTotalCost() >= 0.00 && super.getTotalCost() <= 1.00)

{

insuranceTotal = super.getTotalCost() + 2.45;

}

else if (super.getTotalCost() >= 1.01 && super.getTotalCost() <= 3.00)

{

insuranceTotal = super.getTotalCost() + 3.95;

}

else if (super.getTotalCost() >= 3.01)

{

insuranceTotal = super.getTotalCost() + 5.55;

}

return insuranceTotal;

}

public String toString()

{

return(super.toString() +"Total Insurance Cost: "+insuranceTotal);

}

public void display()

{

System.out.println(toString());

}

}

DenisKa at 2007-7-21 21:37:41 > top of Java-index,Java Essentials,New To Java...
# 58

> anybody see anything in here that is wrong? it returns 0.00 for insuranceTotal

The only place that insuranceTotal gets a non zero value is in the calculateInsurance() method.

Where (in which line of code) do you invoke the calculateInsurance() method? Where should you invoke it?

pbrockway2a at 2007-7-21 21:37:41 > top of Java-index,Java Essentials,New To Java...
# 59
in the constructor?insuranceTotal = calculateInsurance();?
DenisKa at 2007-7-21 21:37:41 > top of Java-index,Java Essentials,New To Java...
# 60
Try it and see.
pbrockway2a at 2007-7-21 21:37:47 > top of Java-index,Java Essentials,New To Java...
# 61
wow it works perfectly! thanks guys! i must have spent endless hours on this thing. now i'm just going to add a option box in the joptionpane for when it asks for insurance.Message was edited by: DenisK
DenisKa at 2007-7-21 21:37:47 > top of Java-index,Java Essentials,New To Java...
# 62

> > public void display()

> {

> System.out.println(toString());

> }

> }

>

Any reason you need this in the child class? Super's display() method does exactly the same thing, right? You can get rid of this one in child.

jverda at 2007-7-21 21:37:47 > top of Java-index,Java Essentials,New To Java...
# 63

the one in the super class will print out something differently than the one in the child.

super

public String toString()

{

return("Weight in Ounces: "+weightInOz +"\n"+ "Shipping Method: "+shipMethod +"\n"+ "Cost Without Insurance: $" +totalCost +"\n");

}

public void display()

{

System.out.println(toString());

}

sub

public String toString()

{

return(super.toString() +"Total Cost With Insurance: $"+insuranceTotal);

}

public void display()

{

System.out.println(toString());

}

DenisKa at 2007-7-21 21:37:47 > top of Java-index,Java Essentials,New To Java...
# 64

> > Any reason you need this in the child class? Super's display() method does

> > exactly the same thing, right? You can get rid of this one in child.

> the one in the super class will print out something differently than the one in the

> child.

Are you sure of that? Take out the toString() in the child class, leaving just the identical one in the parent class, and see what happens.

pbrockway2a at 2007-7-21 21:37:47 > top of Java-index,Java Essentials,New To Java...
# 65
I'm surprised no one has caught little this gem lol:if ("yes".equals(askInsurance))
jellystonesa at 2007-7-21 21:37:47 > top of Java-index,Java Essentials,New To Java...
# 66

> I'm surprised no one has caught little this gem lol:

> if ("yes".equals(askInsurance))

askInsurance is initialised with a call to JOptionPane.showInputDialog() and this method will return null if the user cancels the input. Many people prefer the idiom used by the OP to the longerif((askInsurance != null) && askInsurance.equals("yes"))

pbrockway2a at 2007-7-21 21:37:47 > top of Java-index,Java Essentials,New To Java...