0

I've been working on this project so I get to know how to use custom object and custom object types. The problem I'm having is when I write out System.out.println(fe1.TestAuto); it says identifier expected and I don't know why. Im trying to print out the object but it doesn't work. Another problem I'm having is when I want to use the discountValue() method to give a discount on cost it says double cannot be converted to Feature and I don't know why.

The discountValue() method takes in the input from double c and subtracts that with the double d in the Feature Class.

Here's my code below. Could someone try to help me.

public class Auto
{
    public String name;
    public double size;
    public Auto(String s, double sz){
        name = s;
        size = sz;
    }
    //step 2: method showFeature()
    public Feature showFeature(Feature f){
        
        System.out.println("The feature " +f+" costs: "+f.cost);
        
    }
    public Feature discountValue(String s, double c, double d){
    //Step 3: method to give discount on cost
        
        double z = c - d.cost;
    
        return z;
    }
}



public class TestAuto
{
   public static void main(String args[]){
    //instantiate an Auto object
    Auto at = new Auto("AMG", 73.5);
    //instantiate a Feature object
    Feature fe = new Feature("Leather Seat", 3000);
    //execute method addFeature with Feature object
    at.showFeature(fe);
    //prepare a new Feature object for receving information
    Feature fe1 = new Feature("", 0);
    //Run method discountValue with a feature and a new value
    fe1 = at.discountValue("GPS", 15000, 0.2);
    //print out the object
    System.out.println(fe1);
   }
}







public class Feature
{
    public String name;
    public double cost;
    public Feature(String s, double d){
        //step 1
        name = s;
        cost = d;
    }
    //step 4
    System.out.println(fe1.TestAuto);
}




1
  • Class Feature doesn't have a field TestAuto.
    – NomadMaker
    Nov 3, 2020 at 0:34

2 Answers 2

0

double z = c - d.cost;

d is of type double. It doesn't have a cost field. In fact, it is a primitive, it cannot be dereferenced in the first place (putting a dot after a double is an instant compiler error).

(In Feature.java) System.out.println(fe1.TestAuto);

You can't write this here; inside a class def, you can define fields, methods, constructors, initializers, and inner types. But not actual code (when would it run?) - and that's actual code, thus, not legal. Put it in a method or a constructor or some such.

0

You can't call System.out.println(fe1.TestAuto); (your step 4) because it isn't in a method of your class Feature. If you remove this line, the rest works (just remove .cost from d.cost.

And I don't understand why you call your class TestAuto, who is your main.

2
  • thank you for all the tips but I removed the .cost from d and it says on return z incompatible types: double cannot be converted to Feature. what do I do?>
    – hobmnum
    Nov 3, 2020 at 1:05
  • Oh yes sorry I can recommend following correction: step2 public void instead of public Feature because you return nothing, just print something. step 3 if you want to return a Feature change return z with return new Feature(s,z) and you will need to import class Feature or something like that.
    – zerbene
    Nov 3, 2020 at 1:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.