Introduction to Classes and Methods

Slides:



Advertisements
Similar presentations
Taking Input Java Md. Eftakhairul Islam
Advertisements

Introduction to Programming Overview of Week 2 25 January Ping Brennan
More Java Syntax. Scanner class Revisited The Scanner class is a class in java.util, which allows the user to read values of various types. There are.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Today’s topics: I/O (Input/Output). Scribbler Inputs & Outputs  What are the Scribbler’s inputs and outputs?  reset button  motors/wheel  light sensor.
CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
Evan Korth Scanner class Evan Korth NYU. Evan Korth Java 1.5 (5.0)’s Scanner class Prior to Java 1.5 getting input from the console involved multiple.
Chapter 3 Using Classes and Objects. Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as.
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.
Chapter 2: Java Fundamentals Input and Output statements.
1 TCSS 143, Autumn 2004 Lecture Notes Review. 2 Computer programming computers manipulate data data is often categorized into types numbers (integers,
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.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.
Scanner & Stepwise Refinement Pepper With credit to Dr. Siegfried.
***** SWTJC STEM ***** Chapter 3-1 cg 36 Java Class Libraries & API’s A class library is a set of classes that supports the development of programs. Java.
2-1 CM0551 Week 3 The scanner class – file input A Spelling Checker Time and space requirements for various dictionary structures.
Chapter 2 Section 2.2 Console Input Using The Scanner CLASS Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska.
Simple Programs from Chapter 2 Putting the Building Blocks All Together (corresponds with Chapter 2)
Console Input. So far… All the inputs for our programs have been hard-coded in the main method or inputted using the dialog boxes of BlueJ It’s time to.
Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1.
Sahar Mosleh California State University San MarcosPage 1 System.out.println for console output System.out is an object that is part of the Java language.
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
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.
CSci 111 – computer Science I Fall 2014 Cynthia Zickos WRITING A SIMPLE PROGRAM IN JAVA.
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
Java 1.5 The New Java Mike Orsega Central Carolina CC.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
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.
Using Classes and Objects Chapters 3 Creating Objects – Section 3.1 The String Class – Section 3.2 The Scanner Class – Section 2.6 Instructor: Scott Kristjanson.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
CSE 1201 Object Oriented Programming Using Classes and Objects.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
Copyright © Curt Hill Simple I/O Input and Output using the System and Scanner Objects.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Scanner Review Java Foundations: Introduction to Programming and Data Structures.
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.
© A+ Computer Science - import java.util.Scanner; Try to be as specific as possible when using an import.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Introduction to programming in java
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Introduction to programming in java
Input/Output.
TemperatureConversion
Project 1.
CSC1401 Input and Output (and we’ll do a bit more on class creation)
CS116 OBJECT ORIENTED PROGRAMMING II LECTURE 1 Part II
CSC 1051 – Data Structures and Algorithms I
Maha AlSaif Maryam AlQattan
Something about Java Introduction to Problem Solving and Programming 1.
INPUT STATEMENTS GC 201.
Program Style Console Input and Output
الكلية الجامعية للعلوم التطبيقية
CSS 161 Fundamentals of Computing Introduction to Computers & Java
A+ Computer Science INPUT.
Fundamentals 2.
Chapter 2: Java Fundamentals
A+ Computer Science INPUT.
Object Oriented Programming
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Reading Input and Scanner Class
Optional Topic: User Input with Scanner
Presentation transcript:

Introduction to Classes and Methods

Sample Program // Name: Mr. Brennan // File: Echo2.java // purpose: Prompt the user to enter some text and then // an integer, and echo them to the user. // This uses the Scanner class. import java.io.*; import java.util.Scanner; public class Echo2 { public static void main (String[ ] args) Scanner scan = new Scanner(System.in); System.out.print ("Enter a string: "); String s = scan.nextLine(); System.out.print ("\nEnter an integer: "); int i = scan.nextInt(); System.out.println ("\nYou entered string " + s + " and integer " + i + "\n"); }

Sample Program Tells java where to find the Scanner class. import java.io.*; import java.util.Scanner; public class Echo2 { public static void main (String[ ] args) Scanner scan = new Scanner(System.in); System.out.print ("Enter a string: "); String s = scan.nextLine(); System.out.print ("\nEnter an integer: "); int i = scan.nextInt(); System.out.println ("\nYou entered string " + s + " and integer " + i + "\n"); } Tells java where to find the Scanner class. Declares a variable named scan. Variable scan has a type of Scanner. Scanner is not a primitive data type, and is named with a capital letter.

Sample Program new tells the system to allocate space public class Echo2 { public static void main (String[ ] args) Scanner scan = new Scanner(System.in); System.out.print ("Enter a string: "); String s = scan.nextLine(); System.out.print ("\nEnter an integer: "); int i = scan.nextInt(); System.out.println ("\nYou entered string " + s + " and integer " + i + "\n"); } new tells the system to allocate space for a new Scanner variable. Passing System.in to the Scanner Method will connect the scan variable to the keyboard as in input device.

Sample Program As a “instance” of a Scanner class, the scan public class Echo2 { public static void main (String[ ] args) Scanner scan = new Scanner(System.in); System.out.print ("Enter a string: "); String s = scan.nextLine(); System.out.print ("\nEnter an integer: "); int i = scan.nextInt(); System.out.println ("\nYou entered string " + s + " and integer " + i + "\n"); } As a “instance” of a Scanner class, the scan variable has many methods available to use. Here it is using the nextline() method to read in a line of text (from the keyboard) and store it in a String variable named s.

Sample Program public class Echo2 { public static void main (String[ ] args) Scanner scan = new Scanner(System.in); System.out.print ("Enter a string: "); String s = scan.nextLine(); System.out.print ("\nEnter an integer: "); int i = scan.nextInt(); System.out.println ("\nYou entered string " + s + " and integer " + i + "\n"); } A variable name i is being declared as an integer using the primitive int data type. It is initialized using the next value that is available from the keyboard input using the nextint() method from scan.

From Scanner Class, University of Texas  Method Returns int nextInt() Returns the next token as an int. If the next token is not an integer, InputMismatchException is thrown. long nextLong() Returns the next token as a long. If the next token is not an integer, InputMismatchException is thrown. float nextFloat() Returns the next token as a float. If the next token is not a float or is out of range, InputMismatchException is thrown. double nextDouble() Returns the next token as a long. If the next token is not a float or is out of range, InputMismatchException is thrown. String next() Finds and returns the next complete token from this scanner and returns it as a string; a token is usually ended by whitespace such as a blank or line break. If not token exists, NoSuchElementException is thrown. String nextLine() Returns the rest of the current line, excluding any line separator at the end. void close() Closes the scanner. From Scanner Class, University of Texas http://www.cs.utexas.edu/users/ndale/Scanner.html