What We Will Do Today Definitions, Slides of Computer science

Hello.java:1: class, interface, or enum expected. Public class Hello {. ^. 1 error compiler output: 19. Names. You must give your program a name.

Typology: Slides

2022/2023

Uploaded on 03/01/2023

ekambar
ekambar 🇺🇸

4.8

(25)

264 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Topic 2 Java Basics
"To excel in Java, or any computer language, you want to build skill
in both the "large" and "small". By "large" I mean the sweeping,
strategic issues of algorithms, data structures, ... what we think
of basically as a degree in Computer Science. You also need skill
in the "small" -- 10 or 20 line methods built of loops, logic, strings,
lists etc. to solve each piece of the larger problem. Working with
students in my office hours, I see what an advantage it is for
students who are practiced and quick with their method code.
Skill with the method code allows you to concentrate on the larger
parts of the problem. Or put another way, someone who
struggles with the loops, logic, etc. does not have time for the
larger issues."
- Nick Parlante
Stanford University, Google
What We Will Do Today
What are computer languages?
Writing Java Programs
text editor and command line
Eclipse
First programming concepts
output with println statements
syntax and errors
identifiers, keywords, and comments
Strings
2
Computers and Computer
Languages
Computers are everywhere
how many computers do you own?
Computers are useful because they run
programs
program is simply a set of instructions to
complete some task
how many different programs do you use in a
day?
3
Definitions
program: A set of instructions that are to be carried
out by a computer.
program execution: The act of carrying out the
instructions contained in a program.
this is done by feeding the instructions to the CPU
programming language: A systematic set of rules
used to describe computations, generally in a
format that is readable and editable by humans.
in this class we use Java
4
pf3
pf4
pf5
pf8

Partial preview of the text

Download What We Will Do Today Definitions and more Slides Computer science in PDF only on Docsity!

Topic 2 Java Basics

"To excel in Java, or any computer language, you want to build skill in both the "large" and "small". By "large" I mean the sweeping, strategic issues of algorithms, data structures, ... what we think of basically as a degree in Computer Science. You also need skill in the "small" -- 10 or 20 line methods built of loops, logic, strings, lists etc. to solve each piece of the larger problem. Working with students in my office hours, I see what an advantage it is for students who are practiced and quick with their method code. Skill with the method code allows you to concentrate on the larger parts of the problem. Or put another way, someone who struggles with the loops, logic, etc. does not have time for the larger issues."

  • Nick Parlante Stanford University, Google

What We Will Do Today

What are computer languages?

Writing Java Programs

text editor and command line

Eclipse

First programming concepts

output with println statements

syntax and errors

identifiers, keywords, and comments

Strings

2

Computers and Computer

Languages

Computers are everywhere

how many computers do you own?

Computers are useful because they run

programs

program is simply a set of instructions to

complete some task

how many different programs do you use in a

day?

Definitions

program: A set of instructions that are to be carried

out by a computer.

program execution: The act of carrying out the

instructions contained in a program.

this is done by feeding the instructions to the CPU

programming language: A systematic set of rules

used to describe computations, generally in a

format that is readable and editable by humans.

in this class we use Java

High Level Languages

Computers are fast -8086K Processor on the order of 2 billion transistors (a switch that is on or off) performs tens of billions of operations per second Computers are simple They can only carry out a very limited set of instructions on the order of 100 or so depending on the computer's processor machine language instructions, aka instruction set architecture (ISA) Add, Branch, Jump, Get Data, Get Instruction, Store, (CS429) 5

Machine Code

John von Neumann - co-author of paper in 1946

with Arthur W. Burks and Hermann H. Goldstine,

"Preliminary Discussion of the Logical Design of an Electronic Computing Instrument"

One of the key points

program commands and data stored as sequences of bits in the computer's memory

A program: 1110001100000000

6

Say What?

Programming with Strings of bits (1s or 0s) is not

the easiest thing to do.

Assembly language

mnemonics for machine language instructions .ORIG x LD R1, x AND R3, R3 # LD R4, R BRn x ADD R3, R3, R ADD R1, R1, # LD R4, R BRnzp x 7

High Level Languages

Assembly language, still not so easy, and lots

of commands to accomplish things

High Level Computer Languages provide the

ability to accomplish a lot with fewer commands

than machine or assembly language in a way

that is hopefully easier to understand

int sum = 0;

int count = 0;

while (list[count] != -1) {

sum += list[count];

count = count + 1;

Bigger Java program!

public class Hello { public static void main(String[] args) { System.out.println("Hello, world!"); System.out.println(); System.out.println("This program produces"); System.out.println("four lines of output"); } } Its output: Hello, world! This program produces four lines of output console: Text box into which the program's output is printed. 13

Structure of a Java program

public class { public static void main(String[] args) { ; ; ... ; } }

Every executable Java program consists of a

class,

that contains a method named main, that contains the statements (commands) to be executed. class: a program statement: a command to be executed method: a named group of statements 14

System.out.println

A statement that prints a line of output on the

console.

pronounced "print-

Two ways to use System.out.println :

System.out.println("");

Prints the given message as output.

System.out.println();

Prints a blank line of output.

15

Syntax

syntax: The set of legal structures and commands

that can be used in a particular language.

Every basic Java statement ends with a semicolon ; The contents of a class or method occur between { and }

syntax error (compiler error): A problem in the

structure of a program that causes the compiler to

fail.

Missing semicolon Too many or too few { } braces, braces not matching Class and file names do not match ... 16

Syntax error example

1 public class Hello { 2 pooblic static void main(String[] args) { 3 System.owt.println("Hello, world!")_ 4 } 5 }

Compiler output:

Hello.java: 2 : expected pooblic static void main(String[] args) { ^ Hello.java: 3 : ';' expected } ^ 2 errors The compiler shows the line number where it found the error. The error messages sometimes can be tough to understand: 17

An Important Realization

Computers are stupid.

Computers seldom make mistakes.

If the computer is not doing what we want,

WE made a mistake.

18

More on syntax errors

Java is case-sensitive

Hello and hello are not the same

1 Public class Hello { 2 public static void main(String[] args) { 3 System.out.println("Hello, world!"); 4 } 5 } Hello.java:1: class, interface, or enum expected Public class Hello { ^ 1 error compiler output:

Names

You must give your program a name.

public class

SubstitutionCipherDecoder {

Naming convention: capitalize each word (e.g. MyClassName) Your program's file must match exactly (SubstitutionCipherDecoder.java) includes capitalization (remember, Java is "case-sensitive")

Escape sequences

escape sequence: A special sequence of

characters used to represent certain special

characters in a string.

\t tab character \n new line character " quotation mark character \ backslash character Example: System.out.println("\hello\nhow\tare "you"?\\"); Output: \hello how are "you"?\ 25

Clicker 2

How many visible characters does the following

println statement produce when run?

System.out.println("\t\nn\\t"\tt");

A. 0

B. 1

C. 2

D. 3

E. 4

26

Practice Program 1

What sequence of println statements will

generate the following output?

This program prints the first lines of the song "slots". "She lives in a trailer" "On the outskirts 'a Reno" "She plays quarter slots in the local's casino."

Practice Program 2

What sequence of println statements

will generate the following output?

A "quoted" String is 'much' better if you learn the rules of "escape sequences." Also, "" represents an empty String. Don't forget to use " instead of "! '' is not the same as "

Practice Program 3

What is the output of the following println

statements?

System.out.println("\ta\tb\tc"); System.out.println("\\"); System.out.println("'"); System.out.println("""""); System.out.println("C:\nin\the downward spiral"); 29 29

Answer to Practice Program 3

Output of each println statement:

a b c

\

C:

i

the downward spiral

30

Practice Program 4

Write a println statement to produce this

output:

/ \ // \ /// \\

Answer to Practice Program 4

println statement to produce the line of output:

System.out.println("/ \ // \\ /// \\\");