Courses‎ > ‎AP Computer Science 2‎ > ‎

konstantinovich

Instructor email:  
konstans@stuy.edu

Office hours: 
Periods 6+7 in room 301.
It helps to let me know you are coming so I am aware when you plan to come because sometimes I am not free.

CS Dojo: 
A place to hone your skills, and practice with the guidance of caring supportive upperclassmen. 
(Will start soon)  Mon-Thur in 307

Tools and References: 
Java Practice: CodingBat
Java Visualizers: <UPDATED>

2018-06-11

posted Jun 10, 2018, 7:43 PM by Konstantinovich Samuel

Final Project Exit Survey: (Required for EVERYONE)

2018-06-08

posted Jun 8, 2018, 6:15 AM by Konstantinovich Samuel   [ updated Jun 8, 2018, 6:36 AM ]

One member of each group fill this out:
https://goo.gl/forms/8ZOBu5CqLey4LEEg2

Final Project Repo Readme:
The repo's readme should include:
  - Basic Description
  - List of features
  - NEW features since your demo. (if demo was before Monday)
  - Instructions on how to compile and run on the terminal. 

 

2018-06-04

posted Jun 3, 2018, 7:02 PM by Konstantinovich Samuel   [ updated Jun 3, 2018, 8:02 PM ]

Maze solvers that:
Printed extra were removed
Printed colors failed. 


Lab05 -> I deleted the data files, so everything failed... except someone that had the data files in their repo (fixed now!)

