In: Computer Science
This question concerns writing a Student class to model a
student's name, composed
of a first name, middle name and last name. The Student 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. 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. |
2
Thinking of the problem in terms of testing, there are three
requirements that must be
met:
1. The setNames() method sets the names stored in the object i.e.
the values of
firstName, middleName, lastName.
2. The getFullName() method obtains the name of the student with
the middle name
converted to an initial only.
3. The getFullName() method does not change anything – i.e. it does
not modify the
values of firstName, middleName, lastName
HINT: To start your Student class, we suggest using the following
instance variables:
Instance variables
private String firstName;
private String middleName;
private String lastName;
In order to test the requirement above create a java class called
StudentTest that allows
the user to enter the first name, middle name and last name. These
values must be set
using the created set meothod and afterwards displays the
information back to the
console, however the Middle name is an initial ONLY
// The code for the above problem is given below with the screenshots for indentation
// and output and if you feel any problem then feel free to ask
// importing Scanner for taking the input from the user
import java.util.Scanner;
// defining the class Student according to the requirements
class Student
{
// declaring the private data members for first, middle and last names
private String firstName;
private String middleName;
private String lastName;
// Default constructor to initialize all the data members with empty strings
public Student()
{
firstName = "";
middleName = "";
lastName = "";
}
// setNames function to set the value of data members according to the entered data
public void setNames(String first, String middle, String last)
{
firstName = first;
middleName = middle;
lastName = last;
}
// getFullName function to return the full name of the student with middle converted to initial only
// without changing any of the value of actual data members
public String getFullName()
{
String fullName;
fullName = firstName +" "+Character.toUpperCase(middleName.charAt(0))+". "+lastName;
return fullName;
}
}
// Defining a class StudentTest to test the working of Student class
class StudentTest
{
public static void main(String[] args)
{
// creating an object of Scanner for input from the user
Scanner Sc = new Scanner(System.in);
// creating an object of Student for entering its details
Student S1 = new Student();
// declaring strings to be entered by the user
String firstName,middleName,lastName;
// taking input for the first, middle and last name from the user
System.out.print("Enter the first name: ");
firstName = Sc.nextLine();
System.out.print("Enter the middle name: ");
middleName = Sc.nextLine();
System.out.print("Enter the last name: ");
lastName = Sc.nextLine();
// calling the setNames function to set the name for object S1
S1.setNames(firstName, middleName, lastName);
// Printing the full name of Object S1 with middle name converted to initial only
System.out.println("The full name of the student with middle converted to initial only: "+S1.getFullName());
// closing the object of scanner to prevent memory leaks
Sc.close();
}
}
OUTPUT:-