Courses‎ > ‎AP Computer Science‎ > ‎

Konstantinovich

Email: 
    konstans@stuy.edu

Office Hours:
    Periods 6,7,8  in room 301
    Note: 6th is my lunch and I tend to be out getting food the 1st 10 minutes of the period


2018-09-21 While

posted Sep 21, 2018, 5:48 AM by Konstantinovich Samuel   [ updated Sep 21, 2018, 10:19 AM ]

Goal: While we are learning Strings... we need loops


If you need a better seat because of vision, OR you have a name you prefer to be called instead of your official first name:

please fill this out: https://goo.gl/forms/JIE8eoHyk4WQXhW93


Lab + Homework:

codingbat -> warmup2 ->

stringTimes

frontTimes

countXX

doubleX

stringBits

stringSplosion


String-2

doubleChar

countHi





in python:

while boolean:
  body
x = 0
while x < 10:
  #some more code
x = x + 1

in netlogo:

while [boolean]
[
 ;body
]
let x 0
while [ x < 10][
;some code
set x (x + 1)
]

in java:

while(boolean){
   body
}
int x = 0;
while(x < 10){ //some code here
x = x + 1;
 }
GENERIC CODE:

1. To start, evaluate the boolean when it is true go to INSIDE_LOOP and do step 2, but when it is false go to AFTER_LOOP  and you are done.

2. When you go inside the body, you execute the entire code block. After the body of the loop completes go back to boolean (go back to the step 1).


while (boolean){   

   //INSIDE_LOOP

}

//AFTER_LOOP





Print individual characters in a string:


String s = "doa what"

         //01234567


int i = 0;

while (i < s.length()){  

System.out.println(s.substring(i, i +1));

// s.charAt(i)

i = i + 1;

//OR: i += 1;

//OR: i++;

}


Note that:

a) <= would cause an exception because index 8 doesn't exist, but the length is 8.

b) length() is a method




Make a string with every other chraracter:


String s = "doa whatever";

         //012345678901

String result = "";

int i = 0;

while (i < s.length()){  

result += s.charAt(i);

i = i + 2;

}
//Now result is every even index of s, or just "dawaee"




2018-09-20 Strings

posted Sep 20, 2018, 6:05 AM by Konstantinovich Samuel   [ updated Sep 20, 2018, 10:57 AM ]

Goal : Java Strings

Reminder: Google Mentorship Interest Meeting Friday (after 10th in 307)

Homework:
(Note: You should be paying attention in class, not doing your coding bat homework. I can see the timestamps of when they are completed. It is short sighted of you to to try to "save time" and do this in class, and miss out on details discussed. It is also rude.)
makeTags 
makeOutWord 
extraEnd 
firstTwo
withoutX

You can't multitask efficiently. Yes even YOU.
This is important, and rather than link you to an article here is a picture. 
If you don't believe it, google it, and you will see it is based on multiple studies (by the APA iirc)