OSIS TOTAL PASS TOTAL FAIL 01PASS 02PASS 03PASS 04PASS 05PASS 06PASS 07PASS 08PASS 09PASS 010PASS 010FAIL 010#lin 012PASS 012FAIL 012#lin 013PASS 013FAIL 013#lin 014PASS 014FAIL 014#lin
MAX         139 28 27 8 15 7 20 10 10 6 8 6 6 7 3 3 4 6 4 7 15 15 16
MEDIAN         116 7 27 8 15 6 20 10 10 6 8 6 0 7 2 0 4 4 0 7 0 0 1
MEAN         109 8 26 8 13 5 17 8 9 5 6 4 1 5 2 1 3 3 0 5 2 3 6
209314327 139 2 27 8 15 6 20 10 10 6 8 6 0 7 2 1 4 6 0 7 15 0 16
214521304 138 4 27 8 15 7 20 10 10 6 8 6 0 7 3 0 4 6 0 7 12 3 16
213149834 138 3 27 8 14 7 20 10 10 6 8 6 0 7 3 0 4 6 0 7 13 2 16
214269698 136 2 27 8 12 6 20 10 10 6 8 6 0 7 3 0 4 6 0 7 14 1 16
208652602 134 2 27 8 15 7 20 10 10 6 8 6 0 7 3 0 4 0 0 1 14 1 16
211521372 128 13 27 8 15 6 20 10 10 6 8 6 0 7 3 0 4 6 0 7 3 12 16
209180272 128 13 27 8 15 6 20 10 10 6 8 6 0 7 1 2 4 5 1 7 6 9 16
209908870 126 4 27 8 15 7 20 10 10 6 8 0 0 1 3 0 4 0 0 1 12 3 16
209152545 126 16 27 8 15 7 20 10 10 6 8 6 0 7 3 0 4 6 0 7 0 15 16
214700411 126 16 27 8 15 7 20 10 10 6 8 6 0 7 3 0 4 6 0 7 0 15 16
209062025 126 16 27 8 15 7 20 10 10 6 8 6 0 7 3 0 4 6 0 7 0 15 16
219403144 125 16 26 8 15 7 20 10 10 6 8 6 0 7 3 0 4 6 0 7 0 15 16
208709410 125 1 27 8 15 6 20 10 10 6 8 6 0 7 3 0 4 6 0 7 0 0 1
209729425 125 1 26 8 15 7 20 10 10 6 8 6 0 7 3 0 4 6 0 7 0 0 0
214586992 124 2 27 8 15 6 20 10 10 6 7 6 0 7 3 0 4 6 0 7 0 0 0
208974055 123 16 27 8 15 6 20 10 10 6 8 4 0 5 3 0 4 6 0 7 0 15 16
209381243 122 17 26 8 15 6 20 8 10 6 8 6 0 7 3 0 4 6 0 7 0 15 16
208891606 122 4 27 8 15 6 20 10 10 6 8 6 0 7 3 0 4 3 3 7 0 0 0
209725308 122 20 27 8 15 7 20 10 10 6 8 6 0 7 3 0 4 2 4 7 0 15 16
209563600 122 1 27 8 15 7 20 10 10 6 8 6 0 7 3 0 4 2 0 3 0 0 0
236729943 122 19 27 8 15 6 20 10 10 6 8 6 0 7 2 1 4 4 2 7 0 15 16
209343813 120 5 27 8 15 7 20 10 10 6 0 4 0 5 3 0 4 0 0 1 10 5 16
241802008 120 1 26 8 10 7 20 10 10 6 8 6 0 7 3 0 4 6 0 7 0 0 0
207695149 120 1 27 8 13 6 20 10 10 6 8 6 0 7 0 0 1 6 0 7 0 0 0
233536242 120 7 27 8 15 7 19 10 10 6 8 2 4 7 2 1 4 6 0 7 0 0 0
209180512 119 4 27 8 13 3 20 2 10 6 8 4 0 5 0 0 1 6 0 7 12 3 16
208719062 119 17 27 8 15 7 20 10 10 6 8 6 0 7 0 1 2 1 1 3 1 14 16
236929352 118 1 27 8 13 7 20 10 10 6 8 0 0 1 3 0 4 6 0 7 0 0 1
208544494 118 5 27 8 15 6 20 10 10 3 6 6 0 7 1 2 4 6 0 7 0 0 1
208817148 118 1 27 8 12 7 20 10 10 6 8 6 0 7 3 0 4 1 0 2 0 0 0
208979344 117 0 27 8 15 6 20 10 10 6 0 6 0 7 3 0 4 6 0 7 0 0 0
209319474 116 4 27 8 15 6 20 8 10 6 8 6 0 7 0 3 4 2 0 3 0 0 0
215543406 116 8 27 8 15 6 20 10 10 6 8 4 0 5 0 3 4 2 4 7 0 0 1
208824680 116 8 27 8 15 6 20 10 10 6 7 6 0 7 0 0 1 1 1 3 0 5 6
208942532 115 5 27 8 15 6 20 10 10 6 8 4 2 7 1 2 4 0 0 1 0 0 0
208393082 114 7 27 8 7 7 20 10 10 4 8 4 0 5 3 0 4 6 0 7 0 0 0
209167725 114 9 27 8 15 3 19 8 10 6 8 6 0 7 2 1 4 2 4 7 0 0 0
209767730 114 12 27 8 15 5 20 10 10 6 0 6 0 7 2 1 4 1 0 2 4 11 16
209682293 114 1 24 8 15 6 20 10 10 6 8 4 0 5 3 0 4 0 0 1 0 0 1
209262609 111 19 27 8 8 6 14 8 10 6 8 0 6 7 3 0 4 0 0 1 13 2 16
208313379 111 3 26 7 15 0 20 8 10 6 7 4 0 5 2 1 4 6 0 7 0 0 0
214592685 111 28 27 8 12 4 20 10 10 6 8 0 6 7 2 1 4 4 2 7 0 15 16
208992594 110 4 27 8 15 7 15 10 10 6 8 4 0 5 0 3 4 0 0 1 0 0 0
209489475 110 11 27 8 12 3 18 10 10 4 8 6 0 7 0 3 4 4 2 7 0 0 0
209343888 109 7 27 8 15 3 16 10 10 5 8 6 0 7 1 2 4 0 0 1 0 0 0
211671136 108 3 27 8 15 6 20 10 2 6 8 6 0 7 0 0 1 0 2 3 0 0 0
209322718 107 17 27 8 5 7 20 10 10 6 3 0 0 1 0 3 4 0 0 1 11 4 16
215062944 102 12 27 8 15 3 10 10 10 3 7 0 4 5 3 0 4 6 0 7 0 0 0
213463227 98 17 26 8 15 3 20 10 2 0 5 0 2 3 3 0 4 6 0 7 0 0 1
214377137 97 1 27 8 15 0 20 0 10 0 8 6 0 7 3 0 4 0 0 1 0 0 1
207142159 96 12 26 8 15 0 20 10 2 6 8 0 6 7 0 0 1 1 0 2 0 5 6
231851098 95 6 27 8 15 0 19 10 10 6 0 0 0 1 0 0 1 0 1 2 0 0 1
207519570 91 19 26 1 7 6 20 8 10 0 6 6 0 7 0 0 1 1 1 3 0 0 1
208626622 89 2 27 8 15 7 0 0 10 6 8 0 0 1 3 0 4 5 1 7 0 0 0
208356410 87 20 23 8 15 6 12 8 8 6 1 0 0 1 0 1 2 0 0 1 0 0 1
214466500 84 16 27 8 15 6 11 2 10 0 5 0 0 1 0 0 1 0 0 1 0 0 1
231964784 74 1 27 8 12 7 20 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
208781666 72 8 26 0 15 6 0 8 2 1 7 0 2 3 3 0 4 4 2 7 0 0 0
208791855 56 10 23 8 15 0 0 0 0 0 2 0 0 3 2 1 4 6 0 7 0 0 1
209046838 55 9 27 8 10 7 0 0 0 1 2 0 0 1 0 0 0 0 0 0 0 0 1
209028257 54 21 26 8 0 6 0 0 2 1 4 6 0 7 0 0 1 1 0 2 0 0 0
206939092 54 0 25 8 15 6 0 0 0 0 0 0 0 1 0 0 1 0 0 1 0 0 1
208805713 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

