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

This fourth assignment will allow you to explore four different concepts in one

ID: 3801711 • 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, GPA

Explanation / Answer

//========================== ArrayStack.java ======================//

class ArrayStack<T>   {
   private T[] stack;   
   private int frontIndex;  
   private int backIndex;  
   private static final int DEFAULT_INITIAL_CAPACITY = 50;
  
   public ArrayStack() {
       this(DEFAULT_INITIAL_CAPACITY);
   }
  
   public ArrayStack(int initialCapacity) {
       @SuppressWarnings("unchecked")
       T[] tempStack = (T[]) new Object[initialCapacity + 1];
       stack = tempStack;
       frontIndex = 0;
       backIndex = initialCapacity;
   }   
  
   public void push(T newEntry) {
       ensureCapacity();
       stack[frontIndex] = newEntry;
       frontIndex++;
   }     

   public T pop() {
       T front = null;
       if (!isEmpty()) {
           front = stack[frontIndex];  
       stack[frontIndex] = null;  
       frontIndex--;
       }
       return front;
   }
  
   public T peek() {
       T front = null;
       if (!isEmpty()) {
           front = stack[frontIndex];
       }
       return front;
   }

   public T get(int i) {
       T t = null;
       if (!isEmpty()) {
           t = stack[i];
       }
       return t;
   }
   private void ensureCapacity() {
       if (frontIndex == stack.length) {
           T[] oldStack = stack;
           int oldSize = oldStack.length;  
           @SuppressWarnings("unchecked")
           T[] tempStack = (T[]) new Object[2 * oldSize];
           stack = tempStack;  
           for (int index = 0; index < oldSize - 1; index++) {
               stack[index] = oldStack[index];
           }    
       }
    }

   public boolean isEmpty() {
       return frontIndex == ((backIndex + 1) % stack.length);
   }
  
   public void clear() {
       if(!isEmpty()) {
           for (int index = frontIndex; index != backIndex; index = (index+1)%stack.length)
               stack[index] = null;
           stack[backIndex] = null;
       }
       frontIndex = 0;
       backIndex = stack.length - 1;
   }
  
   public int size(){
       return frontIndex;
   }
   public String toString(){
       return "";
   }
}

//========================== Address.java ======================//

public class Address {
   private String line1;
   private String line2;
   private String city;
   private String state;
   private String zipCode;
   public Address() {
      
   }
   public Address(String l1,String l2, String c,String s, String z) {
       line1 =l1;
       line2 =l2;
       city = c;
       state =s;
       zipCode =z;
   }
   public String getLine1() {
       return line1;
   }
   public void setLine1(String line1) {
       this.line1 = line1;
   }
   public String getLine2() {
       return line2;
   }
   public void setLine2(String line2) {
       this.line2 = line2;
   }
   public String getCity() {
       return city;
   }
   public void setCity(String city) {
       this.city = city;
   }
   public String getState() {
       return state;
   }
   public void setState(String state) {
       this.state = state;
   }
   public String getZipCode() {
       return zipCode;
   }
   public void setZipCode(String zipCode) {
       this.zipCode = zipCode;
   }
}

//========================== Student.java ======================//

public class Student {
   private String fistName;
   private String lastName;
   private Address address;
   private int id;
   private double gpa;
  
   public Student(){}
   public Student(String f,String l,Address add,int i,double g){
       fistName = f;
       lastName = l;
       address = add;
       id = i;
       gpa =g;
   }
   public String getFistName() {
       return fistName;
   }
   public void setFistName(String fistName) {
       this.fistName = fistName;
   }
   public String getLastName() {
       return lastName;
   }
   public void setLastName(String lastName) {
       this.lastName = lastName;
   }
   public Address getAddress() {
       return address;
   }
   public void setAddress(Address address) {
       this.address = address;
   }
   public int getId() {
       return id;
   }
   public void setId(int id) {
       this.id = id;
   }
   public double getGpa() {
       return gpa;
   }
   public void setGpa(double gpa) {
       this.gpa = gpa;
   }
}

//========================== StudentDriver.java ======================//

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class StudentDriver {
  
   ArrayStack<Student> stuList = new ArrayStack<Student>();
   public static void main(String[] args) {
       StudentDriver driver = new StudentDriver();
       driver.readInput();
       driver.Print();
   }
   void Print(){
       for(int i=0;i<stuList.size();i++){
           Student s = stuList.get(i);
           Address add = s.getAddress();
           System.out.println("ID:"+s.getId()+" Name :"+s.getFistName()+" "+s.getLastName()+" Address : "+add.getLine1()+", "+add.getLine2()+", "+add.getCity()+", "+add.getState()+", "+add.getZipCode()+" GPA : "+s.getGpa()+" ");
       }
   }
   void readInput(){
       Scanner sc = null;
       try{
           sc = new Scanner(new File("students.txt"));
           while(sc.hasNext()){
               String line = sc.nextLine();
               String[] data = line.split(",");
              
               Student s = new Student(data[0], data[1], new Address(data[2], data[3], data[4], data[5],data[6]), Integer.parseInt(data[7]),Double.parseDouble(data[8]));
               stuList.push(s);
           }
       }catch(FileNotFoundException e){
          
       }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote