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

JAVA second semester course DESCRIPTION: refresh concepts taught in CSIS-1400. Y

ID: 3662847 • Letter: J

Question

JAVA second semester course

DESCRIPTION:

refresh concepts taught in CSIS-1400. You will write a program that keeps track of a list of students and exposes a given set of choices with a menu. Do so by implementing at least two classes: Student and StudentApp

ad Student : The class Student represents a student. Notice that neither count nor sNumber are passed to the constructor. These two fields also have no set method (mutator).

ad count:

Notice that count is underlined. This indicated that count is a static field.
Count keeps a running count of the Student objects that have been created. It is used to create a unique sNumber for each student

ad constructors:

The generated field sNumber needs to be initialized in both constructors.
Create a unique 7 digit student number for each student based on the static field count. (e.g. 1234567 + count++)

The parameterized constructor uses the values passed to initialize the fields.

*************************************************************

ad StudentApp: class

StudentApp includes the main method .
Use private methods to structure your code.
If you want to add additional classes that is fine, too.

ad main

Create an ArrayList of students that is initialized with 3 different students.
Use a do-while loop and a switch statement to display a menu with choices and to respond to the user selections.
Here is how the menu should look like:

1 Add a student

2 Find a student

3Delete a student

4 Display all students

5 Display number of students in list

6 Exit

The user should not be allowed to enter a student number because it is auto-generated.
If a user tries to find or delete a student based on a student number that doesn’t exist, an appropriate message should be displayed. (see output)

If a student is actually found or deleted corresponding student data should be displayed as part of the response (see output)

***********************************

Sample Output :

Explanation / Answer


/**The Student class resemebles student with first name,
* last name, major, gpa.*/
//Student.java
public class Student
{
  
   //instance variables of Student clas
   private String firstName;
   private String lastName;
   private int sNUmber=1234567;
   private String major;
   private double gpa;
   //static variable to count
   private static int count;
  
   //Default costructor
   public Student()
   {
       firstName="";
       lastName="";
       sNUmber=0;
       major="";
       gpa=0;
       count=0;
   }
  
   //Parameterized costructor
   public Student(String fName, String lName, String maj,
           double gpa)
   {
       firstName=fName;
       lastName=lName;      
       major=maj;
       this.gpa=gpa;      
       sNUmber=sNUmber+count++;
   }
  
   public String getFirstName()
   {
       return firstName;
   }
   public void setFirstName(String fName)
   {
       firstName=fName;
   }
  
   public String getLastName()
   {
       return lastName;
   }
   public void setLastName(String lName)
   {
       lastName=lName;
   }
  
   public int getSNUmber()
   {
       return sNUmber;
   }
  
   public String getMajor()
   {
       return major;
   }
  
   public void setMajor(String maj)
   {
       major=maj;
   }
  
   public double getGpa()
   {
       return gpa;
   }
  
   public void setGpa(double gpa)
   {
       this.gpa=gpa;
   }
  
   //Override the toString method
   @Override
   public String toString()
   {      
       return "S"+getSNUmber()+" "+firstName+" "+lastName+"("+major+")"
               +"gpa:"+gpa;
   }  
}//end of the Student class

-------------------------------------------------------------------------------------------------------------------

/**The java StudentApp that tests the methods of the Student class.
* The class has methods to add, find, delete , display and count of the students
* int the array list.*/
//StudentApp.java
import java.util.ArrayList;
import java.util.Scanner;
public class StudentApp
{
   //Create a Scanner class object
   private static Scanner scanner=new Scanner(System.in);
   //Create an arraylist of type Student
   private static ArrayList<Student>stdList=
           new ArrayList<Student>();
  