2018-05-31

posted May 31, 2018, 5:46 AM by Konstantinovich Samuel   [ updated May 31, 2018, 12:23 PM ]


Stuyvesant is forming a partnership with Google to create a project mentorship program that will being next fall. The program will focus on pairing groups of students (1-4) with Google employees who will help them create a substantial software project. 
There will be an info session for this program TODAY (5/31) at 4pm in room 307. The program will be open to rising Juniors and Seniors.



Rotations:

float x,y,angle;

void setup(){
 size(600,600);
 x = 100;
 y = 250;
 angle = radians(45);
 rectMode(CENTER);
}

void drawShapeAtAngle(float x,float y,float angle){
  
 pushMatrix();
   //change the coordinates for JUST this one object
   translate(x,y);
   rotate(angle);
   rect(0,0,100,100);
 popMatrix();
}


void draw(){
  background(255);
  
  pushMatrix();//backup the original coordinate system
  //Change the coordinates for 
  //all objects then draw everything.
  translate(frameCount % width, frameCount % height);
  
  //Draw all the things you want to draw
  ellipse(0,0,20,20);
  drawShapeAtAngle(100+x,y,angle+radians(frameCount));
  drawShapeAtAngle(100+-x,y,angle+radians(frameCount));
  
  popMatrix();//restore the original coordinate system
}

2018-05-24

posted May 24, 2018, 12:43 PM by Konstantinovich Samuel

Demo Code from class

ArrayList<Mover> movers;
public void setup() {
  size(600, 300);
  movers = new ArrayList<Mover>();
  for (int i = 0; i < 10; i++) {
    movers.add(new Mover(1));
  }
}
public void draw() {
  background(255);
  fill(0);
  text(frameRate, 20, 20);
  for (Mover m : movers) {
    m.display();
    m.update(movers);
  }
}


