This fourth assignment will allow you to explore four different concepts in one
ID: 3805553 • 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
This is Student class:
package com.studentReadFromTextFile;
public class Student {
String First_Name;
String Last_Name;
Address ad;
int Id;
double GPA;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String first_Name, String last_Name, Address ad, String id,
String gPA) {
super();
First_Name = first_Name;
Last_Name = last_Name;
this.ad = ad;
Id = Integer.parseInt(id);
GPA = Double.parseDouble(gPA);
}
public String getFirst_Name() {
return First_Name;
}
public void setFirst_Name(String first_Name) {
First_Name = first_Name;
}
public String getLast_Name() {
return Last_Name;
}
public void setLast_Name(String last_Name) {
Last_Name = last_Name;
}
public Address getAd() {
return ad;
}
public void setAd(Address ad) {
this.ad = ad;
}
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public double getGPA() {
return GPA;
}
public void setGPA(double gPA) {
GPA = gPA;
}
}
This is Address class:
package com.studentReadFromTextFile;
public class Address {
String Address_line1;
String Address_line2;
String City;
String State;
String Zip_code;
public Address() {
super();
// TODO Auto-generated constructor stub
}
public Address(String address_line1, String address_line2, String city,
String state, String zip_code) {
super();
Address_line1 = address_line1;
Address_line2 = address_line2;
City = city;
State = state;
Zip_code = zip_code;
}
public String getAddress_line1() {
return Address_line1;
}
public void setAddress_line1(String address_line1) {
Address_line1 = address_line1;
}
public String getAddress_line2() {
return Address_line2;
}
public void setAddress_line2(String address_line2) {
Address_line2 = address_line2;
}
public String getCity() {
return City;
}
public void setCity(String city) {
City = city;
}
public String getState() {
return State;
}
public void setState(String state) {
State = state;
}
public String getZip_code() {
return Zip_code;
}
public void setZip_code(String zip_code) {
Zip_code = zip_code;
}
}
This is Stack class:
package com.studentReadFromTextFile;
public class Stack<E> {
public int maxSize;
private Student[] stackArray;
private int top;
public Stack(int s) {
maxSize = s;
stackArray = new Student[maxSize];
top = -1;
}
public void push(Student j) {
stackArray[++top] = j;
}
public Student pop() {
return stackArray[top--];
}
public Student peek() {
return stackArray[top];
}
public boolean isEmpty() {
return (top == -1);
}
public boolean isFull() {
return (top == maxSize - 1);
}
}
Note:Before running driver class please put your student.txt file in src folder
This is Driver class:
package com.studentReadFromTextFile;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class Driver {
static Student student=null;
static Stack st=new Stack(10);;
public static void getMenuOption(){
System.out.println(" All Student Operations ");
System.out.println("Enter 1 for load Student");
System.out.println("Enter 2 for print stack");
System.out.println("Enter 3 to Exit");
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
getMenuOption();
while(true){
System.out.print(" Enter your selection [1,2 & 3]: ");
int choice = in.nextInt();
switch (choice)
{
case 1 :
System.out.print("Input file: ");
//Give here your "student.txt" path where you put
String csvFile = in.next();
String line = "";
String cvsSplitBy = ",";
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
while ((line = br.readLine()) != null) {
String[] str = line.split(cvsSplitBy);
String first_Name=str[0];
String last_Name=str[1];
String address_line1=str[2];
String address_line2=str[3];
String city=str[4];
String state=str[5];
String zip_code=str[6];
Address ad=new Address(address_line1, address_line2, city, state, zip_code);
String id=str[7];
String gPA=str[8];
student=new Student(first_Name, last_Name, ad, id, gPA);
st.push(student);
}
}catch (Exception exception)
{
//System.out.println("Error processing file: "+);
}
break;
case 2 : try{
System.out.println(" Student Details");
System.out.println("===========================");
for(int i=0;i<st.maxSize;i++){
Student s=(Student) st.pop();
System.out.println("ID: "+s.getId()+" Name: "+s.getFirst_Name()+" "+s.getLast_Name()+"Address: "+s.ad.getAddress_line1()+" "+s.ad.getAddress_line2()+" "+s.ad.getCity()+" "+s.ad.getState()+" "+s.ad.Zip_code+" GPA: "+s.getGPA());
}
}catch(Exception ee){
System.out.println("Stack is Empty");
}
break;
case 3 :
System.out.println("Bye....THANK YOU");
System.exit(0);
break;
default :
System.out.println("Wrong Entry ");
break;
}
}
}
Output:
All Student Operations
Enter 1 for load Student
Enter 2 for print stack
Enter 3 to Exit
Enter your selection [1,2 & 3]: 1
Input file: student.txt
Error processing file:
Enter your selection [1,2 & 3]: 2
Student Details
===========================
ID: 25142 Name: Raaaa MachoAddress: ggg hhh cd MH 4444 GPA: 6.55
ID: 12555 Name: Sthds kjdsaAddress: eee fff ab MH 4444 GPA: 3.95
ID: 22211 Name: Ralph MaccaoAddress: CCC DDD cd MH 4444 GPA: 9.55
ID: 12345 Name: Stan StanlyAddress: AAA BBB ab MH 4444 GPA: 10.95
Stack is Empty
Enter your selection [1,2 & 3]: 3
Bye....THANK YOU
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.