This fourth assignment will allow you to explore four different concepts in one
ID: 3805245 • Letter: T
Question
This fourth assignment will allow you to explore four different concepts in one program. You will be creating a program that makes use of: an abstract data type (ADT file input/output (IAO), classes & objects, and a UML diagram in an object-oriented (oo) programming language. For this assignment, you are tasked with the responsibility of reading in a list of students from a text file, creating a Student object for each student, pushing the Student object onto a Stack, and then providing an option to print the contents of the Stack by popping the students off the Stack one-by-one You should print each Student on a separate line in the console Sample output). This program will be written in Java and must compile and run on Tesla (tesla.cs.iupui.edu). Your program will be menu driven in which you will provide the following prompts to the user: l. Load Students (From File) 2. Print Stack 3. Exit Program We will assume the each student has the following data available: First name Last name Address o Address line 1 o Address line 2 City o o State o Zip Code Student ID GPA The text file containing sample student data will be provided to you. The filename will be students.txt this file can be found (and downloaded) on Canvas. The file will contain ten (10) students. The format of the file will be as follows: First Name, LastName, streetAddress, Address2, City, State, ZipCode, ID, GPAExplanation / Answer
CODE:
package temp;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Stack;
// Student class
class Student{
// member variables declared
private String firstName;
private String lastName;
private String streetAddress;
private String address2;
private String city;
private String state;
private String zipcode;
private String id;
private String gpa;
// constructor
public Student(String firstName, String lastName, String streetAddress, String address2, String city, String state,
String zipcode, String id, String gpa) {
this.firstName = firstName;
this.lastName = lastName;
this.streetAddress = streetAddress;
this.address2 = address2;
this.city = city;
this.state = state;
this.zipcode = zipcode;
this.id = id;
this.gpa = gpa;
}
// getter methods
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getStreetAddress() {
return streetAddress;
}
public String getAddress2() {
return address2;
}
public String getCity() {
return city;
}
public String getState() {
return state;
}
public String getZipcode() {
return zipcode;
}
public String getId() {
return id;
}
public String getGpa() {
return gpa;
}
}
public class Assignment {
public static void main(String[] args) throws FileNotFoundException{
int choice = 0;
Scanner sc = new Scanner(System.in);
Assignment as = new Assignment();
boolean flg = true;
Stack<Student> st = new Stack<Student>();
while(flg){
System.out.println("Enter the menu items: ");
System.out.println("1. Load Students from the file");
System.out.println("2. Print Stack");
System.out.println("3. Exit");
choice = sc.nextInt();
switch(choice){
case 1: as.loadFromFile(st);
break;
case 2: as.printStack(st);
break;
case 3: flg = false;
break;
default: System.out.println("Enter the correct choice");
}
}
sc.close();
}
private void loadFromFile(Stack<Student> st) throws FileNotFoundException {
//
// please give the complete path to the students.txt file here.
//
File fp = new File("C:\Users\Admin\Desktop\students.txt");
Scanner sc = new Scanner(fp);
while(sc.hasNextLine()){
String line = sc.nextLine();
String[] linedetails = line.split(",");
String fn = linedetails[0];
String ln = linedetails[1];
String srt = linedetails[2];
String ad2 = linedetails[3];
String cty = linedetails[4];
String state = linedetails[5];
String zip = linedetails[6];
String id = linedetails[7];
String gpa = linedetails[8];
Student studnt = new Student(fn,ln,srt,ad2,cty,state,zip,id,gpa);
st.push(studnt);
}
sc.close();
}
private void printStack(Stack<Student> st) {
System.out.println("Students details are: ");
while(st.size() != 0){
System.out.println("------------");
Student stdnt = (Student) st.pop();
System.out.println(stdnt.getFirstName() + " " + stdnt.getLastName() + " " + stdnt.getStreetAddress()
+ stdnt.getAddress2() + " " + stdnt.getCity() + " " + stdnt.getState() + " " + stdnt.getZipcode() + " "
+ stdnt.getId() + " " + stdnt.getGpa());
}
}
}
students.txt
Florence,Forrest,1843 Glenview Drive, Apt 2, Corpus Christi, Tx,78401,123456,3.215
Casey,Roberta,3668 Thunder Road, ,palo alto, CA, 94306, 456789, 2.978
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.