USING JAVA
Assume that you are the in-charge of maintaining student profile for the department of ILT.
You want to obtain the following details from the students.
1. First Name
2. Last Name
3. PeopleSoft Id (Don’t use original)
4. Class standing
5. Email id
6. Address
a. Address line 1
b. City
c. State
d. Zipcode When obtaining the above details, you need to validate each data as follows.
i. First name and last name should not contain any character other than alphabets [az/A-Z]
ii. PeopleSoft id should contain only digits and its length should be <=7.
v. The address line 1 should contain a house number followed by a space and a street name
vi. City name should consist of only alphabets vii. State should be any one of the states in US. AK, AL, AR, AZ, CA, CO, CT, DC, DE, FL, GA, HI, IA, ID, IL, IN, KS, KY, LA, MA, MD, ME, MI, MN, MO, MS, MT, NC, ND, NE, NH, NJ, NM, NV, NY, OH, OK, OR, PA, RI, SC, SD, TN, TX, UT, VA, VT, WA, WI, WV, WY
viii. Zip code should be of length 9 and contain only numbers Your code should perform the following:
A. First, the application should ask how many students’ data (“N”) are to be entered.
B. Then initialize an array of objects to that number
C. Define a Class named Student with the student details from 1. – 6. As fields.
D. The Student Class should have a method “getInfo” to obtain the student details. When reading each input, you should validate the data as described from i. – viii.
E. The Student class should have a method “displayInfo” to display the student details. Each student data needs to be printed in a separated line with each data separated by a space (use “\t”).
F. The Student class should have a static member “count” to count the number of students.
G. While printing, the display should have a header line as “Students Details, Department of ILT”.
H. At the bottom of the display, it should print “Number of students: count”, where count should be equal to N.
//completed.. Thanks
//------------ StudentTest.java
//package project123;
import java.util.Scanner;
class Student {
static int count;
String FirstName;
String LastName;
String PeopleSoftId;
String ClassStanding;
String EmailId;
String Address;
public Student() {
Address=""; // address is initially blank
count++; // increase count by 1 when new object is created
}
public void setFirstName() {
Scanner sc = new Scanner(System.in); // to get input from
user
loop1: while(true) { // loop1: is label for while loop
System.out.print("please enter First Name: ");
FirstName = sc.nextLine();
// check Firstname, and repeat if invalid
for (int i = 0; i < FirstName.length(); i++) {
char ch = FirstName.charAt(i); // get each character from
name
if (Character.isLetter(ch) == false) { // if ch is not letter
System.out.println("*** Invalid FirstName, please try
again...");
continue loop1; // continue while loop1 to try again
}
}
break; // if name is valid then break loop and move ahead
}
}
public void setLastName() {
Scanner sc = new Scanner(System.in); // to get input from
user
loop1: while(true) { // loop1: is label for while loop
System.out.print("please enter Last Name: ");
LastName = sc.nextLine();
// check lastname, and repeat if invalid
for (int i = 0; i < LastName.length(); i++) {
char ch = LastName.charAt(i); // get each character from name
if (Character.isLetter(ch) == false) { // if ch is not letter
System.out.println("*** Invalid LastName, please try
again...");
continue loop1; // continue while loop to try again
}
}
break; // if name is valid then break loop1
}
}
public void setPeopleSoftId() {
Scanner sc = new Scanner(System.in); // to get input from
user
loop1: while(true) { // loop1: is label for while loop
System.out.print("please enter PeopleSoftId: ");
PeopleSoftId = sc.nextLine();
if (PeopleSoftId.length()>7) {
System.out.println("*** max 7 digits are allowed, please try
again...");
continue; // continue while loop to try again
}
// check PeopleSoftId, and repeat if invalid
for (int i = 0; i < PeopleSoftId.length(); i++) {
char ch = PeopleSoftId.charAt(i); // get each character from
name
if (Character.isDigit(ch) == false) { // if ch is not digit
System.out.println("*** only digits are allowed, please try
again...");
continue loop1; // continue while loop to try again
}
}
break; // if name is valid then break loop1
}
}
public String setCity() {
String city;
Scanner sc = new Scanner(System.in); // to get input from
user
loop1: while(true) { // loop1: is label for while loop
System.out.print("please enter city: ");
city = sc.nextLine();
// check city, and repeat if invalid
for (int i = 0; i < city.length(); i++) {
char ch = city.charAt(i); // get each character from name
if (Character.isLetter(ch) == false && ch!=' ') { // if ch
is not letter
System.out.println("*** Invalid city, please try again...");
continue loop1; // continue while loop to try again
}
}
break; // if name is valid then break loop1
}
return city;
}
public String setZipcode() {
String zipcode;
Scanner sc = new Scanner(System.in); // to get input from
user
loop1: while(true) { // loop1: is label for while loop
System.out.print("please enter zipcode: ");
zipcode = sc.nextLine();
if (zipcode.length()!=9) {
System.out.println("*** zipcode must be 9 digits, please try
again...");
continue; // continue while loop to try again
}
// check PeopleSoftId, and repeat if invalid
for (int i = 0; i < zipcode.length(); i++) {
char ch = zipcode.charAt(i); // get each character from zip
if (Character.isDigit(ch) == false) { // if ch is not digit
System.out.println("*** only digits are allowed, please try
again...");
continue loop1; // continue while loop to try again
}
}
break; // if name is valid then break loop1
}
return zipcode;
}
public String setState() {
String State;
String[] StateNames =
{"AK","AL","AR","AZ","CA","CO","CT","DC","DE","FL","GA","HI","IA","ID","IL","IN","KS","KY","LA","MA","MD","ME","MI","MN","MO","MS","MT","NC","ND","NE","NH","NJ","NM","NV","NY",
"OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VA","VT","WA","WI","WV","WY"
};
Scanner sc = new Scanner(System.in); // to get input from
user
loop1: while(true) { // loop1: is label for while loop
System.out.print("please enter State: ");
State = sc.nextLine().toUpperCase(); // make entered state
uppercase
// check if state is valid
boolean flag = false;
for (int i = 0; i < StateNames.length; i++) {
if (State.equals(StateNames[i])) {
flag = true; // if state is found in stateNames then set true
}
}
if (flag==false) { // if flag is still false, it means state is not
found
for (int i = 0; i < StateNames.length; i++) {
System.out.print( StateNames[i] +" ");
}
System.out.println("\n*** Invalid State, must be one of the above,
please try again...");
continue; // continue while loop to try again
}
break; // if state is valid then break loop1
}
return State;
}
public void setAddress() {
Scanner sc = new Scanner(System.in); // to get input from
user
String line1, city, state, zip;
while(true) { // loop1: is label for while loop
System.out.println("please enter Address line1 (house number
");
System.out.print("followed by a space and a street name) :
");
line1 = sc.nextLine();
if (line1.split(" ").length<2) { // if line1 has less than 2
words (houseNumber streetName)
//here line1 is split by space, it gives words array, then that
array's length is checked
System.out.println("*** Invalid address line, please try
again...");
continue; // continue while loop to try again
}
Address += line1 + ", "; // append to line1 address
break; // exit loop and move ahead
}
Address += setCity() + ", "; // append city to address
Address += setState() + ", "; // append state to address
Address += setZipcode(); // append zipcode to address
}
public void getInfo() {
setFirstName();
setLastName();
setPeopleSoftId();
Scanner sc = new Scanner(System.in); // to get input from
user
System.out.print("please enter ClassStanding: ");
ClassStanding = sc.nextLine();
System.out.print("please enter EmailId: ");
EmailId = sc.nextLine();
setAddress();
}
public void displayInfo() {
System.out.println(FirstName + " " + LastName +",\t"+PeopleSoftId
+",\t"+ ClassStanding +",\t"+ EmailId +",\t"+ Address);
}
}
public class StudentTest {
public static void main(String[] args) {
int N;
Scanner sc = new Scanner(System.in); // to get input from
user
System.out.print("Enter students count: ");
N = sc.nextInt();
Student[] students = new Student[N];
for (int i = 0; i < N; i++) {
students[i] = new Student();
System.out.println("-------- enter details for Student: "+ (i+1) +
" of " + N);
students[i].getInfo();
}
System.out.println("\nStudents Details, Department of ILT");
System.out.println("-----------------------------------");
for (int i = 0; i < N; i++) {
students[i].displayInfo();
}
System.out.println("-----------------------------------");
System.out.println("Number of students: "+ Student.count);
}
}
//------------ end of StudentTest.java
Get Answers For Free
Most questions answered within 1 hours.