Introduction to JAVA

Similar documents
CS 106 Introduction to Computer Science I

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

Introduction to Computer Science I

Getting started with Java

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

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

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Primitive Data, Variables, and Expressions; Simple Conditional Execution

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

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

CSc 2010 Principles of Computer Science, Fall 2013 Practice Problems for Midterm 3* * 3 17 % 9-20 % (26 / 7) "2"

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

Section 2: Introduction to Java. Historical note

AP Computer Science Unit 1. Programs

Program Fundamentals

What did we talk about last time? Examples switch statements

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

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

Fundamentals of Programming. By Budditha Hettige

Getting Started Values, Expressions, and Statements CS GMU

This tutorial will teach you about operators. Operators are symbols that are used to represent an actions used in programming.

Lecture Set 2: Starting Java

CHAPTER INTRODUCTION

Programming with Java

Lecture Set 2: Starting Java

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

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

First Java Program - Output to the Screen

Basic Programming Language Syntax

Object-Oriented Programming

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

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

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

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

CS111: PROGRAMMING LANGUAGE II

A variable is a name for a location in memory A variable must be declared

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

How to make a "hello world" program in Java with Eclipse *

Chapter 1 Introduction to java

Lab # 2. For today s lab:

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

Definite Loops. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Using a Variable for Counting

Welcome to CSE 142! Whitaker Brand. University of Washington, Winter 2018

Building Java Programs

Welcome to CSE 142! Zorah Fung University of Washington, Spring Building Java Programs Chapter 1 Lecture 1: Introduction; Basic Java Programs

Week 2: Data and Output

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

Introduction to Computer Programming

COMP 202 Java in one week

Programs, Statements, Variables

Building Java Programs

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs.

Course Outline. Introduction to java

SDKs - Eclipse. SENG 403, Tutorial 2

Variables and data types

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

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

4 Programming Fundamentals. Introduction to Programming 1 1

Lesson 5: Introduction to the Java Basics: Java Arithmetic THEORY. Arithmetic Operators

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

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

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Introduction to Java Applications

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

Chapter 2, Part I Introduction to C Programming

Lecture 3: Variables and assignment

Introduction to TURING

Full file at

Lecture 2: Operations and Data Types

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

B.V. Patel Institute of BMC & IT, UTU 2014

Table of Contents Date(s) Title/Topic Page #s. Abstraction

Chapter. Let's explore some other fundamental programming concepts

Java: Comment Text. Introduction. Concepts

TUGCE KEMEROZ - ASLI OZKAN - AYSE TARTAN. Week 12/02/ /02/2007 Lecture Notes:

An Introduction to Python (TEJ3M & TEJ4M)

Copyright 1999 by Deitel & Associates, Inc. All Rights Reserved.

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

4. Java Project Design, Input Methods

Building Java Programs Chapter 2

Algorithms and Programming I. Lecture#12 Spring 2015

17 Hello world 18 Type: String: literal 19 Standard API: System: out.println() 20 Hello world 21 Statement 22 Statement: simple statements are ended w

Programming for Engineers Introduction to C

CSE 142. Lecture 1 Course Introduction; Basic Java. Portions Copyright 2008 by Pearson Education

Get JAVA. I will just tell you what I did (on January 10, 2017). I went to:

List of Slides 1 Title 2 Chapter 2: Sequential execution and program errors 3 Chapter aims 4 Section 2: Example:Hello world 5 Aim 6 Class: programs ar

Java Bytecode (binary file)

Conditionals, Loops, and Style. Control flow thus far. if statement. Control flow. Common branching statement Evaluate a boolean expression

Conditionals, Loops, and Style

CS 231 Data Structures and Algorithms, Fall 2016

Introduction to Programming in Turing. Input, Output, and Variables

CS106A, Stanford Handout #25. Methods 2

Computer Programming I - Unit 2 Lecture 1 of 13

STUDENT LESSON A7 Simple I/O

Transcription:

