Chapter 2 Introduction to Java programming

Similar documents
Week 1: Review of Java Programming Basics

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

Introduction to Java

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

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

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

Pemrograman Dasar. Basic Elements Of Java

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

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

Chapter 2: Elements of Java

Introduction to Java. CS 3: Computer Programming in Java

Building Java Programs

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

Example of a Java program

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

AP Computer Science Java Subset

Building Java Programs

Topic 11 Scanner object, conditional execution

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

JavaScript: Control Statements I

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

Chapter 1 Java Program Design and Development

ECE 122. Engineering Problem Solving with Java

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

Programming and Data Structures with Java and JUnit. Rick Mercer

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

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

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

Sample CSE8A midterm Multiple Choice (circle one)

Java Interview Questions and Answers

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

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

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

Install Java Development Kit (JDK) 1.8

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

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

An Overview of Java. overview-1

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

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

Java Programming Fundamentals

Using Files as Input/Output in Java 5.0 Applications

Building Java Programs

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

Comp 248 Introduction to Programming

Object Oriented Software Design

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

Homework/Program #5 Solutions

Crash Course in Java

13 File Output and Input

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

Java Basics: Data Types, Variables, and Loops

Arrays. Introduction. Chapter 7

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

Variables, Constants, and Data Types

Chapter 2 Elementary Programming

Object-Oriented Programming in Java

CS 121 Intro to Programming:Java - Lecture 11 Announcements

Chapter 2 Basics of Scanning and Conventional Programming in Java

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

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

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

Chapter 3. Input and output. 3.1 The System class

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

Lecture 5: Java Fundamentals III

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

Computer Programming Tutorial

Course Intro Instructor Intro Java Intro, Continued

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

1.00/ Session 2 Fall Basic Java Data Types, Control Structures. Java Data Types. 8 primitive or built-in data types

JAVA ARRAY EXAMPLE PDF

Lecture 1 Introduction to Java

PL / SQL Basics. Chapter 3

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

Moving from CS 61A Scheme to CS 61B Java

Basics of Java Programming Input and the Scanner class

CS 106 Introduction to Computer Science I

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

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

Object Oriented Software Design

Classes and Objects. Agenda. Quiz 7/1/2008. The Background of the Object-Oriented Approach. Class. Object. Package and import

AP Computer Science Static Methods, Strings, User Input

D06 PROGRAMMING with JAVA

More on Objects and Classes

Introduction to Programming

WRITING DATA TO A BINARY FILE

LOOPS CHAPTER CHAPTER GOALS

Java from a C perspective. Plan

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

VB.NET Programming Fundamentals

Assignment No.3. /*-- Program for addition of two numbers using C++ --*/

AP Computer Science File Input with Scanner

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

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

java Features Version April 19, 2013 by Thorsten Kracht

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Building Java Programs

CS506 Web Design and Development Solved Online Quiz No. 01

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

Java 進 階 程 式 設 計 03/14~03/21

Transcription:

Chapter 2 Introduction to Java programming 1

Keywords boolean if interface class true char else package volatile false byte final switch while throws float private case return native void protected break throw implements short public default try import double static for catch synchronized int new continue finally const long this do transient goto abstract super extends instanceof null 2

A public class that contains a main method public class InvoiceApp // declare the class { // begin the class public static void main(string[] args) { System.out.println( "Welcome to the Invoice Total Calculator"); } } // end the class 3

