Question

In: Computer Science

Question 1 price is an instance variable of a class. Inside that class there is a...

Question 1

price is an instance variable of a class. Inside that class there is a method public void change(int price).

From inside the method " change" how do we refer to the instance variable price and not the parameter price?

Answer:

Question 2

A method of a class can access which of the following:

1 global variables in the same scope

2. local variables defined within the method

3. the instance variables of its class

4. its parameters

Question 3

Which of the following are true of accessor methods:

1. accessor methods are of the same type as the variable to which they provide access.

2. accessor methods take no parameters

3. accessor methods are always of type void

4. accessor methods should be private

Solutions

Expert Solution

Solution :

Answer 1 :
this.price will refer to the instance variable of the class. only "price" will refer to the parameter of the method.

Answer 2 :
1 : global variables can be accessed if they are in same scope.
2 : local variables defined inside method can be accessed inside method.
3 : Instance variable of the class can be accessed inside method too if the method is not static.
4 : parameters passed to the method can be accessed inside method.

Answer 3 :
1. Yes the return type of the accessor method is same as the data type of the variable that is being accessed.
2. Yes, Accessor method doesnt need any parameter, as it returns the value of the variable being accessed.
3. No accessor methods are not void always.
4. no accessor method can not be private.

Answer: 1 and 2 are true


Related Solutions

Create a class Sentence with an instance variable public Word[] words. Furthermore: The class should have...
Create a class Sentence with an instance variable public Word[] words. Furthermore: The class should have a constructor Sentence(int size), where size determines the length of the sentence field of a given Sentence. The class should have an instance method public boolean isValid() that determines the validity of a sentence according to the rules detailed below. Also create a public nested class Word, with instance variables String value and Type type. Within this class, you must create: A public enum...
Define a class named Document that contains an instance variable of type String named text that...
Define a class named Document that contains an instance variable of type String named text that stores any textual content for the document. Create a method named toString that returns the text field and also include a method to set this value. Next, define a class for Email that is derived from Document and includes instance variables for the sender, recipient, and title of an email message. Implement appropriate set and get methods. The body of the email message should...
(JAVA) Referencing the class Oven, complete the Constructor and set method for the instance variable temp....
(JAVA) Referencing the class Oven, complete the Constructor and set method for the instance variable temp. Constructor checks if the temperature is between 0 and 500. If the temperature does not fit the requirement, then set the temperature to be zero. Otherwise, set it to be the value passed to the constructor. Set the power to be off. Set the String to be the color passed to the constructor. Use the same requirement for the constructor for the set temperature...
For question 1 , consider that inside the class Sky, we have already coded the following:...
For question 1 , consider that inside the class Sky, we have already coded the following:     public class Sky      {         private Color color;         public Sky( Color c)           {               color = c;           }       }   Consider the following method header:     public Color getColor(); Is this method a constructor, mutator or accessor?   Inside a method main, we see code like:      Airplane.foo3(34.6); From this, reconstruct the header of method foo3 (which belongs to class...
Write a class named GasTank containing: An instance variable named amount of type double, initialized to...
Write a class named GasTank containing: An instance variable named amount of type double, initialized to 0. An instance variable named capacity of type double. A constructor that accepts a parameter of type double. The value of the parameter is used to initialize the value of capacity. A method named addGas that accepts a parameter of type double. The value of the amount instance variable is increased by the value of the parameter. However, if the value of amount is...
Modify the supplied payroll system to include a private instance variable called joinDate in class Employee...
Modify the supplied payroll system to include a private instance variable called joinDate in class Employee to represent when they joined the company. Use the Joda-Time library class LocalDate as the type for this variable. See http://www.joda.org/joda-time/index.html for details end examples of how to use this library. You may also have to download the Joda-Time library (JAR File) and include this in your CLASSPATH or IDE Project Libraries. Use a static variable in the Employee class to help automatically assign...
Java Implement a class MyInteger that contains: • An int data field/instance variable named value that...
Java Implement a class MyInteger that contains: • An int data field/instance variable named value that stores the int value represented by this object. • A constructor that creates a MyInteger object for a default int value. What default value did you choose? What other values did you consider? Why did you make this choice? • A constructor that creates a MyInteger object for a specified int value. • A getter method, valueOf(), that returns value. • A setter method,...
Write a class called Pen that contains the following information: Private instance variables for the price...
Write a class called Pen that contains the following information: Private instance variables for the price of the pen (float) and color of the pen (String). A two-argument constructor to set each of the instance variables above. If the price is negative, throw an IllegalArgumentException stating the argument that is not correct. Get and Set methods for each instance variable with the same error detection as the constructor. public class Pen {
1. Implement the Vehicle class.  Add the following private instance variables to the Accoun class:...
1. Implement the Vehicle class.  Add the following private instance variables to the Accoun class: • An instance variable called wheelsCount of type int • An instance variable called vType of type String • An instance variable called isTruck of type boolean .  Add getters and setters for the three instance variables  Add the following methods to the Account class: • A void method called initialize that takes a parameter of type int, a String,and one double...
in bluej java Write an application with two classes. Class NumberUtility has one instance variable n...
in bluej java Write an application with two classes. Class NumberUtility has one instance variable n of type int. Constructor initializes instance variable n by using input parameter n. public NumberUtility(int n) Class also has the following methods:   public int getN()                              // Returns instance variable n public boolean isOdd()                  // Returns true if number n is odd and returns false otherwise. public boolean isEven()               // Returns true if number n is even and returns false if it is odd.      // Implement method by...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT