Extra Credit FRQ
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)
{ /* to be implemented in part (a) */ 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)
{ /* implementation not shown */ return 0;}
/** Returns a new, possibly smaller, two-dimensional array that
* contains only rows from array2D with no zeros, as described
* in part (b).
* Precondition: array2D contains at least one column
* and at least one row with no zeros.
* Postcondition: array2D is unchanged.
*/
public static int[][] resize(int[][] array2D)
{ /* to be implemented in part (b) */ return null;}
}
public class ArrayResizer
{
public static boolean isNonZeroRow(int[][] array2D, int r) {
for (int i = 0; i<array2D[r].length; i++) {
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)
{ /* implementation not shown */ return 0;}
/** Returns a new, possibly smaller, two-dimensional array that
* contains only rows from array2D with no zeros, as described
* in part (b).
* Precondition: array2D contains at least one column
* and at least one row with no zeros.
* Postcondition: array2D is unchanged.
*/
public static int[][] resize(int[][] array2D)
{ /* to be implemented in part (b) */ return null;}
}
public class ArrayResizer
{
public static boolean isNonZeroRow(int[][] array2D, int r) {
for (int i = 0; i<array2D[r].length; i++) {
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)
{ /* implementation not shown */ return 0;}
public static int[][] resize(int[][] array2D){
int[][] newArr = new int[this.numNonZeroRows(array2D),3];
int rownew =0;
for (int i = 0; i<array2D.length;i++) {
if (this.isNonZeroRow(array2D,i)) {
newArr[rownew] = array2D[i];
newArr++;
}
}
return newArr;
}
}
import java.util.Arrays;
public class ArrayResizer
// 6/9 Code is function now, but some mistakes were made. There were logical errors inside some of the methods (iterable was incremented more than once)
{
public static boolean isNonZeroRow(int[][] array2D, int r) {
for (int i = 0; i<array2D[r].length; i++) {
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 count = 0;
for(int i = 0; i < array2D.length; i++){
if(isNonZeroRow(array2D, i)){
count++;
}
}
return count;
}
public static int[][] resize(int[][] array2D){
int[][] newArr = new int[ArrayResizer.numNonZeroRows(array2D)][3];
int rownew =0;
for (int i = 0; i<array2D.length;i++) {
if (ArrayResizer.isNonZeroRow(array2D,i)) {
newArr[rownew] = array2D[i];
rownew++;
}
}
return newArr;
}
public static void main(String args[]) {
int[][] testarr = {{2, 1, 0},
{1, 3, 2},
{0, 0, 0},
{4, 5, 6}};
ArrayResizer test = new ArrayResizer();
System.out.println("Is row 0 non-zero? " + test.isNonZeroRow(testarr, 0));
System.out.println("Is row 1 non-zero? " + test.isNonZeroRow(testarr, 1));
System.out.println("Number of Non Zero Arrays: " + test.numNonZeroRows(testarr));
int[][] resizedArray = test.resize(testarr);
System.out.println(Arrays.deepToString(resizedArray));
}
}
ArrayResizer.main(null);
public class MemberInfo
{
/** Constructs a MemberInfo object for the club member with name
* name, graduation year gradYear, and standing hasGoodStanding.
*/
public MemberInfo(String name, int gradYear, boolean hasGoodStanding)
{ /* implementation not shown */ }
/** Returns the graduation year of the club member. */
public int getGradYear()
{ /* implementation not shown */ }
/** Returns true if the member is in good standing and false
* otherwise.
*/
public boolean inGoodStanding()
{ /* implementation not shown */ }
// There may be instance variables, constructors, and methods
// that are not shown.
}
public class ClubMembers
{
private ArrayList<MemberInfo> memberList;
/** 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)
{ /* to be implemented in part (a) */ }
/** 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)
{ /* to be implemented in part (b) */ }
// There may be instance variables, constructors, and methods
// that are not shown.
}
public class ClubMembers
{
private ArrayList<MemberInfo> memberList;
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)
{ /* to be implemented in part (b) */ }
// There may be instance variables, constructors, and methods
// that are not shown.
}
public class ClubMembers
{
private ArrayList<MemberInfo> memberList;
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);
}
}
public ArrayList<MemberInfo> removeMembers(int year){
ArrayList gradMembers = new ArrayList<MemberInfo>;
for (int i=0,i<memberList.length,i++) {
if (memberList[i].getGradYear() <= year) {
if (memberList[i].inGoodStanding()) {
gradMembers.add(memberList[i]);
}
memberList.remove(i);
}
}
return gradMembers;
}
// There may be instance variables, constructors, and methods
// that are not shown.
}
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;
}
public int getGradYear() {
return gradYear;
}
public boolean inGoodStanding() {
return hasGoodStanding;
}
public String toString(){
return (name + " " + gradYear + " " + hasGoodStanding);
}
}
public class ClubMembers
{
// 5/9 Output was messed up at first, no to string method. Arraylist syntax was incorrect: no .get and .size was used, bracket notation and .length used instead
private ArrayList<MemberInfo> memberList = new ArrayList<>();
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);
}
}
public ArrayList<MemberInfo> removeMembers(int year){
ArrayList<MemberInfo> gradMembers = new ArrayList<>();
int i = 0;
while (i<this.memberList.size()) {
if (memberList.get(i).getGradYear() <= year) {
if (memberList.get(i).inGoodStanding()) {
gradMembers.add(memberList.get(i));
}
memberList.remove(i);
} else {
i++;
}
}
return gradMembers;
}
public static void main(String args[]) {
ClubMembers test = new ClubMembers();
String[] setone = new String[]{"Bob", "Rob"};
String[] settwo = new String[]{"Sanjay", "Karthik", "Evan"};
test.addMembers(setone,2021);
test.addMembers(settwo,2024);
System.out.println(test.memberList);
System.out.println(test.removeMembers(2022));
System.out.println(test.memberList);
}
}
ClubMembers.main(null);
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)
{
/* to be implemented in part (a) */
}
/** 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)
{
/* to be implemented in part (b) */
}
/** Returns the number of digits in num. */
public static int getNumberOfDigits(int num)
{
/* implementation not shown */
}
/** 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)
{
/* implementation not shown */
}
// There may be instance variables, constructors, and methods not shown.
}
public class CheckDigit
{
public static int getCheck(int num)
{
int multiplier = 7;
int sum = 0;
for (int i = 1;i<= CheckDigit.getNumberOfDigits(num);i++) {
sum += (CheckDigit.getDigit(num,i))*multiplier;
multiplier--;
}
return CheckDigit.getDigit(sum,CheckDigit.getNumberOfDigits(sum));
}
/** 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)
{
/* to be implemented in part (b) */
}
/** Returns the number of digits in num. */
public static int getNumberOfDigits(int num)
{
/* implementation not shown */
}
/** 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)
{
/* implementation not shown */
}
// There may be instance variables, constructors, and methods not shown.
}
public class CheckDigit
{
public static int getCheck(int num)
{
int multiplier = 7;
int sum = 0;
for (int i = 1;i<= CheckDigit.getNumberOfDigits(num);i++) {
sum += (CheckDigit.getDigit(num,i))*multiplier;
multiplier--;
}
return CheckDigit.getDigit(sum,CheckDigit.getNumberOfDigits(sum));
}
public static boolean isValid(int numWithCheckDigit)
{
int check = numWithCheckDigit % 10;
int restofnum = numWithCheckDigit/10;
return CheckDigit.getCheck(restofnum) == check;
}
/** Returns the number of digits in num. */
public static int getNumberOfDigits(int num)
{
/* implementation not shown */
}
/** 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)
{
/* implementation not shown */
}
// There may be instance variables, constructors, and methods not shown.
}
public class CheckDigit
{
// 5/9. implementation for getCheck was incorrect, logical errors were present. Methods were called incorrectly.
public static int getCheck(int num)
{
int multiplier = 7;
int sum = 0;
for (int i = 1;i<= CheckDigit.getNumberOfDigits(num);i++) {
sum += (CheckDigit.getDigit(num,i))*multiplier;
multiplier--;
}
return CheckDigit.getDigit(sum,CheckDigit.getNumberOfDigits(sum));
}
public static boolean isValid(int numWithCheckDigit)
{
int check = numWithCheckDigit % 10;
int restofnum = numWithCheckDigit/10;
return CheckDigit.getCheck(restofnum) == check;
}
public static int getNumberOfDigits(int num)
{
int count = 0;
while(num > 0){
num /= 10;
count++;
}
return count;
}
public static int getDigit(int num, int n)
{
int digit = 0;
int len = CheckDigit.getNumberOfDigits(num);
for (int i = 0; i < len - n + 1; i++){
digit = num % 10;
num /= 10;
}
return digit;
}
public static void main(String args[]) {
CheckDigit tester = new CheckDigit();
System.out.println(tester.getCheck(159));
System.out.println(tester.isValid(1592));
}
}
CheckDigit.main(null);
//9/9 All methods worked and all output matched collegeboard example
public class AdditionPattern {
private int currentnum = 0;
private int increment;
public AdditionPattern(int start,int increment) {
this.currentnum = start;
this.increment = increment;
}
public int currentNumber() {
return this.currentnum;
}
public void next() {
this.currentnum += this.increment;
}
public void prev() {
if (this.currentnum - this.increment <=2) {
this.currentnum = 2;
} else {
this.currentnum -= increment;
}
}
public static void main (String args[]) {
AdditionPattern test = new AdditionPattern(2,3);
System.out.println(test.currentNumber());
test.next();
System.out.println(test.currentNumber());
test.next();
test.next();
System.out.println(test.currentNumber());
test.prev();
System.out.println(test.currentNumber());
test.prev();
System.out.println(test.currentNumber());
}
}
AdditionPattern.main(null)