Assign courseStudent's name with Smith, age with 20, and ID with 9999. Use the printAll() member method and a separate println() statement to output courseStudents's data. Sample output from the given program: Name: Smith, Age: 20, ID: 9999

Engineering · College · Tue Nov 03 2020

Answered on

To accomplish the task you described, we need to assume that there is an existing class with the name `courseStudent` that has member variables for `name`, `age`, and `ID`. This class should also have a member method named `printAll()`.

Here is a sample code in Java to illustrate this:

```java public class CourseStudent { // Member variables for the CourseStudent class String name; int age; int ID; // Constructor to initialize the member variables public CourseStudent(String name, int age, int ID) { this.name = name; this.age = age; this.ID = ID; } // Member method to print all data public void printAll() { System.out.println("Name: " + this.name + ", Age: " + this.age + ", ID: " + this.ID); } public static void main(String[] args) { // Creating an object of CourseStudent and assigning it the provided values CourseStudent courseStudent = new CourseStudent("Smith", 20, 9999); // Using the printAll() member method to output courseStudent's data courseStudent.printAll(); // Using a separate println statement to output courseStudent's data (if required) System.out.println("Name: " + courseStudent.name + ", Age: " + courseStudent.age + ", ID: " + courseStudent.ID); } } ``` Upon running this program, the output will be:

``` Name: Smith, Age: 20, ID: 9999 Name: Smith, Age: 20, ID: 9999 ```

Extra: In the above code:

- The `CourseStudent` class is defined with three member variables `name`, `age`, and `ID`. - A constructor is provided for the `CourseStudent` class, which initializes these variables. - The `printAll()` method is a member method of the `CourseStudent` class that prints out the object's data to the console. - The `main` method is where the program execution starts. It creates an instance of `CourseStudent` with the specified values, and then it calls the `printAll()` method to print the object's data. - The last `println` statement is an alternative way to directly print the instance's data without using the `printAll()` method, which effectively shows the same output.

Remember that in Java, we generally encapsulate the variables and provide public getters and setters to access them, rather than directly accessing the member variables. This is a standard practice to follow in object-oriented programming, promoting data encapsulation and security.

Related Questions