The same class with different brace placement public class InvoiceApp { // declare and begin the class public static void main(string[] args){ System.out.println( "Welcome to the Invoice Total Calculator"); } } // end the class 4

The rules for naming a class Start the name with a capital letter. Use letters and digits only. Follow the other rules for naming an identifier. Recommendations for naming a class Start every word within a class name with an initial cap. Each class name should be a noun or a noun that s preceded by one or more adjectives. 5

How to declare and initialize a variable in one statement Syntax type variablename = value; Examples int scorecounter = 1; // initialize an integer variable double unitprice = 14.95; // initialize a double variable 6

How to code assignment statements int quantity = 0; int maxquantity = 100; // initialize an // integer variable // initialize another // integer variable // two assignment statements quantity = 10; // quantity is now 10 quantity = maxquantity; // quantity is now 100 7

Naming recommendations for variables Start variable names with a lowercase letter and capitalize the first letter in all words after the first word. Each variable name should be a noun or a noun preceded by one or more adjectives. Try to use meaningful names that are easy to remember. 8

The basic operators for arithmetic expressions Operator Name + Addition - Subtraction * Multiplication / Division 9

Statements that use simple arithmetic expressions // integer arithmetic int x = 14; int y = 8; int result1 = x + y; // result1 = 22 int result2 = x - y; // result2 = 6 int result3 = x * y; // result3 = 112 int result4 = x / y; // result4 = 1 // double arithmetic double a = 8.5; double b = 3.4; double result5 = a + b; // result5 = 11.9 double result6 = a - b; // result6 = 5.1 double result7= a * b; // result7 = 28.9 double result8 = a / b; // result8 = 2.5 10

Statements that increment a counter variable int invoicecount = 0; invoicecount = invoicecount + 1; // invoicecount = 1 invoicecount = invoicecount + 1; // invoicecount = 2 Statements that add amounts to a total double invoiceamount1 = 150.25; double invoiceamount2 = 100.75; double invoicetotal = 0.0; invoicetotal = invoicetotal + invoiceamount1; // invoicetotal = 150.25 invoicetotal = invoicetotal + invoiceamount2; // invoicetotal = 251.00 Statements that mix int and double variables int result9 = invoicetotal / invoicecount // result9 = 125 double result10 = invoicetotal / invoicecount // result10 = 125.50 11

The syntax for declaring and initializing a string variable String variablename = value; Statements that declare and initialize a string String message1 = "Invalid data entry."; String message2 = ""; String message3 = null; 12

How to join strings String firstname = "Bob"; // firstname is Bob String lastname = "Smith"; // lastname is Smith String name = firstname + " " + lastname; // name is Bob Smith How to join a string and a number double price = 14.95; String pricestring = "Price: " + price; 13

How to append one string to another with the + operator firstname = "Bob"; // firstname is Bob lastname = "Smith"; // lastname is Smith name = firstname + " "; // name is Bob followed by a space name = name + lastname; // name is Bob Smith How to append one string to another with the += operator firstname = "Bob"; // firstname is Bob lastname = "Smith"; // lastname is Smith name = firstname + " "; // name is Bob followed by a space name += lastname; // name is Bob Smith 14

Common escape sequences \n \t \r \" \\ 15

A string with a new line String "Code: JSPS\nPrice: $49.50" Result Code: JSPS Price: $49.50 A string with tabs and returns String "Joe\tSmith\rKate\tLewis" Result Joe Kate Smith Lewis 16

A string with quotation marks String "Type \"x\" to exit" Result Type "x" to exit A string with a backslash String "C:\\java\\files" Result C:\java\files 17

How to create an object from a class Syntax ClassName objectname = new ClassName(arguments); Examples Scanner sc = new Scanner(System.in); // creates a Scanner object named sc Date now = new Date(); // creates a Date object named now How to call a method from an object Syntax objectname.methodname(arguments) Examples double subtotal = sc.nextdouble(); // get a double entry from the console String currentdate = now.tostring(); // convert the date to a string 18

How to call a static method from a class Syntax ClassName.methodName(arguments) Examples String sprice = Double.toString(price); // convert a double to a string double total = Double.parseDouble(userEntry); // convert a string to a double 19

Common packages java.lang java.text java.util java.io java.sql java.applet java.awt java.awt.event javax.swing 20

The syntax of the import statement import packagename.classname; or import packagename.*; Examples import java.text.numberformat; import java.util.scanner; import java.util.*; import javax.swing.*; 21

How to use the Scanner class to create an object With an import statement Scanner sc = new Scanner(System.in); Without an import statement java.util.scanner sc = new java.util.scanner(system.in); 22

Relational operators Operator Name == Equality!= Inequality > Greater Than < Less Than >= Greater Than Or Equal <= Less Than Or Equal 23

Examples of conditional expressions discountpercent == 2.3 // equal to a numeric literal subtotal!= 0 // not equal to a numeric literal years > 0 // greater than a numeric literal i < months // less than a numeric variable subtotal >= 500 // greater than or equal to a numeric literal quantity <= reorderpoint // less than or equal to a numeric variable 24

Two methods of the String class equals(string) equalsignorecase(string) Examples userentry.equals("y") // equal to a string literal userentry.equalsignorecase("y") // equal to a string literal (!lastname.equals("jones")) // not equal to a string literal code.equalsignorecase(productcode) // equal to another string variable 25

The syntax of the if/else statement if (booleanexpression) {statements} [else if (booleanexpression) {statements}]... [else {statements}] 26

If statements without else if or else clauses With a single statement if (subtotal >= 100) discountpercent =.2; With a block of statements if (subtotal >= 100) { discountpercent =.2; status = "Bulk rate"; } 27

An if statement with an else clause if (subtotal >= 100) discountpercent =.2; else discountpercent =.1; An if statement with else if and else clauses if (customertype.equals("t")) discountpercent =.4; else if (customertype.equals("c")) discountpercent =.2; else if (subtotal >= 100) discountpercent =.2; else discountpercent =.1; 28

A loop that calculates the sum of the numbers 1 through 4 int i = 1; int sum = 0; while (i < 5) { sum = sum + i; i = i + 1; } 29