Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 177 Recitation Week 1 – Intro to Java. Questions?

Similar presentations


Presentation on theme: "CS 177 Recitation Week 1 – Intro to Java. Questions?"— Presentation transcript:

1 CS 177 Recitation Week 1 – Intro to Java

2 Questions?

3 Computers  Computers can do really complex stuff. How? By manipulating data according to lists of instructions.  Fundamentally, this is all that a computer can actually do.  Different kinds of computers: supercomputers, desktops, laptops, embedded.  Different kinds of computer have different sizes, computing powers, uses, etc.

4 Computers  What is hardware? What are some examples?  What is software? What are some examples?  The physical components of the computer (stuff you can touch).  monitor, CPU, keyboard, memory, hard drive, etc.  Programs that run on the hardware and make it do stuff.  Windows, Firefox, MS Powerpoint, World of Warcraft, etc

5 Computers  What are some important (hardware) parts of a computer?  CPU – does calculations (the brain)  Memory – stores data (cache, RAM, hard drive, etc)  I/O devices – allows the user and the computer to communicate (monitor, mouse, keyboard, etc)

6 Programming  Computers only manipulate data according to a list of instructions.  Computers are stupid, but really fast.  Computers can only do very simple things (like adding two numbers), but they can do millions or billions of simple things a second.  Programming – the process of creating lists of instructions for the computer to follow.

7 Programming  The process of software development: 1) Understand the problem that you need to solve. 2) Plan a solution to the problem. 3) Implement the solution (write the program). 4) Test the program (compile and run it), and fix any errors.

8 Programming Languages  We have to use programming languages to give computers instructions because human languages (like English) are too vague.  We are going to write programs in a programming language called Java.  There are lots of other programming languages too.

9 Programming Languages  We classify programming languages as high or low level.  Low level languages are closer to (or are) what the computer actually understands, and have very simple commands.  ex) machine code, assembly languages  High level languages are easier for humans to understand and have more complex commands.  Programs written in high level languages have to be translated to a low level language before they can be run.  ex) Java, Python

10 Compiling  What special program translates a program written in a high-level language into a low-level language?  a compiler  How things usually work:  The compiler translates the program directly into machine code and the program can be run.

11 Compiling  Java is weird:  The Java compiler translates the program into bytecode, a sort of intermediate language.  When the Java program is run, the Java Virtual Machine compiles/translates the bytecode into machine code, and then the program runs.  The machine code produced is different depending on the computer. The bytecode will be the same no matter what computer the program is compiled on.  The “Whatever.class” file you get after compiling your “Whatever.java” file contains the bytecode.

12 Java  Java programs are just text files, and can be written in any text editor or DrJava.  Java is a language, and therefore has rules that you have to learn and obey.  If you do not follow Java’s rules, your program will not compile or won’t run correctly.

13 Anatomy of a Java Program public class NameOfClass { public static void main(String[] args) { }  Every Java program must be inside a class. The keyword class tells Java that you are making a new class.  Replace NameOfClass with what you want to call your program. The program will need to be saved in a file called NameOfClass.java.  For now, the rest of the stuff is magic words. Just remember to type them in, or your program won’t work.

14 Simple Program public class HelloWorld { public static void main(String[] args) { System.out.print(“Hello, world! ”); System.out.println(100.34); System.out.println(“Programming is fun!”); }  This program just prints some stuff on the screen.  You can use System.out.println() to print anything in quotes (“fish”, “foobar”) or numbers (5, 20.6).  System.out.print() does the same thing, but does not put a newline after the printed stuff.

15 Running a Program  Let’s run our HelloWorld program and see it in action! There are two different ways to do it.  In the terminal or command prompt:  Change to the directory where your Java file is saved.  To compile, type “javac HelloWorld.java”  If you don’t see any messages, your program compiled successfully.  Compiling creates a “class” file with the same name as your Java file (in this case, “HelloWorld.class”).  To run, type “java HelloWorld”.

16 Running a Program  In DrJava:  To compile, push the “Compile” button (upper right corner of the window).  If it says “Compilation completed”, everything worked.  To run, push the “Run” button (near the Compile button).  NOTE: You don’t have to use DrJava if you don’t want to, especially at home. It will probably make your life easier, though.

17 Some Java Details  All statements in Java must end with a semicolon (;).  Think of it like the period at the end of an English sentence.  All classes and methods must have curly braces ({ and }) around their contents.  Java is case sensitive (“class” is not the same as “Class” or “CLASS” or “cLAss”).

18 Comments  Programs can be confusing to read.  Use comments to leave notes for yourself or anyone else who reads your code.  Comments do not affect how the program runs (they’re ignored by the computer).  Two ways to make comments:  // I’m a comment  Use for single-line comments  /* I’m a comment. */  Use for comments that are multiple lines long

19 Coding with Style!  Java ignores whitespace, so use whitespace to make your code more readable.  Indent all the lines of code between a class’ or method’s curly brackets.  This makes your code MUCH easier to read. Really. It does.  We WILL take points off on assignments if you do not indent your code nicely!

20 Indentation Examples  Good public class NameOfClass { public static void main(String[] args) { System.out.println(“Hello, world!”); }  Bad public class NameOfClass { public static void main(String[] args) { System.out.println(“Hello, world!”); }  Horrible! public class NameOfClass { public static void main(String[] args) { System.out.println(“Hello, world!”); }

21 Questions?


Download ppt "CS 177 Recitation Week 1 – Intro to Java. Questions?"

Similar presentations


Ads by Google