Java A001 Hello World Let's study the entire program below: Introduction to JAVA // The "A001" class. import java.awt.*; public class A001 { public static void main (String[] args) { System.out.println("Hello World!"); } // end of main method } // end of A001 class Any portion of the program that starts with "//" is a comment. It is completely ignored by the program, and it only there as an aid to the programmer. (Use lots of comments!) The line that starts with "import" are needed to include specific libraries (called classes in java). The class imported here is the Abstract Window Toolkit (awt) class. The section that starts with "public class A001" is the actual A001 class. The class is always surrounded by braces ("{ }") and is always named starting with a capital letter. See if you can spot the start of the A001 class and the end of it. We then get to the section starting with "public static void main". Believe it or not, this is the (finally) the start of our program. This is where we can concentrate on making the program do what we want it to do. Don't worry too much about what the specific words on this line mean. It will all make sense to you when you start learning more about java. For now, trust that the program must be written this way and trust that you will understand it in due time. Notice that this section is also surrounded with braces. And now, we finally get to the part of the program that does something for us "System.out.println("Hello World!");" This is the line that prints "Hello World!" to the screen. Copy the program exactly the way it is above and then run it. Save as "A001.java".

Java A002 Printing Lots of Lines Create a class named A002. Add two "System.out.println()" statements, one printing your first name, the second printing your last name. Run the program to see what happens. Save as "A002.java". Java A003 Printing Lots of Lines (sort of) Load A002. Replace "A002" with "A003" throughout your program. (Use the Search, Replace menu item to do this.) Save as "A003.java". Change the program by replacing the "println" methods with "print". Run the program to see what happens. What can you say is the difference between println and print? Does the output look okay? See if you can fix the output so it looks better. Re-save your program. Java A004 Concatenation You have now learned that you can use two print() statements to print two things on the same line. But there is a different way that is sometimes more efficient. Put the following code snippets into your program to observe any differences, if any. // print on the same line System.out.print ("abc" + "def"); // separator System.out.println (); // two print statements System.out.print ("abc"); System.out.print ("def"); Notice that the two sets of output are the same? The first method, using the "+" operator to join two "strings" together is called concatenation. It is a very common way to join two or more strings together. Save as "A004.java".

Java A005 Concatenation Assignment Using the knowledge you learned in A004 about concatenation, write a program that produces an output that looks like the following screenshot: It would be easy to print each line using a single println() statement, but for this assignment you must print each word or each series of spaces within its own set of quotes. For example, the first line would be produced with the following statement: System.out.println(" " + "PETE'S" + " " + "CLOTHIER"); Is this the most efficient? No, but it is useful to teach you how concatenation works. Be sure all lines line up the way they do in the screenshot. Save as "A005.java".

Java A006 Easy Math Java math follows the same basic set of precedence rules as regular math BEDMAS. There are some other precedence rules to follow, but they will come a little later on. The following symbols are used to represent the four primary mathematical operators: + addition - subtraction * multiplication / division Exponents are handled quite differently in java, so we will discuss those in a lesson on their own. Also, division in java sometimes does some funny things (try the statement System.out.println(5/2); to see what I mean), so that too will get special treatment in its own lesson. For this assignment, you are to load A005, modify it so it can be saved as A007, then modify the subtotals and the grand total so they are performing the relevant math. Save as "A006.java". Java A007 Field Widths Load program A006, then modify to save as A007. Create a new class application and add the following lines to see what I mean: System.out.print ("PETE'S "); System.out.println ("CLOTHIERS"); If you count the number of spaces taken up by the "PETE'S" string, you will see that it equals 10 spaces. The string "\t" represents a tab, while "\n" represents a new line. These escape characters can be inserted anywhere in a string; they do not have to be used on their own. System.out.println ("PETE'S\t\t\nCLOTHIERS"); Save as "A007.java".

Java A008 Escape Characters The string "\t" represents a tab, while "\n" represents a new line. These escape characters can be inserted anywhere in a string; they do not have to be used on their own. For example, the statement System.out.println("A\tB\nC\t\tD"); produces the following output: Write a program that produces an output similar to the screenshot in A005. The output does not have to look exactly the same; just try to line up the columns using tabs. Do not use "println()" methods in your program, only "print()". Java A009 Times Tables Write a program that uses escape characters and the suitable mathematical operators to produce a neatly formatted times table from 2 x 1 to 2 x 6 similar to the following diagram: Save as "A009.java"..