lecture1
lecture1
Many of the apps you use in an Android phone or tablet are also
written in Java. Netflix uses Java for some of its software too.
Java is used worldwide to create software that we all use.
2
Java
Java is a programming language, which means that we can
use Java to tell a computer what to do.
The source file is something humans can read and edit, and the
class file is code that a computer can understand and can run.
3
Java Terminology
All Java code are organized into units called classes.
class:
(a) A module or program that can contain executable
code.
(b) A description of a type of objects. (Animal class,
Human class, Employee class, Car class)
Output:
Hello, World!
6
First Program: repl.it
We will use repl.it, an online integrated development
environment(IDE), for the first part of this course to write all of
our code.
Click on “run” to compile and
run your code!
7
File naming
The name of the class has to match up with the name of the file.
For example, the class below is called Main therefore the name
of the file is Main.java. On replit, the main class must be called
Main.java.(in other IDEs, you can pick any name.)
8
Printing
9
System.out.println
public class Welcome{
public static void main(String[] args){
System.out.println("Hi there!");
System.out.println(”Welcome to APCS A!");
}
}
Output:
Hi There!
Welcome to APCS A!
10
System.out.print
public class SecondClass{
public static void main(String[] args){
System.out.print("Hi there!");
System.out.println(”Welcome to APCS A!");
System.out.print(”We will learn Java!");
}
}
Output:
Hi There!Welcome to APCS A!
We will learn Java!
11
Find the errors.
pooblic class Errors
public static void main(String args){
System.out.print("Good morning! ")
system.out.print("Good afternoon!);
System.Print "And good evening!";
12
Corrected!
13
Strings
• string: A sequence of characters to be printed.
– Starts and ends with a " quote " character.
• The quotes do not appear in the output.
• Restrictions:
– May not span multiple lines.
"This is not
a legal String."
• Examples:
// This is a one-line comment.
/* This is a very long
multi-line
comment. */
15
Using comments
• Where to place comments:
– at the top of each file (a "comment header")
– at the start of every method (seen later)
– to explain complex pieces of code
16
Comments example
/* Suzy Student, CS 101, Fall 2019
This program prints lyrics about ... something. */
// second verse
System.out.println("diggy said the boogy");
System.out.println("said up jump the boogy");
}
}
17
Indent Nicely!
public class Welcome{ public static void main(String[]
args){ System.out.println("Hi there!”
);System.out.println(”Welcome to APCS A!");}}
The code above will compile and run correctly. Java ignore whitespaces.
But it is very hard to read, please make an effort to indent nicely!
18
Lab 1
Create a new repl on your repl.it account and write a program
that has the following outputs:
Output:
I am Sam. Sam I am. I do not like them, Sam-I-am.
I do not like green eggs and ham.
19