public class Mover {
  float x, xspeed, xacc;
  float y, yspeed, yacc;
  float r;
  color c;

  public Mover() {
    x = width/2;
    y = height/2;
    r = 50;
    c = color(0, 255, 0);
    xspeed= -2.5;
    yspeed= -1.5;
  }

  public Mover(int a) {
    x = random(width-2*r)+r;
    y = random(height-2*r)+r;
    r = 20+random(20);
    c = color(0, 255, 0);
    xspeed= random(1)-.5;
    yspeed= -3.5;
    yacc = 0.5;
  }

  public void update(ArrayList<Mover> others) {
    //change the position etc.
    x += xspeed; 
    y += yspeed; 
    yspeed += yacc;
    xspeed += xacc;
    //check the rest of the world for interactions
    checkWalls();
    checkOthers(others);
  }
  
  public void checkOthers(ArrayList<Mover> others){
    c = color(0,255, 0);
    for(Mover other : others){
      if(this != other){
       //check for collide
       if(dist(x,y,other.x,other.y) < r + other.r ){
          c = color(255, 0, 0); 
       }
      }
    }
  }
  
  

  public void checkWalls() {
    if (x < r) {
      println("OW!");
      xspeed *= -1;
      x = r;//prevents 2 true in a row
    }
    if (x > width - r) {
      println("OW!");
      xspeed *= -1;
      x = width - r;//prevents 2 true in a row
    }
    if (y < r) {
      println("OW!");
      yspeed *= -.9;
      y = r;//prevents 2 true in a row
    }
    if (y > height - r) {
      println("OW!");
      yspeed *= -.9;
      y = height - r;//prevents 2 true in a row
    }
  }
  public void display() {
    fill(c);
    ellipse(x, y, r * 2, r * 2);
  }
}

2018-05-23

posted May 22, 2018, 4:37 PM by Konstantinovich Samuel   [ updated May 22, 2018, 6:48 PM ]

For Students interested in the SHIP program this summer (July 5th-24th)
Fill out this application

Reminder Quiz Friday:

Focusing on But Not Limited To:
Hash Tables
Frontier maze solver
A* / DFS / BreadthFirstSearch / BestFirstSearch
Heaps / Heapsort


Come up with some ideas and choose a partner for the final project. 

If you want to make a game in processing, start with:
Try to make a ball that can bounce around the screen.
Try to make a ball bounce with parabolic motion.  
Try to make a ball bounce off of objects by changing the direction of its velocity.
Try making pong. 

Due Friday:
Have a partner
Have a 1-2 paragraph description of your project.

Due Tuesday:
Prototype
 -More detailed description
 -UML Diagram + How it will work 
  (overview of how it works on a broad level, not specific algorithms)
 -Stages of development

2018-05-16 Labs?

posted May 16, 2018, 11:35 AM by Konstantinovich Samuel   [ updated May 18, 2018, 6:10 AM ]

2018-05-14

posted May 14, 2018, 6:19 AM by Konstantinovich Samuel   [ updated May 14, 2018, 11:28 AM ]

You should have 3 modes for your solve:
Depth First 
Breadth First 
Best-First

Adding A* is now very easy:
+Your maze needs a toggle 
   setAStar(boolean)
   -default is false.

+Your maze can calculate the distance by including how many steps have already been taken in addition to the distance to the goal.
Either:
   -add an extra variable to each node (distanceSoFar) which is always 1 larger than the previous node's distanceSoFar.
   -calculate it each time by seeing how many previous nodes you have to visit before you reach the start 
   (caution this is linear counting but based on distance to start, not based on total nodes in most cases)

+When setAStar is true, the maze includes the distanceSoFar when calculating the distance variable for each constructed node. 

Sample maze that tests A* versus Best First:  (best first goes the long way)

