Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please use Java to finish this program, thanks! Exercise 3: Suppose that you wan

ID: 3869868 • Letter: P

Question

Please use Java to finish this program, thanks!

Exercise 3: Suppose that you want to store student records in the generic stack that you created in Exercise 2. For this, first define a StudentRecord class that holds three attributes: String firstName, String lastName, int bannerID. Add the appropriate constructor, get, set and toString methods. Next, write a client (demo) program that does the following 1. Create an empty stack, stackl, that can hold student records 2. Read a file that contains student records, one on each line, such as Ichabod Crane 123456 Brom Bones 456321 Emboar Pokemon 111222 Rayquazza Pokemon 333111 Cool Dude 101010 Trend Chaser 654321 Chuck Norris 112233 Drum Dude 111222 You may assume that each line has three components only: first name, last name and Id number. You would read the ID number as a String and then convert it to an Integer object A StringTokenizer program is useful to read a file in which the records are arranged as shown above and split them into first name, last name and ID number 3. As each line in the file is read, create a StudentRecord object and push it into stackl 4. Repeat until all the lines in the file are read 5. Create another stack2 that can hold string objects 6. Pop stackl item by item and push only the last name in each student record into stack2 7. Pop stack2 and display the items. With the example set of records given above, this would display the last names in the reverse order: Dude Norris Chaser

Explanation / Answer

/****************************StudentRecord.java************************************/

/**

* The Class StudentRecord.

*/

public class StudentRecord {

/** The first name. */

private String firstName;

/** The last name. */

private String lastName;

/** The banner ID. */

private int bannerID;

/**

* Gets the first name.

*

* @return the firstName

*/

public String getFirstName() {

return firstName;

}

/**

* Sets the first name.

*

* @param firstName the firstName to set

*/

public void setFirstName(String firstName) {

this.firstName = firstName;

}

/**

* Gets the last name.

*

* @return the lastName

*/

public String getLastName() {

return lastName;

}

/**

* Sets the last name.

*

* @param lastName the lastName to set

*/

public void setLastName(String lastName) {

this.lastName = lastName;

}

/**

* Gets the banner ID.

*

* @return the bannerID

*/

public int getBannerID() {

return bannerID;

}

/**

* Sets the banner ID.

*

* @param bannerID the bannerID to set

*/

public void setBannerID(int bannerID) {

this.bannerID = bannerID;

}

@Override

public String toString() {

return "StudentRecord [firstName=" + firstName + ", lastName=" + lastName + ", bannerID=" + bannerID + "]";

}

}

/************************************Excerise.java***************************/

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Iterator;

import java.util.Scanner;

import java.util.Stack;

import java.util.StringTokenizer;

/**

* The Class Excerise3.

*/

public class Excerise3 {

/**

* Prints the.

*

* @param stack

* the stack

*/

public static void print(Stack<?> stack) {

while (!stack.isEmpty()) {

System.out.println(stack.pop());

}

}

/**

* The main method.

*

* @param args

* the arguments

*/

public static void main(String[] args) {

// Instance of stack that hold the StudentRecord Object

Stack<StudentRecord> stack1 = new Stack<>();

Scanner keybord = new Scanner(System.in);

System.out.println("Enter the file name to read: ");

String filename = keybord.nextLine();

File file = new File(filename);

StringTokenizer token;

try {

Scanner inputFile = new Scanner(file);

while (inputFile.hasNext()) {

String line = inputFile.nextLine();

token = new StringTokenizer(line, " ");

String f = token.nextToken();

String l = token.nextToken();

String bString = token.nextToken();

// Converting String to Integer Object

Integer bannerId = Integer.parseInt(bString);

// Creating StudentRerord object

StudentRecord record = new StudentRecord();

record.setFirstName(f);

record.setLastName(l);

record.setBannerID(bannerId);

// Adding StudentRecord into stack

stack1.push(record);

}

// Creating object of stack2

Stack<String> stack2 = new Stack<>();

// Iterating stack1

Iterator<StudentRecord> iterator = stack1.iterator();

while (iterator.hasNext()) {

StudentRecord record = iterator.next();

// Adding last name to stack2

stack2.push(record.getLastName());

}

System.out.println("Printing last name in reverse Order");

// calling print method

print(stack2);

inputFile.close();

keybord.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

}

/*******************************output*************************************/

Enter the file name to read:
test.txt
Printing last name in reverse Order
Dude
Norris
Chaser
Dude
Pokemon
Pokemon
Bones
Crane

/****************************test.txt******************************/

Ichabod Crane 123456
Brom Bones 456321
Emboar Pokemon 111222
Rayquazza Pokemon 333111
Cool Dude 101010
Trend Chaser 654321
Chuck Norris 112233
Drum Dude 111222

Thanks a lot.