   public static void main(String[] args)
   {
       //Add three students to the stdList
       stdList.add(new Student("john", "Smith", "CS", 3.6));
       stdList.add(new Student("Lauren", "Edwards", "CS", 3.8));
       stdList.add(new Student("Alex", "Taylor", "EE", 3.2));  

       int choice;

       do
       {
           choice=menu();

           switch(choice)
           {
           case 1:
               addStudent();
               break;
           case 2:
               findStudent();
               break;
           case 3:
               deleteStudent();
               break;
           case 4:
               displayAll();
               break;
           case 5:
               displayCount();
               break;
           case 6:
               System.exit(0);
          
           }

       }while(choice!=6);
      

   }

   private static void deleteStudent() {
      
       boolean found=false;
       int number;
       Student tempStd = null;
       System.out.print("Find student with S");
       number=Integer.parseInt(scanner.nextLine());      
       for (int i = 0; i < stdList.size() &&!found; i++)
       {
           if(stdList.get(i).getSNUmber()==number)
           {
               tempStd=stdList.get(i);
               stdList.remove(i);
           }
       }      
      
       if(!found)
           System.out.println
           ("S"+tempStd.getSNUmber()+" "+tempStd.getFirstName()+
                   " "+tempStd.getLastName()+" has been deleted");
      
       if(found)
           System.out.println("Student could not be found");      
      
   }


   //print the number of students in the stdList
   private static void displayCount() {      
       System.out.println("Number of students : "+stdList.size());
      
   }

   /*Find a student with sNumber*/
   private static void findStudent()
   {
       boolean found=false;
       int number;
       System.out.print("Find student with S");
       number=Integer.parseInt(scanner.nextLine());      
       for (int i = 0; i < stdList.size() &&!found; i++)
       {
           if(stdList.get(i).getSNUmber()==number)
           {
               System.out.println(stdList.get(i));
           }
       }      
       if(!found)
           System.out.println("Student could not be found");      
   }
  
   //Add a student to the stdList
   private static void addStudent()
   {
       System.out.print("First name:");
       String fName=scanner.nextLine();
       System.out.print("Last name:");
       String lName=scanner.nextLine();
       System.out.print("Major:");
       String maj=scanner.nextLine();
       System.out.print("First name:");
       double gpa=Double.parseDouble(scanner.nextLine());
      
       //Add new student to the stdList
       stdList.add(new Student(fName, lName, maj, gpa));
   }

   //Print all students
   private static void displayAll()
   {      
       for (int index = 0;
               index < stdList.size(); index++)
       {
           System.out.println(stdList.get(index));  
       }      
   }

   //Returns a menu of choice
   public static int menu()
   {      
       System.out.println("1.Add a student");
       System.out.println("2.Find a student");
       System.out.println("3.Delete a student");
       System.out.println("4.Display a student");
       System.out.println("5.Display the total number of students");
       System.out.println("6.Exit");
       System.out.print("Enter your selection: ");
       int choice=Integer.parseInt(scanner.nextLine());
       return choice;
   }
}

----------------------------------------------------------------------------------------------------------------------------------

Sample output:

1.Add a student
2.Find a student
3.Delete a student
4.Display a student
5.Display the total number of students
6.Exit
Enter your selection: 4
S1234567 john Smith(CS)gpa:3.6
S1234568 Lauren Edwards(CS)gpa:3.8
S1234569 Alex Taylor(EE)gpa:3.2
1.Add a student
2.Find a student
3.Delete a student
4.Display a student
5.Display the total number of students
6.Exit
Enter your selection: 2
Find student with S1234568
S1234568 Lauren Edwards(CS)gpa:3.8
Student could not be found
1.Add a student
2.Find a student
3.Delete a student
4.Display a student
5.Display the total number of students
6.Exit
Enter your selection: 3
Find student with S1234569
S1234569 Alex Taylor has been deleted
1.Add a student
2.Find a student
3.Delete a student
4.Display a student
5.Display the total number of students
6.Exit
Enter your selection: 2
Find student with S1234569
Student could not be found
1.Add a student
2.Find a student
3.Delete a student
4.Display a student
5.Display the total number of students
6.Exit
Enter your selection: 6