Knowee
Questions
Features
Study Tools

Write a sample code, that initializes an instance of a person and sets it age to 18!

Question

Write a sample code, that initializes an instance of a person and sets it age to 18!

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple example in Python:

class Person:
    def __init__(self, age):
        self.age = age

# Initialize an instance of Person
person1 = Person(18)

# Print the age of person1
print(person1.age)

In this code:

  1. We first define a Person class with an __init__ method. This method is a special method in Python classes that is automatically called when a new instance of the class is created. It is used to initialize the attributes of the class.

  2. The Person class has one attribute: age.

  3. We then create an instance of the Person class, person1, and set its age to 18.

  4. Finally, we print the age of person1 to verify that it has been correctly set to 18.

This problem has been solved

Similar Questions

Single File Programming QuestionProblem StatementIn a theme park, only people who meet specific criteria can experience the thrill of the roller coaster. Develop a program that takes age and height as inputs, determining eligibility using logical operators. If the age is 18 or older and the height is 150 cm or taller, output "Allowed!" Otherwise, output "Sorry, Not allowed."Input format :The first line of input consists of an integer, which represents the person's age.The second line of input consists of an integer, which represents the person's height (in cm).Output format :The output displays either "Allowed!" or "Sorry, Not allowed" based on the given conditions.Refer to the sample output for the formatting specifications.Code constraints :In the given scenario, the test cases will fall under the following constraints:6 ≤ age ≤ 10050 ≤ height ≤ 200Sample test cases :Input 1 :28151Output 1 :Allowed!Input 2 :17151Output 2 :Sorry, Not allowedInput 3 :6200Output 3 :Sorry, Not allowedInput 4 :18150Output 4 :Allowed!

Write a program in C++ that takes age of five persons and then just display the age of each person usingarrays

Write a java program to define a class Person having name of the person , age of the person and gender of the person. Include accept() and toString() method to display the data. Create 3 objects and display name of the person who is youngest

Create a base class Person and a derived class Student with Person as its base class.Add two methods setname() (which takes the parameter self and name)and getname() which prints the name in the base class.Add two methods in the derived class: setage() (which takes the parameters self and age) which sets the age and getage() which prints the age.Create an instance of Student and name it as s1.Take name and age as inputs from the console.Call the setname() and setage() on this instance by passing the name and age parameters.Call the getname() and getage() on this class, which prints the passed parametersNotice that we have created an object of derived class but it could access the methods of base class.This is inheritance.

Create a program that asks the user to enter their name and their age. Print out a message addressed to them that tells them the year that they will turn 100 years old. Note: for this exercise, the expectation is that you explicitly write out the year (and therefore be out of date the next year).

1/2

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.