Category Archives: Computer Science

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.

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 :

import java.util.Arrays;

public class SeatingChartTesterv4
{
    public SeatingChartTesterv4()
    {
    p("v4, null constructor checks");
    Student amelia = new Student("Amelia", 17);
    Student antonioooo = new Student("antonioooo", 14);

    SeatingChart period3 = new SeatingChart(3, "Mr. M", "A Days");

    
    int correct = 0;
    int wrong  = 0;

    boolean didNotInit = false;
    
    
    if(period3.getTeacherName() == null)
     {
        p("*non* default constructordid not initialize teacherName");
        wrong++;
        didNotInit = true;
        }
    else
     correct++;
        
    if(period3.getRosterrName() == null)
     {
        p("*non* default constructor did not initialize rosterName");
        wrong++;
                didNotInit = true;
        }
    else
     correct++;
       if(period3.getStudentChart() == null)
     {
        p("*non* default constructor did not initialize chart");
        wrong++;
                didNotInit = true;
        }
    else
     correct++;
     
     if ( didNotInit == true)
     {
        throw new Error("You must fix your constructor (non default) before doin anything else \n\t exiting tester");
         
         
     }
     // now let's do the same for default  constructor
                     //SeatingChart(int per, String teacher, String _rosterName
     SeatingChart dfault = new SeatingChart( 2, "Mr. Lorensax", "periods 2 ");
     
     if(dfault.getRosterrName() == null)
     {
        p(" default constructor did not initialize rosterName");
        wrong++;
                didNotInit = true;
        }
    else
     correct++;
       if(dfault.getStudentChart() == null)
     {
        p(" default constructor did not initialize chart");
        wrong++;
                didNotInit = true;
        }
    else
     correct++;
     
     if ( didNotInit == true)
     {
        throw new Error("You must fix your constructor (  default) before doin anything else \n\t exiting tester");
         
         
     }
     //end  non default constct test
     
     
    Student[][] currentChart = dfault.getStudentChart();
    if(currentChart[0].length == SeatingChart.DEFAULT_COLS && currentChart.length == SeatingChart.DEFAULT_ROWS)
        correct++;
    else
        {
        p("*default constructor's 2-d Array incorrect dimensions");
        wrong++;
        }

    boolean result = period3.setSeat(5,6, amelia );

    if(result)
        correct++;
    else
        {
        p("setSeat() return value error");
        }

    Student[][] chartNow = period3.getStudentChart();
    if(chartNow[5][6] == amelia)
    {
        correct++;

        if( period3.getStudent(5,6) == amelia)
            correct++;
        else
            {
            p("Error, looks like getStudent() ");
            }

    }    
    else
        {
        p("A) erorr, probably from setSeat() or getStudentChart() ");
        wrong++;
        }



    int[] locaxn = period3.indexOf(amelia);
    if(locaxn[0]==5 && locaxn[1] == 6)
        correct++;
    else
        {
        p("B) setSeat() or indexOf() error");
        wrong++;
        }
    result =period3.setSeat(5,6, antonioooo );

    if(result== false)
        correct++;
    else
        {
        p("C) setSeat() return value error");
        wrong++;
        }

    String st = period3.toString();
    
   // System.out.println(st) ;

    if(st.indexOf("SeatingChart") == 0)
        correct++;
    else{
        p("toString() error. ClassName should be first characters");
        wrong++;
    }

    if(st.contains("Mr. M"))
        correct++;
    else{
        p("toString() error. ");
        wrong++;
    }
   
    if(st.contains("A Days"))
        correct++;
    else{
        p("toString() error. ");
        wrong++;
    }
//new seating chart
Student tim = new Student("tim");
Student[][]  someKids= {
                    { new Student("giorgio"),null,  tim , },
                    { new Student("michael"), new Student("joe", 12) , null },
                    { null, new Student("Alex"), new Student("Eliam", 999), null},
                    { new Student("Jen"),null,  new Student("antonio", 5),  },
                        };

    SeatingChart period4 = new  SeatingChart(4, "MR M", "p4", someKids);
//System.out.println( period4 );
//make sure that someKids was actually set to the chart
 if(period4.getStudentChart() == null)
        {
        wrong++;
        p("Non default constructor's chart not initialized");
        }
    else
    correct++;
    
    
Student[][] p4Chart = period4.getStudentChart();
if( Arrays.equals(p4Chart , someKids))
    correct++;
else
    {
    wrong++;
    p("Error . Looks like you did not correctly instantiate 2-d array in constructor ");
    }




    boolean isCorrect = true;
Student oldest  = null ;
    try{
    oldest = period4.oldest();
    }
    catch(NullPointerException e){
        isCorrect = false;    
            p("Error @ oldest(), you cannot call .getAge() on a null object");
        }
   
if(isCorrect && oldest != null && oldest.getAge() ==999)
correct++;
else
    {
    wrong++;
    p("Error @ oldest()");
    }
    locaxn = period4.indexOf(tim);
    



    if(locaxn[0]==0 && locaxn[1] == 2)
        correct++;
    else
        {
        p("A incorrect student. Could be constructor");
        wrong++;
        }
       
if ( period4.getStudent(0,2) == tim )
        correct++;
    else
        {
        p("B incorrect student. Could be constructor, getStudent()");
        wrong++;
        }
    ///


int emptySeats = period4.countEmptySeats();



if(emptySeats == 5)
        correct++;
    else
        {
        p("countEmptySeats() error");
        wrong++;
        }


Student vinnie = new Student("Vinnie", 16);
Student yuya = new Student("Yuya", 18);
Student[][]  p5Kids = {
                        { yuya, vinnie}
                        };

SeatingChart period5 = new  SeatingChart(4, "MR M", "p4", p5Kids);

int[]  yuyaLocation_befo = period5.indexOf(yuya);
int[]  vinniaLocation_befor = period5.indexOf(vinnie);



period5.swap(vinnie,yuya) ;
int[]  yuyaLocation_now = period5.indexOf(yuya);
int[]  vinniaLocation_now = period5.indexOf(vinnie);

if( yuyaLocation_now[0] == vinniaLocation_befor[0] &&  yuyaLocation_now[1] == vinniaLocation_befor[1]  )
        correct++;
    else
        {
        p("Error Swap");
        wrong++;
        }
if( vinniaLocation_now[0] == yuyaLocation_befo[0] &&  vinniaLocation_now[1] == yuyaLocation_befo[1]  )
        correct++;
    else
        {
        p("Error Swap");
        wrong++;
        }
////
//


Student rany = new Student("rany", 16);
Student[][]  p6Kids = {
                        { rany, null,null,null},
                        { null, null,null,null}
                        };

SeatingChart period6 = new  SeatingChart(4, "MR M", "p4", p6Kids );
period6.removeStudentAt(0,0);
if( period6.getStudent(0,0) == null )
        correct++;
    else
        {
        p("Error @ removeStudentAt() ");
        wrong++;
        }

if( period5.equals(period6) == false)
        correct++;
    else
        {
        p("A Error @ equals() ");
        wrong++;
        }
if( period5.equals(period5) )
        correct++;
    else
        {
        p("B Error @ equals() ");
        wrong++;
        }

SeatingChart period6Clone = new  SeatingChart(4, "MR M", "p4", p6Kids );

if( period6.equals(period6Clone) )
        correct++;
    else
        {
        p("C Error @ equals() ");
        wrong++;
        }




p("----------------------------------");
p("Correct : " + correct);
p("Wrong : " + wrong);


    }//end of constructor

void p(String x) { System.out.println(x); }
void p(int x) { System.out.println(x); }
void p(boolean x) { System.out.println(x); }
void p(double x) { System.out.println(x); }
void p2d(int[] x) { System.out.println(x[0] + ",  " + x[1]); }

}
SeatingChartTester

