16
CS260 Intro to Java & Android 01.JDK Intro Winter 2020 Winter 2020 CS260 - Intro to Java & Android 1

CS260 Intro to Java & Android 01.JDK Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/01.JDKIntro.IP.pdfSelect Kotlin/JVM Additional Libraries and Frameworks 3. Keep Create project

  • Upload
    others

  • View
    19

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS260 Intro to Java & Android 01.JDK Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/01.JDKIntro.IP.pdfSelect Kotlin/JVM Additional Libraries and Frameworks 3. Keep Create project

CS260 Intro to Java & Android

01.JDK Intro

Winter 2020

Winter 2020 CS260 - Intro to Java & Android 1

Page 2: CS260 Intro to Java & Android 01.JDK Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/01.JDKIntro.IP.pdfSelect Kotlin/JVM Additional Libraries and Frameworks 3. Keep Create project

Java Tutorials

The main sources of Java documentation will be the Java Tutorials

http://download.oracle.com/javase/tutorial/ Read Trails Covering the Basics

Getting Started

Learning the Java Language

Essential Java Classes

Collections

http://www.tutorialspoint.com/java/index.htm Read Java Basics and Java Object Oriented

Winter 2020 CS260 - Intro to Java & Android 2

Page 3: CS260 Intro to Java & Android 01.JDK Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/01.JDKIntro.IP.pdfSelect Kotlin/JVM Additional Libraries and Frameworks 3. Keep Create project

Hello World

public class HelloWorld

{

public static void main (String[] args)

{

System.out.println ("Hello World");

}

}

Winter 2020 CS260 - Intro to Java & Android 3

Page 4: CS260 Intro to Java & Android 01.JDK Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/01.JDKIntro.IP.pdfSelect Kotlin/JVM Additional Libraries and Frameworks 3. Keep Create project

Java Development Environment

JDK 8u231 (Java Development Kit includes JRE)On turing in CS260-01Public\2020

IntelliJ IDEA 2019.3.1

Winter 2020 CS260 - Intro to Java & Android 4

Page 5: CS260 Intro to Java & Android 01.JDK Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/01.JDKIntro.IP.pdfSelect Kotlin/JVM Additional Libraries and Frameworks 3. Keep Create project

Java Development Environment

After installing the JDK, add the path of the compiler (javac) & interpreter (java) to your PATH

Control Panel->System & Security->Change Settings->Advanced->Environment Variables

Also, create/set user variables JAVA_HOME & JDK_HOME to the location of the JDK. In my case it’s C:\Program Files\Java\jdk1.8.0_231

Instructions DevToolsInstallation.README on turing in CS260-01Public\2020\DevTools

Winter 2020 CS260 - Intro to Java & Android 5

Page 6: CS260 Intro to Java & Android 01.JDK Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/01.JDKIntro.IP.pdfSelect Kotlin/JVM Additional Libraries and Frameworks 3. Keep Create project

Environment Variables

Figure 01.1

Path

Delete C:\Program Files (x86)\Common Files\Oracle\Java\javapath if it exists

Winter 2020 CS260 - Intro to Java & Android 6

Page 7: CS260 Intro to Java & Android 01.JDK Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/01.JDKIntro.IP.pdfSelect Kotlin/JVM Additional Libraries and Frameworks 3. Keep Create project

PATH Variable

If your Environment variables are set correctly, you will see the following:

C:\Users\ryandj\Desktop\MyAddition\app\libs>java -version

java version "1.8.0_231"

Java(TM) SE Runtime Environment (build 1.8.0_231-b11)

Java HotSpot(TM) 64-Bit Server VM (build 25.231-b11, mixed mode)

C:\Users\ryandj\Desktop\MyAddition\app\libs>javac -version

javac 1.8.0_231

C:\Users\ryandj\Desktop>

Winter 2020 CS260 - Intro to Java & Android 7

Page 8: CS260 Intro to Java & Android 01.JDK Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/01.JDKIntro.IP.pdfSelect Kotlin/JVM Additional Libraries and Frameworks 3. Keep Create project

Compile & Execute HelloWorld

Figure 01.2

Winter 2020 CS260 - Intro to Java & Android 8

Page 9: CS260 Intro to Java & Android 01.JDK Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/01.JDKIntro.IP.pdfSelect Kotlin/JVM Additional Libraries and Frameworks 3. Keep Create project

