Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Consider the following class. public class Person (private String name; public Person(String name) { this.name = } name; @Override public String tostring() { return

 

Consider the following class. public class Person (private String name; public Person(String name) { this.name = } name; @Override public String tostring() { return this.getClass().getName() + "[name=" + this.name + "]"; } } If we have a Person object whose attribute name is a reference to the String object "Alice", calling toString() on this object will return the String: "Person[name=Alice]" Now consider the following class. public class Customer extends Person { private int balance; } In your answer to the parts below, do not modify the class Person. You may only use the parts of the classes Person and Customer shown above. (a) Override the method public String toString() in the class Customer so that 5 marks it returns a String that contains the word "Customer", the name, and the balance. For example, calling toString!) on a Customer with name "Alice" and balance 1200 is supposed to return the following String: "Customer[name=Alice][balance=1200]" (b) Write two public constructors for the class Customer: 11 marks The first constructor has two parameters of types String and int with the name and the balance. It initialises the instance variables with the values of the corresponding parameters.

Step by Step Solution

3.48 Rating (164 Votes )

There are 3 Steps involved in it

Step: 1

a To override the toString method in the Customer class youll need to call the supe... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Java How To Program Late Objects Version

Authors: Paul Deitel, Deitel & Associates

8th Edition

0136123716, 9780136123712

More Books

Students also viewed these Programming questions