metamorphisis
metamorphisis

Reputation: 61

Java parameter in constructor issue with class modifier

My constructor keeps giving an error at public Customer(String initName) with:

a { expected.

Here's my code:

public class CustomerConstructorTestProgram {
    public static void main(String args[]) {
        Customer c1, c2, c3;
        // Create Bob
        c1 = new Customer("Bob");
        c1.name = "Bob";
        c1.age = 17;
        c1.money = 10;
        // Create Dottie
        c2 = new Customer("Dottie");
        c2.name = "Dottie";
        c2.age = 3;
        c2.money = 0;
        // Create blank customer
        c3 = new Customer("Jane");

        public Customer(String initName) {
            name = initName;
            age = 0;
            money = 0.0f;
        }



        System.out.println("Bob looks like this: " + c1.name + ", " +
                c1.age + ", " + c1.money);
        System.out.println("Dottie looks like this: " + c2.name + ", "
                + c2.age + ", " + c2.money);
        System.out.println("Customer 3 looks like this: " + c3.name +
                ", " + c3.age + ", " + c3.money);
    }
}

Upvotes: 0

Views: 61

Answers (1)

Sagar Kulkarni
Sagar Kulkarni

Reputation: 503

  1. The Customer class has to be properly decalred (the class Customer doesn't exist in your code). You've decalred the public class CustomerConstructorTestProgram where the public Customer(String initName) becomes a method and not a constructor! For that, you need to create a separate Customer class or modify the class name!

  2. The class should contain the members i.e, in your case the 3 variables name, age, money (again missing in your code)

So, I will provide you with 2 possible solutions! Choose the way you need it for your use.

Solution 1 : Unified use of class

public class Customer
{
    String name;
    int age;
    float money;

    public Customer(String initName)
    {
        name = initName;
        age = 0;
        money = 0.0f;
    }

    public static void main(String args[])
    {
        Customer c1, c2, c3;

        // Create Bob
        c1 = new Customer("Bob");
        c1.age = 17;
        c1.money = 10;

        // Create Dottie
        c2 = new Customer("Dottie");
        c2.age = 3;
        c2.money = 0;

        // Create blank customer
        c3 = new Customer("Jane");

        System.out.println("Bob looks like this: " + c1.name + ", " +
                c1.age + ", " + c1.money);

        System.out.println("Dottie looks like this: " + c2.name + ", "
                + c2.age + ", " + c2.money);

        System.out.println("Customer 3 looks like this: " + c3.name +
                ", " + c3.age + ", " + c3.money);
    }
}

Solution 2 : Separation of classes

class Customer
{
    String name;
    int age;
    float money;

    public Customer(String initName)
    {
        name = initName;
        age = 0;
        money = 0.0f;
    }
}

public class CustomerConstructorTestProgram
{
    public static void main(String args[])
    {
        Customer c1, c2, c3;

        // Create Bob
        c1 = new Customer("Bob");
        c1.age = 17;
        c1.money = 10;

        // Create Dottie
        c2 = new Customer("Dottie");
        c2.age = 3;
        c2.money = 0;

        // Create blank customer
        c3 = new Customer("Jane");

        System.out.println("Bob looks like this: " + c1.name + ", " +
                c1.age + ", " + c1.money);

        System.out.println("Dottie looks like this: " + c2.name + ", "
                + c2.age + ", " + c2.money);

        System.out.println("Customer 3 looks like this: " + c3.name +
                ", " + c3.age + ", " + c3.money);
    }
}

Upvotes: 2

Related Questions