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

In Java This fourth assignment will allow you to explore four different concepts

ID: 3801764 • Letter: I

Question

In Java

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

import java.io.File;

import java.io.PrintStream;
import java.util.Scanner;

class Stack <Element>
{
private Element container[]; // array of elements
private int top;
private final static int DEFAULT_SIZE = 10; // default size of 10

public Stack ()
{
this(DEFAULT_SIZE);
}
// constructor with custom size
public Stack (int initSize)
{
container = (Element[]) new Object [initSize];
top = -1;
}

public Element getTop()
{
if (top == -1)
return null;
return container[top];
}

// check if its empty or not
public boolean isEmpty()
{
return (top == -1);
}

//pop element
public Element pop()
{
if (top == -1)
return null;
return container[top--];
}

//push element
public void push(Element element)
{
container[++top] = element;
}

//return size
public int size()
{
return (top + 1);
}
}

class Student{
  
   private String firstName;
   private String lastName;
   private Address address;
   private String studentId;
   private String GPA;
   /**
   * @return the firstName
   */
   public String getFirstName() {
       return firstName;
   }
   /**
   * @param firstName the firstName to set
   */
   public void setFirstName(String firstName) {
       this.firstName = firstName;
   }
   /**
   * @return the lastName
   */
   public String getLastName() {
       return lastName;
   }
   /**
   * @param lastName the lastName to set
   */
   public void setLastName(String lastName) {
       this.lastName = lastName;
   }
   /**
   * @return the address
   */
   public Address getAddress() {
       return address;
   }
   /**
   * @param address the address to set
   */
   public void setAddress(Address address) {
       this.address = address;
   }
   /**
   * @return the studentId
   */
   public String getStudentId() {
       return studentId;
   }
   /**
   * @param studentId the studentId to set
   */
   public void setStudentId(String studentId) {
       this.studentId = studentId;
   }
   /**
   * @return the gPA
   */
   public String getGPA() {
       return GPA;
   }
   /**
   * @param gPA the gPA to set
   */
   public void setGPA(String gPA) {
       GPA = gPA;
   }
  
   @Override
   public String toString() {
       String info="";
       info+="ID : "+studentId;
       info+=" Name : "+firstName+" "+lastName;
       info+=" Address : ";  
       if(address.getAddress2()!=null && address.getAddress2().length()>0){
           info+=" "+address.getAddress2();
       }
       if(address.getAddress1()!=null && address.getAddress1().length()>0){
           info+=" "+address.getAddress1();
       }
      
      
       info+=" "+address.getCity();
       info+=" ,"+address.getState();
       info+=" "+address.getZipCode();
       info+=" GPA : "+GPA;
      
       return info;
   }
}


class Address{
   private String address1;
   private String address2;
   private String city;
   private String state;
   private String zipCode;
   /**
   * @return the address1
   */
   public String getAddress1() {
       return address1;
   }
   /**
   * @param address1 the address1 to set
   */
   public void setAddress1(String address1) {
       this.address1 = address1;
   }
   /**
   * @return the address2
   */
   public String getAddress2() {
       return address2;
   }
   /**
   * @param address2 the address2 to set
   */
   public void setAddress2(String address2) {
       this.address2 = address2;
   }
   /**
   * @return the city
   */
   public String getCity() {
       return city;
   }
   /**
   * @param city the city to set
   */
   public void setCity(String city) {
       this.city = city;
   }
   /**
   * @return the state
   */
   public String getState() {
       return state;
   }
   /**
   * @param state the state to set
   */
   public void setState(String state) {
       this.state = state;
   }
   /**
   * @return the zipCode
   */
   public String getZipCode() {
       return zipCode;
   }
   /**
   * @param zipCode the zipCode to set
   */
   public void setZipCode(String zipCode) {
       this.zipCode = zipCode;
   }
  
  
}

public class Driver {

