Commit 49688219 authored by MostafaRahmati's avatar MostafaRahmati

last question is not solved yet

parents
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/../../../../../../:\Users\Mostafa\Desktop\libSessionTwo\.idea/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_15" default="true" project-jdk-name="openjdk-15" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/libSessionTwo.iml" filepath="$PROJECT_DIR$/libSessionTwo.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
/**
* The Lab class represents a Lab class with all of students and
* referred information
* It holds the student details relevant in our context.
*
* @author SpNova
* @version 0.0
*/
public class Lab {
// stores a list of attendant students
private Student[] students;
// global variable for lab class average grade
private int avg;
// global variable for the day that lab class runs
private String day;
// global variable for capacity of lab class
private int capacity;
// global variable for attending number of students
private int currentSize;
/**
* constructor for lab class
*
* @param cap set capacity of lab class
* @param d set day of lab class
*/
public Lab(int cap, String d) {
capacity = cap;
day = d;
students = new Student[cap];
}
/**
* checks if lab class has capacity to enroll a student or not
* enrolls student to the lab class if there is enough capacity
* prints an error if lab is full
*
* @param std a student instance is called
*/
public void enrollStudent(Student std) {
if (currentSize < capacity) {
students[currentSize] = std;
currentSize++;
} else {
System.out.println("Lab is full!!!");
}
}
/**
* prints all lab students personal data
* at last prints the average grade of all students in lab class
*/
public void print() {
for (int i = 0; i < students.length; i++) {
System.out.println("FirstName: " + students[i].getFirstName() + "\tLastName: " + students[i].getLastName() +
"\t\tID:" + students[i].getId() + "\t\tGrade:" + students[i].getGrade());
}
calculateAvg();
System.out.println("AVERAGE GRADE: " + getAvg());
}
/**
* returns a list of all attendant students
*
* @return students list
*/
public Student[] getStudents() {
return students;
}
/**
* student field must be an array that contains all required data
*
* @param students set students list
*/
public void setStudents(Student[] students) {
this.students = students;
}
/**
* get average grade of the class
*
* @return average field in INTEGER
*/
public int getAvg() {
return avg;
}
/**
* calculates the average grade of class
*/
public void calculateAvg() {
int sum = 0;
for (int i = 0; i < students.length; i++) {
sum = sum + students[i].getGrade();
}
avg = sum / students.length;
}
/**
* get the day that student attends the class
*
* @return day field
*/
public String getDay() {
return day;
}
/**
* @param day set the day that student attends the class
*/
public void setDay(String day) {
this.day = day;
}
/**
* get capacity of the class
*
* @return capacity field
*/
public int getCapacity() {
return capacity;
}
/**
* @param capacity set capacity of the class
*/
public void setCapacity(int capacity) {
this.capacity = capacity;
}
/**
* changes the first name of first student passed to this class
*/
public void changeFirstStudentsFirstName() {
students[1].setFirstName("FOO");
}
}
\ No newline at end of file
public class Run {
public static void main(String[] args) {
Student std1 = new Student("Ehsan", "Edalat", "9031066");
Student std2 = new Student("Seyed", "Ahmadpanah", "9031806");
Student std3 = new Student("Ahmad", "Asadi", "9031054");
std1.print();
std1.setGrade(15);
std1.print();
std2.print();
std2.setGrade(11);
std2.print();
std3.print();
std3.setFirstName("HamidReza");
std3.print();
//Another Part Of HomeWork
System.out.println("-------------------------------------------------------");
Lab lab1 = new Lab(3, "Saturday Morning");
lab1.enrollStudent(std1);
lab1.enrollStudent(std2);
lab1.enrollStudent(std3);
lab1.print();
//"Think About It" Question
System.out.println("-------------------------------------------------------");
System.out.println("Sent Student Object Had The Value Of " + std1.getFirstName() + " For Fist Name");
lab1.changeFirstStudentsFirstName();
System.out.println("After Changing Method Now It Has Value Of " + std1.getFirstName() + " For Fist Name");
System.out.println("We See No Change In The Original Object! So Java Passes Objects By Their Value!");
//DEBUGGING Question
System.out.println("-------------------------------------------------------\n");
System.out.println("The Problem Is That Lab Average Is Not Calculated And Called Before Printing");
}
}
/**
* The Student class represents a student in a student
* administration system.
* It holds the student details relevant in our context.
*
* @author Ehsan
* @version 0.0
*/
public class Student {
// the student’s first name
private String firstName;
// the student’s last name
private String lastName;
// the student ID
private String id;
//the grade
private int grade;
/**
* Create a new student with a given name and ID number.
*
* @param fName first name of student
* @param lname last name of student
* @param sID student ID
*/
public Student(String fName, String lname, String sID) {
firstName = fName;
lastName = lname;
id = sID;
grade = 0;
}
/**
* get the first name of student
*
* @return firstName field
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName set first name of a student
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* get the first name of student
*
* @return lastName field
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName set first name of a student
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* get the first name of student
*
* @return id field
*/
public String getId() {
return id;
}
/**
* @param id set first name of a student
*/
public void setId(String id) {
this.id = id;
}
/**
* get the first name of student
*
* @return grade field
*/
public int getGrade() {
return grade;
}
/**
* @param grade set first name of a student
*/
public void setGrade(int grade) {
this.grade = grade;
}
/**
* Print the student’s last name and ID number to the
* output terminal.
*/
public void print() {
System.out.println(lastName + ", student ID: "
+ id + ", grade: " + grade);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment