Question: Classes and Objects To explain the concept of Objects and Classes, we will implement a very rudimentary system that creates and stores Users and some

Classes and Objects

To explain the concept of Objects and Classes, we will implement a very rudimentary system that creates and stores Users and some attributes of each of the Users, and then prints out a listing of this information, formatted in some particular way.

Because we know we will be storing Users, and those users have attributes, we know that the best way to organize this information would be to have a "template" for what a user should have. We'll start with the creation of a User class.

  • Create the LessonExecution2 class and create a main method in it. This will be the Class that starts the program, and will make use of the User class.

  • Create User class

  • Create two attributes: a name and an age inside User class

  • Create User and store it, we'll specify the name and the age of the user, and then, we will print that data: The output should be something like:

User Name: Rui
User Age: 35

Creating Methods

In the previous section, the printing of data of the User object is being done on the main method, which is not very wise if one wants to print the contents of several User objects, because it would mean that there would have to be as many System.out.println as twice the number of User objects created.

A method that prints this same data could be easily created. Lets do it.

  • Go to the User class source code file, and add printData() method

Now the main method of LessonExecution2 must be modified to call the method printData instead of having the two System.out.println statements.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!