2D Array

public class ArrayResizer {
/** Returns true if and only if every value in row r of array2D

* is non-zero.

* Precondition: r is a valid row index in array2D.

* Postcondition: array2D is unchanged.

*/

public static boolean isNonZeroRow(int[][] array2D, int r){
    //loop through row r
        for (int i=0; i<array2D[r].length; i++){
            //check element in row r equal to 0 or not
            if(array2D[r][i] == 0){
                return false;
            }
        }
        return true;
    }

 
/** Returns the number of rows in array2D that contain all

* non-zero values.

* Postcondition: array2D is unchanged.

*/

public static int numNonZeroRows(int[][] array2D){  
    int counter = 0;
        for (int r=0; r<array2D.length; r++){
            boolean nonZeroRow = true;
            for (int c=0; c<array2D[0].length; c++)
                 if(array2D[r][c] == 0){
                    nonZeroRow = false;
              }
              if (nonZeroRow){
                counter++;
              }
           }
           return counter;
      }

public static int[][] resize(int[][] array2D){ //return type is 2d array
    int[][] nonZero2dArray = new int[numNonZeroRows(array2D)][array2D[0].length]; //numNonZeroRows needs to input an 2d array and it returns an integer
    int counter = 0;  //counter is for iterating through the row index in nonZero2dArray
        for(int i=0; i<array2D.length; i++) { 
            if(isNonZeroRow(array2D, i)){ //check if row i in 2dArray is non zero
                for(int c = 0; c < array2D[i].length; c++){ //we need this forloop to find the specific colume index - we can put the whole row in, we need to find the row and column index and put each element into the new 2dArray
                    int column = array2D[i][c]; //create int column (primitive) because 2dArray is reference type (address) so change 2dArray won't affect new 2d array
                    nonZero2dArray[counter][c] = column; 
                }
                counter++; //if isNonZeroRow returns true and we put the elements of that non zero row into the new 2d array, counter ++ -> move to the next row of new 2d array
            }
    
        }
        return nonZero2dArray; //return a 2d array
    
    }

public static void tester(String[] args) {
    int[][] arr = {{2, 1, 0}, {1, 3, 2}, {0, 0, 0}, {4, 5, 6}};

    System.out.println(isNonZeroRow(arr, 1));
    System.out.println(numNonZeroRows(arr));
    int[][] newArray = resize(arr);
    for(int i = 0; i<newArray.length; i++){
        System.out.println(Arrays.toString(newArray[i]));
    }


    }
}

ArrayResizer.tester(null);
true
2
[1, 3, 2]
[4, 5, 6]

Array & Arraylist

public class MemberInfo

{

private String name;
private int gradYear;
private boolean hasGoodStanding;

public MemberInfo(String name, int gradYear, boolean hasGoodStanding){
    this.name = name;
    this.gradYear = gradYear;
    this.hasGoodStanding = hasGoodStanding;
}


/** Returns the graduation year of the club member. */

public int getGradYear(){ 
    return gradYear;
}

 

/** Returns true if the member is in good standing and false

* otherwise.

*/

public boolean hasGoodStanding(){
    return hasGoodStanding;
}

public String getName(){
    return name;
}

}
public class ClubMembers{
    private ArrayList<MemberInfo> memberList;

