Answer to Question #228941 in Java | JSP | JSF for swastica k g

Question #228941

Write a program to print the names of students by creating a

Student class. If no name is passed while creating an object of

Student class, then the name should be "Unknown", otherwise the

name should be equal to the String value passed while creating

object of Student class.


1
Expert's answer
2021-08-23T18:32:11-0400
public class Student {
    private String name;

    public Student() {
        name = "Unknown";
    }

    public Student(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}


public class Main {
    public static void main(String[] args) {
        Student[] students = {new Student(), new Student("Tom"), new Student("Sam"), new Student()};
        for (Student student : students) {
            System.out.println(student.getName());
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS