27
CS 121 Week 1 - Friday

Week 1 - Friday. What did we talk about last time? Our first Java program

Embed Size (px)

Citation preview

Page 1: Week 1 - Friday.  What did we talk about last time?  Our first Java program

CS 121Week 1 - Friday

Page 2: Week 1 - Friday.  What did we talk about last time?  Our first Java program

Last time

What did we talk about last time? Our first Java program

Page 3: Week 1 - Friday.  What did we talk about last time?  Our first Java program

Questions?

Page 4: Week 1 - Friday.  What did we talk about last time?  Our first Java program

Java refresh

The full Hello World program Remember that everything is in a class The class name must match the file name

(Hello.java) The main()method is where the program starts The print statement outputs information on the

screen

public class Hello{public static void main(String[] args){

System.out.println("Hello, world!");}

}

Page 5: Week 1 - Friday.  What did we talk about last time?  Our first Java program

More Java Syntax

Page 6: Week 1 - Friday.  What did we talk about last time?  Our first Java program

Semicolons

In Java, like C, C++, and many other languages, we separate different statements with a semicolon ( ; )

If we want to do a number of statements, we just type them in order, with a semicolon after each one

Page 7: Week 1 - Friday.  What did we talk about last time?  Our first Java program

Sequencing

For example, instead of one print statement, we can have several:

Each statement is an instruction to the computer

They are printed in order, one by one

System.out.println("Hello, world!");System.out.println("Hello, galaxy!");System.out.println("Goodbye, world!");

Page 8: Week 1 - Friday.  What did we talk about last time?  Our first Java program

Case Sensitivity

Java is a case sensitive languageClass is not the same as classSystem.out.println("Word!");

prints correctlysystem.Out.Println("Word!");

does not compile

Page 9: Week 1 - Friday.  What did we talk about last time?  Our first Java program

Whitespace

Java generally ignores whitespace (tabs, newlines, and spaces)

is the same as:

You should use whitespace effectively to make your code readable

System.out.println("Hello, world!");

System.out.

println( "Hello, world!");

Page 10: Week 1 - Friday.  What did we talk about last time?  Our first Java program

Comments

Programs can be confusing Sometimes you want to leave notes

for yourself or anyone else who is reading your code

The standard way to do this is by using comments

Although comments appear in the code, they do not affect the final program

Page 11: Week 1 - Friday.  What did we talk about last time?  Our first Java program

Comments

There are two kinds of comments (actually 3)

Single line comments use //

Multi-line comments start with a /* and end with a */

System.out.println("Hi!"); // this is a comment

System.out.println("Hi!"); /* this is a multi-linecomment */

Page 12: Week 1 - Friday.  What did we talk about last time?  Our first Java program

What we know

Java is a large, complex language Even so, there are only a few tasks

that you can ask it to do You have already learned:

Sequencing Basic output

Page 13: Week 1 - Friday.  What did we talk about last time?  Our first Java program

Where we are headed

There are not that many other things you can tell Java to do1. Storing numbers and text2. Basic mathematical operations3. Choosing between several options4. Doing a task repetitively5. Storing lists of things6. More complicated input and output7. Naming a task so that you can use it over and

over again That’s basically it

Page 14: Week 1 - Friday.  What did we talk about last time?  Our first Java program

Software Development

Page 15: Week 1 - Friday.  What did we talk about last time?  Our first Java program

What is programming again?

The process of giving computers very detailed instructions about what to do

How do we do that exactly? First, we need a programming

language like Java How do we turn a set of instructions

written so that a human can read them into a set of instructions that a computer can read?

Magic, of course!

Page 16: Week 1 - Friday.  What did we talk about last time?  Our first Java program

First, let’s talk about languages

There are many different programming languages: Java C/C++ ML …thousands more

Each has different advantages in different situations

Page 17: Week 1 - Friday.  What did we talk about last time?  Our first Java program

High vs. low

We classify languages as high or low level

High level languages allow you to give more abstract commands that are more like human thought processes or mathematics

Low level languages are closer to the computer world and give explicit instructions for the hardware to follow

MLJavaC++C

Assembly

Language

Machine CodeLow High

Page 18: Week 1 - Friday.  What did we talk about last time?  Our first Java program

Compilers

We use a program called a compiler to turn a high level language into a low level language

Usually, the low level language is machine code

With, Java it's a little more complex

Page 19: Week 1 - Friday.  What did we talk about last time?  Our first Java program

How does that work in general?

Computer!

Solve a problem;

Compile

010101010

010100101

001110010

Execute

Source Code

Machine Code Hardwar

e

Page 20: Week 1 - Friday.  What did we talk about last time?  Our first Java program

What’s the issue with Java?

Java is a more complicated Java runs on a virtual machine, called

the JVM Java is compiled to an intermediate

stage called bytecode, which is platform independent

Then, the JVM runs a just-in-time compiler whenever you run a Java program, to turn the bytecode into platform dependent machine code

Page 21: Week 1 - Friday.  What did we talk about last time?  Our first Java program

Compilation and execution for Java

class A{

Problem p;

p.solve();}

Compile

101110101101011010110010011

Compile

010101010010100101001110010

Compile

Java SourceCode

Machine Code Hardwar

e

Java Bytecod

e

PlatformIndependent

PlatformDependent

Page 22: Week 1 - Friday.  What did we talk about last time?  Our first Java program

Let’s review the steps we’ll use

1. Write a program in Java2. Compile the program into bytecode3. Run the bytecode using the JVM

(which automatically compiles the bytecode to machine code)

Page 23: Week 1 - Friday.  What did we talk about last time?  Our first Java program

Software development

Often goes through phases similar to the following:

1. Understand the problem2. Plan a solution to the problem3. Implement the solution in a

programming language4. Test the solution5. Maintain the solution and do bug fixesFactor of 10 rule!

Page 24: Week 1 - Friday.  What did we talk about last time?  Our first Java program

Lab 1

Page 25: Week 1 - Friday.  What did we talk about last time?  Our first Java program

Upcoming

Page 26: Week 1 - Friday.  What did we talk about last time?  Our first Java program

Next time…

We'll talk about data representation

Page 27: Week 1 - Friday.  What did we talk about last time?  Our first Java program

Reminders

Read Chapter 3