    public ClubMembers() {
        memberList = new ArrayList<MemberInfo>();
        memberList.add(new MemberInfo("Emily", 2024, true));
        memberList.add(new MemberInfo("Alice", 2022, true));
        memberList.add(new MemberInfo("David", 2025, false));
    }


/** Adds new club members to memberList, as described in part (a).

* Precondition: names is a non-empty array.

*/

public void addMembers(String[] names, int gradYear){
    for (int i=0; i<names.length; i++) {
        MemberInfo newMember = new MemberInfo(names[i], gradYear, true);
        memberList.add(newMember);
    }
}
 
/** Removes members who have graduated and returns a list of

* members who have graduated and are in good standing,

* as described in part (b).

*/

public ArrayList<MemberInfo> removeMembers(int year){
    //create an arraylist
    ArrayList<MemberInfo> graduatesList = new ArrayList<MemberInfo>();

    //check if graduated using gradyear
    for(int i=memberList.size()-1; i >=0; i--){
        if(memberList.get(i).getGradYear() <= year){
            //check if good standing
            if (memberList.get(i).hasGoodStanding()){
            graduatesList.add(memberList.get(i));
        }
        //remove regardless of in good standing or not
        memberList.remove(i); 
    }
 }
return graduatesList;
}

public static void tester(String[] args) {
    String[] names = {"Chloe", "Sophia"};
    ClubMembers club = new ClubMembers();
    club.addMembers(names, 2024);
    System.out.println("New memberList: ");
    for (MemberInfo member : club.memberList) {
        System.out.println(member.getName());
    }
    System.out.println(" ");

    ArrayList<MemberInfo> graduatesList = club.removeMembers(2024);
    System.out.println("Graduates List: ");
    for (MemberInfo member : graduatesList) {
        System.out.println(member.getName());
    }
}

}

ClubMembers.tester(null);
New memberList: 
Emily
Alice
David
Chloe
Sophia
 
Graduates List: 
Sophia
Chloe
Alice
Emily

Class

public class AdditionPattern{
private int start;
private int addingNum;
private int current;

public AdditionPattern(int start, int addingNum){
    this.start = start;
    this.addingNum = addingNum;
    current = start;
}

public int currentNumber(){
    return current;
}

public void next(){
    current+=addingNum;
}

public void prev(){
    if(current>start){
        current-=addingNum;
    }
}

public static void tester(String[] args){
    AdditionPattern plus3 = new AdditionPattern(2, 3);
    System.out.println(plus3.currentNumber());
    plus3.next();
    System.out.println(plus3.currentNumber());
    plus3.prev();
    System.out.println(plus3.currentNumber());
    plus3.prev();
    System.out.println(plus3.currentNumber());
    }
}

AdditionPattern.tester(null);
2
5
2
2

Methods and Control Structures

public class CheckDigit{

/** Returns the check digit for num, as described in part (a).

* Precondition: The number of digits in num is between one and six, inclusive.

* num >= 0

*/

public static int getCheck(int num){
    int total = 0;

    for(int i = 1; i<=getNumberOfDigits(num); i++){
        total += getDigit(num, i) * (8 - i);
    }
    int checkDigit = getDigit(total, getNumberOfDigits(total));
    return checkDigit;
 }

 
/** Returns true if numWithCheckDigit is valid, or false otherwise, as described in part (b).

 * Precondition: The number of digits in numWithCheckDigit is between two and seven, inclusive.

* numWithCheckDigit >= 0

 */

 public static boolean isValid(int numWithCheckDigit){
    int numWithoutCheckDigit = numWithCheckDigit/10;
    int checkDigit = getCheck(numWithoutCheckDigit);
    if (getDigit(numWithCheckDigit, getNumberOfDigits(numWithCheckDigit)) == checkDigit){
      return true;
    }
    else {
      return false;
    }
 }

 
/** Returns the number of digits in num. */

public static int getNumberOfDigits(int num){
    String numStr = Integer.toString(num);
    int x = numStr.length();
    return x;
}


/** Returns the nth digit of num.

* Precondition: n >= 1 and n <= the number of digits in num

*/

public static int getDigit(int num, int n){
    String numStr = Integer.toString(num);
    String digitStr = numStr.substring(n-1, n);
    int digit = Integer.parseInt(digitStr);
    return digit;

}

public static void tester(String[] args){
    int num = 283415;
    System.out.println(getCheck(num)); //2*7+8*6+3*5+4*4+1*3+5*2 = 106
    System.out.println(getNumberOfDigits(num));
    System.out.println(getDigit(num, 2));
    
    }
}

CheckDigit.tester(null);
6
6
8