1

Given the following class:

public class Customer {
    protected String firstName;
    protected String lastName;
    protected String ID;
    protected float  amountSpent;

    // Contructor
    // Accessors and mutators
}

public class Gold extends Customer {
    protected discount;

    // overloaded contructor
    // Accessors and mutator
}

and the following code

Customer[][] arr = new Customer[2][1];

Customer[] preferredArr = new Gold[3];
Customer[] regularArr = new Customer[3];

preferredArr[0] = new Gold("John", "Doe", "1234", 45, .12)
regularArr[0] = new Customer("Caroline", "Merritt", "5678", 60)

arr[0] = preferredArr;
arr[1] = regularArr;

How would I access John's information using preferredArr[0].getFirstName() if it is inside of the arr array. Also I can't use ArrayList as specified by my professor. Thanks for the help!

1
  • 1
    I would suggest you arr[0][0].getFirstName() but you have to be sure that preferedArr will stay at index 0 when launching your program. Commented Sep 15, 2020 at 14:11

1 Answer 1

2

preferredArr[0].getFirstName(), work because the object is in preferredArr. to be exact the reference of the object.
arr[0][0].getFirstName(): also, work, because also, only the reference to the same object is stored in arr[0][0].

class Customer {
    protected String firstName;
    protected String lastName;
    protected String ID;
    protected float  amountSpent;

    // Contructor
    Customer(String firstName, String lastName, String ID, float  amountSpent)
    {
        this.firstName = firstName;
        this.lastName = lastName;
        this.ID = ID;
        this.amountSpent = amountSpent;
    }
    
    // Accessors and mutators
    
    public String getFirstName()
    {
        return this.firstName;
    }
}

class Gold extends Customer {
    protected int discount;

    // overloaded contructor
    Gold(String firstName, String lastName, String ID, int discount, float  amountSpent)
    {
        super(firstName, lastName, ID, amountSpent);
        this.discount = discount;
    }
    // Accessors and mutator
}

public class Main
{
    public static void main(String[] args) 
    {
        
        Customer[][] arr = new Customer[2][1];

        Customer[] preferredArr = new Gold[3];
        Customer[] regularArr = new Customer[3];
        
        preferredArr[0] = new Gold("John", "Doe", "1234", 45, 0.12f);
        regularArr[0] = new Customer("Caroline", "Merritt", "5678", 60);
        
        arr[0] = preferredArr;
        arr[1] = regularArr;
        
        System.out.println("preferredArr[0].getFirstName() = "+preferredArr[0].getFirstName());
        
        System.out.println("arr[0][0].getFirstName() = "+arr[0][0].getFirstName());   

    }
}

The result :

preferredArr[0].getFirstName() = John                                                           
arr[0][0].getFirstName() = John

You can check this : two references to the same object.
And where the object in java are stored
Good Luck.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.