Author Archives: Mr. M

SUPA – Web 2022-23

Quarter 4 project

 

Prior iterations : https://mrmonline.org/supa-2020/ , https://mrmonline.org/web-design-programming-18-19/

 

Templates

Bootstrap

Vim 

SeatingChart Java OOP Project – 2D Arrays

Let’s write a program for something like PowerSchool that helps organize a seating chart for students.  There is a tester file at the page’s bottom.

First class : Student   , from https://mrmonline.org/student-class/

Class SeatingChart

This class mainly manages a 2-d  array of student objects. Note: an empty seat in the chart will be designated by a null  value.

private Student[][] chart ; // rows and columns for where kids are
private String rosterName; //name of this roster
private String teacherName; 
private int period ; //which period is this chart associated 
public static final int DEFAULT_ROWS = 10;
public static final int DEFAULT_COLS = 10;

Constructor(s)

  • public SeatingChart(int per, String teacher, String _rosterName)  Initializes parameters to respective arguments and creates a 2-d array  of null  objects with default dimensions DEFAULT_ROWS , DEFAULT_COLS
  • public SeatingChart(int per, String teacher, String _rosterName, Student[][] table)

Methods:

public String getTeacherName() // returns teacherName

public String getRosterrName() // returns  rosterName

public int getPeriod()  // returns period

public int countEmptySeats()  ; //returns the number of empty seats. As noted earlier, an empty seat is represented by a null  value in the 2-d array of student s

Student[][] getStudentChart()  @returns chart

public int[] indexOf(Student stu);  returns a 2 element array representing the row and column position of stu in chart ; [-1 , -1] should be returned if stu is not in chart

public boolean setSeat(int row, int col, Student stu) –  places Student Stu   at that row  and column .  This can only be done if there is no student in that location. If a student already inhabits that row and column then the method should return false  and not make any changes to chart .

public void swap(Student s1, Student s2)  // this swaps the locations of s1  and s2

public Student getStudent(int row, int col)  returns the student at the given row  and col ; or null  if the seat is empty

public void removeStudentAt(int row, int col)   sets value of object at row  and col  to null 

public boolean equals(SeatingChart other)  // returns true  if all aspects of other are equivalent to self.

public void colMajorForm()  . This prints out the student chart in column major form.   Examine the diagram below showing an example seating chart on the left and the printed output on the right.seating chart col major

public String toString();  //follow the conventions we have been using. When you print out the chart , let’s show “E!” t o indicate an empty seat; for seats with students, just display their name.SeatingChart to string v2

public Student oldest()  // returns the student object with the greatest age

Extra credit

public void randomize()  // this randomizes the chart. Please be prepared to meet with me to discuss your code. (You cannot use a library to do all the work, if such a library even exists)

 

SeatingChartTesterv2.java :

