A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to C++ Programming. A Simple Program: Print a Line of Text // My First C++ Program #include int main( ) { cout
Scanner Pepper With credits to Dr. Siegfried. The Scanner Class Most programs will need some form of input. At the beginning, all of our input will come.
Lab session 3 and 4 Topics to be covered Escape sequences Escape sequences Variables /identifiers Variables /identifiers Constants Constants assignment.
Strings as objects Strings are objects. Each String is an instance of the class String They can be constructed thus: String s = new String("Hi mom!");
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Console Input Using the Scanner Class Starting with version 5.0, Java includes a class for doing.
1 Chapter 2: Elementary Programming Shahriar Hossain.
1 Character Strings and Variables Character Strings Variables, Initialization, and Assignment Reading for this class: L&L,
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Computer Programming Lab(4).
Chapter 2 Section 2.2 Console Input Using The Scanner CLASS Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings.
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Chapter 2 Elementary Programming
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 4 Mathematical Functions, Characters,
Chapter 7 Strings  Use the String class to process fixed strings.  Use the StringBuffer class to process flexible strings.  Use the StringTokenizer.
Chapter 2: Java Fundamentals
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
String and Scanner CS 21a: Introduction to Computing I First Semester,
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
CHAPTER 5 GC 101 Input & Output 1. INTERACTIVE PROGRAMS  We have written programs that print console output, but it is also possible to read input from.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 3 Mathematical Functions, Strings, and Objects.
Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)
Lecture 4 – Scanner & Style. Console Output The console that starts a Java application is typically known as the standard output device. The standard.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Computer Programming with Java Chapter 2 Primitive Types, Assignment, and Expressions.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING String and Scanner Objects.
 CSC111 Quick Revision. Problem Write a java code that read a string, then show a list of options to the user to select from them, where:  L to print.
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Java Scanner Class Keyboard Class. User Interaction So far when we created a program there was no human interaction Our programs just simply showed one.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Introduction to programming in java
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings Chapter.
Chapter 4 Mathematical Functions, Characters, and Strings 1.
1.  Algorithm: 1. Read in the radius 2. Compute the area using the following formula: area = radius x radius x PI 3. Display the area 2.
(Dreaded) Quiz 2 Next Monday.
Strings CSE 1310 – Introduction to Computers and Programming
Lecture 4 – Scanner & Style
Strings CSE 1310 – Introduction to Computers and Programming
Chapter 4 Mathematical Functions, Characters, and Strings
Chapter 2 Clarifications
Strings CSE 1310 – Introduction to Computers and Programming
Input/Output.
Strings.
Maha AlSaif Maryam AlQattan
Char data type and the String class.
Something about Java Introduction to Problem Solving and Programming 1.
OUTPUT STATEMENTS GC 201.
Introduction to Classes and Methods
An overview of Java, Data types and variables
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
CSE 1321 Modules 1-5 Review Spring 2019
Input and Formatted Output (printf)
Presentation transcript:

A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[] args){ System.out.println( i ); int i = 20; int i = 20;}}

A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[] args) { int lenSquare = 4; areaSquare = lenSquare * lenSquare; areaSquare = lenSquare * lenSquare; System.out.println( areaSquare ); System.out.println( areaSquare ); }}

A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[] args) { int x = 2; int y = 3; int z = x%3; System.out.println(“z"); System.out.println(“z"); }}

A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[] args) { int lenSquare = 4; int areaSquare = lenSquare * lenSquare; System.out.println(“area of a square is: “+areaSquare+” cm”); System.out.println(“area of a square is: “+areaSquare+” cm”); }}

Variable Declaration  Name  Data type  Initialize with value double radius = 2.0;

Screen Output Using System.out.print/println int radius = 5; System.out.println(“radius”); Output: radius System.out.println(radius); Output: 5 System.out.println(“radius: “+radius); Output: radius: 5

Escape Sequences for Special Characters Description Escape Sequence Unicode Backspace \b\u0008 Tab \t\u0009 New line \n\u000A Carriage return \r\u000D Backslash \\\u005C Single Quote \ ' \u0027 Double Quote \ " \u0022

More Printing with System.out.println(); Todayissunny System.out.println(“Today\nis\nsunny”); NameIDAddress

Getting Input Using Scanner Scanner myScanner = new Scanner(System.in);  Scanner: pre-defined Java program (class object) to read in text input  myScanner: variable name  new Scanner(System.in) : new instance of the class object containing a reference to the default input of the system (keyboard)

Scanner Methods for Input 1. Create a Scanner object Scanner myScanner = new Scanner(System.in); 2. Use methods myScanner.next() => read in a string and put it in String.nextByte()=>read in a number put it in byte.nextShort() =>read in a number put it in short.nextInt()=>read in a number put it in int.nextLong() =>read in a number put it in long.nextFloat() =>read in a number put it in float.nextDouble() =>read in a number put it in double.nextBoolean() =>read in a string and convert to boolean **By default, Scanner input is delimited by white space characters (space, tab, carriage return)

Getting Input Using Scanner import java.util.Scanner; public class My1stJava { public static void main (String args[]) { Scanner myScanner = new Scanner(System.in); System.out.println("please enter text:"); String str = myScanner.next(); System.out.println(str); } Input: Hello World Output ?

The String Class ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ lV“Hello”

Constructing Strings String message = new String(stringLiteral); String message = new String(); //empty String Since strings are used frequently, Java provides a shorthand initializer for creating a string: String message = “ Welcome to Java ” ;

Strings Are Immutable A String object is immutable; its contents cannot be changed. String message = "Java"; String message = "Java"; message = "HTML"; message = "HTML"; In the 2 nd statement, Java actually created a new string container of type String to hold “ HTML ” and s now represent this new string container

Finding String Length Finding string length using the length() method: message = "Welcome"; message.length() (returns 7 )