BeginningObjects.pptx

advertisement
You should understand everything on this slide
public class TestClass {
public static void main(String[] args) {
System.out.println("public static void main(String[] args) is where it all
begins!");
int myIntegerVariable = 77;
System.out.println("I can have cool integer variables like "
+ myIntegerVariable);
TestClass myObject = new TestClass();
// I can also call functions like this
myObject.myFunction("Test");
// ...but I haven't fully explained yet what the deal with "new" is
// and how myObject is involved.
}
public void myFunction(String foo) {
int[] arrayVariable = new int[25];
for(int i = 0; i < arrayVariable.length; i++) {
System.out.println("Array Value at index " + i + " is " + arrayVariable[i]);
}
}
}
Classes and Objects
ArrayLists
HashMaps
HashSets
When arrays are just not cool enough.
In case you missed the email…
1. The Link is now staffed 5pm-11pm, SundayThursday! Get help!
2. You first APT assignment is due tomorrow at
8am. How many folks are planning to be at the
Link tonight?
3. Hangman (due next Wednesday 1/25 at 8am) is
posted and ready to go. Many other things are
coming due too!
4. Videos and notes will be linked off the syllabus
In the Optional Textbook
• Read chapters 1-3 (should be fast, but do
some of the exercises at the end of Chapter 3)
• The later part of this class will also deal with
bits of chapter 16
The Story Thus Far
public class TestClass {
public static void main(String[] args) {
System.out.println("public static void main(String[] args) is where it all
begins!");
int myIntegerVariable = 77;
System.out.println("I can have cool integer variables like "
+ myIntegerVariable);
TestClass myObject = new TestClass();
// I can also call functions like this
myObject.myFunction("Test");
// ...but I haven't fully explained yet what the deal with "new" is
// and how myObject is involved.
}
public void myFunction(String foo) {
int[] arrayVariable = new int[25];
for(int i = 0; i < arrayVariable.length; i++) {
System.out.println("Array Value at index " + i + " is " + arrayVariable[i]);
}
}
}
Objects: they combine data and
functions together
Data: “gattac”
Functions:
setStrandData
getStrandData
mutate
addBasePair
Data: “cgga”
Functions:
setStrandData
getStrandData
mutate
addBasePair
addBasePair function
DnaStrand strand = new DnaStrand();
strand.setStrandData(“cga”);
//prints cga
System.out.println(strand.getStrandData());
strand.addBasePair(“t”);
//prints cgat
System.out.println(strand.getStrandData());
Why Objects?
• See the coolness of java’s String class!
BEWARE!
Never compare 2 strings like this:
String foo;
String bar;
//some other code
if(foo == bar) {
System.out.println(“Strings are equal!”)
}
Always compare them like this:
if(foo.equals(bar)) {
System.out.println(“Strings are equal!”)
}
is3rdBasePairCytosine
DnaStrand strand = new DnaStrand();
strand.setStrandData(“tac”);
if(strand.is3rdBasePairCytosine());
{
System.out.println(“cytosine!”);
}
The function is3rdBasePairCytosine returns a boolean – a
primitive type that is either true or false
You might check out the String Javadoc for some useful
functions
4 Classes You Will Get to Know is
CompSci 100
• String
• ArrayList – an expandable list you can add or
remove elements from
• HashSet – a list that prevents duplicate
elements
• HashMap – a “dictionary” where you can
associate a particular key with a particular
value
getBaseCounts
public HashMap<Character,Integer> getBaseCounts()
{
HashMap<Character,Integer> baseCounts = new
HashMap<Character,Integer>();
baseCounts.put(‘c’, 0);
baseCounts.put(‘g’, 0);
baseCounts.put(‘a’, 0);
baseCounts.put(‘t’, 0);
for(int i = 0; i < myData.length(); i++) {
char currentBase = myData.charAt(i);
//your code goes here
}
return baseCounts;
}
When you’re finished, submit your code via ambient.
What you should know
1. What the difference between class and
objects is, and how objects work
2. 4 key java objects to help you with apts and
homeworks:
–
–
–
–
String
ArrayList
HashMap
HashSet
Download