CS 152: Data Structures with Java Hello World with the IntelliJ IDE

Similar documents
Anatomy of a Java Program: Comments

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Please answer the following questions. Do not re-code the enclosed codes if you have already completed them.

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Java Bytecode (binary file)

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming

Course Outline. Introduction to java

Input. Scanner keyboard = new Scanner(System.in); String name;

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Pace University. Fundamental Concepts of CS121 1

CS 302: Introduction to Programming

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Full file at

AP Computer Science Unit 1. Writing Programs Using BlueJ

Object-oriented programming in...

Lab # 2. For today s lab:

Full file at

AP Computer Science Unit 1. Programs

Lecture Set 2: Starting Java

Introduction to Java. Java Programs Classes, Methods, and Statements Comments Strings Escape Sequences Identifiers Keywords

COMP 202 Java in one week

Lecture Set 2: Starting Java

Lab5. Wooseok Kim

CS 231 Data Structures and Algorithms, Fall 2016

I. Variables and Data Type week 3

COMP 110 Project 1 Programming Project Warm-Up Exercise

2.8. Decision Making: Equality and Relational Operators

Introduction to Java Applications; Input/Output and Operators

AP Computer Science Unit 1. Writing Programs Using BlueJ

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

What did we talk about last time? Examples switch statements

Assignment Marking Criteria

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

Midterms Save the Dates!

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Introduction to Java Unit 1. Using BlueJ to Write Programs

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

COMP 202 Java in one week

3 CREATING YOUR FIRST JAVA APPLICATION (USING WINDOWS)

CHAPTER 2 Java Fundamentals

Section 2.2 Your First Program in Java: Printing a Line of Text

Chapter 2: Programming Concepts

St. Edmund Preparatory High School Brooklyn, NY

Getting Started With Java

Chapter 1 Lab Algorithms, Errors, and Testing

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Chapter 4: Conditionals and Recursion

STUDENT LESSON A7 Simple I/O

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

AP Computer Science Java Mr. Clausen Program 6A, 6B

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

SDKs - Eclipse. SENG 403, Tutorial 2

IT 374 C# and Applications/ IT695 C# Data Structures

Chapter 2: Java Fundamentals

8/23/2014. Chapter Topics. Chapter Topics (2) Parts of a Java Program. Parts of a Java Program. Analyzing The Example. Chapter 2: Java Fundamentals

Basic Computation. Chapter 2

CSS 161 Fundamentals of Compu3ng. Assignments, Expressions, Operators, Console input & output October 1, 2012

Software and Programming 1

CS-201 Introduction to Programming with Java

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Object-Oriented Programming in Java

Programming in Java Prof. Debasis Samanta Department of Computer Science Engineering Indian Institute of Technology, Kharagpur

Do this by creating on the m: drive (Accessed via start menu link Computer [The m: drive has your login id as name]) the subdirectory CI101.

Array. Prepared By - Rifat Shahriyar

Chapter 4: Loops and Files

Using Java Classes Fall 2018 Margaret Reid-Miller

S8352: Java From the Very Beginning Part I - Exercises

CS11 Java. Fall Lecture 1

CS 106 Introduction to Computer Science I

Lec 3. Compilers, Debugging, Hello World, and Variables

Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal

Basic Computation. Chapter 2

Week 2: Data and Output

Text User Interfaces. Keyboard IO plus

CS 231 Data Structures and Algorithms Fall Event Based Programming Lecture 06 - September 17, Prof. Zadia Codabux

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

Section 003 Fall CS 170 Exam 1. Name (print): Instructions:

CS111: PROGRAMMING LANGUAGE II

Objectives. Problem Solving. Introduction. An overview of object-oriented concepts. Programming and programming languages An introduction to Java

Lecture 02, Fall 2018 Friday September 7

Darrell Bethea May 10, MTWRF 9:45-11:15 AM Sitterson 011

Section 2: Introduction to Java. Historical note

13 th Windsor Regional Secondary School Computer Programming Competition

Introduction to Java & Fundamental Data Types

CS520 Setting Up the Programming Environment for Windows Suresh Kalathur. For Windows users, download the Java8 SDK as shown below.

Programming Exercise 7: Static Methods

M e t h o d s a n d P a r a m e t e r s

! definite loop: A loop that executes a known number of times. " The for loops we have seen so far are definite loops. ! We often use language like

CHAPTER 7 OBJECTS AND CLASSES

Chapter Two Bonus Lesson: JavaDoc

A token is a sequence of characters not including any whitespace.

