Programmierpraktikum



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

Java Crash Course Part I

Introduction to Java

1001ICT Introduction To Programming Lecture Notes

Object-Oriented Programming in Java

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

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

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

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

Chapter 3. Input and output. 3.1 The System class

CS 106 Introduction to Computer Science I

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

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

Introduction to Object-Oriented Programming

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Install Java Development Kit (JDK) 1.8

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

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

Introduction to Java. CS 3: Computer Programming in Java

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

Chapter 2 Elementary Programming

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Sample CSE8A midterm Multiple Choice (circle one)

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

Lecture 5: Java Fundamentals III

Some Scanner Class Methods

Java with Eclipse: Setup & Getting Started

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

Using Files as Input/Output in Java 5.0 Applications

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

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

6.1. Example: A Tip Calculator 6-1

AP Computer Science Static Methods, Strings, User Input

Comp 248 Introduction to Programming

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

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

Tutorial: Getting Started

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

Programming Languages CIS 443

Chapter 2 Basics of Scanning and Conventional Programming in Java

Informatica e Sistemi in Tempo Reale

Installing Java (Windows) and Writing your First Program

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

Building Java Programs

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.

Chapter 1 Java Program Design and Development

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

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

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

How to use the Eclipse IDE for Java Application Development

AP Computer Science Java Mr. Clausen Program 9A, 9B

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

Example of a Java program

Chapter 2: Elements of Java

Java Programming Fundamentals

Building Java Programs

Introduction to Programming

CSCI 1301: Introduction to Computing and Programming Summer 2015 Project 1: Credit Card Pay Off

Java CPD (I) Frans Coenen Department of Computer Science

Introduction to Java Lecture Notes. Ryan Dougherty

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

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

Arrays in Java. Working with Arrays

Building Java Programs

Moving from CS 61A Scheme to CS 61B Java

13 File Output and Input

Lexical Analysis and Scanning. Honors Compilers Feb 5 th 2001 Robert Dewar

The programming language C. sws1 1

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

Chapter 2 Introduction to Java programming

LAB 1. Familiarization of Rational Rose Environment And UML for small Java Application Development

A Short Introduction to Writing Java Code. Zoltán Majó

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

Computational Mathematics with Python

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Pemrograman Dasar. Basic Elements Of Java

Chapter 8. Living with Java. 8.1 Standard Output

Java Software Development Kit (JDK 5.0 Update 14) Installation Step by Step Instructions

Question1-part2 What undesirable consequences might there be in having too long a DNS cache entry lifetime?

Introduction to Python

Computational Mathematics with Python

Quick Introduction to Java

Data Structures Lecture 1

1.1 Your First Program

Topic 11 Scanner object, conditional execution

Programming in Java. What is in This Chapter? Chapter 1

IRA EXAMPLES. This topic has two examples showing the calculation of the future value an IRA (Individual Retirement Account).

For live Java EE training, please see training courses

LOOPS CHAPTER CHAPTER GOALS

Java Application Developer Certificate Program Competencies

Computer Programming Tutorial

Java Basics: Data Types, Variables, and Loops

ANALYZER. Test Coverage Monitor for C/C ++, COBOL, Java and PL/I

Basics of Java Programming Input and the Scanner class

CPLEX Tutorial Handout

Crash Course in Java

CS 241 Data Organization Coding Standards

JAVA ARRAY EXAMPLE PDF

Transcription:

Programmierpraktikum Claudius Gros, SS2012 Institut für theoretische Physik Goethe-University Frankfurt a.m. 1 of 21 10/16/2012 09:29 AM

Java - A First Glance 2 of 21 10/16/2012 09:29 AM

programming languages compiled Algol C C++ Fortran scripting Phyton PHP Unix shell scripts (Word) (Latex) Lisp Java Pascal computing environments Mathlab Mathematica 3 of 21 10/16/2012 09:29 AM

development environments Eclipse 4 of 21 10/16/2012 09:29 AM

compilation and execution javac Java programming language compiler javac Program.java: compile the source code Program.java and create the executable Program.class the executable.class file is independent of the operating system (Linux, Windows,...) java the Java application launcher java Program: launch (execute) the Java executable Program.class java -jar file.jar: launches the Java program in the package 5 of 21 10/16/2012 09:29 AM

file.jar abort with Ctrl + C javadoc creating html documentation javadoc Program: scans the Java code Program.java for comments formatted in javadoc style and creates html-doc pages all official Java docs are creates this way 6 of 21 10/16/2012 09:29 AM