[pastacode lang=”cpp” manual=”import%20java.util.Arrays%3B%0A%0Apublic%20class%20SeatingChartTesterv4%0A%7B%0A%20%20%20%20public%20SeatingChartTesterv4()%0A%20%20%20%20%7B%0A%20%20%20%20p(%22v4%2C%20null%20constructor%20checks%22)%3B%0A%20%20%20%20Student%20amelia%20%3D%20new%20Student(%22Amelia%22%2C%2017)%3B%0A%20%20%20%20Student%20antonioooo%20%3D%20new%20Student(%22antonioooo%22%2C%2014)%3B%0A%0A%20%20%20%20SeatingChart%20period3%20%3D%20new%20SeatingChart(3%2C%20%22Mr.%20M%22%2C%20%22A%20Days%22)%3B%0A%0A%20%20%20%20%0A%20%20%20%20int%20correct%20%3D%200%3B%0A%20%20%20%20int%20wrong%20%20%3D%200%3B%0A%0A%20%20%20%20boolean%20didNotInit%20%3D%20false%3B%0A%20%20%20%20%0A%20%20%20%20%0A%20%20%20%20if(period3.getTeacherName()%20%3D%3D%20null)%0A%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20p(%22*non*%20default%20constructordid%20not%20initialize%20teacherName%22)%3B%0A%20%20%20%20%20%20%20%20wrong%2B%2B%3B%0A%20%20%20%20%20%20%20%20didNotInit%20%3D%20true%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20else%0A%20%20%20%20%20correct%2B%2B%3B%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20if(period3.getRosterrName()%20%3D%3D%20null)%0A%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20p(%22*non*%20default%20constructor%20did%20not%20initialize%20rosterName%22)%3B%0A%20%20%20%20%20%20%20%20wrong%2B%2B%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20didNotInit%20%3D%20true%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20else%0A%20%20%20%20%20correct%2B%2B%3B%0A%20%20%20%20%20%20%20if(period3.getStudentChart()%20%3D%3D%20null)%0A%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20p(%22*non*%20default%20constructor%20did%20not%20initialize%20chart%22)%3B%0A%20%20%20%20%20%20%20%20wrong%2B%2B%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20didNotInit%20%3D%20true%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20else%0A%20%20%20%20%20correct%2B%2B%3B%0A%20%20%20%20%20%0A%20%20%20%20%20if%20(%20didNotInit%20%3D%3D%20true)%0A%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20throw%20new%20Error(%22You%20must%20fix%20your%20constructor%20(non%20default)%20before%20doin%20anything%20else%20%5Cn%5Ct%20exiting%20tester%22)%3B%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%7D%0A%20%20%20%20%20%2F%2F%20now%20let’s%20do%20the%20same%20for%20default%20%20constructor%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2FSeatingChart(int%20per%2C%20String%20teacher%2C%20String%20_rosterName%0A%20%20%20%20%20SeatingChart%20dfault%20%3D%20new%20SeatingChart(%202%2C%20%22Mr.%20Lorensax%22%2C%20%22periods%202%20%22)%3B%0A%20%20%20%20%20%0A%20%20%20%20%20if(dfault.getRosterrName()%20%3D%3D%20null)%0A%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20p(%22%20default%20constructor%20did%20not%20initialize%20rosterName%22)%3B%0A%20%20%20%20%20%20%20%20wrong%2B%2B%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20didNotInit%20%3D%20true%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20else%0A%20%20%20%20%20correct%2B%2B%3B%0A%20%20%20%20%20%20%20if(dfault.getStudentChart()%20%3D%3D%20null)%0A%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20p(%22%20default%20constructor%20did%20not%20initialize%20chart%22)%3B%0A%20%20%20%20%20%20%20%20wrong%2B%2B%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20didNotInit%20%3D%20true%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20else%0A%20%20%20%20%20correct%2B%2B%3B%0A%20%20%20%20%20%0A%20%20%20%20%20if%20(%20didNotInit%20%3D%3D%20true)%0A%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20throw%20new%20Error(%22You%20must%20fix%20your%20constructor%20(%20%20default)%20before%20doin%20anything%20else%20%5Cn%5Ct%20exiting%20tester%22)%3B%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%7D%0A%20%20%20%20%20%2F%2Fend%20%20non%20default%20constct%20test%0A%20%20%20%20%20%0A%20%20%20%20%20%0A%20%20%20%20Student%5B%5D%5B%5D%20currentChart%20%3D%20dfault.getStudentChart()%3B%0A%20%20%20%20if(currentChart%5B0%5D.length%20%3D%3D%20SeatingChart.DEFAULT_COLS%20%26%26%20currentChart.length%20%3D%3D%20SeatingChart.DEFAULT_ROWS)%0A%20%20%20%20%20%20%20%20correct%2B%2B%3B%0A%20%20%20%20else%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20p(%22*default%20constructor’s%202-d%20Array%20incorrect%20dimensions%22)%3B%0A%20%20%20%20%20%20%20%20wrong%2B%2B%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20boolean%20result%20%3D%20period3.setSeat(5%2C6%2C%20amelia%20)%3B%0A%0A%20%20%20%20if(result)%0A%20%20%20%20%20%20%20%20correct%2B%2B%3B%0A%20%20%20%20else%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20p(%22setSeat()%20return%20value%20error%22)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20Student%5B%5D%5B%5D%20chartNow%20%3D%20period3.getStudentChart()%3B%0A%20%20%20%20if(chartNow%5B5%5D%5B6%5D%20%3D%3D%20amelia)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20correct%2B%2B%3B%0A%0A%20%20%20%20%20%20%20%20if(%20period3.getStudent(5%2C6)%20%3D%3D%20amelia)%0A%20%20%20%20%20%20%20%20%20%20%20%20correct%2B%2B%3B%0A%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20p(%22Error%2C%20looks%20like%20getStudent()%20%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%7D%20%20%20%20%0A%20%20%20%20else%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20p(%22A)%20erorr%2C%20probably%20from%20setSeat()%20or%20getStudentChart()%20%22)%3B%0A%20%20%20%20%20%20%20%20wrong%2B%2B%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%0A%0A%20%20%20%20int%5B%5D%20locaxn%20%3D%20period3.indexOf(amelia)%3B%0A%20%20%20%20if(locaxn%5B0%5D%3D%3D5%20%26%26%20locaxn%5B1%5D%20%3D%3D%206)%0A%20%20%20%20%20%20%20%20correct%2B%2B%3B%0A%20%20%20%20else%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20p(%22B)%20setSeat()%20or%20indexOf()%20error%22)%3B%0A%20%20%20%20%20%20%20%20wrong%2B%2B%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20result%20%3Dperiod3.setSeat(5%2C6%2C%20antonioooo%20)%3B%0A%0A%20%20%20%20if(result%3D%3D%20false)%0A%20%20%20%20%20%20%20%20correct%2B%2B%3B%0A%20%20%20%20else%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20p(%22C)%20setSeat()%20return%20value%20error%22)%3B%0A%20%20%20%20%20%20%20%20wrong%2B%2B%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20String%20st%20%3D%20period3.toString()%3B%0A%20%20%20%20%0A%20%20%20%2F%2F%20System.out.println(st)%20%3B%0A%0A%20%20%20%20if(st.indexOf(%22SeatingChart%22)%20%3D%3D%200)%0A%20%20%20%20%20%20%20%20correct%2B%2B%3B%0A%20%20%20%20else%7B%0A%20%20%20%20%20%20%20%20p(%22toString()%20error.%20ClassName%20should%20be%20first%20characters%22)%3B%0A%20%20%20%20%20%20%20%20wrong%2B%2B%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20if(st.contains(%22Mr.%20M%22))%0A%20%20%20%20%20%20%20%20correct%2B%2B%3B%0A%20%20%20%20else%7B%0A%20%20%20%20%20%20%20%20p(%22toString()%20error.%20%22)%3B%0A%20%20%20%20%20%20%20%20wrong%2B%2B%3B%0A%20%20%20%20%7D%0A%20%20%20%0A%20%20%20%20if(st.contains(%22A%20Days%22))%0A%20%20%20%20%20%20%20%20correct%2B%2B%3B%0A%20%20%20%20else%7B%0A%20%20%20%20%20%20%20%20p(%22toString()%20error.%20%22)%3B%0A%20%20%20%20%20%20%20%20wrong%2B%2B%3B%0A%20%20%20%20%7D%0A%2F%2Fnew%20seating%20chart%0AStudent%20tim%20%3D%20new%20Student(%22tim%22)%3B%0AStudent%5B%5D%5B%5D%20%20someKids%3D%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%20new%20Student(%22giorgio%22)%2Cnull%2C%20%20tim%20%2C%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%20new%20Student(%22michael%22)%2C%20new%20Student(%22joe%22%2C%2012)%20%2C%20null%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%20null%2C%20new%20Student(%22Alex%22)%2C%20new%20Student(%22Eliam%22%2C%20999)%2C%20null%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%20new%20Student(%22Jen%22)%2Cnull%2C%20%20new%20Student(%22antonio%22%2C%205)%2C%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%3B%0A%0A%20%20%20%20SeatingChart%20period4%20%3D%20new%20%20SeatingChart(4%2C%20%22MR%20M%22%2C%20%22p4%22%2C%20someKids)%3B%0A%2F%2FSystem.out.println(%20period4%20)%3B%0A%2F%2Fmake%20sure%20that%20someKids%20was%20actually%20set%20to%20the%20chart%0A%20if(period4.getStudentChart()%20%3D%3D%20null)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20wrong%2B%2B%3B%0A%20%20%20%20%20%20%20%20p(%22Non%20default%20constructor’s%20chart%20not%20initialized%22)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20else%0A%20%20%20%20correct%2B%2B%3B%0A%20%20%20%20%0A%20%20%20%20%0AStudent%5B%5D%5B%5D%20p4Chart%20%3D%20period4.getStudentChart()%3B%0Aif(%20Arrays.equals(p4Chart%20%2C%20someKids))%0A%20%20%20%20correct%2B%2B%3B%0Aelse%0A%20%20%20%20%7B%0A%20%20%20%20wrong%2B%2B%3B%0A%20%20%20%20p(%22Error%20.%20Looks%20like%20you%20did%20not%20correctly%20instantiate%202-d%20array%20in%20constructor%20%22)%3B%0A%20%20%20%20%7D%0A%0A%0A%0A%0A%20%20%20%20boolean%20isCorrect%20%3D%20true%3B%0AStudent%20oldest%20%20%3D%20null%20%3B%0A%20%20%20%20try%7B%0A%20%20%20%20oldest%20%3D%20period4.oldest()%3B%0A%20%20%20%20%7D%0A%20%20%20%20catch(NullPointerException%20e)%7B%0A%20%20%20%20%20%20%20%20isCorrect%20%3D%20false%3B%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20p(%22Error%20%40%20oldest()%2C%20you%20cannot%20call%20.getAge()%20on%20a%20null%20object%22)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%0Aif(isCorrect%20%26%26%20oldest%20!%3D%20null%20%26%26%20oldest.getAge()%20%3D%3D999)%0Acorrect%2B%2B%3B%0Aelse%0A%20%20%20%20%7B%0A%20%20%20%20wrong%2B%2B%3B%0A%20%20%20%20p(%22Error%20%40%20oldest()%22)%3B%0A%20%20%20%20%7D%0A%20%20%20%20locaxn%20%3D%20period4.indexOf(tim)%3B%0A%20%20%20%20%0A%0A%0A%0A%20%20%20%20if(locaxn%5B0%5D%3D%3D0%20%26%26%20locaxn%5B1%5D%20%3D%3D%202)%0A%20%20%20%20%20%20%20%20correct%2B%2B%3B%0A%20%20%20%20else%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20p(%22A%20incorrect%20student.%20Could%20be%20constructor%22)%3B%0A%20%20%20%20%20%20%20%20wrong%2B%2B%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%0Aif%20(%20period4.getStudent(0%2C2)%20%3D%3D%20tim%20)%0A%20%20%20%20%20%20%20%20correct%2B%2B%3B%0A%20%20%20%20else%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20p(%22B%20incorrect%20student.%20Could%20be%20constructor%2C%20getStudent()%22)%3B%0A%20%20%20%20%20%20%20%20wrong%2B%2B%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%2F%2F%2F%0A%0A%0Aint%20emptySeats%20%3D%20period4.countEmptySeats()%3B%0A%0A%0A%0Aif(emptySeats%20%3D%3D%205)%0A%20%20%20%20%20%20%20%20correct%2B%2B%3B%0A%20%20%20%20else%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20p(%22countEmptySeats()%20error%22)%3B%0A%20%20%20%20%20%20%20%20wrong%2B%2B%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%0AStudent%20vinnie%20%3D%20new%20Student(%22Vinnie%22%2C%2016)%3B%0AStudent%20yuya%20%3D%20new%20Student(%22Yuya%22%2C%2018)%3B%0AStudent%5B%5D%5B%5D%20%20p5Kids%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%20yuya%2C%20vinnie%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%3B%0A%0ASeatingChart%20period5%20%3D%20new%20%20SeatingChart(4%2C%20%22MR%20M%22%2C%20%22p4%22%2C%20p5Kids)%3B%0A%0Aint%5B%5D%20%20yuyaLocation_befo%20%3D%20period5.indexOf(yuya)%3B%0Aint%5B%5D%20%20vinniaLocation_befor%20%3D%20period5.indexOf(vinnie)%3B%0A%0A%0A%0Aperiod5.swap(vinnie%2Cyuya)%20%3B%0Aint%5B%5D%20%20yuyaLocation_now%20%3D%20period5.indexOf(yuya)%3B%0Aint%5B%5D%20%20vinniaLocation_now%20%3D%20period5.indexOf(vinnie)%3B%0A%0Aif(%20yuyaLocation_now%5B0%5D%20%3D%3D%20vinniaLocation_befor%5B0%5D%20%26%26%20%20yuyaLocation_now%5B1%5D%20%3D%3D%20vinniaLocation_befor%5B1%5D%20%20)%0A%20%20%20%20%20%20%20%20correct%2B%2B%3B%0A%20%20%20%20else%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20p(%22Error%20Swap%22)%3B%0A%20%20%20%20%20%20%20%20wrong%2B%2B%3B%0A%20%20%20%20%20%20%20%20%7D%0Aif(%20vinniaLocation_now%5B0%5D%20%3D%3D%20yuyaLocation_befo%5B0%5D%20%26%26%20%20vinniaLocation_now%5B1%5D%20%3D%3D%20yuyaLocation_befo%5B1%5D%20%20)%0A%20%20%20%20%20%20%20%20correct%2B%2B%3B%0A%20%20%20%20else%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20p(%22Error%20Swap%22)%3B%0A%20%20%20%20%20%20%20%20wrong%2B%2B%3B%0A%20%20%20%20%20%20%20%20%7D%0A%2F%2F%2F%2F%0A%2F%2F%0A%0A%0AStudent%20rany%20%3D%20new%20Student(%22rany%22%2C%2016)%3B%0AStudent%5B%5D%5B%5D%20%20p6Kids%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%20rany%2C%20null%2Cnull%2Cnull%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%20null%2C%20null%2Cnull%2Cnull%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%3B%0A%0ASeatingChart%20period6%20%3D%20new%20%20SeatingChart(4%2C%20%22MR%20M%22%2C%20%22p4%22%2C%20p6Kids%20)%3B%0Aperiod6.removeStudentAt(0%2C0)%3B%0Aif(%20period6.getStudent(0%2C0)%20%3D%3D%20null%20)%0A%20%20%20%20%20%20%20%20correct%2B%2B%3B%0A%20%20%20%20else%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20p(%22Error%20%40%20removeStudentAt()%20%22)%3B%0A%20%20%20%20%20%20%20%20wrong%2B%2B%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0Aif(%20period5.equals(period6)%20%3D%3D%20false)%0A%20%20%20%20%20%20%20%20correct%2B%2B%3B%0A%20%20%20%20else%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20p(%22A%20Error%20%40%20equals()%20%22)%3B%0A%20%20%20%20%20%20%20%20wrong%2B%2B%3B%0A%20%20%20%20%20%20%20%20%7D%0Aif(%20period5.equals(period5)%20)%0A%20%20%20%20%20%20%20%20correct%2B%2B%3B%0A%20%20%20%20else%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20p(%22B%20Error%20%40%20equals()%20%22)%3B%0A%20%20%20%20%20%20%20%20wrong%2B%2B%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0ASeatingChart%20period6Clone%20%3D%20new%20%20SeatingChart(4%2C%20%22MR%20M%22%2C%20%22p4%22%2C%20p6Kids%20)%3B%0A%0Aif(%20period6.equals(period6Clone)%20)%0A%20%20%20%20%20%20%20%20correct%2B%2B%3B%0A%20%20%20%20else%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20p(%22C%20Error%20%40%20equals()%20%22)%3B%0A%20%20%20%20%20%20%20%20wrong%2B%2B%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%0A%0A%0Ap(%22———————————-%22)%3B%0Ap(%22Correct%20%3A%20%22%20%2B%20correct)%3B%0Ap(%22Wrong%20%3A%20%22%20%2B%20wrong)%3B%0A%0A%0A%20%20%20%20%7D%2F%2Fend%20of%20constructor%0A%0Avoid%20p(String%20x)%20%7B%20System.out.println(x)%3B%20%7D%0Avoid%20p(int%20x)%20%7B%20System.out.println(x)%3B%20%7D%0Avoid%20p(boolean%20x)%20%7B%20System.out.println(x)%3B%20%7D%0Avoid%20p(double%20x)%20%7B%20System.out.println(x)%3B%20%7D%0Avoid%20p2d(int%5B%5D%20x)%20%7B%20System.out.println(x%5B0%5D%20%2B%20%22%2C%20%20%22%20%2B%20x%5B1%5D)%3B%20%7D%0A%0A%7D%0A” message=”SeatingChartTester” highlight=”3″ provider=”manual”/]