#########
#     S##
# ####  #
# ##### #
#   ### #
# #  E# #
#  ## #
##     ##
#########





2018-05-09 PriorityQueue

posted May 9, 2018, 6:16 AM by Konstantinovich Samuel

Priority Queue Class: (estimated work time: 2 days + 1 day to integrate with Maze Solver)

This is a good demo for different pathfinding algorithms:

New Class: Priority Queue.

We will use a heap to store data. The data needs to have a priority associated with it.
Removing by highest/lowest priority will ensure nodes are processed by priority.

Why are we doing this?

By making a frontier that processes nodes by smallest distance, we can make a greedy maze solver!

Best-First Search:
Instead of choosing the 1st node added, or last node added,
Choose the node from the frontier that is closest to the goal.




Lab (extension of the frontier maze solver): 
//Locations need to have an extra field that must be added to the constructor
//This is used in the compareTo() function
public class Location implements Comparable<Location>{
    private double distance;
       
}

public class FrontierPriorityQueue implements Frontier{
    //min heap of Locations.
    private MyHeap<Location> data;
}



2018-05-04

posted May 3, 2018, 9:12 PM by Konstantinovich Samuel



Frontier Maze Solver


The TEXT DESCRIPTION BELOW trumps the image.

Some of the fields are wrong, but the main idea is correct. I will remake the diagram later.
For now remember: The TEXT DESCRIPTION BELOW trumps the image.

To maximize productivity, here is a starting point for your maze solver: Get to work on this, hopefully by Tuesday you have a working super simple solver. 


1. Frontier interface

public interface Frontier{
  public Location next();
  public void add(Location n);
  public boolean hasNext();
}


2. Location (see image) 

public class Location{
    private int x,y;
    private Location previous;

    public Location(int _x, int _y, Location prev){
    }
}
Methods: 
  -accessors as needed

Later:
We can make it implement Comparable<Location>



3. FrontierQueue - implements Frontier
       Store a Queue instance variable (or deque)


4. FrontierStack - implements Frontier
       Store a Stack instance variable (or deque)


5. MazeSolver : For now solve, toString, constructor

public class MazeSolver{
  private Maze maze;
  private Frontier frontier;

  public MazeSolver(String mazeText){
    
  }

  //Default to BFS
  public boolean solve(){ return solve(0); }

  //mode: required to allow for alternate solve modes.
  //0: BFS
  //1: DFS
  public boolean solve(int mode){
    //initialize your frontier
    //while there is stuff in the frontier:
    //  get the next location
    //  process the location to find the locations (use the maze to do this)
    //  check if any locations are the end, if you found the end just return true!
    //  add all the locations to the frontier
    //when there are no more values in the frontier return false
    return false;
  }

  public String toString(){
    return maze.toString();
  }
}


6. Maze : Collaborate on a toStringColor() method!

public class Maze{
  private Location start,end;
  private char[][] board;

  public Maze(String mazeText){
  }

  // '#' - wall 
  // ' ' - open space
  // '?' - frontier space
  // '.' - visited space
  // 'E' - end space (do not replace this)
  // '@' - part of solution
  // 'S' - starting space (do not replace this)
  public String toString(){
  }
   
  // Work on this method as a group! 
  public String toStringColor(){
  }

  //return a list of Locations that are:
  // adjacent to n and  not visited
  // all the Locations in this list should have their previous set to n.
  public Location[] getNeighbors(Location n){
    return null;
  }

  public Location getStart(){
    return null;
  }

  public Location getEnd(){
    return null;
  }



}




LATER: FrontierPriorityQueue - implements Frontier
      This is just your Priority queue that we have yet to write!






Here is a more robust Maze class. You still need to write some methods. Read through it carefully. 

import java.io.*;
import java.io.FileNotFoundException;
import java.util.*;
public class Maze{
  private static final String CLEAR_SCREEN =  "\033[2J";
  private static final String HIDE_CURSOR =  "\033[?25l";
  private static final String SHOW_CURSOR =  "\033[?25h";
  Location start,end;
  private char[][]maze;