Creating a Program in JCreator. JCreator is then used to create our program. But the first step is to create a new file.

Chapter 4: Loops and Files

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Arrays

Software Installation for CS121

Introduction to Java Applications

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

Transcription:

CS 152: Data Structures with Java Hello World with the IntelliJ IDE Instructor: Joel Castellanos e-mail: joel.unm.edu Web: http://cs.unm.edu/~joel/ Office: Electrical and Computer Engineering building (ECE). Room 233 8/21/2017

Install Java Development Kit (JDK) 1.8 http://www.oracle.com/technetwork/java/javase/downloads/index.html Most computers already have the Java Runtime Environment installed. However, to create Java programs, the Java Development Kit is required. Note: the JDK includes a copy of the matching version of the JRE. Download and Install JDK 1.8 2

IntelliJ IDE (Integrated Development Environment) https://www.jetbrains.com/idea/download/ IntelliJ Community Edition (free) version 14.1. Java is a Programming Language. IntelliJ is an Integrated Development Environment. 3

Project Default Settings (Code Style) Use to change the code style and other settings that will be used for all new projects created. 4

Code Style: IntelliJ File Settings Manage to create custom scheme. 5

6 Code Style: Braces

7 IntelliJ: Create New Project (1 of 5)

IntelliJ: Create New Project (2 of 5) If the Project SDK dropdown does not contain Java 1.8, then the Java JDK has not been correctly installed (see slide 1). 8

9 IntelliJ: Create New Project (3 of 5)

IntelliJ: Create New Project (4 of 5) This creates a folder in the project root location. For the first half of CS 259, all our projects will use what is called the default package. If you are using a lab computer, select a folder on your USB drive as the root. The default package has a blank Base package. 10

IntelliJ: Create New Project (5 of 5) Change project panel to Project Files Notice highlighting of matching braces at curser position. 11

12 Application: Add Inputs (Similar to Listing 1.1) 1. import java.util.scanner; 2. 3. 4. public class Toy 5. { 6. public static void main(string[] args) 7. { 8. System.out.println("Hello World"); Filename MUST be Toy.java From IntelliJ Project Files panel, Right-click filename and select: Refactor Rename 9. System.out.println("I want two numbers!"); 10. 11. Scanner keyboard = new Scanner(System.in); 12. int n1 = keyboard.nextint(); 13. int n2 = keyboard.nextint(); 14. 15. System.out.println(n1+n2); 16. } 17.}

Compile, Run, and the Console 1. System.out.println("Hello World"); 2. System.out.println("I want two numbers!"); 3. 4. Scanner keyboard = new Scanner(System.in); 5. int n1 = keyboard.nextint(); //reads characters until 6. int n2 = keyboard.nextint(); 7. 8. System.out.println(n1+n2); 13 Compile and Run User Input