Simple Java Program

• In CS260-01\Public on turing is HelloWorld.java

Put on Desktoppublic class HelloWorld

{

public static void main (String[] args)

{

System.out.println ("Hello, World");

}

}

Open cmd promptType javac HelloWorld.javaType java HelloWorld

Hello, World

Winter 2020 CS260 - Intro to Java & Android 9

Page 10: CS260 Intro to Java & Android 01.JDK Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/01.JDKIntro.IP.pdfSelect Kotlin/JVM Additional Libraries and Frameworks 3. Keep Create project

IntelliJ IDEA

Winter 2020 CS260 - Intro to Java & Android 10

• Launch IntelliJ IDEA Community

• Default folder is C:\users\punetid\IdeaProjects\

• Create a project HelloWorld.

1. Project SDK is 8 (java version 1.8.0_231)

2. Select Kotlin/JVM Additional Libraries and Frameworks

3. Keep Create project from template unchecked

4. Project name: HelloWorld

5. Right click on src and add New Java Class HelloWorldNote: The project name and class name must match

Page 11: CS260 Intro to Java & Android 01.JDK Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/01.JDKIntro.IP.pdfSelect Kotlin/JVM Additional Libraries and Frameworks 3. Keep Create project

IntelliJ IDEA

Winter 2020 CS260 - Intro to Java & Android 11

File -> Figure 01.3

Settings->

Editor->Code Style ->

Java->

Scheme->

Import Scheme->

Eclipse xml Profile->

JavaCodingStandardsV1.4Prefs.xml

ctl+shift+alt+L will reformat code

Page 12: CS260 Intro to Java & Android 01.JDK Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/01.JDKIntro.IP.pdfSelect Kotlin/JVM Additional Libraries and Frameworks 3. Keep Create project

IntelliJ IDEA

Winter 2020 CS260 - Intro to Java & Android

• Create a new Project called punetidRational

• Right-click src, New->Packageedu.pacificu.cs.rational

• Copy Rational.java code from CS260-01Public\2020

• Right-click package, New->Java ClassRational.java

• Right-click package, New->Java ClassRationalDriver.java

• Create a Rational object in main that represents ½

• Output the rational number using method print

12

Don’t type numerator & denominator

Page 13: CS260 Intro to Java & Android 01.JDK Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/01.JDKIntro.IP.pdfSelect Kotlin/JVM Additional Libraries and Frameworks 3. Keep Create project

Unit Testing

• Different schools of thought exist as to where to do unit tests– in a directory called test at the level of src

– in the same directory as the class definition

• For now we will create unit tests in the same directory as the class being tested

• I'm staying with JUnit4 instead of JUnit5. The URL lists differences between 4 & 5.

https://stackabuse.com/unit-testing-in-java-with-junit-5/

Winter 2020 CS260 - Intro to Java & Android 13

Page 14: CS260 Intro to Java & Android 01.JDK Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/01.JDKIntro.IP.pdfSelect Kotlin/JVM Additional Libraries and Frameworks 3. Keep Create project

Unit Testing

• Problem: Create a unit test for class Rational

1. Open the class file and place the cursor on the class name

2. Press Alt+Enter then select Create Test

3. See Figure 01.4 on next slide. Only create unit tests for getNumerator () and getDenominator ()

https://www.jetbrains.com/help/idea/create-tests.html

Winter 2020 CS260 - Intro to Java & Android 14

Page 15: CS260 Intro to Java & Android 01.JDK Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/01.JDKIntro.IP.pdfSelect Kotlin/JVM Additional Libraries and Frameworks 3. Keep Create project

Unit Testing

• Figure 01.4

• Run RationalTest

Winter 2020 CS260 - Intro to Java & Android 15

Page 16: CS260 Intro to Java & Android 01.JDK Introzeus.cs.pacificu.edu/ryand/cs260/2020/Lectures/01.JDKIntro.IP.pdfSelect Kotlin/JVM Additional Libraries and Frameworks 3. Keep Create project

Unit Testing

Winter 2020 CS260 - Intro to Java & Android 16

• Might need to add Junit to your pathonce again, alt-enter is your friend

• Let's write tests for getNumerator, getDenominator

• http://junit.sourceforge.net/javadoc/org/junit/Assert.html