Visar inlägg med etikett Stanford Uni. Visa alla inlägg
Visar inlägg med etikett Stanford Uni. Visa alla inlägg

onsdag 13 juni 2012

Skapa jar-fil i eclipse

Föreläsning 26 handlade om hur du skapar en jar-fil i eclipse och om "standardjava", dvs hur man kodar om man inte har tillgång till acm-biblioteket. Han gick bland annat igenom main, vilket gav mig en härlig känsla av att det här kan jag ju redan.

måndag 11 juni 2012

Thread

Föreläsning 25.

Thread - att göra många saker, som du upplever att du gör, samtidigt. Ex kollar mailen, surfar, kollar play.
Multithreading pratade han också om. Hittade dock den här sidan som tar upp det mer ingående.

Struktur

Föreläsning 24.

Denna gång går Mehran igenom ett programs struktur, hur du ska tänka när du bygger ditt program.

Principles:
nouns (substantiv) - classes
verbs - methods (associated with your classes)
unique identifier ex. Stanford Uni IDnr, ISBN nr, Artist + album + song

Design
collection of objects

Ex a musicstore called FlyTunes
add Songs
add Albums

class Song:   name - String
                    band - String
                    price - Double
Think about: What's gonna stay static and what's gonna change?

fredag 8 juni 2012

Searching and sorting

Lecture 23.

Searching: Linear and Binary
Finding a particular element in an array or some other kind of sequence. The element is called a key.

Linear Search
- timeconsuming

private int linearSearch (int key, int[] array){
   for (int i = 0; i < array.length; i++) {
      if (key == array[i]) return i;
   }
   return -1;
}

Binary search
Ask if the nr is higher/lower than the middle nr. Then ask again and again...
The idea of the binary search is the fact that area code array is an ascending order makes it possible to find a particular value much more efficiently. The key insight is that you get more information by starting at the middle element than you do by starting at the beginning.

private int binarySearch(int key){
   int kh = 0;
   int lh = display.length() -1;
      while ( lh <= rh){
         int mid = (lh+rh) / 2;
      if (key == disp.get(mid)) return mid;
      if (key < disp.get(mid)){
         rh = mid -1; //to move from the midpoint
}
   else{
      lh = mid +1;
}

Sorting
Selection sort and Radix sort algorithm
}
return -1;
}


tisdag 5 juni 2012

Components and containers

Lecture 22.

Mehran talks shortly about Components and containers berfore he goes into a codefrenzy :-)  

Components is anything that can appear in a window and containers contains other components. Think of a bag that contains (components and) another bag. Bag in a bag in a...

måndag 4 juni 2012

TextField, layout, text&graphics

Lecture 21.

TextField, Layout (Grid and Table) and Text & Graphics. Again a lecture with a lot of code. I am going to dive right into that any second now.

Interactors( GUI)

Lecture 20.

GUI - Graphical User Interface
Interactors

A lot of coding in this lecture. I had to change a lot in his code because i don't have access to the Stanfords own library (which what I understand has  a lot of methods already written in it to simplify for the students). Anyhow, interactors are buttons, sliders, checkboxes, radio buttons, combo box and text box.

Don't forget to create an instance variable if you need to keep track of the variable between methodcalles.
Ex.
   pick = new JComboBox();
......
}
private JComboBox pick;

fredag 1 juni 2012

Iterator

Lecture 19.

Iterator
  -> lists through set of values

ArrayList

ArrayList<String> name new ArrayList<String>( );
Iterator<String> it = names.iterator( );

//Always ask if there are any values left
   while(it.hasNext()){
       println(it.next());
 }

HashMap

Don't have the values in order.

//You ask for the key and not the corresponding value
   Iterator<String> i = phonebook.keySet( ).iterator( );
//And then use the whileloop

If you don't want to create the iterator there is a new way of writing:
   for (String name : phonebook.keySet()){
          println(name)
}

Interface, Map and HashMap

Lecture 19.
Interface
-> set of methods
-> common functionality among a set of classes

Syntax: public class ClassName implements InterfaceName


Interface is more general than extends.

Map (interface)
Key
Value


An association between a key and a value. Always keysensitive.
Ex. Dictionary - a map
      words - key
      defintions - value
Ex. Phonebook - a map
      names - key
      numbers - value

HashMap (class)
     -> implements Map
template -> 2 types ( one type for your keys and one for your values)

HashMap<String, Integer> phonebook = new HashMap<String, Integer>( );

Methods most commonly used: put and get, but also remove, containskey and size.

phonebook.put("Mehran", 7236059);
phonebook.put("Jenny", 8675309);

Integer MehranNumber = phonebook.get("Mehran");

phonebook.remove(key); //removes key AND value
phonebook.containskey(key); // returns a boolean

THINK of it as a bag where you put things in pairs. No order.

Debugging

Lecture 18.

Design - Architect
Coding - Engineer
Testing - Vandal!
Debugging - Detective

Always be a vandal! 

Bad values, faulty logic and unwarranted assumptions (ex. feet instead of meters) causes bugs.