Array Fun 2 [2023]

Complete all the methods below. When you are done, copy and paste the    testmethods.v5.1  into your class and run it to see if you have any obvious errors.  The above code does not guarantee you a 100 but will help catch most of the common errors that students make.

Note: You may not use any external libraries (like Java.Arrays etc )or import any code. Everything can be done with just loops and variables.

int sumEveryN(int[] nums, int n)

Description:This method returns the sum of every n  elements of nums ..

Method Call return value/output
sumEveryN( {1 , 2 , 3 , 4 }, 2 ) 4( ie 1 +3)
sumEveryN( {13 , 42, 15, 33 , 44 , 16 , 52} ,3) 98 ( ie 13 + 33+ 52)

String[] doubleArr(String[] strs)

Description: This method returns a new version of strs  in which each element now appears twice. This can be done with a for-each loop, which I believe is easier and more intuitive.

Method Call return value/output
doubleArr( {“a”,”b”,”c”} ) {“a”,”a”,”b”,”b”, “c” , “c”}
doubleArr( {“math”,”ware”,”house”,”.com” }) {“math”,”math”,”ware”,”ware”,”house”,”house”,”.com”, “.com”}

int  indexOf5(int[] nums )

Description:This returns the index of the first occurrence element 5  or -1 if 5 does not appear anywhere in the array.

