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

The following is a homework problem (java programming): get/set the String that

ID: 3574599 • Letter: T

Question

The following is a homework problem (java programming):

get/set the String that will contain the Student's id

get/set the String that will indicate whether the Student is an undergraduate (UGRAD) or a graduate (GRAD) student

get/set the String(s) that will contain the Student's academic program (i.e. their major/degree). Note: students can have more than one academic program

get/set the boolean that indicates whether student is a full time student

get/set the boolean that indicates whether student is part time student

a toString method

I understand how to create the student class extending the predefined Person class, but I'm lost on how to populate this with the values from the file we were given, and then how to store all of the information in the Student class in a Vector (which is what we were instructed to do) and then print out its contents.

This is what I have for student class:

package chapter10homework;

public class Student extends Person {

   private String id;

   private String status;

   private String major;

   private String degree;

private boolean fullTime;

private boolean partTime;

  

  

   public Student () {

       super();  

   }

  

   public Student (String id){

       super();

       setId(id);

   }

  

   public String getId (){

       return id;

   }

  

   public String getStatus(){

       return status;

   }

  

   public String getMajor() {

       return major;

   }

  

   public String getDegree() {

       return degree;

   }

  

public boolean getFt (){

       return fullTime;

   }

  

public boolean getPt (){

       return partTime;

      

   }

  

  

   public void setId(String id) {

       this.id = id;

   }

  

   public void setStatus (String val){

       status = val;

   }

  

   public void setMajor (String val){

       major = val;

   }

  

   public void setDegree (String val){

       major = val;

   }

  

   public void setfullTime (boolean val){

       fullTime = val;

   }

  

   public void setpartTime (boolean val){

       partTime = val;

   }

  

   public String toString(){

       return super.toString() +

               "id = " + id + " "+

               "grad or ugrad = " + status + " "+

               "academic program = "+ major + " "+ degree + " "+

               "part time = "+ partTime + " "+

               "full time = "+ fullTime + " ";   

   }

}

Explanation / Answer

import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;
//Class Person
class Person
{
   //Data members of Person class
   private String firstName;
   private String lastName;
   private String Phone;
   private String Email;
   //Returns the student first name
   public String getFirstName ()
   {
   return firstName;
   }
   //Sets the student first name
   public void setFirstName (String fn)
   {
   firstName = fn;
   }
   //Returns the student last name
   public String getLastName ()
   {
   return lastName;
   }
   //sets the student last name
   public void setLastName (String ln)
   {
   lastName = ln;
   }
   //Returns the student phone number
   public String getPhone()
   {
   return Phone;
   }
   //sets the student phone number
   public void setPhone(String ph)
   {
   Phone = ph;
   }
   //Retuens the student Email id
   public String getEmail()
   {
   return Email;
   }
   //sets the student email id
   public void setEmail(String em)
   {
   Email = em;
   }
}
//Derived class Student from person
class Student extends Person
{
    //Data member of student class
   private String id;
   private String status;
   private String major;
   private String degree;
   private boolean fullTime;
   private boolean partTime;
   //Retuens the student ID
public String getId (){
return id;
}
   //Retuens the student status
public String getStatus(){
return status;
}
   //Retuens the student Major
public String getMajor() {
return major;
}
   //Retuens the student degree status
public String getDegree() {
return degree;
}
   //Retuens true for full time or false
public boolean getFt (){
return fullTime;
}
//Returns true for part time or false
public boolean getPt (){
return partTime;
  
}
//Sets the student id
public void setId(String id) {
this.id = id;
}
//Sets the student status
public void setStatus (String val){
status = val;
}
//Sets the student Major
public void setMajor (String val){
major = val;
}
//Sets the student degree
public void setDegree (String val){
major = val;
}
//Sets the full time
public void setfullTime (boolean val){
fullTime = val;
}
//Sets the part time
public void setpartTime (boolean val){
partTime = val;
}
//Display function
public void ToString()
   {
  
System.out.println(" First Name: " + getFirstName() + " Last Name: " + getLastName() + " Phone: " + getPhone() + " Email: " + getEmail());
System.out.println(" ID: " + getId() + " Status: " + getStatus() + " Major: " + getMajor() + " Full Time: " + String.valueOf(getFt()) + " Part Time: " + String.valueOf(getPt()));
}
}
//Driver class
public class StudentDemo
{
   public static void main(String st[])
   {
       //c counts number of student information
       int c = 0, co = 0;              
       String str[] = new String[100];
       //Open file for read
       File file = new File("StudentInfo.txt");
       try
       {
           Scanner in = new Scanner(file);
           //Loops till end
           while (in.hasNextLine())
           {    // iterates each line in the file
               str[c] = in.nextLine();
               c++;
           }
           in.close();
       }
       catch (FileNotFoundException e)
       {
       e.printStackTrace();
       }
       //Calculates number of student
       int counter = c / 9;

       //Creates student object based on the number of student
       Student ss[] = new Student[counter];

       //Initializes each object
       for(int t = 0; t < counter; t++)
           ss[t] = new Student();
       co = 0;
       //Stores the data in class
       for(int t = 0; t < counter; t++)
       {
           ss[t].setFirstName(str[++co]);
           ss[t].setLastName(str[++co]);
           ss[t].setPhone(str[++co]);
           ss[t].setEmail(str[++co]);
           ss[t].setId(str[++co]);
           ss[t].setStatus(str[++co]);
           ss[t].setMajor(str[++co]);
           ss[t].setfullTime(Boolean.parseBoolean(str[++co]));
           ss[t].setpartTime(Boolean.parseBoolean(str[++co]));
       }

       //Display student information
       for(int t = 0; t < counter; t++)
       {
           ss[t].ToString();
       }
   }
}

Output

First Name: Mohan
Last Name: 9040994433
Phone: pyari
Email: 123321

ID: GRAD
Status: major
Major: true
Full Time: false
Part Time: false

First Name: Swain
Last Name: 7740994433
Phone: Amiya
Email: 789987

ID: UGRAD
Status: degree
Major: false
Full Time: true
Part Time: false