   public static void main(String[] args) {
       Scanner sc=new Scanner(System.in);
       String choice="";
       Stack stack=new Stack();
       do{
           System.out.println("------------------------------------");
           System.out.println("1. Load Students (From File)");
           System.out.println("2. Print Stack");
           System.out.println("3. Exit Program");
           System.out.println("Enter your selection : ");
           choice=sc.next();
           switch (choice) {
           case "1":
               Scanner inputFile=readFile();
               if(inputFile==null){
                   System.err.println("Error in opening input file");
                   break;
               }
              
               while(inputFile.hasNext()){
                   String studenInfo=inputFile.nextLine();
                   String [] studentArray=studenInfo.split(",");// splitting the info on the basis of delimiter ,
                   Student student=new Student();
                   Address address=new Address();
                   int i=0;
                   for(String info:studentArray){
                       if(i==0){
                           student.setFirstName(info);
                       }else if(i==1){
                       student.setLastName(info);  
                       }else if(i==2){
                           address.setAddress1(info);  
                           }else if(i==3){
                               address.setAddress2(info);  
                           }else if(i==4){
                               address.setCity(info);  
                           }else if(i==5){
                               address.setState(info);      
                           }else if(i==6){
                               address.setZipCode(info);  
                           }else if(i==7){
                           student.setStudentId(info);  
                           }else if(i==8){
                           student.setGPA(info);  
                           }
                      
                       i++;
                       }
                  
                   student.setAddress(address);
                   stack.push(student);
                  
               }
               System.out.println("File loaded");
               break;
              
           case "2":
               int stackSize=stack.size();
               if(stackSize==0){
                   System.out.println("Please load the file first");
                   break;
               }
              
               for(int i=0;i<stackSize;i++){
                   System.out.println(stack.pop());
               }
               break;  
           case "3":
               System.out.println("Good Bye!");
               System.exit(0);
               break;  

           }
       }while(!choice.equals("3"));

   }
  
   public static Scanner readFile(){
       Scanner inputFile=null;
         
       try{
           inputFile=new Scanner(new File("D:\Student.txt"));
           }catch(Exception e){
           System.out.println("Difficulties opening the file! "+e);
       }
         
       return inputFile;
   }

}

-----------------------------------------------------------------output------------------------------------------------------------------------

------------------------------------
1. Load Students (From File)
2. Print Stack
3. Exit Program
Enter your selection :
1
File loaded
------------------------------------
1. Load Students (From File)
2. Print Stack
3. Exit Program
Enter your selection :
2
ID : 1234 Name : Jay Kishore Address : wakad d302 lorelle pune ,maharastra 411057 GPA : 9.9
ID : 1234 Name : Jay Kishore Address : wakad d302 lorelle pune ,maharastra 411057 GPA : 9.9
ID : 1234 Name : Jay Kishore Address : wakad d302 lorelle pune ,maharastra 411057 GPA : 9.9
ID : 1234 Name : Jay Kishore Address : wakad d302 lorelle pune ,maharastra 411057 GPA : 9.9
ID : 1234 Name : Jay Kishore Address : wakad d302 lorelle pune ,maharastra 411057 GPA : 9.9
ID : 1234 Name : Jay Kishore Address : wakad d302 lorelle pune ,maharastra 411057 GPA : 9.9
ID : 1234 Name : Jay Kishore Address : wakad d302 lorelle pune ,maharastra 411057 GPA : 9.9
ID : 1234 Name : Jay Kishore Address : wakad d302 lorelle pune ,maharastra 411057 GPA : 9.9
ID : 1234 Name : jay Kishore Address : wakad d302 lorelle pune ,maharastra 411057 GPA : 9.9
ID : 1234 Name : Jay Kishore Address : wakad d302 lorelle pune ,maharastra 411057 GPA : 9.9
------------------------------------
1. Load Students (From File)
2. Print Stack
3. Exit Program
Enter your selection :
3

Good Bye!

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