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;
}


torsdag 7 juni 2012

Blogg

En blogg för nybörjare med vad det verkar, bra och enkla förklaringar och en hel del kod. Me like! A Beginning Programmer's guide to Java.

Using Graphics

I am trying to find some useful code to understand Graphics better. This one and two pages was quite useful.


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...

Text Areas

Den här förmiddagen roar jag mig med den här sidan; How to use Text Areas.

Tänkte lägga upp koden men internet strular på den andra datorn (ja jag sitter med två datorer samtidigt, hehe).

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!

Kodat

På förmiddagen har jag ägnat mig åt att skriva klart koden till arraylistövningen som Mehran går igenom i lektion 17. Det gick lätt som en plätt. Sen försökte jag mig på att göra den sista övningen han gick igenom men fastnade tyvärr. Hade varit lite roligt att få till "regnande siffror" med hjälp av musklickande. Menmen tids nog...

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);

2 dimension array

type [ ] [ ] name = new type [size][size];

int [ ] [ ] twoD = new int [2][3];

for (int i =0; i < 2; i++){
   for (int j =0; j < 3; j++){
     twoD[i][j] = 1;
}
}
     

++, post and pre increment

++ is a method and it returns something.

x ++; is called post increment and it adds 1 after the value is returned.

++x; is called pre  increment and adds 1 and then returns the new value.

Swapping values using arrays

private void SwapElements (int [ ] arrayName; int position1, int position2){
   int temporary = arrayName[position1];
   arrayName[position1] = arrayName[position2];
   arrayName[position2] = temporary;
}

You have to send (to the method) the actual array and the positions of the elements in it that you want to swap. If you send only the elements (int arrayName[1], int arrayName[3]) you are only sending a COPIE and it will change nothing in the array itself.

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++;