Method Call return value/output
indexOf5( { 2 , 3 , 5 , 4 } ) 2
indexOf5( { 2 , 3 , 5 , 4, 5  } ) 2
indexOf5( { 2 , 3 ,7  , 4, 3,   } ) -1

More Sample calls and return vals

screenshot.41


int indexOf(int[] nums, int num)

Description: This method returns the index value of the first appearance of num  or -1 if num  is not an element of nums  .

Method Call return value/output
indexOf( {6,4 ,7,3, 4 }, 4) 1
indexOf( {6,4 7 ,3,2,7}, 7) 2
indexOf( {6,4 ,2,3}, 22) -1

 


int[] randos(int start, int end, int howMany)

Description:  This method returns an array of random numbers between [start,end] . Note make sure that each element in the new array attempts to make a new random int.  Use Math.random() , do not use any other mechanism for finding a random number. 

 randos_loop
 More example calls and returns :
randos2

double meanBetween(int[] nums, int min, int max)

Description:  This method returns the mean of nums  ; however, this method only counts values within the range (min,max) as shown in the examples below:

screenshot.12
(Not inclusive, so do not count min  or max  )

int secondSmallest(int[] nums )

Description: This method returns the element of nums  with the second smallest value.

Note: You may not modify the input array. For instance, you may not put nums in order,  which would be bad because you were not asked to modify the array.

@precondition: nums.length >= 2

Note: You will lose credit if you use a constant to represent the smallest or second smallest number. See pseudocode 

Method Call return value/output
secondSmallest( { 2 , 18 , 22, 4 , 6 } ) 4
secondSmallest( { 3 , 7 , 15 , 1 ,101} ) 3

boolean isPalindromic(int[] nums)

Description:This method returns true if the elements of nums  are a palindrome. 

Method Call return value/output
isPalindromic(( { 5 , 2, 7 , 2 , 5} ) true
isPalindromic( { 5 , 2, 7 , 3 , 5} )) false
isPalindromic(( { 1 , 2, 1} ) true

Old versions:

Array Fun 1 

Array F un 2 (v 1)

 

Array Fun 3 (resizing included)

String 1 and Array 1 Exercises

Exercises for String 1 and Array 1 (no loops)

When you are done, you can copy and past the score() method below into your class. It will test some (but, by no means all) of the things that your code should do

Create a class called StrArr1  and add each method below into it.

screenshot.1

 

 

For the absVals()  method below use the Math.abs()  Java method.

screenshot.3

screenshot.1

 

screenshot.4

For the randos1to10()   method below use the Math.random()  Java method. Create a new array to store random integers. Each integer should be [1,10]

screenshot.5