Keyword: import 1. import java.util.scanner; 2. 3. 4. 5. 6. 7. 8. public class Toy_1_1 9. { 10. public static void main(string[] args) 11. { 12. System.out.println("Hello World"); 13. Gets the Scanner class from the package (library) java.util. All import statements are placed at the top of the source file. Note: The Java programming language is case-sensitive. 14

Keyword: class 15 1. import java.util.scanner; 2. 3. public class Toy 4. { 5. public static void main(string[] args) In Java, all code is enclosed in a class. 6. { 7. System.out.println("Hello World"); 8. System.out.println("I start with an upper-case want letter. two numbers!"); 9. 10. Scanner keyboard = new Scanner(System.in); 11. int The n1 class = keyboard.nextint(); header must be followed by an 12. int n2 open = keyboard.nextint(); curly bracket, {, and ended with a 13. matching close curly bracket, }. 14. System.out.println(n1+n2); 15. } 16.} class header. Class names are your choice but... should All code within those brackets is part of the class.

Method Signature 16 1. import java.util.scanner; 2. 3. public class Toy 4. { 5. public static void main(string[] args) 6. { 7. System.out.println("Hello World"); 8. System.out.println("I Every Java application must want have two a numbers!"); main method. 9. By convention, method names start with a lower- 10. Scanner keyboard = new Scanner(System.in); case letter. 11. int n1 = keyboard.nextint(); 12. int A method n2 = keyboard.nextint(); signature must be followed by an 13. opening curly bracket { and a matching closing 14. System.out.println(n1+n2); 15. } 16.} Line 5 is a method signature. curly bracket }. Code within the brackets is part of the method.

Keyword: public 17 1. import java.util.scanner; 2. 3. public class Toy 4. { 5. public static void main(string[] args) 6. { 7. System.out.println("Hello World"); 8. 9. 10. 11. 12. 13. 14. The keyword public means that the class, field (variable), or method is visible to other classes. The outermost class in each Java source file must be public. The main method must be public.

Parts of a Method Signature public static void main(string[] args) static Discussed later. The method s argument list must be enclosed in parenthesis: (). main requires one argument : args which is a field of type String[] (an array of String objects). Method's return type (void, int, float, String,...) The return type is not part of the method signature. 18 Public: The method is visible outside the class.

Block Structure 1. public class Toy 2. { 3. // Comment inside Toy. 4. public static void main(string[] args) 5. { 6. // Comment inside main which is inside Toy. 7. System.out.println("Hello World"); 8. } 9. } 19 The nesting of curly brackets defines the block structure. Good programming style requires block indentation. In CS-259, we will use exactly two spaces per level.

Program Execution Entry Point 1. public class Toy 2. { 3. public static void main(string[] args) 4. { 5. When a Java program runs, code execution starts at main. Every Java application must have at least one class with a main method. It is ok for an application to have more than one class with main. When a user runs a Java application, the user must specify which class contains the main he or she wants to use. 20

Method Calls 1. System.out.println("Hello World"); 2. System.out.println("I want two numbers!"); Line 1 Calls the println method of the object System.out with the argument "Hello World". The println method takes a String object for an argument (input). The println method displays its argument in the Java Console and then displays a newline character, '/n'. 21

Keyword new: Instantiating a Class Scanner keyboard = new Scanner(System.in); Declares a field (with the programmer defined name keyboard) to be of type Scanner. The type Scanner is defined in import java.util.scanner argument list Creates an instance of the Scanner class. 22 The = symbol assigns keyboard to the memory address of the newly created instance of Scanner.

Reading User Input from the Keyboard 1. Scanner keyboard = new Scanner(System.in); 2. int n1 = keyboard.nextint(); 23 Line 2 does many things: 1) Defines n1 to be a field of type int. 2) Allocates memory for n1. 3) Calls the nextint method of the keyboard instance of Scanner. The nextint method: i. Reads input from the keyboard. ii. Echoes the input to the Console. iii. Tries to convert the user input to an int. iv. Returns the converted input. 4) Assigns the returned int value to n1.

End of Statement ; { Start of Block 24 1. import java.util.scanner; 2. 3. public class Toy 4. { 5. public static void main(string[] args) 6. { 7. System.out.println("Hello World"); 8. System.out.println("I want 2 numbers!"); 9. 10. Scanner keyboard = new Scanner(System.in); 11. int n1 = keyboard.nextint(); 12. int n2 = keyboard.nextint(); 13. 14. System.out.println(n1+n2); 15. } 16.}

Whitespace: Ignored Between Identifiers 1. System.out.println("Hello World"); 2. 3. System. out. 4. println ( 5. "Life is Good" 6. ); Single statement split across multiple lines. 25

Error: Whitespace Within Identifiers One extra space, causes 6 errors!! Tabs on scrollbar are hyperlinks to error locations. IntelliJ s suggestions can sometimes save typing, but often are incorrect.

Find and Fix the Syntax Error: #1 27 1. import java.util.scanner; 2. 3. public class Toy 4. { 5. public static void main(string[] args) 6. { 7. System.out.println("Input 2 numbers"); 8. 9. Scanner in = new Scanner(System.in); 10. int n1 = keyboard.nextint(); 11. int n2 = keyboard.nextint(); 12. 13. System.out.println(n1+n2); 14. } 15.}

Find and Fix the Syntax Error: #2 28 1. import java.util.scanner; 2. 3. public class Toy 4. { 5. public static void main(string[] args) 6. 7. System.out.println("Input 2 numbers"); 8. 9. Scanner bob = new Scanner(System.in); 10. int n1 = bob.nextint(); 11. int n2 = bob.nextint(); 12. 13. System.out.println(n1+n2); 14. } 15.}

Find and Fix the Syntax Error: #3 29 1. import java.util.scanner; 2. 3. public class Toy 4. { 5. public static void main(string[] args); 6. { 7. System.out.println("Input 2 numbers"); 8. 9. Scanner in = new Scanner(System.in); 10. int n1 = in.nextint(); 11. int n2 = in.nextint(); 12. 13. System.out.println(n1+n2); 14. } 15.}

30 Setting IntelliJ to Project Files View