©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING String and Scanner Objects.

Slides:



Advertisements
Similar presentations
Designing a Program & the Java Programming Language
Advertisements

Chapter 2: Java Fundamentals cont’d
Today’s lecture Review of Chapter 1 Go over homework exercises for chapter 1.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
© 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.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
1 Fall 2008ACS-1903 Chapter 2 Java Fundamentals parts of a java program print, println, java api variables and iiterals primitive data types arithmetic.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.3 The Class String.
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;
Lecture 14 The String Class Lecture 14 The String Class Instructor: Craig Duckett.
CS0007: Introduction to Computer Programming Scope, Comments, Code Style, Keyboard Input.
Chapter 2 Section 2.2 Console Input Using The Scanner CLASS Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska.
Intro to CS – Honors I Documentation and Coding Style GEORGIOS PORTOKALIDIS
Chapter 2: Java Fundamentals Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis.
Java Applications & Program Design
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA Prof. Dr. Paulo Brasko Ferreira Fall 2014.
 2005 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 2: Java Fundamentals Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis.
Source Code: Chapters 1 and 2
Java Fundamentals Asserting Java Chapter 2: Introduction to Computer Science ©Rick Mercer.
11 Chapter 5 METHODS CONT’D. 22 MORE ON PASSING ARGUMENTS TO A METHOD Passing an Object Reference as an Argument to a Method Objects are passed by reference.
The Java Programming Language
Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Chapter 2: Java Fundamentals
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
Lecture 4 – Scanner & Style. Console Output The console that starts a Java application is typically known as the standard output device. The standard.
Java Fundamentals Part Integer Division Division can be tricky. In a Java program, what is the value of 1/2? You might think the answer is.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
Ihsan Ayyub Qazi Introduction to Computer Programming Recitation 3 Ihsan Ayyub Qazi.
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC INTRO TO COMPUTING - PROGRAMMING If Statement.
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
Chapter 2: Java Fundamentals Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
CSC 110 – Intro to Computing - Programming
Interactive Programs Programs that get input from the user 1 In PowerPoint, click on the speaker icon then click the "Play" button to hear the narration.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING For Loop.
Chapter 2: Java Fundamentals Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis.
Lecture 4 – Scanner & Style
Lecture 2b- Java Fundamentals
Chapter 2: Java Fundamentals by Tony Gaddis and Godfrey Muganda
Chapter 2: Java Fundamentals by Tony Gaddis and Godfrey Muganda
Chapter 2: Java Fundamentals
Scope, Comments, Code Style, Keyboard Input
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA
Chapter 2: Java Fundamentals
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Variables ICS2O.
Chapter 2: Java Fundamentals
Lecture 2b- Java Fundamentals
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Chapter 2: Java Fundamentals
Chapter 2: Java Fundamentals cont’d
Chapter 2: Java Fundamentals
Chapter 2: Java Fundamentals cont’d
Presentation transcript:

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING String and Scanner Objects

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-2 The String Class Java has no primitive data type that holds a series of characters. The String class from the Java standard library is used for this purpose. In order to be useful, the a variable must be created to reference a String object. String number; Notice the S in String is upper case. By convention, class names should always begin with an upper case character.

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-3 Primitive vs. Reference Variables Primitive variables actually contain the value that they have been assigned. number = 25; The value 25 will be stored in the memory location associated with the variable number. Objects are not stored in variables, however. Objects are referenced by variables.

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-4 Primitive vs. Reference Variables When a variable references an object, it contains the memory address of the object’s location. Then it is said that the variable references the object. String cityName = "Charleston"; Charleston Address to the object cityName The object that contains the character string “Charleston”

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-5 String Objects A variable can be assigned a String literal. String value = "Hello"; Strings are the only objects that can be created in this way. A variable can be created using the new keyword. String value = new String("Hello"); This is the method that all other objects must use when they are created. See example: StringDemo.javaStringDemo.java

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. StringDemo.java // A simple program demonstrating String objects. public class StringDemo { public static void main(String[] args) { String greeting = "Good morning, "; String name = "Herman"; System.out.println(greeting + name); }

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-7 The String Methods Since String is a class, objects that are instances of it have methods. One of those methods is the length method. stringSize = value.length(); This statement runs the length method on the object pointed to by the value variable. See example: StringLength.javaStringLength.java

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. StringLength.java // This program demonstrates the String class's length method. public class StringLength { public static void main(String[] args) { String name = "Herman"; int stringSize; stringSize = name.length(); System.out.println(name + " has " + stringSize + " characters."); }

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-9 String Methods The String class contains many methods that help with the manipulation of String objects. String objects are immutable, meaning that they cannot be changed. Many of the methods of a String object can create new versions of the object. See example: StringMethods.javaStringMethods.java

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. StringMethods.java // This program demonstrates a few of the String methods. public class StringMethods { public static void main(String[] args) { String message = "Java is Great Fun!"; String upper = message.toUpperCase(); String lower = message.toLowerCase(); char letter = message.charAt(2); int stringSize = message.length(); System.out.println(message); System.out.println(upper); System.out.println(lower); System.out.println(letter); System.out.println(stringSize); }

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved Scope Scope refers to the part of a program that has access to a variable’s contents. Variables declared inside a method (like the main method) are called local variables. Local variables’ scope begins at the declaration of the variable and ends at the end of the method in which it was declared. See example: Scope.java (This program contains an intentional error.)Scope.java

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Scope.java // This program can't find its variable. public class Scope { public static void main(String[] args) { System.out.println(value); // ERROR! int value = 100; }

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved Commenting Code Java provides three methods for commenting code. Comment Style Description // Single line comment. Anything after the // on the line will be ignored by the compiler. /* … */ Block comment. Everything beginning with /* and ending with the first */ will be ignored by the compiler. This comment type cannot be nested. /** … */ Javadoc comment. This is a special version of the previous block comment that allows comments to be documented by the javadoc utility program. Everything beginning with the /** and ending with the first */ will be ignored by the compiler. This comment type cannot be nested.

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved Commenting Code Javadoc comments can be built into HTML documentation. See example: Comment3.javaComment3.java To create the documentation: –Run the javadoc program with the source file as an argument –Ex: javadoc Comment3.java The javadoc program will create index.html and several other documentation files in the same directory as the input file.

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Comment3.java /** This class creates a program that calculates company payroll. */ public class Comment3 { /** The main method is the program's starting point. */ public static void main(String[] args) { double payRate; // Holds the hourly pay rate double hours; // Hours holds the hours worked int employeeNumber; // Holds the employee number // The Remainder of This Program is Omitted. }

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved Commenting Code Example index.html:index.html

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved Programming Style Although Java has a strict syntax, whitespace characters are ignored by the compiler. The Java whitespace characters are: –space –tab –newline –carriage return –form feed See example: Compact.javaCompact.java

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Compact.java public class Compact {public static void main(String[] args){int shares=220; double averagePrice=14.67; System.out.println( "There were "+shares+" shares sold at $"+averagePrice+ " per share.");}}

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved Indentation Programs should use proper indentation. Each block of code should be indented a few spaces from its surrounding block. Two to four spaces are sufficient. Tab characters should be avoided. –Tabs can vary in size between applications and devices. –Most programming text editors allow the user to replace the tab with spaces. See example: Readable.javaReadable.java

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Readable.java /** This example is much more readable than Compact.java. */ public class Readable { public static void main(String[] args) { int shares = 220; double averagePrice = 14.67; System.out.println("There were " + shares + " shares sold at $" + averagePrice + " per share."); }

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved The Scanner Class To read input from the keyboard we can use the Scanner class. The Scanner class is defined in java.util, so we will use the following statement at the top of our programs: import java.util.Scanner;

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved The Scanner Class Scanner objects work with System.in To create a Scanner object: Scanner keyboard = new Scanner (System.in); Scanner class methods are listed in Table in the text. See example: Payroll.javaPayroll.java

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Payroll.java import java.util.Scanner; // Needed for the Scanner class /** This program demonstrates the Scanner class. */ public class Payroll { public static void main(String[] args) { String name; // To hold a name int hours; // Hours worked double payRate; // Hourly pay rate double grossPay; // Gross pay

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Payroll.java (cont) // Create a Scanner object to read input. Scanner keyboard = new Scanner(System.in); // Get the user's name. System.out.print("What is your name? "); name = keyboard.nextLine(); // Get the number of hours worked this week. System.out.print("How many hours did you work this week? "); hours = keyboard.nextInt(); // Get the user's hourly pay rate. System.out.print("What is your hourly pay rate? "); payRate = keyboard.nextDouble();

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Payroll.java (cont) // Calculate the gross pay. grossPay = hours * payRate; // Display the resulting information. System.out.println("Hello, " + name); System.out.println("Your gross pay is $" + grossPay); }