So, you want to Skip CS II

It is very rare that I recommend anyone skip from the Intro class, the easiest CS class,  to SUPA, the hardest CS class , without the foundation that we build in Computer Science II.

The path to skipping

If you  really want to try to skip CS II, the path would be to complete the online component of CS II curriculum during the school year and to then sit down with me for an interview. If I am satisfied that you are ready to skip CS II, I will then make the recommendation.

Much, though not all, of the CS II curriculum is online and so can be completed at a student’s convenience.

Please know, that most of the students who have followed this path and who found the Intro to CS class too easy and who then skipped into SUPA have ended up regretting this and, more often than not, they have had to drop the SUPA class.

Ask yourself– Would you skip Calculus I and instead jump right to Calculus II?

Probably not.

And it’s not much different here;  while it has been done,  for the most part it has only been successfully pulled off by students who are “hardcore” programmers and who have voluminous experience coding outside the classroom. Kids who have made their own apps and learned how to do real programming on their own.

Now, if that describes you and you have made real apps that you can show, then maybe this is the right move, but otherwise, this jump almost never is.

So, what students should do is this – Have your guidance counsellor sign you up for CS II for next year.   If you complete the online  part of the CS II curriculum, by June, then we can sit down and I will have an assessment of your readiness to make this radical jump.

