Consider the following definition of the class MyClass

public class My Class {
private int x, private static int count, private MyClass ( ) {
} public MyClass (int a) {
} public void setX({ant a) {
} public void printx() {
} public static void printCount () {
} public static int incrementCount () } }

a. White a Java statement that increments the value of count by 1
b. Write a Java statement that outputs the value of count
c. Write the definition of the constructors and methods of the class MyClass as described above (3)
d. Write a Java statement that declares myObject1 to be a MyClass object and initialises its instance variable x to 5 (1)
e. Write a Java statement that declares myObject2 to be a MyClass object and initialises its instance variable x to 7 (1)

Respuesta :

Answer:

(a) count++;

(b) System.out.println(count);

(c)

public class My Class {

  private int x;

  private static int count;

  private MyClass ( ) {

   this.x = 0;

   incrementCount();

  }

  public MyClass (int a) {

     this.x = a;

     incrementCount();

  }

  public void setX(int a) {

      this.x = a;

  }  

 

  public void printx() {  

     System.out.println(this.x);

  }

  public static void printCount () {

     System.out.println(count);

  }

  public static int incrementCount () {

     count++;

     return count;

  }

}

(d) MyClass myObject1 = new MyClass(5);

(e) MyClass myObject2 = new MyClass(7);

Explanation:

(a) To increment the value of a variable by 1, just put the ++ operator after the variable. For example to increment the value of variable m by 1, simply write m++

This (m++) can also be written as;

m = m + 1

Therefore, to increment the value of count by 1, simply write;

count++

or

count = count + 1

(b) To output a value, simply use any of the methods for printing to the console. Such as

System.out.println()

System.out.print()

Therefore, to output the value of count, we have

System.out.println(count);

(c)

Starting with the constructors.

There are two constructors in the code.

i. a private default constructor. This should initialize the instance variable x to its default value of 0 and the static variable count should be incremented by calling the incrementCount() method. Therefore it can be written as;

==============================

private MyClass ( ) {

   this.x = 0;

   incrementCount();

  }

==============================

ii. a public custom constructor. This should initialize the instance variable x to the value of the variable a specified in its parameter. It should also increment the value of count by calling the incrementCount() method. Therefore, it can be written as follows;

==============================

  public MyClass (int a) {

     this.x = a;

     incrementCount();

  }

==============================

Next are the methods

i. The first method is the setX() method. It sets the value of the instance variable x to the value of the variable a specified in its parameter. Therefore it can be written as;

==============================

  public void setX(int a) {

      this.x = a;

  }  

==============================

ii. The second method is the printx() method. This method prints the value of the instance variable x.

==============================

  public void printx() {  

     System.out.println(this.x);

  }

==============================

iii. The third method is the printCount() method. This method prints the value of the static variable count. It can be written as follows;

==============================

  public static void printCount () {

     System.out.println(count);

  }

==============================

iv. The fourth method is the incrementCount() method. This method increments the value of the static variable count. It then return the value of the new count. It can be written as;

==============================

   public static int incrementCount () {

     count++;

     return count;

  }

==============================

(d) This simply means creating an object myOjbect1 of the class MyClass and then initializing its instance variable x to 5.

To create an object of a class, we use the new keyword and follow the following format:

ClassName objectName = new Constructor

In this case;

ClassName is MyClass

objectName is myObject1

Constructor is one of the constructors of the MyClass which will initialize the instance variable x to 5. That is the second constructor in the code.

Therefore, the object can be created as follows;

MyClass myObject1 = new MyClass(5);

(e) This simply means creating an object myOjbect2 of the class MyClass and then initializing its instance variable x to 7.

To create an object of a class, we use the new keyword and follow the following format:

ClassName objectName = new Constructor

In this case;

ClassName is MyClass

objectName is myObject2

Constructor is one of the constructors of the MyClass which will initialize the instance variable x to 7. That is the second constructor in the code.

Therefore, the object can be created as follows;

MyClass myObject2 = new MyClass(7);