Below Is The Beginning Of A Java Class, Complete This Class

Below Is The Beginning Of A Java Classa Complete This Class So That

Below is the beginning of a Java class. a) Complete this class so that: i. The instance variables receive values when an object of the class is created. ii. The class has methods to both access and change all instance variables individually. iii. The toString method is overridden. b) Write a single sub-class of the Implement class that includes at least the following: i. The addition of a single piece of data unique to the sub-class. ii. Methods to access and return this data. iii. A call to the super-class’s constructor. c) Create an array of objects of the sub-class. i. Add two objects to the array ii. Loop through the array and print what each element is constructed from.

Paper For Above instruction

Complete implementation of the described Java classes involves creating a base class with proper encapsulation, inheritance, and object management, followed by instantiating objects and demonstrating their usage.

First, let's consider the base class, which we'll call `Implement`. The class should have instance variables that are initialized upon object creation, and it should provide getter and setter methods for each variable. Additionally, the `toString()` method ought to be overridden to give a meaningful string representation of an object.

Suppose `Implement` has two fields: `name` (String) and `value` (int). The constructor will initialize these fields, and accessor methods (`getName()`, `setName()`, `getValue()`, `setValue()`) will allow individual access and modification. Overriding `toString()` provides a concise description of an object instances' current state.

Next, a subclass can be created from `Implement`, say `ExtendedImplement`. This subclass adds a unique data member; for example, `extraInfo` (String). It provides its own constructor which calls the super class constructor and sets its unique data. Accessor method `getExtraInfo()` returns this additional data.

Finally, for demonstration, instantiate an array of `ExtendedImplement` objects, add two objects to this array with differing data, and iterate through the array to print their string representations, illustrating how data is constructed and accessed across inheritance levels.

Below is the complete code reflecting these descriptions, with proper class definitions, object creation, and data output:

```java

// Base class Implement

public class Implement {

private String name;

private int value;

// Constructor

public Implement(String name, int value) {

this.name = name;

this.value = value;

}

// Accessor methods

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getValue() {

return value;

}

public void setValue(int value) {

this.value = value;

}

// Override toString method

@Override

public String toString() {

return "Implement[name=" + name + ", value=" + value + "]";

}

}

// SubClass extending Implement

public class ExtendedImplement extends Implement {

private String extraInfo;

// Constructor calling super constructor

public ExtendedImplement(String name, int value, String extraInfo) {

super(name, value);

this.extraInfo = extraInfo;

}

// Method to access extraInfo

public String getExtraInfo() {

return extraInfo;

}

// Method to set extraInfo

public void setExtraInfo(String extraInfo) {

this.extraInfo = extraInfo;

}

// Override toString method to include extraInfo and call super toString

@Override

public String toString() {

return super.toString() + ", extraInfo=" + extraInfo;

}

}

// Main class to demonstrate

public class Main {

public static void main(String[] args) {

// Create an array of ExtendedImplement objects

ExtendedImplement[] objects = new ExtendedImplement[2];

// Add two objects to the array

objects[0] = new ExtendedImplement("Object1", 10, "Extra data 1");

objects[1] = new ExtendedImplement("Object2", 20, "Extra data 2");

// Loop through the array and print details

for (ExtendedImplement obj : objects) {

System.out.println(obj.toString());

}

}

}

```

This code demonstrates complete class design with constructors, accessors, mutators, inheritance, and data representation suitable for further extension or integration. The array instantiates multiple objects, and printing each confirms their constructed states, fulfilling all parts of the assignment.

References

  • Deitel, P. J., & Deitel, H. (2014). Java: How to Program. Pearson.
  • Horstmann, C. (2018). Core Java Volume I--Fundamentals. Pearson.
  • Gosling, J., Joy, B., Steele, G., Bracha, G., & Buckle, G. (2014). The Java Language Specification. Oracle.
  • Bloch, J. (2008). Effective Java. Addison-Wesley.
  • Fowler, M. (2018). Refactoring: Improving the Design of Existing Code. Addison-Wesley.