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

Create a Java class with these requirements: *If you wish, create this class you

ID: 3676559 • Letter: C

Question

Create a Java class with these requirements:

*If you wish, create this class yourself, rather than using a pre-defined class from a Java standard library.

Write code for a Java program as follows: Create a class of your own with a main method that calls the constructor(s) and methods of your class can be reference variables (another class*) something interesting with your class's data. Perhaps use a conditional loop in your code in this method Create a minimum of three private data attributes for your class. These can all be primitive variables (int, char, long, double etc..), or they * Besides getter and setter methods, and at least one constructor in addition to the default, create at least one method that does

Explanation / Answer

public class School {
   private int capacity;
   private Student[] students;
   private int count; // current count of students in school
   private String name; // name of school
   // default constructor
   public School(){
       count=0;
       capacity = 20; // default capacity
       students = new Student[capacity]; // creating student array of capacity 20
   }
   // constructor , having capacity as params
   public School(int capacity, String name){
       count=0;
       this.capacity = capacity;
       students = new Student[capacity];
       this.name= name;
   }
  
   public int getCapacity(){
       return capacity;
   }
  
   public int getCount(){
       return count;
   }
  
   public Student[] getStudentList(){
       return students;
   }
   // add student in school
   public void addStudent(Student s){
       if(count == capacity){
           System.out.println("School does not have enough");
       }
       students[count] = s;
       count++; // increasing count
   }
  
   public String getName(){
       return name;
   }
   public void setName(String name){
       this.name= name;
   }
  
   // main function
   public static void main(String[] args) {
       School school = new School(30, "DAV SuperFase");
       System.out.println("Name of school: "+school.getName());
      
       // creating student object
       Student s1 = new Student("Alex", 1);
       Student s2 = new Student("bob", 2);
       Student s3 = new Student("Pk", 3);
      
       // adding student to school
       school.addStudent(s1);
       school.addStudent(s2);
       school.addStudent(s3);
      
       // getting student list from school
       Student []s = school.getStudentList();
       int count = school.getCount();
       //iterating over list
       for(int i=0; i< count; i++){
           System.out.println(s[i]);
       }
      
   }

}

// Student class
class Student{
   private String name;
   private int id;
  
   public Student(String name, int id){
       this.name = name;
       this.id = id;
   }
  
   public int getId(){
       return id;
   }
   public String getName(){
       return name;
   }
   @Override
   public String toString() {
       return "Name: "+name+", Id: "+id;
   }
}

/*

Output:

Name of school: DAV SuperFase
Name: Alex, Id: 1
Name: bob, Id: 2
Name: Pk, Id: 3

*/