First Java Programs. V. Paúl Pauca. CSC 111D Fall, 2015. Department of Computer Science Wake Forest University. Introduction to Computer Science



Similar documents
Pemrograman Dasar. Basic Elements Of Java

Chapter 2 Introduction to Java programming

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

CS 106 Introduction to Computer Science I

Chapter 1 Java Program Design and Development

ECE 122. Engineering Problem Solving with Java

Introduction to Java

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

J a v a Quiz (Unit 3, Test 0 Practice)

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Install Java Development Kit (JDK) 1.8

Lecture 5: Java Fundamentals III

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Java Crash Course Part I

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

CS 106 Introduction to Computer Science I

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Variables, Constants, and Data Types

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

AP Computer Science Java Subset

JavaScript: Control Statements I

Introduction to Java. CS 3: Computer Programming in Java

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Computer Programming Tutorial

Chapter 3. Input and output. 3.1 The System class

Programming Languages CIS 443

Chapter 2: Elements of Java

Using Files as Input/Output in Java 5.0 Applications

Week 1: Review of Java Programming Basics

Introduction to Programming

Crash Course in Java

Java Programming Fundamentals

Moving from CS 61A Scheme to CS 61B Java

Building Java Programs

13 File Output and Input

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Java Basics: Data Types, Variables, and Loops

1001ICT Introduction To Programming Lecture Notes

Building Java Programs

Chapter 2 Elementary Programming

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Some Scanner Class Methods

Topic 11 Scanner object, conditional execution

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Introduction to Object-Oriented Programming

Programming and Data Structures with Java and JUnit. Rick Mercer

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

Part I. Multiple Choice Questions (2 points each):

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

Java Interview Questions and Answers

Java CPD (I) Frans Coenen Department of Computer Science

Sources: On the Web: Slides will be available on:

public static void main(string[] args) { System.out.println("hello, world"); } }

02 B The Java Virtual Machine

The following program is aiming to extract from a simple text file an analysis of the content such as:

Introduction to Java Applets (Deitel chapter 3)

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Third AP Edition. Object-Oriented Programming and Data Structures. Maria Litvin. Gary Litvin. Phillips Academy, Andover, Massachusetts

Installing Java (Windows) and Writing your First Program

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Programmierpraktikum

CS 111 Classes I 1. Software Organization View to this point:

Konzepte objektorientierter Programmierung

LAB4 Making Classes and Objects

Java from a C perspective. Plan

Programming in Java Course Technology, a part of Cengage Learning.

CS 1302 Ch 19, Binary I/O

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

Sample CSE8A midterm Multiple Choice (circle one)

Building Java Programs

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Computers. An Introduction to Programming with Python. Programming Languages. Programs and Programming. CCHSG Visit June Dr.-Ing.

Introduction to Python

Informatica e Sistemi in Tempo Reale

University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python

Example of a Java program

JavaScript: Introduction to Scripting Pearson Education, Inc. All rights reserved.

CS106A, Stanford Handout #38. Strings and Chars

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

AP Computer Science Static Methods, Strings, User Input

Arrays in Java. Working with Arrays

Explain the relationship between a class and an object. Which is general and which is specific?

Basic Java Syntax. Program Structure

Object Oriented Software Design

Reading Input From A File

Transcription:

First Java Programs V. Paúl Pauca Department of Computer Science Wake Forest University CSC 111D Fall, 2015

Hello World revisited / 8/23/15 The f i r s t o b l i g a t o r y Java program @author Paul Pauca / public class Hello { public s t a t i c void main ( S t r i n g [ ] args ) { System. out. p r i n t l n ( " Hello World! " ) ; } }

Using IntelliJ IDEA Create project: Create a new package: Right click src: New > Package Create a Java class: Right click on the package: New > Java class To compile: right click anywhere in your code: Choose Run Hello.main() Output is shown in the console

Analyzing the Program I Comments Everything in between /** / This l i n e i s ignored This one too / */ is ignored A line starting with // is ignored / / This l i n e i s ignored This i s not Comments are for the human user only

Analyzing the Program II Defining a Java class public class MyClass { } public : means any other class can use this class class : means we are defining a class { } : delimits the body of the class

Analyzing the Program III Defining the main method public s t a t i c void main ( S t r i n g [ ] args } { } main method: Group of instructions, named main public, static, void : specify attributes of main { } delimits body of the method Methods must always be defined inside a class!

