Labs
Lab 1 and 2 with extra seed.
Grading
public class CheckDigit {
public int getCheck(int num) {
int multiplier = 7;
int sum = 0;
for (int i = 1;i<= this.getNumberOfDigits(num);i++) {
sum += (this.getDigit(num,i))*multiplier;
multiplier--;
}
return this.getDigit(sum, this.getNumberOfDigits(sum));
}
public boolean isValid(int numWithCheckDigit) {
int check = numWithCheckDigit % 10;
int restofnum = numWithCheckDigit/10;
return this.getCheck(restofnum) == check;
}
public int getNumberOfDigits(int num) {
int count = 0;
while(num > 0){
num /= 10;
count++;
}
return count;
}
public int getDigit(int num, int n) {
int digit = 0;
int len = this.getNumberOfDigits(num);
for (int i = 0; i < len - n + 1; i++){
digit = num % 10;
num /= 10;
}
return digit;
}
// Tester method
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);
public class Bus {
private int stops = 1;
private int currentStop = 1;
public Bus(int stops){
this.stops = stops;
this.currentStop = currentStop;
}
public void move() {
if (currentStop < stops) {
currentStop++;
}
else {
--currentStop;
}
}
public int getCurrentStop() {
return currentStop;
}
public void setStops(int stops) {
this.stops = stops;
}
public static void main(String[] args) {
Bus bus1 = new Bus(3);
System.out.println(bus1.getCurrentStop());
bus1.move();
System.out.println(bus1.getCurrentStop());
bus1.move();
System.out.println(bus1.getCurrentStop());
bus1.move();
System.out.println(bus1.getCurrentStop());
bus1.move();
bus1.move();
System.out.println(bus1.getCurrentStop());
System.out.println(bus1.getCurrentStop());
Bus bus2 = new Bus(5);
System.out.println(bus1.getCurrentStop());
System.out.println(bus2.getCurrentStop());
}
}
Bus.main(null);
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 {
private ArrayList<MemberInfo> memberList = new ArrayList<>();
// Add members by name and year
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);
}
}
// Remove members function by year
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;
}
// Tester method
public static void main(String args[]) {
ClubMembers test = new ClubMembers();
String[] setone = new String[]{"Smith", "Steve"};
String[] settwo = new String[]{"Maria", "Braeden", "Andrew"};
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 Item {
private String name;
private int value;
public Item(String itemName, int itemValue) {
name = itemName;
value = itemValue;
}
public String getName() {
return name;
}
public int getValue() {
return value;
}
}
public class ItemGrid {
private Item[][] grid;
// Constructor not shown
public ItemGrid(Item[][] grid) {
this.grid = grid;
}
/** Returns true if xPos is a valid row index and yPos is a valid
* column index and returns false otherwise.
*/
public boolean isValid(int xPos, int yPos) {
// Get the number of rows and columns in the grid
int numRows = grid.length;
int numCols = grid[0].length;
// Check if the given xPos and yPos values are within the grid bounds
if (xPos >= 0 && xPos < numRows && yPos >= 0 && yPos < numCols) {
// The position is valid
return true;
} else {
// The position is invalid
return false;
}
}
/** Compares the item in row r and column c to the items to its
* left and to its right. Returns the name of the item with
* the greatest value, as described in part (a).
* Precondition: r and c are valid indices
*/
public String mostValuableNeighbor(int r, int c) {
int maxValue = grid[r][c].getValue();
String correctReturn = grid[r][c].getName();
if (isValid(r, c-1) && grid[r][c-1].getValue() >= maxValue) {
maxValue = grid[r][c-1].getValue();
correctReturn = grid[r][c-1].getName();
}
if (isValid(r, c+1) && grid[r][c+1].getValue() >= maxValue) {
maxValue = grid[r][c+1].getValue();
correctReturn = grid[r][c+1].getName();
}
return correctReturn;
}
/** Returns the average value of all the items in grid,
* as described in part (b).
*/
public double findAverage() {
double sum = 0;
double count = 0;
for (Item[] row : grid) {
for (Item item : row) {
sum += item.getValue();
count++;
}
}
return sum / count;
}
public static void main(String[] args){
Item[][] igArray = new Item[3][4];
// Initialize the array with new Item objects
igArray[0][0] = new Item("acorn", 7);
igArray[0][1] = new Item("book", 10);
igArray[0][2] = new Item("carrot", 8);
igArray[0][3] = new Item("desk", 9);
igArray[1][0] = new Item("egg", 5);
igArray[1][1] = new Item("flag", 8);
igArray[1][2] = new Item("globe", 8);
igArray[1][3] = new Item("harp", 9);
igArray[2][0] = new Item("island", 7);
igArray[2][1] = new Item("jacket", 19);
igArray[2][2] = new Item("kale", 8);
igArray[2][3] = new Item("lunch", 16);
ItemGrid ig = new ItemGrid(igArray);
System.out.println(ig.mostValuableNeighbor(0, 2));
System.out.println(ig.mostValuableNeighbor(1, 1));
System.out.println(ig.mostValuableNeighbor(2, 0));
System.out.println(ig.mostValuableNeighbor(2, 3));
}
}
ItemGrid.main(null);