Intro to Java
-
Upload
annagerber -
Category
Technology
-
view
233 -
download
3
Transcript of Intro to Java
![Page 1: Intro to Java](https://reader035.fdocuments.in/reader035/viewer/2022062708/558b3367d8b42a68648b4695/html5/thumbnails/1.jpg)
Introduction to
Java
http://www.slideshare.net/annagerber/intro-to-java
![Page 2: Intro to Java](https://reader035.fdocuments.in/reader035/viewer/2022062708/558b3367d8b42a68648b4695/html5/thumbnails/2.jpg)
instructions
![Page 3: Intro to Java](https://reader035.fdocuments.in/reader035/viewer/2022062708/558b3367d8b42a68648b4695/html5/thumbnails/3.jpg)
Programming languages
![Page 4: Intro to Java](https://reader035.fdocuments.in/reader035/viewer/2022062708/558b3367d8b42a68648b4695/html5/thumbnails/4.jpg)
.java file
.class file
compile
![Page 5: Intro to Java](https://reader035.fdocuments.in/reader035/viewer/2022062708/558b3367d8b42a68648b4695/html5/thumbnails/5.jpg)
Integrated Development Environment (IDE)
![Page 6: Intro to Java](https://reader035.fdocuments.in/reader035/viewer/2022062708/558b3367d8b42a68648b4695/html5/thumbnails/6.jpg)
Objects & ClassesObjects have:
State (fields)
Behaviour (methods)
Classes provide “blueprints” for different kinds of objects
public class MyClass {
}
![Page 7: Intro to Java](https://reader035.fdocuments.in/reader035/viewer/2022062708/558b3367d8b42a68648b4695/html5/thumbnails/7.jpg)
main methodpublic class MyClass {
public static void main(String[] args){
// This is a comment
}
}
![Page 8: Intro to Java](https://reader035.fdocuments.in/reader035/viewer/2022062708/558b3367d8b42a68648b4695/html5/thumbnails/8.jpg)
StatementsString name = “Anna”;
int numberOfThings = 0;
MyClass m = new MyClass();
m.doSomething();
// This is a comment on a single line
/* This is a longer comment * that can go over more than one line */
![Page 9: Intro to Java](https://reader035.fdocuments.in/reader035/viewer/2022062708/558b3367d8b42a68648b4695/html5/thumbnails/9.jpg)
Input and Output (IO)
![Page 10: Intro to Java](https://reader035.fdocuments.in/reader035/viewer/2022062708/558b3367d8b42a68648b4695/html5/thumbnails/10.jpg)
Exercise 1Write a program to output “Hello, World!”
![Page 11: Intro to Java](https://reader035.fdocuments.in/reader035/viewer/2022062708/558b3367d8b42a68648b4695/html5/thumbnails/11.jpg)
Exercise 2Write a program that asks someone to enter their name and then prints “Hello, <name>”
![Page 12: Intro to Java](https://reader035.fdocuments.in/reader035/viewer/2022062708/558b3367d8b42a68648b4695/html5/thumbnails/12.jpg)
Conditional behaviourBoolean logic:
and &&
or ||
not !
equals ==
not equal !=
greater than >
less than <
if (anna.isTired) { if (isNightTime && !anna.isWorking) { anna.sleep(); } else { anna.drinkCoffee(); }}
![Page 13: Intro to Java](https://reader035.fdocuments.in/reader035/viewer/2022062708/558b3367d8b42a68648b4695/html5/thumbnails/13.jpg)
Comparing Strings== and != are for basic types like integers
For objects use equals() e.g. to compare Strings:
If (string1.equals(string2)) …
If (string1.equalsIgnoreCase(string2)) …
![Page 14: Intro to Java](https://reader035.fdocuments.in/reader035/viewer/2022062708/558b3367d8b42a68648b4695/html5/thumbnails/14.jpg)
Loopswhile (coffee) {
person.drink(coffee);
}
for (int i = 0; i < 10; i++) {
// do something 10 times
}
![Page 15: Intro to Java](https://reader035.fdocuments.in/reader035/viewer/2022062708/558b3367d8b42a68648b4695/html5/thumbnails/15.jpg)
Exercise 3Write a program with the following behaviour:
Ask the person to enter some text If the person says “yay” the program responds with “hey” If the person says “boo” the program responds with “hoo” If the person says “how” the program responds with “now”
Challenge: Keep prompting and responding until the input is “bye”
![Page 16: Intro to Java](https://reader035.fdocuments.in/reader035/viewer/2022062708/558b3367d8b42a68648b4695/html5/thumbnails/16.jpg)
Putting it all together: Choose your own adventureWrite a choose your own adventure game!
System.out.println(“You are being chased by a T-Rex, do you a) run or b) fight?”);input = scan.next();if ( input.equals(“a”)) ...