Analyzing the Program IV Instructions for producing output System. out. p r i n t l n ( " Hello there " ) ; Instructions must be specified inside a method! System.out : An object for controlling output System.out.println() : tells System.out to print to the screen "Hello there" : anything inside " and " is printed literally to the screen

Analyzing the Program V Compiling and Running the program Compile: check if program syntax is correct, produce executable code Run the code: Execute every instruction in main() one after the other, top down Program stops when the last instruction is executed

Fancy Hello World revisited / 8/23/13 The f a n c i e r h e l l o world @author Paul Pauca / import javax. swing. JOptionPane ; public class FancyHello { public s t a t i c void main ( S t r i n g [ ] args ) { JOptionPane. showmessagedialog ( null, " Hello World! ", " Display Message ", JOptionPane. INFORMATION_MESSAGE ) ; System. out. p r i n t l n ( " Goodbye c r u e l p l a n e t... " ) ; } }

Analyzing the Program I Output in Dialog Windows JOptionPane.showMessageDialog() needs the javax.swing package IntelliJ: imports packages automatically with: Alt + Enter In Mac: Option + Enter showmessagedialog() method : needs 4 arguments separated by commas 1 null value (indicates absence, no value) 2 Message to show in the dialog window (in double quotes) 3 Name of dialog window (in double quotes) 4 INFORMATION_MESSAGE

Java Syntax I Case-sensitivity Case matters, e.g. public and Public are not the same! White spaces Separate identifiers in Java instructions public static void main(string[] args) Number of white spaces (including new lines) don t matter to Java, i.e. public static void main( String[] args) Use white spaces to improve readability

Java Syntax II Identifiers Words used in Java code, can be reserved (e.g. class, public) or made up by programmer, (e.g. the name of a class) Rule to make an identifier: Must start with a character, can use numbers, cannot use spaces or punctuation symbols (e.g., :. ( ], etc) Examples 1HelloWorld (bad) 1counter (bad) Hello World (bad) count er (bad) Hello,World (bad) count,er (bad) HelloWorld1 (good) counter1 (good) HelloWorldOne (good) counterone (good) Reserved words

Java Syntax III Java reserves certain words and associates them with particular syntax in the language. abstract default goto* package this assert do if private throw boolean double implements protected throws break else import public transient byte enum instanceof return true case extends int short try catch false interface static void char final long strictfp volatile class finally native super while const* float new switch continue for null synchronized You cannot use these words in any other than the way intended by Java syntax rules.

Java Syntax IV Literals Values that you can use in your source code Some examples: "Hello World" (string literal), 1 (integer literal), null, (no value literal) Literals are not identifiers

Java Syntax V Punctuation symbols E.g.: ( ) { } [ ] < > : ;,. @ ( { [ are not interchangeable! Semicolon ; terminates instructions, e.g. System. out. p r i n t l n ( " Hello World! " ) ; System. out. p r i n t l n ( " Goodbye c r u e l p l a n e t " ) ; More on other punctuation symbols later

Putting It All Together Debugging. Find the errors (bugs) in the following piece of code. public Class HelloWorld ( System. Out. p r i n t l n ( Hello World! ), / p r i n t the sum of 345332 and 424948 System. Out p r i n t l n (345332+424948), JOptionPane. showmessagedialog ( null ; " Hello World! " ; Display Message ; JOptionPane. INFORMATION_MESSAGE), )

Getting User Input I Scanner object Need a Scanner object and System.in to read data from keyboard Scanner i n p u t = new Scanner ( System. i n ) ; input is an identifier you make up as the programer to be the name of the Scanner object

Getting User Input II Creating a variable to store user data Need a variable in the program to store data entered with the keyboard S t r i n g data ; Variables are stored in memory while the program runs data is an identifier you make up as the programmer String is the type of literal that data can store

Getting User Input III Using the Scanner object data = i n p u t. next ( ) ; input.next() : captures anything typed by the user, until the Return key is typed = stores the data captured by input.next() into variable data Now the content data and can be used in your program

Putting It Together public class MySecondProgram { public s t a t i c void main ( S t r i n g [ ] args ) { / / Create the Scanner o b j e c t Scanner i n p u t = new Scanner ( System. i n ) ; / / Create S t r i n g v a r i a b l e S t r i n g data ; / / Ask the user f o r h i s name and save i t i n data System. out. p r i n t ( " What i s your name? " ) ; data = i n p u t. next ( ) ; } } / / Use v a r i a b l e data System. out. p r i n t l n ( " Nice to meet you " + data ) ; System. out. p r i n t l n ( "My name i s SIRI " ) ; The symbol + concatenates two strings