cs260 intro to java & android 01.jdk...
TRANSCRIPT
CS260 Intro to Java & Android
01.JDK Intro
Winter 2020
Winter 2020 CS260 - Intro to Java & Android 1
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
Hello World
public class HelloWorld
{
public static void main (String[] args)
{
System.out.println ("Hello World");
}
}
Winter 2020 CS260 - Intro to Java & Android 3
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
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
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
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
Compile & Execute HelloWorld
Figure 01.2
Winter 2020 CS260 - Intro to Java & Android 8
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
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
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
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
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
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
Unit Testing
• Figure 01.4
• Run RationalTest
Winter 2020 CS260 - Intro to Java & Android 15
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