Create a class called Person.java with the following attributes:
Person.java
Add a constructor to the class to initialize these attributes.
Implement the following methods:
displayInformation:
modifyAddress:
Create at least two objects of the Person class and initialize their attributes with different data.
Person
Use the methods displayInformation() and modifyAddress() to display information about the people and change the address of one of them.
displayInformation()
modifyAddress()
No input required
Information of Person 1: Name: John Age: 30 Address: Street A Information of Person 2: Name: Mary Age: 25 Address: Street B The address has been modified to: Street C Name: John Age: 30 Address: Street C
public class Person { // Attributes String name; int age; String address; // Constructor public Person(String name, int age, String address) { this.name = name; this.age = age; this.address = address; } // Method to display person's information public void displayInformation() { System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("Address: " + address); } // Method to modify the person's address public void modifyAddress(String newAddress) { this.address = newAddress; System.out.println("The address has been modified to: " + newAddress); } public static void main(String[] args) { // Create objects of the "Person" class Person person1 = new Person("John", 30, "Street A"); Person person2 = new Person("Mary", 25, "Street B"); // Display information of the persons System.out.println("Information of Person 1:"); person1.displayInformation(); System.out.println("\nInformation of Person 2:"); person2.displayInformation(); // Modify the address of Person 1 person1.modifyAddress("Street C"); person1.displayInformation(); } }
Click here to view the exercise solution
Share it on your social media and challenge your friends to solve programming problems. Together, we can learn and grow.
The code has been successfully copied to the clipboard.
Continue improving your Java programming skills with our selection of practical exercises from the lesson. Click on Practice and challenge your knowledge!
Delve into the basics of object-oriented programming in Java. Learn about classes and objects, the essential components that breathe life into modularity and code reusability in this versatile programming language.
Dive into the essence of object-oriented programming in Java by gaining a deep understanding of attributes and methods. Explore how these fundamental elements are crucial for creating classes and objects, providing modularity and efficiency in software development.
Discover the importance of constructors in Java and how they crucially initialize objects. Learn best practices to ensure consistency in instance creation and optimize your code with our detailed examples.
Learn how to create secure interfaces with getters and setters methods in Java. Explore how these methods promote encapsulation and provide precise control over attribute modification.
Create a Java program that requests three names of people from the user and stores them in an array of objects of type Person.
Create a Java program that prompts the user for three names of people and stores them in an array of Person-type objects.
The instanceof operator in Java is used to check if an object is an instance of a specific class, subclass, or implements a specific interface.
Create a new Java project with three classes plus another class to test the logic of the code. The main classes of the program are the following.
Nested and Inner Classes in Java are defined within another class and have access to the members of the containing class, including private members.
Inheritance in Java is one of the fundamental pillars of Object-Oriented Programming (OOP). It provides a mechanism through which a class can inherit properties and behaviors from another class.
Create a Java program to manage a photo book using object-oriented programming.
Create a class called 'Person' with attributes like name, age, and address. Then, create objects of the 'Person' class and initialize their attributes. Implement methods to display and modify these attributes.
An abstract class is a class that cannot be instantiated directly. This means you cannot create objects directly from an abstract class using the new operator.
Create a Java program that represents the following UML class diagram.
Create a Java program that implements an IVehiculo interface with two methods, one for Drive of type void and another for Refuel of type bool that has a parameter of type integer with the amount of gasoline to refuel.
Create a Java program that implements an abstract class Animal that has a Name property of type text and three methods SetName (string name), GetName and Eat.
Practice object-oriented programming exercises in Java. Learn to use constructors, destructors, inheritance, interfaces among others.
Free programming course with practical exercises and solutions in C#. Start learning right now!
Take your Exercises C# lessons everywhere with our Android app. Download it now from Google Play
Own and third party cookies to improve our services. If you go on surfing, we will consider you accepting its use.