Strings
Strings are not primitives, they are Objects. I wanted to give you an example of an object before we learn how to make them. Object types start with a capital first letter.
They contain methods! (You saw python string methods!


Operations that work on strings:
+    concatenation 

You cannot use other math operations.
You cannot use == ,  >, <= etc for comparison.

In class code demo.


METHODS

Strings documentation page lists all methods:



AP Exam Critical Methods:
substring  : like a python slice
equals  :  determining identical contents
compareTo : for less than/ greater than / equal  comparison
length

Additional Class Critical Methods:
charAt   


Escape Sequences:
Special characters sometimes need to go in strings, but cannot be typed. 
We can put them in strings using the backslash to escape the character from being treated like a regular symbol. 
This works in both char and String types.

\n is the newline character, so you can write literals (both char and String) like:  '\n'  ,  "\n" ,  or "fish\nfood"
\t  tab
\'  single quote
\"  double quote
\\ backslash

System.out.println("\\why\no//\"!\'?")  would print:
\why
o//"!'?
It has a length of 12 (excludes the escape characters and the double quotes that designate the string.)


Each escaped character counts as 1 unit for the length of the string and charAt and substring treat them as single characters (only we see 2, the views these symbols as 1 thing each)



2018-09-18

posted Sep 18, 2018, 6:13 AM by Konstantinovich Samuel   [ updated Sep 18, 2018, 10:22 AM ]


There is going to be an interest meeting for the google mentorship program this friday at 4pm in room 307.

Do Now: 
Share your coding bat work with me if you did it in class (or didn't do it at all), then complete Logic-1   ->  in1To10



Did you use an if statement? If you did, try again without an if statement. Just return the boolean.
DO NOT do this:
if( boolean ){
 return true;
}
else{
 return false;
}


INSTEAD you SHOULD do this:
return boolean;



Classwork:


Too often, students type without thinking. You don't solve the problem on the computer, just like you don't start building any complicated structure without plans. If you do, it may work sometimes, but when it doesnt work, it will take you MUCH longer to try to fix.


Lets make some bricks by solving a problem with your neighbors:
DO NOT WRITE ANY CODE UNTIL YOU SOLVE THE PROBLEM!
On paper you need a plan. (You need to WANT to do this step... I promise you
How can you break down the possibilities into categories.
If the probelm was asking if you had an EXACT number of bricks the problem would be very easy, why?
What are the other possibilities? / When are those possibilities true/false?

Homework:

A. CodingBat:

http://codingbat.com/java/Logic-2   (easy popcorn problems)
    -loneSum
    -LuckySum
    -noTeenSum
    -roundSum


B (if you can do this,  you are on track! )
-Make a new Git repo:  MKS21X-CtoF
-Create a java program with a class name of CtoFTester.java
-Create two functions, celsiusToFahrenheit and fahrenheitToCelsius
-BEFORE Writing the code, think about the following questions:
    -What should the functions have as parameter(s) ? What type should they be?    
    -What should the functions return?
-Compile and run as you write this, using a main function to run and test your two functions.
-Commit any time you make progress towards your goal!

2018-09-17

posted Sep 17, 2018, 5:51 AM by Konstantinovich Samuel   [ updated Sep 17, 2018, 10:04 AM ]


Homework:
1. Create an account on http://codingbat.com/ please do NOT use the Mozilla Persona thing.

IF YOU DID 1b IN CLASS, PLEASE DO IT AGAIN (I reset it period 6)
1b. In your profile SHARE with konstans@stuy.edu

1c. Change your MEMO to be your name formatted like:  PD.Last.First   (no spaces in your name please)
  e.g.  
    3.Kim.Jamal
    7.WeiMin.Fahran
    8.Keiko.Jimenez

2. Do any 2 warmup-1 problems to see how the site works.
3. Be careful, when you want to work on problems you should LOG IN first, or the progress will not be saved. 
This site is another great resource and has many problems to practice on! I will also assign many problems from here.


4. Look at the Logic-1  section, and complete these 4 problems:
 > sortaSum 
 > dateFashion 
 > caughtSpeedin
 > squirrelPlay

Goal: Converting more of your prior CS into Java specific CS.

Do Now: Predict the result of each print statement

note that:
- Strings only work with the + operator (concatenation), not other operators.

System.out.println( 1 + 2 + 3 / 3);
System.out.println( 2.0 + 1 / 2);
System.out.println( 2 + 1.0 / 2);
System.out.println( "hey" + "you");
System.out.println( "2+3" + "4");
System.out.println( "5"+ 6 + 7);
System.out.println( 1 + 2 + "3");
System.out.println( 23 % 4);
//remember ASCII?
System.out.println('A'+1);
System.out.println('C'-1+" B");
System.out.println('A'+'A');

Functions!

public static TYPE IDENTIFIER( PARAMS ){
   //body goes here      
}


parameters:  0 or more variables with their types:

Just some function headers as examples:
public static int foo(int a)
public static float bar(int x, String s)
public static void foobleh()


Complete function examples:
    public static int foo(int x){
        return 2 * x + 1;
    }

    public static double bar(int x, int y){
        return x * x + y + 1.0;
    }

    //void means there is no return type!
    public static void bah(int x, int y){
        System.out.println("Shhh!");
    }

    Evaluate:
    System.out.println(   foo(2)    );
    System.out.println(   foo( foo(2) ) );
    int u = 2;
    System.out.println(   foo( u + foo(3) ) );




Boolean expressions are created using: and, or, not. Java does not have those keywords however instead use the following. 
and:    &&
or:     ||
not:     !   
example:   a && !b || !(a && b)

and, or, and not only work on boolean values or expressions. They do not work on numbers.

To turn other values into booleans we use comparisons:
>    <=
<    >=
==   !=
  
Conditional Statements:

if ( BOOLEAN1 ) {
  // run here if BOOLEAN1 is true
}



if ( BOOLEAN2 ) {
  //A only run this section when <boolean2> is true
}
else{
  //B only run this section when <boolean2> is false
}
//At this point either A or B have executed, but never both (always exactly one)


else can be connected to an additional if statement:
if( a ){

}else if( b ){

} //OPTIONALLY ADD ANOTHER ELSE



2018-09-14

posted Sep 14, 2018, 6:17 AM by Konstantinovich Samuel   [ updated Sep 14, 2018, 7:16 AM ]

HW:
0. If you did not fill out the info collection form from the first day or two, you MUST do that.
1. Test the JDK by running this on a terminal:  
    javac -version
2. Compile and run a hello world program.
3. Make a new repository  MKS21X-Hello
4. Create a java file and add the blank file to your repo: Hello.java
After you add the file, write a java program that outputs :

Hello my name is x, y.

x should be replaced with your name
y should be replaced with the second part of your greeting, this is totally up to you, keep it clean!

You should have at least 3 commits. 1 created, 2 added empty file, 3 wrote code that works.

Languages:
  • Compiled Language
    • Human readable source code is translated (compiled) into machine language. (binary)
    • Machine language contains instructions that are run directly by the computer.
  • Source code and machine code are different files.
  • Java is a hybrid of compiled and interpreted language
  • Java programs compile into java byte code, that does not run directly on a computer, they go through the java virtual machine (JVM)
    • The JVM is like an interpreter. It translates from byte code to machine instructions.
  • Source files have a .java extension and are human readable. They _must_ end in .java 
  • Source files are compiled into java byte code by the compiler (javac) and are stored in files which have .class extension.
  • .class files are readable by the JVM
  • All .class files are cross-platform (can be run on any OS that has a JVM)
  • Each platform has a specific JVM. Mac, Windows, Linux, etc.


Comments:

Double forward slash will make the compiler ignore the text afterwards

//this is a comment

Printing
System.out.println( value );
//replace value with a value or expression

Note that variables have values, and functions that return values have values.
It is very easy to print a literal string with the text you want to appear.
e.g.
System.out.println("Please give me a drink.");
System.out.println("Here, have a Kuro Mitsu Matcha Latte");

Semicolons:
Semicolons are used to separate statements and are placed at the end of most statements. Exceptions will be discussed.

Variables : They store values, and you can change them by re-assigning after they are assigned. You cannot use a variable before it is assigned. 

declaration:
<type> <name>;
e.g.
int x;
float z;

assignment:
<name> = <expression>;
x = 3;
z = 2*3.14159+1;

declare + assign:
<type> <name> = <expression>;
char c = 'f';
String s = "whoa!";

Reassignment - can have the original variable in the expression:
x = x + 1; 
fish = fish + cat; //assumes fish and cat have values and are compatible with addition.



Compile and run code:
    javac is the command to compile a java source code (the java file)
    java is the command to run a java program (the class file. do not type .java)

e.g.
$javac Hello.java
$java Hello
Hello Human!


Java Files must be named to match the class names:

file: HelloWorld.java

public class HelloWorld{
    public static void main(String[] args){
        System.out.println("Hello world");
    }
}


Java runs the main method. You cannot place statements outside of methods.

2018-09-13 Types

posted Sep 13, 2018, 6:04 AM by Konstantinovich Samuel   [ updated Sep 13, 2018, 10:30 AM ]

Stuyvesant has competed in an international  team programming competition that programs robots in outer space (Yes, real robots in the International Space Station).  Last year, our team came in first.  We have an interest meeting today, Thursday, after 10th period, at 3:45 in room 325.  Come on down and found out what it's like to program robots out there.  If you have questions, see (and try to find) Mr. Brooks.

Homework:
Install the java development kit:  (version 8 or 9 are both fine)


0. Terminal Stuff

Commands you should know and love:
cp     mv     
mkdir    rmdir
ls      
cd      pwd     
rm
cat    man

Commands you should learn to love:
nano   
ssh   
less    more 

Cool Commands that won't be required:

grep
|        (pipe)  lets you run the output of one program into another program
wc 




More detailed Linux/Unix tutorial:


Java Types

-Java is statically-typed (variables must be declared before they can be used)

The primitive data types: (you need the ones underlined)
byte/short/int/long
float/double
boolean
char

1. Integer

Integer types can hold whole numbers such as 123 and −96. The size of the values that can be stored depends on the integer type that we choose.

The range of values is calculated as −(2n−1) to (2n−1)−1; where n is the number of bits required. For example, the byte data type requires 1 byte = 8 bits. Therefore, the range of values that can be stored in the byte data type is:

−(28−1) to (28−1)−1
= −27 to (27) -1

= −128 to 127

Type Size Range of values that can be stored
byte 1 byte −128 to 127   OR  −27 to (27) -1
short 2 bytes −32768 to 32767   OR  −215 to (215) -1
int 4 bytes −2,147,483,648 to 2,147,483,647   OR −231 to (231) -1 
long 8 bytes −9,223,372,036,854,775,808 to
9,223,372,036,854,755,807  OR −263 to (263) -1


2. Floating Point

Floating point data types are used to represent numbers with a fractional part. Single precision floating point numbers occupy 4 bytes and Double precision floating point numbers occupy 8 bytes.  There are two subtypes, double can have both more significant digits, and a larger exponent. (Think scientific notation)

Type Size Digits that can be stored
float 4 bytes approximately 7 significant digits
double 8 bytes approximately 16 significant digits

3. Character

Character data type char hold single symbols such as

't', 'T' , '%' , '5' etc.

Special characters can be created using the \ (backslash) to escape the special character.

'\n',    '\t' , '\'' ,       '\\'

newline, tab,   single quote, bacsklash

The char data type is 2 bytes (ascii only requires 1), but it can hold only a single character because char stores unicode character sets. It has a minimum value of ‘u0000’ (or 0) and a maximum value of ‘uffff’ (or 65,535, inclusive).

4. Boolean

Boolean data types are used to store values with two states: true or false.

boolean (size not defined, because different JVM's can implement it differently)

5. Other Data Types

A String is not a primitive type. Strings are special because they are objects, and we will learn about them more later.

6. Literals and Identifier

Literal - a fixed value written in the source code. Not a variable or expression.
e.g.
54
"hello"
1.3
true
-341.823
1.23e2 (doubles can be written in engineering notation!)

Identifier - the name given to a variable/function.
Can contain:
     letters,
    digits,
     _ ,
    $  (Don't use dollar signs, and don't use underscores except when discussed in class)
May not start with a digit

e.g.
abbyNormalBrain
fish
bitter_sweet_chocolate_pie
x1y1z2_123
num$dollarsx123

2018-09-12

posted Sep 12, 2018, 6:26 AM by Konstantinovich Samuel   [ updated Sep 12, 2018, 10:23 AM ]

Goal: Number bases*

*Notes for number bases are drawn on the board.


Basic Terminal Stuff:

Successful students know their way around the terminal.

Minimally you need to know how to navigate the file system, and open/edit/moe/delete files.

Successful students are able to ssh into their accounts when needed.

Windows users: putty is an ssh client.

Supplemental material on SSH and SSH keys can be found here:
https://www.ssh.com/ssh/   (including the ssh-copy-id command)


You can ssh into *.stuy.edu port 22
* = marge , homer, lisa, or bart

You can also ssh into 149.89.x.y
where:
x is 150,151,160, or 161.
y is 100,101,102,103,104, or 105.

You are not getting heavy homework assignments, so use your time at home to familiarize yourself with the tools required. If you are confused about any of them there will be a google group added soon.

2018-09-07 SSH Keys

posted Sep 7, 2018, 6:16 AM by Konstantinovich Samuel   [ updated Sep 12, 2018, 6:06 AM ]

Homework:
2 - Set up git at home.
  • download git here https://git-scm.com/download/
  • Mac/Linux users can do the same steps that we did in class by opening a regular terminal.
  • Windows users should run git-bash (it comes with your download) This is a shell that has the commands you need. This includes the SSH keygen ability we worked on in class.


Goal: Add SSH keys to your git accounts.

Resource - git cheat sheet:

1. NETWORK

Make sure your network settings are set up properly! (stuycs labs)

-Click settings (gearbox icon)

-Click network

-Click Network Proxy -> manual ->

enter the two values in ALL 4 lines:

Address                 Port

149.89.1.30 3128


2. GIT Configuration

0b. Configure your local git (on every computer you use!)

(use your name and your git related email)


git config --global user.name "John Doe"
git config --global user.email johndoe@example.com

git config --global core.editor EDITORNAME


where the EDITORNAME is the editor you want:

atom (I will use in class)

nano (most basic - use this for now)

emacs

vim

etc.


3. CREATE SSH KEY:

  1. Open Terminal. (ctrl + alt + T)

  2. Paste the text below, substituting in your GitHub email address.


ssh-keygen -t rsa -b 4096 -C "StuyCS Lab email@whatever.com"

This creates a new ssh key, using the provided email as a label. Note that the -C is a comment, so any text will work, but this kind of comment helps manage keys


  1. When you're prompted:

Enter a file in which to save the key (/home/you/.ssh/id_rsa): [Press enter]


Press Enter. This accepts the default file location.



  1. At the prompt, type a pasphrase (twice) the shell will NOT SHOW YOU TYPING. This is desired behavior so you cannot see how many letters it is.


4. Add ssh key to github.

This key can be used to authenticate on github or other computers. For now you just need to add it to github.

  1. Open id_rsa.pub using a text editor.
  2. Log into your git account
  3. In the upper-right corner of any page, click your profile photo, then click Settings.
  4. In the user settings sidebar, click SSH and GPG keys.
  5. Click New SSH key or Add SSH key.
  6. Paste your key into the "Key" field.And add a label to the "Title" field (e.g. Stuy cs lab
  7. Click "Add SSH key" and possibly, confirm your git password.
  8. TO TEST YOUR KEYS - Enter the following:

    ssh -T git@github.com
    #it should successfully authenticate and say you don't get a shell with github.
6. Verify you can do it!

- Create a repository on the github website.

- Since theis is the first time using this repo on this machine you need to Clone the repository on your local machine.
*IMPORTANT* use the ssh link found on the repo.
    git clone LINK_FROM_REPO
(future times using the repo you will just git pull, since it has already been cloned)

-Change files, save them.

-Commit your changes by adding and commiting.

-Push your changes... then check the repo on the website to verify that it worked.

1-8 of 8