  /*
  YOU MUST COMPLETE THIS METHOD!!!
  YOU MUST COMPLETE THIS METHOD!!!
  YOU MUST COMPLETE THIS METHOD!!!
  */
  public Location[] getNeighbors(Location L){
    return null;
  }

  public Location getStart(){
    return start;
  }
  public Location getEnd(){
    return end;
  }


  private static String go(int x,int y){
    return ("\033[" + x + ";" + y + "H");
  }
  private static String color(int foreground,int background){
    return ("\033[0;" + foreground + ";" + background + "m");
  }

  public void clearTerminal(){
    System.out.println(CLEAR_SCREEN+"\033[1;1H");
  }
  public Maze(String filename){
    ArrayList<char[]> lines = new ArrayList<char[]>();
    int startr=-1, startc=-1;
    int endr=-1,endc=-1;
    try{
      Scanner in = new Scanner(new File(filename));
      while(in.hasNext()){
        lines.add(in.nextLine().toCharArray());
      }
    }catch(FileNotFoundException e){
      System.out.println("File not found: "+filename);
      System.exit(1);
    }
    maze = new char[lines.size()][];
    for(int i = 0; i < maze.length; i++){
      maze[i]=lines.get(i);
    }
    for(int r=0; r<maze.length;r++){
      for(int c=0; c<maze[r].length;c++){
        if(maze[r][c]=='S'){
          if(startr == -1){
            startr=r;
            startc=c;
          }else{
            System.out.println("Multiple 'S' found!");
            System.exit(0);
          }
        }

        if(maze[r][c]=='E'){
          //erase E
          //maze[r][c]=' ';
          if(endr == -1){
            endr=r;
            endc=c;
          }else{
            System.out.println("Multiple 'E' found!");
            System.exit(0);
          }
        }
      }
    }
    if(startr == -1 || endr == -1){
      System.out.println("Missing 'S' or 'E' from maze.");
      System.exit(0);

    }

    /*
    THIS IS AN IMPORTANT PART BECAUSE YOU WILL NEED TO CHANGE IT LATER!
    The start/end Locations may need more information later when we add
    other kinds of frontiers!
    */
    end = new Location(endr,endc,null);
    start = new Location(startr,startc,null);
  }

  public String toStringColor(){
    return toStringColor(50);
  }

  public String toStringColor(int delay){
    try{
      Thread.sleep(delay);
    }catch(InterruptedException e){

    }
    return HIDE_CURSOR+CLEAR_SCREEN+go(1,1)+colorize(toString())+SHOW_CURSOR;
  }

  public String toString(){
    int maxr = maze.length;
    int maxc = maze[0].length;
    String ans = "";
    for(int i = 0; i < maxr * maxc; i++){
      int row = i/maxc;
      int col = i%maxc;

      char c =  maze[row][col];
      ans+=c;
      if( col == maxc-1 ){
        ans += "\n";
      }

    }
    return ans + "\n";
  }

  public char get(int row,int col){
    return maze[row][col];
  }
  public void set(int row,int col, char n){
    maze[row][col] = n;
  }
  public static String colorize(String s){
    String ans = "";
    Scanner in = new Scanner(s);
    while(in.hasNext()){
      String line ="";
      for(char c : in.nextLine().toCharArray()){
        if(c == '#'){
          line+= color(37,47)+c;
        }
        else if(c == '@'){
          line+= color(33,40)+c;
        }
        else if(c == '?'){
          line+= color(37,42)+c;
        }
        else if(c == '.'){
          line+= color(36,40)+c;
        }
        else if(c == ' '){
          line+= color(35,40)+c;
        }else{
          line+=color(37,40)+c;
        }

      }
      ans += line+color(37,40)+"\n";
    }
    return ans;
  }
}


1-10 of 50