Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java programming(inheritance practice ) class NN(name & number) The following pr

ID: 3576289 • Letter: J

Question

Java programming(inheritance practice )
class NN(name & number)
The following program has 11 stubs, the job is to replace the following 11 stubs as indicated:
//<main>
Replace with main's body. main has 2 things to do: first,create 2 NN objects, use the non-static add method to create an NN object that represents the sum of those 2 objects, then System. out.println the sum object, thereby implicitly calling NN's toString method. Second, with an array of NN objects, use the static add method to find the sum of the numbers in the array, and output that result.
return 0;//<adds>
Replace with the body of the static add method. add's job is to compute and return the sum of all the numbers in array a. add does no input or output, and doesn't change a.
//<NN2>
Replace with the body of NN's 2-arg constructor. Just set the data members to the corresponding args ;no error-checking or output required. (The idea is that the name of an NN object is the English word for the number, so we might have an NN object holding {"three",3}.)
//<NN1>
Replace with the body of NN's 1 constructor. The 1-arg ctor is supposed to result in the name being "?" and the number being the arg. The 1-arg ctor is supposed to accomplish this by calling the 2-arg ctor.
return null;//<getName>
Replace with the body of the accessor method getName
return 0;//<getNumbet>
Replace with the body of the accessor method getNumber
return null;//<setName>
Replace with the body of the mutator method setName. No error checking required, just set the data member. Return a reference to the invoking object.
return 0;//<setNumber>
Replace with the body of the mutator method setNumber. No error checking required, just set the data member. Return a reference to the invoking object.
return null;//<toString>
Replace with the body of the toString method. The returned String is supposed to be the name followed by the number in parens. For example, if n were an NN object holding {"three",3},then n+"" would be "three(3)".
return null;//<addns>
Replace with the body of the non-static add method. Create and return an NN object whose name is "?" and whose number is the sum of the numbers in the invoking object and the arg. This method doesn't do any I/O, and does not change either the invoking object or the arg object.

Explanation / Answer

PROGRAM CODE:

package simple;

public class NN {

  

   private String name;

   private int number;

  

   public NN(String aName, int aNumber) {

       this.name = aName;

       this.number = aNumber;

   }

   public NN(int aNumber)

   {

       this("?", aNumber);

   }

   public String getName() {

       return name;

   }

   public void setName(String name) {

       this.name = name;

   }

   public int getNumber() {

       return number;

   }

   public void setNumber(int number) {

       this.number = number;

   }

  

   @Override

   public String toString() {

      

       return name + "(" + number + ")";

   }

  

   public NN addns(NN a[])

   {

       NN nnObject;

       int sum = 0;

       for(int i=0; i<a.length; i++)

       {

           sum += a[i].getNumber();

       }

       sum += this.getNumber();

       nnObject = new NN(sum);

       return nnObject;

   }

   public static int adds(NN a[])

   {

       int sum = 0;

       for(int i=0; i<a.length; i++)

       {

           sum += a[i].getNumber();

       }

       return sum;

   }

  

  

   public static void main(String[] args) {

      

       NN first = new NN("three", 3);

       NN second = new NN("four", 4);

       NN nnArray[] = {second, new NN("five", 5)};

       //calling the non static add method

       NN sum = first.addns(nnArray);

       System.out.println(sum);

      

       //calling the static add method

       System.out.println(adds(nnArray));

   }

}

OUTPUT:

?(12)

9

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote