Question 3-ArrayList(Unit 6)

Situation: You are developing a student management system where you need to store and analyze the grades of students in a class.

(a) Define an arrayList in Java. Explain its significance and usefulness in programming.

An arrayList is a Java object that are similar to arrays, in terms of their ability to store data, but while arrays have a fixed size determined at their creation, you can change the size of an arrayList when necessary. This is very useful in cases where you do not know the size of the data you want to store before creating the array/arrayList, for example, if you want to create a list of all the people in CSA who have completed their hacks on time. You don’t know how many people will do this, so using an arrayList would be appropriate.

(b) Code:

You need to implement a method calculateAverageGrade that takes an arrayList grades of integers representing student grades and returns the average of all the elements in the arrayList. Write the method signature and the method implementation. Include comments to explain your code.

public class classGrades {
    ArrayList<Double> grades = new ArrayList<Double>();
    public classGrades(ArrayList<Double> grades) {//get grades from user
        this.grades = grades;
    }
    public double calculateAverageGrade() {//method to calculate average
        double sum = 0;
        for(int i=0; i<grades.size(); i++) {//iterate through each value in the grades arrayList and add it to sum variable
            sum+=grades.get(i);
        }
        double avg = sum/grades.size();//calculate average
        return avg;
    }
}
public class Q3 {
    public static void main(String[] args) {
        ArrayList<Double> grades = new ArrayList<Double>();//add grades to arraylist
        grades.add(91.2);
        grades.add(89.9);
        grades.add(85.3);
        grades.add(55.7);
        grades.add(75.0);
        classGrades avgCalc = new classGrades(grades);//create new object
        System.out.println(avgCalc.calculateAverageGrade());//print out average
    }
}
Q3.main(null);
79.42

Question 5: If, While, Else (Unit 3-4)

Situation: You are developing a simple grading system where you need to determine if a given score is passing or failing.

(a) Explain the roles and usage of the if statement, while loop, and else statement in Java programming. Provide examples illustrating each. If statements only evaluate the code inside them if the statement inside their parentheses evaluates to true. They are useful when you only want a certain piece of code to execute once certain conditions are met. Else statements are related to if statements, they are only evaluated if their corresponding if statement evaluates to false. You might use them if you need a piece of code to evaluate if a certain condition isn’t met. While loops evaluate the code inside of them as long as the conditions inside their parentheses evaluates to true. You might use them when you need a certain piece of code to execute for some time, until a certain condition is met.

(b) Code:

You need to implement a method printGradeStatus that takes an integer score as input and prints “Pass” if the score is greater than or equal to 60, and “Fail” otherwise. Write the method signature and the method implementation. Include comments to explain your code.

public class passFail {
    public passFail() {//initializer
        System.out.println("Ready");
    }
    public void printGradeStatus(double score) {//method to determine if student passes or fails
        if (score>=60) {//if statement to determine if student passes
            System.out.println("Pass!");
        } else {//else statement in case student fails
            System.out.println("Fail!");
        }
    }
}
public class Q5 {
    public static void main(String[] args) {
        double score1 = 75.3;
        double score2 = 59.9;
        passFail passer = new passFail();//new object
        passer.printGradeStatus(score1);
        passer.printGradeStatus(score2);
    }
}
Q5.main(null);
Ready
Pass!
Fail!