Knowee
Questions
Features
Study Tools

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

Question

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

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

Solution

Sure, here is a simple Java program that defines a class Person with name, age, and gender attributes. It includes an accept() method to set the data and a toString() method to display the data. It also creates 3 Person objects and displays the name of the youngest person.

import java.util.*;

class Person {
    String name;
    int age;
    String gender;

    public Person(String name, int age, String gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    public String toString() {
        return "Name: " + this.name + ", Age: " + this.age + ", Gender: " + this.gender;
    }
}

public class Main {
    public static void main(String[] args) {
        Person person1 = new Person("John", 25, "Male");
        Person person2 = new Person("Jane", 22, "Female");
        Person person3 = new Person("Bob", 30, "Male");

        Person[] people = {person1, person2, person3};

        Person youngest = people[0];

        for (Person person : people) {
            if (person.age < youngest.age) {
                youngest = person;
            }
        }

        System.out.println("The youngest person is: " + youngest.name);
    }
}

This program first creates 3 Person objects with different names, ages, and genders. It then puts these objects into an array. It sets the first person as the youngest initially. It then iterates over the array, and if it finds a person who is younger than the current youngest person, it updates the youngest person. Finally, it prints the name of the youngest person.

This problem has been solved

Similar Questions

WAJP o 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 youngestedited 09:01 AM

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.

Design a class named Person and its two subclasses named Student and Employee.  Make Faculty and Staff subclasses of Employee. A person has a name,  address, phone_number, and e-mail address. A student has a status (freshman, sophomore, junior, or senior).  An employee  has an office, salary. A faculty member has office_hours and a rank. A staff member has a title. Override the toString() method in each of these classes to display their details. Write a Java application and subsequent pseudocode to implement/simulate the same.Input :First line must read the Type of the Object(Person(P)/Employee(E)/Faculty(F)/Student(S)/Staff(T).Second line onwards read details such as name, address, phoneNo, Email and soonOutput :Print ClassName : Name, Address, PhonNo, Email, and rest of the datamembers of that class. For Example :if Type = P, then print Person : Name, Address, PhoneNo, Emailif Type = S, then print Student : Name, Address, PhoneNo, Email, Statusif Type = E, then print Employee : Name, Address, PhoneNo, Email, Office, Salaryif Type = T, then print Staff : Name, Address, PhoneNo, Email, Office, Salary, Titleif Type = F, then print Faculty : Name, Address, PhoneNo, Email, Office, Salary, Office_Hrs, RankNote :The Status for Student is freshman, sophomore, junior, or senior.Overide only toString() method in all the Classes.

Select the correct answerConsider the following Java code:public class Student {    private String name;    private int age;    public Student(String name, int age) {        this.name = name;        this.age = age;    }    // Override the equals() method    @Override    public boolean equals(Object obj) {        // TODO: Implement the equals() method    }    public static void main(String[] args) {        Student student1 = new Student("Alice", 20);        Student student2 = new Student("Bob", 22);        Student student3 = new Student("Alice", 20);        System.out.println(student1.equals(student2));         System.out.println(student1.equals(student3));    }}What should be implemented inside the equals() method to correctly compare two Student objects based on their name and age?A. return this.name.equals(obj.name) && this.age == obj.age;B. return this.name.equals(((Student) obj).name) && this.age == ((Student) obj).age;C. return super.equals(obj);D. return this == obj;OptionsCA and BOnly AD

Select the correct answerConsider the following Java class: public class MyClass {    private String name;    private int age;    public MyClass(String name, int age) {        this.name = name;        this.age = age;    }    // Overriding the toString() method    @Override    public String toString() {        return "MyClass{" +                "name='" + name + '\'' +                ", age=" + age +                '}';    }    public static void main(String[] args) {        MyClass obj = new MyClass("John", 25);         System.out.println(obj);    } }What is the purpose of overriding the toString() method in this class?A. To convert the object to a JSON representation.B. To provide a string representation of the object for easy debugging and logging.C. To make the object eligible for garbage collection.D. To create a new instance of the class.OptionsA and BOnly BOnly AB and C

1/3

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.