This IS JAVA and JGRASP is used!! Write a Student class to model a student\'s na
ID: 666641 • Letter: T
Question
This IS JAVA and JGRASP is used!! Write a Student class to model a student's name, composed of a first name, middle name and last name. The class should meet the following specification:
Class Student
A Student object represents a student. A student has a first name, middle name and last name.
Instance variables
private String firstName;
private String middleName;
private String lastName;
Methods
public void setNames(String first, String middle, String last)
// Set the first, middle and last names of this Student object.
public String getFullName()
// Obtain the full name of this Student with the middle name converted to an initial only.
Write a driver class called ‘TestStudent’ containing a main method. The main method should
1. Ask the user to input a student’s first, middle and last names,
2. Create an instance of the Student class.
3. Call setNames using the data input by the user.
4. Call the method to compute the full name and output it.
Sample I/O
Enter first name:
Tito
Enter middle name:
GimmeYourMoney
Enter last name:
Mboweni
The full name is: Tito G. Mboweni
Explanation / Answer
The java code is:-
import java.util.Scanner;
class Student {
private String firstName;
private String middleName;
private String lastName;
public void setNames(String first, String middle, String last){
firstName=first;
middleName=middle;
lastName=last;
}
public String getFullName(){
return firstName+" "+middleName.charAt(0)+". "+lastName;
}
}
public class TestStudent{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String firstName,middleName,lastName;
System.out.println("Enter first name: ");
firstName=sc.next();
System.out.println("Enter middle name: ");
middleName=sc.next();
System.out.println("Enter last name: ");
lastName=sc.next();
Student s=new Student();
s.setNames(firstName, middleName, lastName);
System.out.println("The full name is: "+s.getFullName());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.