In: Computer Science
Assignment 8 – UML Diagram - Students Class Revised 9/2019
Throughout Chapters 9 & 10, there are UML class diagrams (Unified Modeling Language) which represent the data fields and methods in a class. From any class given to you, you should be able to design the UML diagram. Likewise, if given a UML, you should be able to define & implement the class.
Please do this in Java please, add comments so it's easy to understand and read so I know how to be able to do it myself.
For the assignment this week, you are given the UML diagram for a class named Students. You will define the Students class and then implement it with a driver program called StudentsDriver.
The StudentsDriver class with the main method should:
• Create an object of the Students class using the no-arg constructor
• get user input for student objects (see sample output below): first name, last name, student id, and birth year and then populate the object using setters (do not use constructors)
• create & initialize 2 objects of the Students class using the overloaded constructor as follows
o first name = Lisa Ann; last name = Larraby; student id = 54321; birth year = 1993 o first name = Karl; last name = Van der Hutten; student id = 54123; birth year = 1983
• call the class toString to display the output for each object as noted in sample output
Validation should be created in the Students class (not driver program):
• Range for student id is 54000 through 54999, inclusive
• Range for student age is from 13 years old to 100 years old, inclusive
• HINTS: in setters for studentID & for birthYear, move the assignment to the appropriate validation method. Then, in each setter, call the appropriate validation method.
*Note: for those already familiar with Java programming, please keep this simple: try/catch, exceptions, GUI, etc. Please use only the concepts discussed up to this point.
Before starting this lab, be sure to refer to the “How to Submit Assignments” document in eLearn for proper documentation, indention, naming conventions, etc. Points may be deducted if submitted incorrectly.
REMINDER: Any sources you use besides the textbook, class demo files, or the instructor must be documented in your program with details such as the scope of help received & URL.
Required project name: LastnameFirstname08 Required package name: chap10 Required class names: as noted above
Sample output. You’re encouraged to use valid, invalid, & boundary test cases. User input in green text:
Enter First Name: Juan Mateo Enter Last Name: Lopez
Enter Student ID: 53500 ID range is 54000 to 54999. Please try again: 53600 ID range is 54000 to 54999. Please try again: 53700 ID range is 54000 to 54999. Please try again: 55000 ID range is 54000 to 54999. Please try again: 56000 ID range is 54000 to 54999. Please try again: 54000
Enter 4-Digit Year of Birth: 1900 Range is 1919 to 2006. Please try again: 1910 Range is 1919 to 2006. Please try again: 1915 Range is 1919 to 2006. Please try again: 2010 Range is 1919 to 2006. Please try again: 2015 Range is 1919 to 2006. Please try again: 1960
Juan Mateo Lopez
Student ID: 54000 Age in 2019: 59 years old
Lisa Ann Larraby
Student ID: 54321 Age in 2019: 26 years old
Karl Van der Hutten Student ID: 54123 Age in 2019: 36 years old
Students
- firstName: String - lastName: String - studentID: int - birthYear: int - CURRENT_YEAR: int - YOUNGEST_AGE:int - OLDEST_AGE:int - LOW_ID:int - HIGH_ID:int
+ Students() + Students(first: String, last: String, studentID: int, birthYear: int)
+ getFirstName(): String + setFirstName (firstName: String): void + getLastName(): String + setLastName (lastName: String): void + getStudentID(): int + setStudentID (studentID: int): void + getBirthYear(): int + setBirthYear(birthYear: int): void + getCurrentYear(): int + getYoungestAge(): int + getOldestAge(): int + getLowId(): int + getHighId(): int
+ validateID(int id): int + validateYr(int yr): int + calcAge(): int + toString(): String <>
You will be graded according to the following rubric (each item is worth one point):
• The Students class is defined exactly & completely as indicated in the UML class diagram.
• Values for constants are used only in initialization and are not hard coded in the methods of the Students class.
• One object is initialized with user input using the Students class setters.
• Two objects are initialized using the Students class overloaded constructor.
• Validation is executed in 2 methods in the Students class.
• In all methods of the Students class, there are no hard coded values.
• Your program compiles & runs.
• You follow standard coding conventions (e.g. variable names, indentation, comments, etc.).
• File name, global comment, citations, etc. as indicated on How to Submit Assignments (Course Overview).
• Your Eclipse project was exported correctly (see Hello World Video Tutorial in Topic 1 Learning Activities).
//Java code
package chap10;
import java.util.Scanner;
public class Students {
private String firstName;
private String lastName;
private int studentID;
private int birthYear;
private final int CURRENT_YEAR=2019;
private final int YOUNGEST_AGE =13;
private final int OLDEST_AGE = 100;
private final int LOW_ID = 54000;
private final int HIGH_ID = 54999;
//No arg Constructor
public Students()
{
firstName="";
lastName ="";
studentID =0;
birthYear = 0;
}
//Constructor with args
public Students(String firstName, String lastName, int
studentID, int birthYear) {
setFirstName(firstName);
setLastName(lastName);
setBirthYear(birthYear);
setStudentID(studentID);
}
//getters and setters
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getStudentID() {
return studentID;
}
public void setStudentID(int studentID) {
Scanner input = new Scanner(System.in);
while (validateID(studentID)==-1)
{
System.out.print("ID range is "+getLowId()+" to "+getHighId()+".
Please try again: ");
studentID = input.nextInt();
}
this.studentID = studentID;
}
public int getBirthYear() {
return birthYear;
}
public void setBirthYear(int birthYear) {
Scanner input = new Scanner(System.in);
while (validateYr(birthYear)==-1)
{
System.out.print("Range is 1919 to 2006. Please try again:");
birthYear = input.nextInt();
}
this.birthYear = birthYear;
}
public int getCurrentYear()
{
return CURRENT_YEAR;
}
public int getYoungestAge()
{
return YOUNGEST_AGE;
}
public int getOldestAge()
{
return OLDEST_AGE;
}
public int getLowId()
{
return LOW_ID;
}
public int getHighId()
{
return HIGH_ID;
}
public int validateID(int id)
{
if(id >= LOW_ID && id<= HIGH_ID)
return id;
else
return -1;
}
public int validateYr(int yr)
{
birthYear = yr;
int age = calcAge();
if(age >= YOUNGEST_AGE && age<=OLDEST_AGE)
{
return yr;
}
else {
birthYear =0;
return -1;
}
}
public int calcAge()
{
return getCurrentYear()-birthYear;
}
@Override
public String toString() {
return firstName+" "+lastName+"\n Student ID: "+studentID+" Age in
"+getCurrentYear()+": "+calcAge()
+" years old";
}
}
//=================================================
package chap10;
import java.util.Scanner;
public class StudentsDriver {
public static void main(String[] args)
{
String firstName,lastname;
int birthYear,id;
Scanner input = new Scanner(System.in);
/**
* Create an object of the Students class using the no-arg
constructor
*/
Students student1 = new Students();
System.out.print("Enter First Name: ");
firstName = input.nextLine();
student1.setFirstName(firstName);
System.out.print("Enter Last Name: ");
lastname = input.nextLine();
student1.setLastName(lastname);
System.out.print("Enter Student ID: ");
id = input.nextInt();
student1.setStudentID(id);
System.out.print("Enter 4-Digit Year of Birth:: ");
birthYear = input.nextInt();
student1.setBirthYear(birthYear);
/**
* create & initialize 2 objects of the Students class
using
* the overloaded constructor
*/
Students student2 = new Students("Lisa Ann",
"Larraby",54321,1993);
Students student3 = new Students("Karl","Van der
Hutten",54123,1983);
//Display all three students
System.out.println(student1);
System.out.println(student2);
System.out.println(student3);
}
}
//Output
//
If you need any help regarding this solution... please leave a
comment ...... Thanks