SOFTWARE DEVELOPMENT 1. Strings and Enumerations 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

Similar documents
Enumerated Types. CSE 114, Computer Science 1 Stony Brook University

Lab 14 & 15: String Handling

Object-Oriented Programming

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

Overloaded Methods. Sending Messages. Overloaded Constructors. Sending Parameters

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Class Library java.lang Package. Bok, Jong Soon

Chapter 8: A Second Look at Classes and Objects

09 Objects III: String and StringBuffer, Singly (Doubly) Linked Lists

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

Java Strings Java, winter semester

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Appendix 3. Description: Syntax: Parameters: Return Value: Example: Java - String charat() Method

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics


Objectives. Describe ways to create constants const readonly enum

Introduction to Programming Using Java (98-388)

Creating Strings. String Length

Computational Expression

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

More non-primitive types Lesson 06

2. All the strings gets collected in a special memory are for Strings called " String constant pool".

Building Strings and Exploring String Class:

Lecture Notes K.Yellaswamy Assistant Professor K L University

Topic 5: Enumerated Types and Switch Statements

Language Features. 1. The primitive types int, double, and boolean are part of the AP

Cloning Enums. Cloning and Enums BIU OOP

Java enum, casts, and others (Select portions of Chapters 4 & 5)

"Hello" " This " + "is String " + "concatenation"

Strings. Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.

String. Other languages that implement strings as character arrays

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

RTL Reference 1. JVM. 2. Lexical Conventions

Strings. Strings and their methods. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

More on variables and methods

Selected Questions from by Nageshwara Rao

A variable is a name for a location in memory A variable must be declared

Index COPYRIGHTED MATERIAL

Letterkenny Institute of Technology

CMPT 125: Lecture 3 Data and Expressions

TEST (MODULE:- 1 and 2)

Java Identifiers, Data Types & Variables

CSC Java Programming, Fall Java Data Types and Control Constructs

Java s String Class. in simplest form, just quoted text. used as parameters to. "This is a string" "So is this" "hi"

Lesson:9 Working with Array and String

Class. Chapter 6: Data Abstraction. Example. Class

Lecture 12. Data Types and Strings

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

String related classes

Chapter 10: Text Processing and More about Wrapper Classes

Timing for Interfaces and Abstract Classes

15CS45 : OBJECT ORIENTED CONCEPTS

1 Shyam sir JAVA Notes

CS18000: Problem Solving And Object-Oriented Programming

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

3. Java - Language Constructs I

CS112 Lecture: Characters and Strings

H212 Introduction to Software Systems Honors

Wrapper Classes double pi = new Double(3.14); 3 double pi = new Double("3.14"); 4... Zheng-Liang Lu Java Programming 290 / 321

Darshan Institute of Engineering & Technology for Diploma Studies Unit 2

Operators and Expressions

TEXT-BASED APPLICATIONS

Datatypes, Variables, and Operations

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Final Examination

Java is an objet-oriented programming language providing features that support

Computer Science II Data Structures

Java Module Lesson 2A Practice Exercise

Getting started with Java

Outline. Why Java? (1/2) Why Java? (2/2) Java Compiler and Virtual Machine. Classes. COMP9024: Data Structures and Algorithms

CSCI312 Principles of Programming Languages!

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

Agenda: Discussion Week 7. May 11, 2009

5/23/2015. Core Java Syllabus. VikRam ShaRma

Chapter 29: String and Object References Bradley Kjell (Revised 06/15/2008)

STUDENT LESSON A10 The String Class

Chapter 2 Elementary Programming

Getting started with Java

[TAP:AXETF] Inheritance

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

J.43 The length field of an array object makes the length of the array available. J.44 ARRAYS

CSE1710. Big Picture. Today is last day covering Ch 6. Tuesday, Dec 02 is designated as a study day. No classes. Thursday, Dec 04 is last lecture.

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Array. Prepared By - Rifat Shahriyar

CS2141 Software Development using C/C++ C++ Basics

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Fall 2017 CISC124 9/16/2017

Java Classes & Primitive Types

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

COE318 Lecture Notes Week 5 (Oct 3, 2011)

Java Classes & Primitive Types

COE318 Lecture Notes Week 4 (Sept 26, 2011)

Other conditional and loop constructs. Fundamentals of Computer Science Keith Vertanen

MODULE 02: BASIC COMPUTATION IN JAVA

JAVA Programming Fundamentals

Michele Van Dyne Museum 204B CSCI 136: Fundamentals of Computer Science II, Spring

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Program Fundamentals

Transcription:

SOFTWARE DEVELOPMENT 1 Strings and Enumerations 2018W (Institute of Pervasive Computing, JKU Linz)

CHARACTER ENCODING On digital systems, each character is represented by a specific number. The character set (short charset) determines, which number represents a character. There are numerous different character sets in use: US-ASCII: 128 characters (7 bit) one of the oldest standards; the first 128 characters of most character sets are identical to ASCII Windows ANSI: 256 characters (8 bit) commonly encountered on Windows systems ISO 8859-1: 256 characters (8 bit) common for western websites UTF-8, UTF-16,... (Unicode): variable character width (e.g. UTF-8 has 8-32 bit). Supports pretty much any language on the planet. Recommended by most international institutions. Java works internally always with characters (char) in UTF-16 format. During input / output, characters need to be converted. Software Development 1 // 2018W // 2

CHARACTER SEQUENCES :: STRINGS In Java, character sequences are contained in the class String. String Overview (see API documentation for more details): class String { // Constructors public String() { /* Create a new empty String */ public String(char value[]) { /* String from character array */ public String(byte[] bytes, String charsetname) { /* String from byte array, with specified character set */ // Methods public int length() { /* number of characters */ public char charat(int index) { /* single char at position */ public int indexof(int ch) { /* first position of a char */ public String substring(int beginindex) { /* part of the String */ public String concat(string str) { /* join 2 Strings */ public int compareto(string anotherstring) { /* compare Strings */ public static String valueof(double d) { /* convert a double to String */ Software Development 1 // 2018W // 3

CONSTRUCTORS OF CLASS STRING public String(char value[]) Convert array of characters to a String. public String(byte[] bytes, String charsetname) Convert array of bytes to a String. The specified character encoding is used for conversion. public String(byte[] bytes) Convert with system default character set. public String(StringBuffer buffer) Convert a StringBuffer to String. Beispiele: char[] c = {'F', 'r', 'o', 'g', ' ', 's'; String sc = new String(c); byte[] b = {0x20, 0x6c, 0x65, 0x67, 0x73; String sb = new String(b, "US-ASCII"); System.out.println(sc + sb); // Output: Frog s legs Software Development 1 // 2018W // 4

CREATING STRINGS String from a literal: String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; Convert from arrays of characters, bytes... String s1 = new String( new char[] {'A', 'B', 'C', 'D', 'E' ); String s2 = new String(s1); // copy of s1 Convert primitive data types: // int, double, long, float, boolean, short... String s3 = String.valueOf( x == 2.4 ); String pi = String.valueOf(3.141592653589793238); Concatenate 2 Strings with the + operator: String firstname = new String("Walter"); String lastname = "Wazinger"; // short form (literal) String name = firstname + lastname; // new String object System.out.println("Age: " + age + " years"); Software Development 1 // 2018W // 5

String rector = "Hagelauer"; rector.length(); // 9 "Hagelauer".length(); // 9 length() is a method, not a field as it is with arrays! rector.charat(2); // 'g' rector.indexof("a"); // 1 first index of "a" rector.indexof("a", 6); // -1 next index of "a", starting at index 6 rector.indexof("x"); // -1 "x" is not found rector.indexof('g'); // 2 'g' is character, not String String s4 = rector.substring(0, 2); // "Ha" substring [0..2[ String s5 = rector.substring(3); // "elauer" substring [3...] String s = s4 + s5; // "Haelauer" boolean error = s.startswith("hagel"); Software Development 1 // 2018W // 6

EXAMPLE :: CHARACTER INDEX class Alphabet { protected static final String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; protected static int numberinalphabet(char c) { return alphabet.indexof(c) + 1; Get index of character in alphabet (1-26) public static void main(string[] args) { for (int i = 0; i < args.length; i++) { char c; c = args[i].charat(0); System.out.println("Character " + c + " is number " + numberinalphabet(c)); SWE1.10 / Alphabet.java Software Development 1 // 2018W // 7

CONVERTING TO/ FROM STRINGS Converting to primitive data types int i = Integer.parseInt("666"); int j = new Integer("666").intValue(); double pi = Double.valueOf("3.1415").doubleValue(); Converting from primitive data types int three = 3; String s = Integer.toString(three); String number = String.valueOf(666); Converting to character arrays char[] alphabet; alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(); Converting from characters or character arrays String t = String.valueOf('i'); String lettersequence = new String(alphabet); Software Development 1 // 2018W // 8

EXAMPLE :: CONVERTING STRINGS TO INT //Display sum of all application arguments public class Convert { public static void main(string[] args) { //convert String arguments to int int[] a = new int[args.length]; for (int i = 0; i < args.length; i++) { // convert with Integer object Integer number = new Integer(args[i]); a[i] = number.intvalue(); // convert directly to int a[i] = Integer.parseInt(args[i]); // build sum and display int sum = 0; for (int i = 0; i < a.length; i++) sum += a[i]; System.out.println("Sum: " + sum); SWE1.10 / Convert.java Software Development 1 // 2018W // 9

COMMON METHODS public String tolowercase() public String touppercase() Transforms letters to lower / upper case. Also works with local letters (e.g. ö, ü, ß) touppercase converts "ß" to "SS"! It can not be converted back with tolowercase. Example: String a = "Schoßhündchen"; System.out.println(a.toLowerCase()); // Output: schoßhündchen System.out.println(a.toUpperCase()); // Output: SCHOSSHÜNDCHEN System.out.println(a.toUpperCase().toLowerCase()); // Output: schosshündchen Software Development 1 // 2018W // 10

COMMON METHODS public boolean equals(object anobject) Overwrites equals method from class Object. Checks if String content is identical. Example: String name1 = "Max"; String name2 = "Max"; String name3 = new String("Max"); String name4 = "max"; System.out.println(name1 == name2); System.out.println(name1 == name3); name1 and name2 do not only have the same contents, they are referencing the same object! Compare the references, not the content! // true // false! System.out.println(name1.equals(name3)); // true System.out.println(name1.equalsIgnoreCase(name4)); // true Compare the String content. Software Development 1 // 2018W // 11

COMMON METHODS public int compareto(string anotherstring) Performs a lexical comparison of 2 Strings. Returns 0, in case the Strings are identical, a value lower than 0, in case the String lexicographically precedes the argument, and a value greater than 0, in case the String lexicographically follows the argument. Example: String name = "Max"; System.out.println(name.compareTo("Franz")); // result > 0 System.out.println(name.compareTo("Max")); // result = 0 System.out.println(name.compareTo("Peter")); // result < 0 Software Development 1 // 2018W // 12

CLASS STRINGBUFFER AND STRINGBUILDER String objects can not be changed once they are created. To edit character sequences, create an instance of either StringBuffer or StringBuilder. Both have identical interface and functionality, but StringBuilder is not thread safe. If only a single thread is accessing the character sequence, StringBuilder should be used since it is slightly faster. Constructors: public StringBuffer() Create a new (empty) character sequence. public StringBuffer(int length) Create a new (empty) character sequence with an initial capacity. The capacity it automatically expanded when necessary. public StringBuffer(String str) Create a new character sequence and initialize it with a String. Software Development 1 // 2018W // 13

STRINGBUFFER :: METHODS StringBuffer a = new StringBuffer("Bazinga!"); int i = a.length(); // Number of characters in a a.append("!!!"); a.append(3); a.append(3.14); // a = "Bazinga!!!!" // append is overloaded for // all primitive data types a.delete(8, a.length()); // a = "Bazinga!" // Delete from index 11 to the end a.insert(0, "3 x "); // a = "3 x Bazinga!" // Insert at index 0 Software Development 1 // 2018W // 14

STRINGBUFFER :: METHODS a.replace(0,1,"100"); // a = "100 x Bazinga!" // Replace index [0..1[ a.deletecharat(2); // a = "10 x Bazinga!" // Delete char at index 2 a.setlength(4); // a = "10 x" // Set to new length; cut off rest a.reverse(); // a = "x 01" // Reverse sequence String s1 = a.tostring(); // Convert buffer to String String s2 = a.substring(0,1); // Only take part of the buffer Software Development 1 // 2018W // 15

STRINGBUFFER :: EXAMPLE // Read int numbers from standard input public class ReadInt { // read a line from standard input and convert to number private static int readint() throws IOException { // read line StringBuilder buffer = new StringBuilder(); // new StringBuilder int c = System.in.read(); // read first char while (c >= 0 && c!= '\n') { // while not end of line buffer.append((char)c); // add char to buffer c = System.in.read(); // parse number in line and return int return Integer.parseInt(buffer.toString()); // test our method public static void main(string[] args) throws IOException { int i = readint(); System.out.println("Your number: " + i); // read next char It is much more efficient to use a StringBuilder than concatenating Strings ( s = s + (char)c; ) Exceptions are not handled SWE1.10 / ReadInt.java Software Development 1 // 2018W // 16

ENUMERATIONS One way to define enumerations is a list of constants: public static final int DAY_MONDAY = 0; public static final int DAY_TUESDAY = 1; public static final int DAY_WEDNESDAY = 2; public static final int DAY_THURSDAY = 3; public static final int DAY_FRIDAY = 4; public static final int DAY_SATURDAY = 5; public static final int DAY_SUNDAY = 6; int dayofevent = DAY_WEDNESDAY; Define all weekdays as constant integers. Assign a weekday to a variable. This approach has several disadvantages: That a constant is part of the enumeration has to be indicated with a name prefix. No type safety: Any int value may be assigned to variables that should hold a weekday. The compiler can only check the type int. Outputting a weekday only shows a numeric value. Complicated and long-winded declaration. Software Development 1 // 2018W // 17

ENUMERATIONS Declaration: enum Name { CONSTANT_1, CONSTANT_2,... All enumerations are subclasses of java.lang.enum Coding conventions: Since enumerations are classes, their name should be formatted like a class name: first letter of each word in uppercase. The values are constants (static final) and therefore should be in all uppercase, with underscores to separate words. Example: enum DaysOfWeek { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY Declaration of enumeration references: DaysOfWeek dayofevent = DaysOfWeek.WEDNESDAY; Software Development 1 // 2018W // 18

ENUMERATIONS Internally Java translates enumerations in classes, that look like this: enum DaysOfWeek { MONDAY, TUESDAY,... class DaysOfWeek extends Enum { public static final DaysOfWeek MONDAY = new DaysOfWeek(); public static final DaysOfWeek TUESDAY = new DaysOfWeek();... The enumeration values are static final references to a new instance of the class DaysOfWeek This should also explain the following code pieces: DaysOfWeek dayofevent = DaysOfWeek.WEDNESDAY; import static DaysOfWeek.*;... DaysOfWeek dayofevent = WEDNESDAY; The object WEDNESDAY was instantiated once at program start. No further new required! import static allows us to reference enumeration values directly. Software Development 1 // 2018W // 19

ENUMERATIONS Since enumerations are actually classes, they can also contain fields, methods and constructors: enum DaysOfWeek { MONDAY(1), TUESDAY(2), WEDNESDAY(3), THURSDAY(4), FRIDAY(5), SATURDAY(6), SUNDAY(7); This would translate to something like this: class DaysOfWeek extends Enum { public static final DaysOfWeek MONDAY = new DaysOfWeek(1); public static final DaysOfWeek TUESDAY = new DaysOfWeek(2);... private int number; DaysOfWeek(int number) { this.number = number; Private object field. Constructor public String tostring() { return super.tostring() + "(" + number + ")"; Overwrite the default tostring method. Software Development 1 // 2018W // 20

ENUMERATIONS :: EXAMPLE public enum IntSize { BYTE, SHORT, INT, LONG; public static IntSize getsize(long i) { // find min. size for number if (i >= Byte.MIN_VALUE && i <= Byte.MAX_VALUE) return BYTE; if (i >= Short.MIN_VALUE && i <= Short.MAX_VALUE) return SHORT; if (i >= Integer.MIN_VALUE && i <= Integer.MAX_VALUE) return INT; return LONG; // try with some numbers public static void main(string[] args) { long[] nums = {34567, 280, -10, 3456789012L; for (long n : nums) { IntSize minsize = IntSize.getSize(n); System.out.print(n + ": "); switch (minsize) { case BYTE: System.out.println("min size is 8 Bit"); break; case SHORT: System.out.println("min size is 16 Bit"); break; case INT: System.out.println("min size is 32 Bit"); break; case LONG: System.out.println("min size is 64 Bit"); Method to find the smallest primitive numeric data type that can store the number. Enumerations can also be used in switch-statements Software Development 1 // 2018W // 21

SOFTWARE DEVELOPMENT 1 Strings and Enumerations 2018W (Institute of Pervasive Computing, JKU Linz)