Sei sulla pagina 1di 18

1

System.out.println(“Hello world!”);
DR. JOSHUA WAXMAN
2
What will we learn this semester?

 Java?  Design Principles


 Not so much; this isn’t another Intro  DRY vs. WET
 Surely you already know Java…  Open / Closed Principle
 Maybe some acclimation  …
 BeakerX with Java Scripting
 IntelliJ IDEA; advanced features  Design Patterns
 Object Oriented Programming  How great Java programmers solved
these problems
 You covered a lot already in Intro, Data
Structures  Circumvent limitations of Java
 Start this from scratch
 Encapsulation, etc.
3
Books

 $19 a year for ACM Student Membership  Head First Java, 2nd Edition
 learning.acm.org  https://www.safaribooksonline.com/library/vi
ew/head-first-java/0596009208/
 Put membership on your resume
 Head First Object-Oriented Analysis and
 Lots of benefits Design
 https://www.safaribooksonline.com/library/vi
 Computer Science: An Interdisciplinary Approach, ew/head-first-object-oriented/0596008678/
Sedgewick & Wayne  Head First Design Patterns
 https://www.safaribooksonline.com/library/view/co  https://www.safaribooksonline.com/library/vi
mputer-science-an/9780134076461/ ew/head-first-design/0596007124/
 Video series of the same:  Coursera Design Patterns
 https://www.coursera.org/learn/cs-programming-  https://www.coursera.org/learn/design-
java/home/welcome patterns/home/welcome

 Resources:
https://www.wickedlysmart.com/head-first-
java/
4
Software packages

 On Mac / Windows:  TO DO: in class, run BeakerX notebook


 IntelliJ IDEA (from JetBrains)
 JDK 12 (Java 13 will be released in  TO DO: in class, run Hello World
March) program from IntelliJ (next page)

 On Windows:  TO DO: in class, run Hello World


 Ubuntu (a Linux flavor) program from Unix command line
 Standard way to run javac, etc.
without worrying about paths
 TO DO: in class, solve the actual Java
 Equal to Mac terminal exam
5
Hello world (Main.java)

 Open curly brackets: on same line or


on new line?

 public static void main(String[] args) {


 keywords
 Whatever does it mean?
 Boilerplate

 static functions (for now) like def


6
Compilation

 Python is an interpreted language  Java is a compiled language


 Translated to machine code on the fly  Actually somewhat hybrid
 Starts immediately  Java bytecode for JVM
 Slower to run  Translation before running
 Can crash while running because of  Once, time consuming to translate
s = ‘hello’ + 23 everything

 Dynamic typing (vs static), “duck”  Much faster to run

 Compile time error for most type errors


 Static typing
7
Getting javac running in Ubuntu

 sudo apt-get update


 sudo apt install openjdk-11-jdk-
headless
 pico Main.java
8
Continuing compilation, running…
9
Classwork #1: Use Arguments

 args[] is an array of Strings, the


command line arguments

 args[0] is the first command line


argument.

 Volunteer, please. 
 Your classmates can help you.
10
Classwork #2: Tracing

 Trace on paper, Excel

 Try this in IntelliJ IDEA, using the


integrated Debugger
11
Type safety

 Java is statically typed, will prevent  “hello” * 5


you from doing “bad things” with
 int x; x = 3.0;
types
 int x; int y = x * 3;

 Some types:
 int
 float
 double
 char
 String
12
char vs. String
13
Quadratic (illustrates type conversion)

public class Quadratic


{
public static void main(String[] args)
{
double b = Double.parseDouble(args[0]);
double c = Double.parseDouble(args[1]);
double discriminant = b*b - 4.0*c;
double d = Math.sqrt(discriminant);
System.out.println((-b + d) / 2.0);
System.out.println((-b - d) / 2.0);
}
}
14
booleans
15
IsLeapYear

public class LeapYear


{
public static void main(String[] args)
{
int year = Integer.parseInt(args[0]);
boolean isLeapYear;
isLeapYear = (year % 4 == 0);
isLeapYear = isLeapYear && (year % 100 != 0);
isLeapYear = isLeapYear || (year % 400 == 0);
System.out.println(isLeapYear);
}
}
16
Library methods…
17
Library methods
18
Reading & Homework

 Watch videos for Coursera course, Week 1: Basic Programming


Concepts
 Do homework for week 1. Use the auto-grader on Coursera, but also
submit the files to me on Canvas.
 Watch videos for Coursera course, Week 2: Conditionals and loops.
Think about how these control structures differ from what you know in
Python

Potrebbero piacerti anche