trivial Java source example create file HelloWorld.java a first program copy/paste complete source code compile source code using javac HelloWorld.java run program using java HelloWorld /** Class HelloWorld prints out a standard string. * This is a comment in javadoc format. * @date 11.11.2011 * @author Hebert Fasching * @version 11.11 */ public class HelloWorld { public static void main(string[] args){ // --- this is another comment, explaining the command // --- print the line (println) 'Hello World!' // --- the standard (default) output, the console System.out.println("Hello World!"); 7 of 21 10/16/2012 09:29 AM

8 of 21 10/16/2012 09:29 AM

entry point and scope class: the fundamental building block; nothing exists outside a class (object) main(): entry point; here does the execution starts, stopping automatically at the end of main(){ main() is a member function of the class HelloWorld; there is a strict naming convention {... : scope defines begining and end of a class, function, control loop,... everything defined within a given scope is not known outside if available use keyboards with english layout a program is a sequence of commands, any command ends with a semicolon ; 9 of 21 10/16/2012 09:29 AM

class HelloWorld { public static void main(string[] args){ System.out.println("Hello World!"); 10 of 21 10/16/2012 09:29 AM

example: calculation example programs, systematics of commands and definitions later the format specfiers like %3d used for formated printing with prinf will be explained later /** Evaluates sum of integers. */ public class ExampleCalc { public static void main(string[] args) { // --- define an integer variable and set its value to zero int sum = 0; // --- loop example: do a calculation many times // --- increase 'index' by one as long as 'index' is smaller than 10 for (int index=0; index<10; index=index+1) { sum = sum + index; System.out.printf("index and sum: %2d %3d\n",index, sum); // the loop over 'index' ends here // the main() routine of the class ExampleCalc ends here // the class ExampleCalc ends here 11 of 21 10/16/2012 09:29 AM

12 of 21 10/16/2012 09:29 AM

example: functions. : dot operator for access to member functions and variables return the result of a function predefined mathematical functions in class Math /** Evaluates the area of a circle. */ public class ExampleFunction { /** Caclulates the area of a circle with of a given input radius. * Both input and return value are real (double precison) numbers. */ static public double area(double radius) { // --- the predefined (real) constant 'PI' of the predefined // --- class Math is accessed via the dot operator return Math.PI*radius*radius; public static void main(string[] args) { // --- define real(double) precision variables double result; 13 of 21 10/16/2012 09:29 AM

double inputradius = 10.0; // --- the member function area() of the class ExampleFunction // --- is accessed via the dot operator result = ExampleFunction.area(inputRadius); // --- a command end with a semicolon, can wrap an // --- arbitrary number of lines System.out.printf("radius and area: %10.4f %10.4f\n", inputradius, result); // --- another time inputradius = Math.sqrt(10.0); result = ExampleFunction.area(inputRadius); System.out.printf("radius and area: %10.4f %10.4f\n", inputradius, result); 14 of 21 10/16/2012 09:29 AM

example: input and output Scanner: a predefined Java class to access console input information about Java classes available on the web, search with new a new object is created (instantiated), more details lateron import java.util.scanner; public class ExampleIO { /** A simple subroutine function multiplying two integer numbers * and returning the integer (int) result. */ public static int area(int length, int width) { int result = length * width; // make calculation return result; // return result public static void main(string[] args) { // --- creating an object of type Scanner named scanner // --- we will learn lateron what this does exactly means Scanner scanner = new Scanner(System.in).useDelimiter("\n"); 15 of 21 10/16/2012 09:29 AM

System.out.print("Enter length: "); // input int a = scanner.nextint(); // length System.out.print("Enter width: "); int b = scanner.nextint(); // width // -- call the (static) function area() of the class ExampleIO int calculatedvalue = ExampleIO.area(a, b); System.out.println("calculation complete"); System.out.printf("area with length %d and width %d is %d\n", a, b, calculatedvalue); // formatted output // end of ExampleIO.main() // end of class ExampleIO 16 of 21 10/16/2012 09:29 AM

output basics formated ouput is easier to read and cuts down debugging time substantially printf() takes exactly the same formating options as in C %b: boolean %c: single char %d: integer of arbitrary length %s: string of arbitrary length %10s: allocat 10 spaces for the string %12.4f: real number with 4 decimals \n: end of line (linebreak) public class OutputBasics { public static void main(string[] args) 17 of 21 10/16/2012 09:29 AM

{ // non-standard bracket positioning final int nargs = 1; if (args.length!= nargs) { // non-standard bracket positioning return; String[] sentence = {"This","is","a","sentence"; System.out.println(" "); for (int ss=0; ss<sentence.length; ss++) System.out.printf("%s ",sentence[ss]); System.out.printf("\n"); double number = Double.parseDouble(args[0]); System.out.println(" "); System.out.printf("floating-point %10.3f number\n",number); System.out.printf(" exponential %10.3e number\n",number); System.out.printf(" fixed-width %10s string \n","short"); System.out.format(" fixed-width %10d integer \n",11); // printf (from C) and format are equivalent user@pc:~$ java OutputBasics 13.445 This is a sentence floating-point 13.445 number exponential 1.345e+01 number fixed-width short string fixed-width 11 integer 18 of 21 10/16/2012 09:29 AM

19 of 21 10/16/2012 09:29 AM

language and locale the language setting is always via the locale browser, desktop, Java,... attention german 3,141, US 3.141 import java.util.*; // for Locale, Scanner public class TestLocale { public static void main(string[] args) { // --- in- and ouput with German numbers "3 Komma 141" Locale.setDefault(Locale.GERMAN); Scanner scanner = new Scanner(System.in).useDelimiter("\n"); System.out.print("enter German floating point number: "); double rr = scanner.nextdouble(); System.out.printf("you have entered (German): %8.3f\n\n",rr); // --- in- and ouput with US numbers "3 dot 141" Locale.setDefault(Locale.US); scanner = new Scanner(System.in).useDelimiter("\n"); System.out.print("enter US floating point number: "); rr = scanner.nextdouble(); 20 of 21 10/16/2012 09:29 AM

System.out.printf("you have entered (US): %8.3f\n\n",rr); // end of TestLocale.main() // end of TestLocale user@pc:~$ java TestLocale enter German floating point number: 3,345 you have entered (German): 3,345 enter US floating point number: 7.145 you have entered (US): 7.145 21 of 21 10/16/2012 09:29 AM