Create a Java program that simply outputs the text "Hello World" when you run it. Call your main class
"Application". In the same file as your main "Application" class, define a new class called "Person". You
don't need to put anything in the class. Now, right below where you output "Hello World" in your main
method, create an object from that (Person) class. Modify your "Person" class to add a constructor.
Make the constructor output the text "Constructor running!". Add another constructor in addition to
the constructor it's already got. Make this second constructor accept a parameter called name of type
String. Make this second constructor print the name parameter using System.out.println(). Finally,
change your "main" method, create two objects of Person class and call both constructors.
class Person {
  public Person() {
    System.out.println("Constructor running!");
  }
  Â
  public Person(String name) {
    System.out.println(name);
  }
}
public class Main {
  public static void main(String[] args) {
    Â
      System.out.println("Hello World!");
      Person person = new Person();
      Person person2 = new Person("Bob");
  }
}
Comments
Leave a comment