Java 1.5 The New Java Mike Orsega Central Carolina CC.

Slides:



Advertisements
Similar presentations
1 LECTURE#7: Console Input Overview l Introduction to Wrapper classes. l Introduction to Exceptions (Java run-time errors). l Console input using the BufferedReader.
Advertisements

Java 1.5 & Effective Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
18 File handling1June File handling CE : Fundamental Programming Techniques.
Reading Information From the User Making your Programs Interactive.
1 TCSS 143, Autumn 2004 Lecture Notes Review. 2 Computer programming computers manipulate data data is often categorized into types numbers (integers,
1 Introduction to Console Input  Primitive Type Wrapper Classes  Converting Strings to Numbers  System.in Stream  Wrapping System.in in a Buffered.
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!");
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Scanner & Stepwise Refinement Pepper With credit to Dr. Siegfried.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
MSc IT Programming Methodology (2). MODULE TEAM Dr Aaron Kans Dr Sin Wee Lee.
2-1 CM0551 Week 3 The scanner class – file input A Spelling Checker Time and space requirements for various dictionary structures.
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Example 1 :- Handling integer values public class Program1 { public static void main(String [] args) { int value1, value2, sum; value1 = Integer.parseInt(args[0]);
Conditional If Week 3. Lecture outcomes Boolean operators – == (equal ) – OR (||) – AND (&&) If statements User input vs command line arguments.
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES.
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
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.
The String Class A String is an object. An object is defined by a class. In general, we instantiate a class like this: String myString = new String(“Crazy.
Textbook: Data Structures and the Java Collections Framework 3rd Edition by William Collins William Collins.
Objects First With Java A Practical Introduction Using BlueJ Supplementary Material for Java
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
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.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
1 / 65 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 12 Programming Fundamentals using Java 1.
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
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.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
© 2007 Lawrenceville Press Slide 1 Chapter 4 Review Assignment Statement An assignment statement gives a value to a variable. Assignment can take several.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
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.
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.
Building Java Programs Chapter 4 Lecture 4-1: Scanner ; cumulative algorithms reading: 3.3 – 3.4, 4.2.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter 7 Arrays…. 7-2 Arrays An array is an ordered list of values An array of size N is indexed from.
Introduction to programming in java
Introduction to Computer Science and Object-Oriented Programming
Reading Parameters CSCI 201 Principles of Software Development Jeffrey Miller, Ph.D.
Lecture 4 CS140 Dick Steflik. Reading Keyboard Input Import java.util.Scanner – A simple text scanner which can parse primitive types and strings using.
CompSci 230 S Programming Techniques
Building Java Programs
Input/Output.
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
TemperatureConversion
Objectives You should be able to describe: Interactive Keyboard Input
Interactive Standard Input/output
Lecture Note Set 1 Thursday 12-May-05
Computer Programming Methodology Input and While Loop
INPUT STATEMENTS GC 201.
Introduction to Classes and Methods
Know for Quiz Everything through Last Week and Lab 7
Building Java Programs
Building Java Programs
Building Java Programs
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Exception Handling Contents
Building Java Programs
Optional Topic: User Input with Scanner
Presentation transcript:

Java 1.5 The New Java Mike Orsega Central Carolina CC

Class Scanner Enumerated Types Autoboxing Variable Length Parameter Lists Generic Types New for Loop New Features in Java 1.5

Located in the java.util package Provides methods for:  Input  Parsing The Scanner Class

Input (the old way) import java.io.*; public class Hello { public static void main (String [] args) throws IOException { BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); String name; System.out.print("Enter your name: "); name = br.readLine(); System.out.println("Hello " + name); } Import java.io.* Throws a checked IOException Create a BufferedReader with an InputStreamReader using System.in

Input (Java 1.5) import java.util.Scanner; public class Hello { public static void main (String [] args) { Scanner in = new Scanner(System.in); String name; System.out.print("Enter your name: "); name = in.nextLine(); System.out.println("Hello " + name); } Import java.util.Scanner No checked Exception Create a Scanner object only

Scanner is an iterator (collection of items) String nextLine()  Returns the remaining keyboard input line as a String. Note: ‘\n’ is read and discarded. boolean nextBoolean() double nextDouble() int nextInt() Throws an InputMismatchException if the next token isn’t of the correct type. This Exception is NOT checked Scanner Class

useDelimiter(String)  Makes the String the only delimiter used to separate input. next()  Returns input up to, but not including, the first delimiter (any whitespace character is the default) More Scanner Class

import java.util.Scanner; public class Age { public static void main(String [] args) { Scanner in = new Scanner(System.in); int age, age2; System.out.print("Enter your age: "); age = in.nextInt(); age2 = in.nextInt(); System.out.println("Boy " + age + " is old!"); System.out.println(age2 + " is even worse!"); } OUTPUT Enter your age: Boy 10 is old! 12 is even worse! Examples OUTPUT

import java.util.Scanner; public class Delimit { public static void main(String [] args) { Scanner in = new Scanner(System.in); String word1, word2; in.useDelimiter("#"); System.out.println("Enter two words: "); word1 = in.next(); word2 = in.next(); System.out.println(word1 + " was entered first"); System.out.println("Then you entered " + word2); } OUTPUT Enter two words: one#two# one was entered first Then you entered two OUTPUT Enter two words: one two# three# one two was entered first Then you entered three More Examples OUTPUT

An enumerated type creates a new type of variable and establishes all possible values by listing them. enum Grade { A, B, C, D, F}; Grade myGrade = Grade.A; if (myGrade == Grade.F) System.out.println(“You fail”); System.out.println(myGrade); Enumerated Types

No invalid value can be assigned Internally, each value has a corresponding ordinal value, starting at 0 You cannot use the ordinal values directly. Instead you can use the ordinal() method Java also provides a name() method enum types cannot be declared locally You can pass enum variables Enumerated Types

public class Enumerated { enum Grade { A, B, C, D, F}; public static void main(String [] args) { Grade myGrade = Grade.A; if (myGrade == Grade.F) System.out.println("You fail"); else System.out.println("You pass"); System.out.println("myGrade: " + myGrade); System.out.println("myGrade.ordinal(): " + myGrade.ordinal()); System.out.println("myGrade.name(): " + myGrade.name()); } Enumerated Example

Autoboxing is the automatic conversion from a primitive value to the corresponding wrapper object Integer myInt; int number = 5; myInt = number; Unboxing is the conversion from the object to the primitive type Integer myInt2 = new Integer(15); int number; number = myInt2; Autoboxing

Java methods can now accept a variable number of arguments These arguments will be stored in an array public static void main(String [] args) { myMethod("A", "B", "C", "D"); } public static void myMethod(String... data) { for (int i=0; i<data.length;i++) System.out.println(data[i]); } OUTPUT A B C D Variable Length Arguments OUTPUT

Methods can accept other parameters, in addition to the varargs The variable length parameter must come last in the argument list Can only accept one variable length paramter void method2 (int a, double b, int … numbers) Variable Length Arguments

Classes and methods can have a type parameter Any class type can be plugged in The class definition is compiled just like any other class Once compiled, it can be used like any other class, except that the code must specify a class type to replace the parameter Generic Types

public class Generic { private T item; public void setItem(T _item) { item = _item; } public T getItem() { return item; } } Generics Example Placeholder for data type

public class TestGeneric { public static void main(String [] args) { Generic myGen = new Generic (); myGen.setItem("Hello!"); System.out.println("Item is: " + myGen.getItem()); Generic intGen = new Generic (); intGen.setItem(5); System.out.println("Item is: " + intGen.getItem()); } } OUTPUT Item is: Hello! Item is: 5 Generics Example (cont) Class type here OUTPUT

A new version of the for loop has been added to make going through an iterator easier: If you had a NameList iterator that stores Name objects, we could use: for (Name myName : NameList) System.out.println(myName); This is equivalent to: Name myName; while (NameList.hasNext()) { myName = NameList.next(); System.out.println(myName); } Declare variable Iterator object The New for Loop

This works with array variables as well int [] list = {1, 2, 3, 4, 5}; for (int num : list) System.out.println(num); OUTPUT The New for Loop OUTPUT

Questions