•Write a Student class that has: •name: a String •bannerId: a String •3 construc
ID: 3704074 • Letter: #
Question
•Write a Student class that has:
•name: a String
•bannerId: a String
•3 constructors
•A constructor that name and bannerId
•A constructor that takes the name only
•A constructor that takes no arguments
•Accessor and mutator method for the 2 instance fields
•print(): which prints the name and bannerId a user friendly way.
•Write a main method that displays a menu asking the user if they want to enter the name of the student only or the name and bannerId. Use the appropriate constructor to create the Student and print it back out.
CODE IN JAVA
Explanation / Answer
StudentTest.java
import java.util.Scanner;
public class StudentTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Menu: 1. Enter only name 2. Enter name and banner id Enter you choice:");
int choice = scan.nextInt();
Student s;
scan.nextLine();
if(choice==1) {
System.out.println("Enter the name: ");
String name = scan.nextLine();
s = new Student(name);
} else {
System.out.println("Enter the name: ");
String name = scan.nextLine();
System.out.println("Enter the banner id:");
String bannerid = scan.nextLine();
s = new Student(name,bannerid);
}
s.print();
}
}
Student.java
public class Student {
private String name, bannerId;
public Student() {
}
public Student(String name) {
this.name = name;
}
public Student(String name, String bannerId) {
this.name = name;
this.bannerId = bannerId;
}
public void print() {
System.out.println("Name: "+name+" Banner Id: "+bannerId);
}
}
Output:
Menu:
1. Enter only name
2. Enter name and banner id
Enter you choice:
2
Enter the name:
Suresh Murapaka
Enter the banner id:
Welcome
Name: Suresh Murapaka Banner Id: Welcome
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.