The purpose of this is to create a class and a driver that can be used for diffe
ID: 3666337 • Letter: T
Question
The purpose of this is to create a class and a driver that can be used for different applications and have it so you can reuse the class and driver for other programs. Create a class with 2 private attributes where one is a String, one constructor which accepts at least one parameter. Setters and getters are required for each field where applicable. The class will be called the Cheese class where cheeseType is a private String attribute and CheesePrice is a double. The constructor receives parameters for newCheeseType as a String and newCheesePrice as a double. The methods are getCheeseType() returning a String, getCheesePrice() returning a double, setCheeseType() excepting a String parameter and setCheesePrice() accepting a double parameter. The driver should prompt the user for the attributes, instantiate the object in memory using the input and display the attributes from the object using the getters. Thanks a lot!
Explanation / Answer
As you have already seen, it is possible for a Java constructor to take parameters. These parameters can then be used to initialize the internal state (fields) of the newly created object. Here is an example:
In this example the Java constructor declaration is marked in bold. As you can see, three parameters are declared: first, last and year. Inside the body of the constructor the values of these three parameters are assigned to the fields of the Employee object.
The line breaks after each parameter are optional. The Java compiler ignores line breaks here. You can also write the parameter declaration in a single line if you want, like this:
To call this constructor that takes three parameters, you would instantiate an Employee object like this:
The parameters are passed to the constructor inside the parentheses after the class name on the right side of the equal sign. The object is then created, and the constructor executed. After execution of the above constructor, the fields initialized by the constructor will have the values of the parameters passed to the constructor.
A Java constructor parameter can have the same name as a field. If a constructor parameter has the same name as a field, the Java compiler has problems knowing which you refer to. By default, if a parameter (or local variable) has the same name as a field in the same class, the parameter (or local variable) "shadows" for the field. Look at this constructor example:
Inside the constructor of the Employee class the firstName, lastName and birthYear identifiers now refer to the constructor parameters, not to the Employee fields with the same names. Thus, the constructor now just sets the parameters equal to themselves. The Employee fields are never initialized.
To signal to the Java compiler that you mean the fields of the Employee class and not the parameters, put the this keyword and a dot in front of the field name. Here is how the Java constructor declaration from before looks with that change:
Now the Employee fields are correctly initialized inside the constructor.
Calling a Constructor
You call a constructor when you create a new instance of the class containing the constructor. Here is a Java constructor call example:
This example invokes (calls) the no-argument constructor for MyClass as defined earlier in this text.
In case you want to pass parameters to the constructor, you include the parameters between the parentheses after the class name, like this:
This example passes one parameter to the MyClass constructor that takes an int as parameter.
You can add getters and setters for class variables only. In your code, the class is perform and it has two methods - Main() and run(). All the variables you are using in your code and either in Main() or run(). Hence, those are local method variables, and not class variables.
getter and setter methods allow you to efficiently manage the access of class variables by other external classes. Your local method variables won't be accessed by other external classes. Also, a local variable inside one method cannot be directly accessed in another method, even if it is inside the same class because a local variable is only alive inside the method it is declared.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.