Seed Extra Cred
public class Book {
private String title;
private double price;
public Book(String bookTitle, double bookPrice) {
title = bookTitle;
price = bookPrice;
}
public String getTitle() {
return title;
}
public String getBookInfo() {
return title + "-" + price;
}
}
public class Textbook extends Book {
public int edition;
public Textbook(String bookTitle, double bookPrice, int editionnum) {
super(String bookTitle, double bookPrice);
edition = editionnum;
}
public int getEdition() {
return edition;
}
public String getBookInfo() {
super.getBookInfo() + "-" + edition;
// return title + "-" + price + "-" + editionnum;
}
public boolean canSubsituteFor(Textbook two) {
if (two.getTitle().equals(getTitle()) && two.getEdition() <= getEditon()) {
return true;
}
else {
return false;
}
}
}
- edition needs to be private
- the super in the constructor should not redefine bookTItle as String, etc
- forgot the return in getBookInfo()
- if else not needed
- misspelled getEdition()
Corrected code (essential parts)
public class Textbook extends Book {
private int edition;
public Textbook(String bookTitle, double bookPrice, int editionnum) {
super(bookTitle, bookPrice);
edition = editionnum;
}
public int getEdition() {
return edition;
}
public String getBookInfo() {
return super.getBookInfo() + "-" + edition;
// return title + "-" + price + "-" + editionnum;
}
public boolean canSubsituteFor(Textbook two) {
if (two.getTitle().equals(getTitle()) && two.getEdition() <= this.getEdition()) {
return true;
}
else {
return false;
}
}
public static void main(String[] args) {
Textbook test = new Textbook("Harry Potter", 5.0, 3);
Textbook test2 = new Textbook("Harry Potter", 5.0, 4);
System.out.println(test.getBookInfo());
System.out.println(test.canSubsituteFor(test2));
}
}
Textbook.main(null);
public class FrogSimulation {
private int goalDistance;
private int maxHops;
private int random;
private int distance = 0;
private int counter;
private double prop;
public FrogSimulation(int dist, int numHops) {
goalDistance = dist;
maxHops = numHops;
}
private int hopDistance() {
random = (int) ((Math.random()*2-1)*(Math.random()*goalDistance));
return random;
}
public boolean simulate() {
//a
for (int i=0; i<maxHops; i++) {
distance += this.hopDistance();
if (distance >= goalDistance) {
return true;
}
if (distance < 0) {
return false;
}
}
return false;
}
public double runSimulations(int num) {
//b
for (int i=0; i<num; i++) {
if (this.simulate()) {
counter++;
}
}
prop = (double) counter/num;
return prop;
}
public static void main(String[] args) {
FrogSimulation test = new FrogSimulation(50, 5);
System.out.println("the prop is " + test.runSimulations(500));
}
}
FrogSimulation.main(null);
public class MemberInfo
{
private String n;
private int gy;
private boolean hgs;
public MemberInfo(String name, int gradYear, boolean hasGoodStanding) {
n = name;
gy = gradYear;
hgs = hasGoodStanding;
}
public String getName() {
return n;
}
public int getGradYear() {
return gy;
}
public boolean inGoodStanding() {
return hgs;
}
}
public class ClubMembers
{
private ArrayList<MemberInfo> memberList;
private ArrayList<MemberInfo> filler = new ArrayList<MemberInfo>();
public ClubMembers() {
memberList = new ArrayList<MemberInfo>();
filler = new ArrayList<MemberInfo>();
}
public void addMembers(String[] names, int gradYear) {
//a
for (int i=0; i<names.length; i++) {
MemberInfo add = new MemberInfo(names[i], gradYear, true);
memberList.add(add);
}
}
public ArrayList<MemberInfo> removeMembers(int year) {
//b
for (int i=0; i<memberList.size(); i++) {
if (memberList.get(i).getGradYear() >= year) {
if (memberList.get(i).inGoodStanding()) {
MemberInfo print = new MemberInfo(names[i], gradYear, true);
filler.add(print);
}
memberList.remove(i);
}
}
return filler;
}
}
Issues
- going from 0 to memberList.size() will not work because we would be removing the indexes which would screw up the indexes for the rest. Better to start at the back
- did names[i] and gradYear in removeMembers method when should be access MemberInfo object
Fixed code
public class ClubMembers
{
private ArrayList<MemberInfo> memberList;
private ArrayList<MemberInfo> filler;
public ClubMembers() {
memberList = new ArrayList<MemberInfo>();
filler = new ArrayList<MemberInfo>();
}
public void addMembers(String[] names, int gradYear) {
//a
for (int i=0; i<names.length; i++) {
// For testing purposes
MemberInfo add = new MemberInfo(names[i], gradYear, true);
if (names[i].compareTo("Aidan") == 0) {
add = new MemberInfo(names[i], gradYear, false);
}
memberList.add(add);
}
}
public ArrayList<MemberInfo> removeMembers(int year) {
//b
for (int i=memberList.size()-1; i>=0; i--) {
if (memberList.get(i).getGradYear() <= year) {
if (memberList.get(i).inGoodStanding()) {
MemberInfo print = new MemberInfo(memberList.get(i).getName(), memberList.get(i).getGradYear(), true);
filler.add(print);
}
memberList.remove(i);
}
}
return filler;
}
// extra get method
public ArrayList<MemberInfo> getMemberList() {
return this.memberList;
}
public static void main(String[] args) {
ClubMembers test = new ClubMembers();
String[] names24 = {"Aidan", "William", "Lily", "Serafina"};
test.addMembers(names24, 2024);
String[] names25 = {"and", "people", "who", "graduates", "in", "2025"};
test.addMembers(names25, 2025);
ArrayList<MemberInfo> enrolled = test.getMemberList();
System.out.print("Currently enrolled: ");
for (MemberInfo member: enrolled) {
System.out.print(member.getName() + " ");
}
System.out.println();
ArrayList<MemberInfo> graduated = test.removeMembers(2024);
System.out.print("Graduated with good standing (Aidan didn't): ");
for (MemberInfo member: graduated) {
System.out.print(member.getName() + " ");
}
System.out.println();
enrolled = test.getMemberList();
System.out.print("Currently enrolled: ");
for (MemberInfo member: enrolled) {
System.out.print(member.getName() + " ");
}
}
}
ClubMembers.main(null);
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) {
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 total = 0;
for (int r = 0; r < array2D.length; r++) {
if (isNonZeroRow(array2D,r)) {
total++;
}
}
return total;
}
/** 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) {
int numOfNonZeroRows = numNonZeroRows(array2D);
int[][] resized = new int[numOfNonZeroRows][array2D[0].length];
int count = 0;
int len = 0;
while (len < array2D.length) {
if (isNonZeroRow(array2D,i)) {
resized[count] = array2D[i];
count++;
}
len++;
}
}
}
Issues
- forgot to change i to len when editing code
- forgot to put return statement for resize
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) {
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 total = 0;
for (int r = 0; r < array2D.length; r++) {
if (isNonZeroRow(array2D,r)) {
total++;
}
}
return total;
}
/** 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) {
int numOfNonZeroRows = numNonZeroRows(array2D);
int[][] resized = new int[numOfNonZeroRows][array2D[0].length];
int count = 0;
int len = 0;
while (len < array2D.length) {
if (isNonZeroRow(array2D,len)) {
resized[count] = array2D[len];
count++;
}
len++;
}
return resized;
}
// tester
public static void main() {
int[][] arr = {{2,1,0},
{1,3,2},
{0,0},
{4,5,6}};
System.out.println("Number of non zero rows = " + ArrayResizer.numNonZeroRows(arr));
int[][] smaller = ArrayResizer.resize(arr);
System.out.println(Arrays.deepToString(smaller));
}
}
ArrayResizer.main();