Question

In: Computer Science

This question concerns writing a Student class to model a student's name, composed of a first...

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

Solutions

Expert Solution

// 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:-


Related Solutions

JAVA StudentId: Consist of the first two characters of the student's first name, student's birth year,...
JAVA StudentId: Consist of the first two characters of the student's first name, student's birth year, and the last two characters of the last name. For instance, if the student full name is John Doe and birthyear is 1995, then the id will be Jo1995oe. Birthday is using GregorianCalendar. String firstname String lastname GregorianCalendar birthday
A Java question. You are given a Student class. A Student has a name and an...
A Java question. You are given a Student class. A Student has a name and an ArrayList of grades (Doubles) as instance variables. Write a class named Classroom which manages Student objects. You will provide the following: 1. public Classroom() a no-argument constructor. 2. public void add(Student s) adds the student to this Classroom (to an ArrayList 3. public String hasAverageGreaterThan(double target) gets the name of the first student in the Classroom who has an average greater than the target...
#promt the user to enter student name for i in range(0, 3): print ("Enter student's first...
#promt the user to enter student name for i in range(0, 3): print ("Enter student's first and last name") #variable for name name = input() #prompt the user to enter number of book purchased print ("Enter the number of book the student purchased this month") #varibale for number of book book_number = input() book_number = int (book_number) #point = int (point) if (book_number <=0): points = 0 elif (book_number <=3): points = 5 elif (book_number <=6): points = 10 elif...
Question 1 (10) Create a class Student with public member variables: Student name, student number, contact...
Question 1 (10) Create a class Student with public member variables: Student name, student number, contact number, ID number. The following specifications are required:  Add init() method of the class that initializes string member variables to empty strings and numeric values to 0. (2)  Add the method populate() to the class. The method is used to assign values to the member variables of the class. (2)  Add the method display() to the class. The method is used...
JAVA PROGRAMMING Part 1 Create a class Student, with attributes id, first name, last name. (All...
JAVA PROGRAMMING Part 1 Create a class Student, with attributes id, first name, last name. (All the attributes must be String) Create a constructor that accepts first name and last name to create a student object. Create appropriate getters and setters Create another class StudentOperationClient, that contains a main program. This is the place where the student objects are created and other activities are performed. In the main program, create student objects, with the following first and last names. Chris...
Create a class called Student which stores • the name of the student • the grade...
Create a class called Student which stores • the name of the student • the grade of the student • Write a main method that asks the user for the name of the input file and the name of the output file. Main should open the input file for reading . It should read in the first and last name of each student into the Student’s name field. It should read the grade into the grade field. • Calculate the...
IN JAVA ECLIPSE PLEASE The xxx_Student class A Student has a: – Name - the name...
IN JAVA ECLIPSE PLEASE The xxx_Student class A Student has a: – Name - the name consists of the First and Last name separated by a space. – Student Id – a whole number automatically assigned in the student class – Student id numbers start at 100. The numbers are assigned using a static variable in the Student class • Include all instance variables • Getters and setters for instance variables • A static variable used to assign the student...
Python question Write a program that asks the user to enter a student's name and 8...
Python question Write a program that asks the user to enter a student's name and 8 numeric tests scores (out of 100 for each test). The name will be a local variable. The program should display a letter grade for each score, and the average test score, along with the student's name. Write the following functions in the program: calc_average - this function should accept 8 test scores as arguments and return the average of the scores per student determine_grade...
In Java, Here is a basic Name class. class Name { private String first; private String...
In Java, Here is a basic Name class. class Name { private String first; private String last; public Name(String first, String last) { this.first = first; this.last = last; } public boolean equals(Name other) { return this.first.equals(other.first) && this.last.equals(other.last); } } Assume we have a program (in another file) that uses this class. As part of the program, we need to write a method with the following header: public static boolean exists(Name[] names, int numNames, Name name) The goal of...
The program reads a text file with student records (first name, last name and grade on...
The program reads a text file with student records (first name, last name and grade on each line) and determines their type (excellent or ok). <--- Completed Need help on this Then it prompts the user to enter a command, executes the command and loops. The commands are the following: "all" - prints all student records (first name, last name, grade, type). "excellent" - prints students with grade > 89. "ok" - prints students with grade <= 89. "end" -...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT