java Features Version April 19, 2013 by Thorsten Kracht



Similar documents
AP Computer Science Java Subset

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

Week 1: Review of Java Programming Basics

Java Interview Questions and Answers

Introduction to Programming

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Introduction to Java Lecture Notes. Ryan Dougherty

Third AP Edition. Object-Oriented Programming and Data Structures. Maria Litvin. Gary Litvin. Phillips Academy, Andover, Massachusetts

Computer Programming I

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Java Programming Fundamentals

Introduction to Java. CS 3: Computer Programming in Java

Introduction to Java

Programmation 2. Introduction à la programmation Java

Part I. Multiple Choice Questions (2 points each):

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

Crash Course in Java

public static void main(string[] args) { System.out.println("hello, world"); } }

Chapter 2: Elements of Java

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

CS 111 Classes I 1. Software Organization View to this point:

Fundamentals of Java Programming

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

J a v a Quiz (Unit 3, Test 0 Practice)

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Chapter 2 Introduction to Java programming

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

CS506 Web Design and Development Solved Online Quiz No. 01

CS 106 Introduction to Computer Science I

Lecture 5: Java Fundamentals III

Contents. 9-1 Copyright (c) N. Afshartous

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Using Files as Input/Output in Java 5.0 Applications

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

An Overview of Java. overview-1

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

INPUT AND OUTPUT STREAMS

More on Objects and Classes

CSC 551: Web Programming. Fall 2001

CS 1302 Ch 19, Binary I/O

JAVA.UTIL.SCANNER CLASS

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

Java from a C perspective. Plan

13 File Output and Input

Programmation Orientée Objets. et langage Java

Java Programming. Binnur Kurt Istanbul Technical University Computer Engineering Department. Java Programming. Version 0.0.

Quick Introduction to Java

Chapter 13 - Inheritance

The Sun Certified Associate for the Java Platform, Standard Edition, Exam Version 1.0

Java and J2EE (SCJA Exam CX ) 50 Cragwood Rd, Suite 350 South Plainfield, NJ 07080

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Building Java Programs

Java Crash Course Part I

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

CS 106 Introduction to Computer Science I

CS170 Lab 11 Abstract Data Types & Objects

Eryq (Erik Dorfman) Zeegee Software Inc. A Crash Course in Java 1

For live Java EE training, please see training courses

Chapter 1 Java Program Design and Development

Object-Oriented Programming in Java

Sample CSE8A midterm Multiple Choice (circle one)

Iteration CHAPTER 6. Topic Summary

Building Java Programs

Web Development and Core Java Lab Manual V th Semester

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

An Incomplete C++ Primer. University of Wyoming MA 5310

Java CPD (I) Frans Coenen Department of Computer Science

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Object Oriented Software Design

Chapter 2 Elementary Programming

CSE 452: Programming Languages. Acknowledgements. Contents. Java and its Evolution

Chapter 2 Basics of Scanning and Conventional Programming in Java

Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Classes and Objects. Agenda. Quiz 7/1/2008. The Background of the Object-Oriented Approach. Class. Object. Package and import

Object Oriented Software Design

Chapter 3. Input and output. 3.1 The System class

CSE 8B Midterm Fall 2015

Carron Shankland. Content. String manipula3on in Java The use of files in Java The use of the command line arguments References:

Java (12 Weeks) Introduction to Java Programming Language

6.1. Example: A Tip Calculator 6-1

Inheritance, overloading and overriding

Building Java Programs

Programming Languages CIS 443

Event-Driven Programming

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

Implementation Aspects of OO-Languages

WHAT ARE PACKAGES? A package is a collection of related classes. This is similar to the notion that a class is a collection of related methods.

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

Transcription:

java Features Version April 19, 2013 by Thorsten Kracht

Contents 1 Introduction 2 1.1 Hello World................................................ 2 2 Variables, Types 3 3 Input/Output 4 3.1 Standard I/O................................................ 4 3.2 Scanner.................................................. 4 4 Operators 5 4.1 Boolean.................................................. 5 4.2 Increment/Decrement........................................... 5 5 Control Structures 6 5.1 If...................................................... 6 5.2 Loops................................................... 6 6 Standard Classes 7 6.1 Import................................................... 7 6.2 Math.................................................... 7 6.3 Random.................................................. 7 6.4 String................................................... 8 6.5 tostring.................................................. 8 6.6 Wrapper.................................................. 9 7 Classes 10 7.1 General.................................................. 10 7.2 Definition................................................. 10 7.3 Classes in separate files.......................................... 10 7.4 Overloading................................................ 11 7.5 Referenced objects can be changed.................................... 11 7.6 Encapsulation............................................... 12 7.7 Inheritance................................................ 12 7.8 Abstract classes, polymorphism..................................... 13 1

Chapter 1 Introduction This note is intended to help people getting started with java. It contains some explanations and covers the topics by examples. 1.1 Hello World // // file Hello.java // class Hello public static void main ( String[] args ) System.out.println("Hello World!"); This program can be compiled and executed with: javac Hello.java creates Hello.class java Hello executes Hello.class 2

Chapter 2 Variables, Types These are the primitive data types: byte 8 bit, -128 - +127 short 16 bit, -32768 - +32767 int 32 bit, -2E9 - +2E9 long 64 bit, -10e18 - +10e18 (long i = 3L;) float 32 bit, -3.4E38 - +3.4e38 (float x = 1.23F;) double 64 bit, -1.7e308 - +1.7e308 (double x = 123.0D;) char 16 bit (!) boolean true, false) The type names can be used as cast operators: int i = (int) 1.5; Primitive variables cannot be changes by a method. Reference variables can have the value null. String a = null; if( a!= null)... 3

Chapter 3 Input/Output 3.1 Standard I/O System.in System.out System,err 3.2 Scanner import java.util.scanner; // loads java.io implicitly class echo String line; Scanner inp = new Scanner( System.in ); System.out.println("Enter:"); line = inp.nextline(); System.out.println("You typed:" + line ); 4

Chapter 4 Operators 4.1 Boolean A == B A < B A <= B A > B A >= B A!= B &&! 4.2 Increment/Decrement i++; i--; 5

Chapter 5 Control Structures 5.1 If class iftest double x = 1.0; if( x < 0) System.out.println(" x < 0"); else if (x == 0.) System.out.println(" x == 0"); else System.out.println(" x > 0"); 5.2 Loops class iftest double x = 1.0; if( x < 0) System.out.println(" x < 0"); else if (x == 0.) System.out.println(" x == 0"); else System.out.println(" x > 0"); 6

Chapter 6 Standard Classes The examples in this section can be compiled and executed by: $ javac mathtest.java && java mathtest cos 60.0 = 0.5000000000000001 The string mathtest has to be replaces by the actual class name. 6.1 Import // import a single class import java.util.random; // import all classes of a package import java.util.*; 6.2 Math class mathtest double degree = 60.; System.out.println(" cos " + degree + " = " + Math.cos( Math.toRadians( degree))); $ javac mathtest.java && java mathtest cos 60.0 = 0.5000000000000001 6.3 Random import java.util.random; public class randtest public static void main ( String[] args ) Random rand = new Random(); int i = 10; while ( i > 0 ) // other methods: nextfloat(), nextdouble(), nextgaussian() 7

System.out.println("random " + rand.nextint(1000) ); // numbers from 0 to 999 i = i - 1; 6.4 String The string class (package java.lang.string) contains these functions (and others): public char charat( int index ) public String concat( String str ) public boolean endswith( String suffix ) public boolean equals( Object anobject ) str1.equals( str2) public boolean equalsignorecase( String anotherstring ) public int indexof( int ch ) public int indexof( String str ) public int length() public boolean startswith( String prefix ) public String substring( int beginindex ) public String substring( int beginindex, int endindex ) public String tolowercase() public String touppercase() public String trim() Example: // class Beispiel public static void main ( String[] args ) java.lang.string str; str = new String( "ein test string"); String str1 = new String( "noch ein string");; System.out.println( "string = " + str + " length " + str.length()); System.out.println( "uppercase-str1 = " + str1.touppercase()); 6.5 tostring Produces a string representation of an object. import java.awt.*; class tostringtest Point a = new Point( 3, 4); System.out.println(" tostring " + a.tostring()); Result:

$ javac tostringtest.java && java tostringtest tostring java.awt.point[x=3,y=4] 6.6 Wrapper The wrapper classes, package java.lang, create an object. primitive type wrapper type byte Byte short Short int Integer long Long float Float double Double char Character boolean Boolean Example: Integer i = new Integer(123);

Chapter 7 Classes 7.1 General static members belong a class not the opject. private visible from within the object. public visible from outside the object. If a member is neither declared private or public, it is public to the package. 7.2 Definition class Hello void say() System.out.println( "this is Hello.say"); class classtest Hello a = new Hello(); a.say(); 7.3 Classes in separate files Here is a main programm that uses code which is stored in a different file: // file classtest.java class classtest classhello a = new classhello( "moin"); a.say(); The class classhello: // file classhello.java class classhello 10

String msg; classhello( String a) this.msg = a; System.out.println( "this is the Constructor, classhello.hello"); void say() System.out.println( "this is classhello.say: " + this.msg); This command compiles both files and executes main(). $ javac classtest.java classhello.java && java classtest 7.4 Overloading class classhello private String msg; classhello( String a) this.msg = a; public void say() System.out.println( "this is classhello.say: // overloading a function public void say( String str) System.out.println( "this is classhello.say: " + this.msg); " + str); class classtest classhello a = new classhello( "Moin"); a.say(); String b = new String( "Guten Tag"); a.say( b); 7.5 Referenced objects can be changed class Value public int i = 1; class Klasse void func( Value obj) obj.i = 11; return;

class classtest Value b = new Value(); Klasse a = new Klasse(); System.out.println( "Vorher " + b.i); a.func( b); System.out.println( "Nachher " + b.i); 7.6 Encapsulation class Value private int i = 1; public void setvalue( int argin) i = argin; public int getvalue( ) return i; class classtest Value b = new Value(); System.out.println( "Vorher " + b.getvalue()); b.setvalue( 2); System.out.println( "Nachher " + b.getvalue()); 7.7 Inheritance class Human int age; String name; public Human( String name, int age) this.name = name; this.age = age; class male extends Human String gender; public male( String name, int age) super( name, age); this.gender = "male"; class classtest

male a = new male( "Fritz", 30); System.out.println( a.name + " " + a.age + " " + a.gender); 7.8 Abstract classes, polymorphism An abstract class is never instanciated. It servers the purpose of being a superclass for other classes. abstract class Animal String name; public Animal( String name) this.name = name; // abstract methods have to be implemented by subclasses public abstract String sound(); class dog extends Animal public dog( String name) super( name); public String sound() return "wau"; class cat extends Animal public cat( String name) super( name); public String sound() return "miau"; class classtest Animal a = new dog( "Rex"); Animal b = new cat( "Miezzi"); Animal c; c = a; System.out.println( c.name + " says " + c.sound()); c = b; System.out.println( c.name + " says " + c.sound()); Polymorphism: depending which type of animal is referenced by c, the sound() method produces a different result.

Index abstract classes, 13 encapsulation, 12 inheritance, 12 overloading, 11 polymorphism, 13 tostring, 8 14