Mini Open Ended Project

Normally we dedicate an entire quarter to a project. We will instead be dedicating about 2/3 of the quarter.

The parameters are similar but not quite so grand in scope. To get an A, you must create a complex web product . This could be a single interactive application or multiple pages . Simple ideas to get to that point include:

  • a multipage form validation product
    • Multiple pages like the login form
      • validate other things – maybe credit cards
  • A to do list that uses jQuery for animation
  • a game that uses Phaser.io ( not trivial,  but fun)
  • Learn php and mysql to create a simple login system. This would only be for those of you who have been able to successfully setup apache, mysql –probably only the windows users.

Ways to add complexity:

  • include loops
  • include arrays
  • include a 3rd party javascript library like jQuery, Vue  etc..
  • I know  jQuery or undescore , if you want something where I can add somequick tidbits when you’re stuck

 

Everybody must use git and have at least 20 commits over the span of the rest of the quarter.

 

Examples of finished product and grades:

  1. Advanced Form Validation
    • credit card validation using luhn algorithm
    • registration page
      • email
      • password
      • confirm password
      • gender
      • etc..
  2. 1-3 calculators. The # should depend on the complexity
  3. 2 or 3 pages that involve form validation
    • Example : https://directpay.irs.gov/directpay/payment?execution=e1s2
    • Maybe not the most fun but closes to what we have done in class
  4. Come up with something that you want to do and make it happen.

Mr Morris, I just want to pass.

  • then, do a multi page layout that makes use of foundation instead of bootstrap. Foundation is a similar css  framework, No javascript needed but grade ceiling will be low.

Quarter 4 Project

Objective: To create a complex, industry level, project that builds off the skills we learned this year. You must learn at least one new technology . Your grade will be based on the level of complexity of your final project. I have listed some examples of technologies to learn and rated their general complexity. Ultimately, complexity will be evaluated by me and you are welcome to ask for feedback on where the complexity of your project will land you.

Projects from last class

Links and Resources for learning Phaser

Note: I am not familiar with many of the technologies below.  The purpose here is for you to learn something, yourself, as web developers are often forced to do in the real world. If you think you will need help, I suggest going with Phaser because most students will do it and you can help each other . I have limited knowledge of Phaser.

Technologies

Phaser ( The most popular 4th quarter project topic is to create phaser games housed in a bootstrap based website). There is a great range in complexity that using Phaser allows for

Example Projects, probable complexity and grade.

The table below lists technologies you can use and provides a general guideline for its complexity and likely grade outcome. If something is classified as an “add-on,” then it is a way to increase complexity but should not, in and of itself, be considered complex enough on its own.

Often the grades are a “range” because it depends on what, ultimately your project does. Take “vue” for instance. This is a very popular, very modern javascript framework so it can be quite complex, but ultimately it depends on what your program actually does that will determine the final grade.

Technology Complexity Complexity lvl Grade
Phaser (meh) Edits one of the pre-built games on phaser.io, nothing much new very low C
Phaser (moderate) Creates one new game, but it is relatively simple (asteroids) and submits 1 page website that uses bootstrap Moderate B-
Phaser (good) Creates completely new complex game (maybe uses physics or other unique coding) moderate-high B+/ A
Node.js/Gulp   Learns how to use nodejs and employs Gulp to improve workflow Moderate add on
 Remote git Learns how to push and pull from remote git repo on github low add-on
 Vue Learns how to use the vue.js framework for javascript development high B /A-
Foundation Learn a bootstrap alternative and create several web pages moderately low C+
Angular Creates a multi page website with the angular .js framework high B/A
Mathjax Learns how to integrate the Mathjax math library for rendering professional looking mathematics low add-on
Vim Consistently edits with Vim . I will quiz you on how to use VIM and I will expect to see you editing with VIM during class moderate add on

List of other utitlites and libraries to use:

 

Notes: these can be combined to add complexity and professionalism. For instance, you can create a multipage website based on Vue and some of the pages can house phaser games that you create. This has the potential of an A+, assuming the Phaser games are original.

Not comfortable with Programming? Here is a multi technology example that will get a B:

This is an example that will get people who are not very comfortable with programming into the “B” range: Learn how to use Foundation, or another bootstrap alternative, and also how to integrate Mathjax to display math equations. You can create a multi page math website full of equations. You could add using remote git or Gulp to get the grade up even a bit more.

Javascript framework comparisons : https://www.codeinwp.com/blog/angular-vs-vue-vs-react/