- Most of the problems are SIMPLE
- Be systematic
- Don't make assumptions about the problem(s)
- Be critical (< instead of >)
And don't panic!

Use println to find bugs. Ex. before and after a method or writing out variables.
Unit test - test one unit at the time (with values you give)

Arraylist vs array

Lecture 18.
Arraylist

+ dynamic resized
+ other operations

- less efficient than an array
- syntax is bulky (more errors can occur)
- pre version 5.0

If you have a fixed size (almost) always use an array!

torsdag 31 maj 2012

ArrayList

Lecture 17 - ArrayList.

ArrayList is an object. The actual size equals (almost all the times) the effective size.
import java.util.*;

ArrayList<String> slist = new ArrayList<String>( ); //an arraylist of strings

An arraylist holds objects. You can't use primitve types as int, double etc. However you can use the wrapperclass; Integer, Double, Boolean, Character. Notice: They are immuntable, just like Strings, which means that you can't change them. You have to create a new one and override the old one.

But when you write your code you can write like this:

ArrayList<Integer> ilist = new <Integer>( );
   int x = 5;
   ilist.add(x);
   int z = ilist.get(0);

Arrays

Lektion 16 handlar bland annat om Arrayer.

An array is ordered and homogeneous. You are creating space for the object. An array is passed by reference (as an object) and NOT as a copie.

type[ ] name = new type[size];

There is a difference between acutal size (what you declare) and effective size (what you really are using).

Ex. asking the user for the values in the array
   int[ ] myArray = new int[5];
      for (int i = 0; i < 5; i++){
           myArray[i] = readInt("Value?:");
       }

Ex. Ask the user for the length of the array
  int maxLength = readInt("Max sixe of the array:");
  int[ ] myArray = new int [maxLength];

Ex. To keep track of where you are putting your values
   private static final int SENTINEL = -1;
   int numberOfScores = 0;
      for (int i = 0; i < maxLength; i ++){
         myArray[i] = readInt ("Next score:");
         if (myArray[i] == SENTINEL) break;
            numberOfScores++;
   

tisdag 22 maj 2012

Heap and stack

Lecture 14 handlade om memory allocation, heap and stack.

Heap
- här sparas de dynamiska variablerna, ex  = new GOval( ... )
- "växer nedåt", adresserna ökar

Stack
- Lokala variablar och parametrar. När programmet stängs så försvinner dessa.
- "växer uppåt", adresserna minskas

Att se som postlådor med olika adresser.

OBS! Gällande objekt. Om du kallar på en metod och skickar med ett objekt så skickar du själva objektet och inte en kopia. Vilket innebär att om du ändrar i objektet i den metoden och skickar det vidare så har du ändrat objektet. (Tänk Mona Lisa som du sågar itu).
You are passing by object (reference)

Detta till skillnad gällande primitiva värden (int, double, boolean, char) där "you pass by value".

torsdag 10 maj 2012

String, Tokenizers and encryption

Föreläsning 13.
String:
- ta reda på hur många upperCase letters i en String
- to find a position of a String inside a bigger String and replace that String with another.

Tokenizers:
- delimited by space
- hello there Mary and hello, there (allt understruket är tokens, lägg märke till hello, )

import java.util.*;
String line = readLine();
String Tokenizer tokenizer = new String Tokenizer (line);

Encryption:
- For ex. Caeser Cipher. Rotate alphabet by n letters, n is called the key. Wrap-around at the end.  

OBS Att gå igenom en String en bokstav i taget:
 
   for (int i = 0; i < str.length(); i++) {
      char ch = str.charAt(i);
       

String och char

Föreläsning nr 12. Genomgång av char och String.
Char är primitiv dvs when you pass a char you pass a copie of the char. Detta till skillnad från String som är en klass (med objekt). String är immutable, dvs när du väl deklarerat en String så kan du inte ändra i den, istället får du skapa en ny String.

För att ex. ändra till stora bokstäver gör du följande;
char: char ch;
         ch = Character.toUpperCase(ch);
String: String str;
            str = str.toUpperCase();

- För String kan du ej använda == och < >. Använd istället equals och CompareTo

tisdag 8 maj 2012

Image class, compound class, event-driven programs

Lecture 11 Programming methodology. Mehran går igenom The Image class, Compound class och event-driven programs, bland annat.

The compound class allows for combining several graphics objects so they behave like one Object. You add objects to a compound (like it was a canvas) and you can treat the whole Compound as one object.

Event-driven programs
- Whens users interact with computer they generate events (e.g. moving/clicking the mouse, typing etc)
- Can respond to event by having listener for events.
                    addKeyListeners()
                    import java.awt.event.*

Asynchronously; it happens but you don't know when it happens

Constructors

Kollade igen på Lecture 9, Programming Methodology Stanford University. Objects and constructors. Mycket bra föreläsningsserie btw!

Constructors:
- Name of class is used as constructor name
- Constructor does not specify return type
- Responsibly for initializing object
- It is called when an object is created