Upload
nguyenhanh
View
220
Download
0
Embed Size (px)
Citation preview
Lecture 1: Introduction
Software Construction
in Java for HSE Moscow
Tom Verhoeff
Eindhoven University of Technology
Department of Mathematics & Computer Science
Software Engineering & Technology Group
www.win.tue.nl/˜wstomv/edu/sc-hse
c© 2014, T. Verhoeff @ TUE.NL 1/28 Software Construction: Lecture 1
About the Lectures
• In English
– Slow me down, if it goes too fast
– Interrupt me, if it is not clear
• Audio/Video link has limitations
• Slides, tool demo
• Not enough time to explain all details in lecture:
You must explore yourself
c© 2014, T. Verhoeff @ TUE.NL 2/28 Software Construction: Lecture 1
About the Lecturer
• Study: Applied Mathematics
• PhD Thesis: A Theory of Delay-Insensitive Systems
• Running: long-distance
• Music: recorder (wooden flute) quartet
• Mathematical Art (with my father)
• Teach math enrichment in a primary school
• Teach/Research at TU/e: Model-Driven Engineering
c© 2014, T. Verhoeff @ TUE.NL 3/28 Software Construction: Lecture 1
Overview
• Introductory problem
• Organizational issues, the big picture
• Motivation, background
• Imperative core of the Java programming language
• Study material
c© 2014, T. Verhoeff @ TUE.NL 4/28 Software Construction: Lecture 1
Introductory Problem
• What is a problem?
c© 2014, T. Verhoeff @ TUE.NL 5/28 Software Construction: Lecture 1
Introductory Problem
• Problem =
Difference between
what you want and
what you have
c© 2014, T. Verhoeff @ TUE.NL 6/28 Software Construction: Lecture 1
Dice Game: What We Want
• 5 players , 2 cubic dice , each producing values 1 through 6
• Per round , each player rolls the 2 dice
• Player with unique highest roll wins round; otherwise, no winner
Player Result
1 2 3 4 5
3 8 7 10 8 Player 4 wins
6 11 4 8 11 no winner
12 12 13 12 12 ?
• Player 1 requests to roll a single dodecahedron , whose faces arenumbered 1 through 12
• Question: How does this influence the game?
c© 2014, T. Verhoeff @ TUE.NL 7/28 Software Construction: Lecture 1
Solving a Problem through a Program: What We Have
What you have:
• Programming language
• Programming tools
• Programming techniques
What you do:
• Analyze the problem
• Design a program (text)
• Run the program, provide input, use output
c© 2014, T. Verhoeff @ TUE.NL 8/28 Software Construction: Lecture 1
Weekly Schedule
Course: 2 × 7 weeks
Day Moscow Time What
Monday 16:40 – 18:00 Lecture
Monday 18:00 – 18:30 Question & Answer
(if schedule permits it)
Thursday 23:00 Homework deadline
c© 2014, T. Verhoeff @ TUE.NL 9/28 Software Construction: Lecture 1
Homework
• We use peach3 at ext.peach3.nl
• Register
Username = initial + family name; e.g. sshershakov
• Join course Software Construction for HSE (2014–2015)
• Submit your work
• Receive feedback
– from peach3, automatically
– from me or HSE assistants
– from your peers (peer review)
c© 2014, T. Verhoeff @ TUE.NL 10/28 Software Construction: Lecture 1
Study Material
• [DE] Introduction to Programming Using Java
by David Eck, Version 7.0, August 2014
• [EB] Programming in the Large with Design Patterns
by Eddie Burris, Pretty Print Press, 2012
• Slides, handouts, example code, assignments
• Miscellaneous (web) resources
• Frequently Asked Questions
• Subversion repository (see FAQ)
c© 2014, T. Verhoeff @ TUE.NL 11/28 Software Construction: Lecture 1
Additional Study Material
• The Java Language Specification
by James Gosling et al.
c© 2014, T. Verhoeff @ TUE.NL 12/28 Software Construction: Lecture 1
Tools
JDK Java Development Kit, Java 7 Standard Edition
DrJava IDE with Interactions
NetBeans IDE with GUI designer, and plug-ins
Javadoc Java documentation tool
JUnit Java Unit Testing Framework
JaCoCo Java Code Coverage plug-in
UML Unified Modeling Language
SQE Software Quality Environment
Profiler Performance Analysis
c© 2014, T. Verhoeff @ TUE.NL 13/28 Software Construction: Lecture 1
Motivation
• Assumption: You have some programming experience
Syntax, semantics, pragmatics (conventions)
Types, variables, expressions, statements, input/output
• Goal: Systematic design of larger object-oriented Java programs
• Design: Blueprint; also: activity leading to blueprint
• Concepts, terminology, notation
How to communicate about programs and about programming
• Rational design
How to reason about programs, how to motivate design decisions
c© 2014, T. Verhoeff @ TUE.NL 14/28 Software Construction: Lecture 1
Product, Process, and Documentation
Product: machine-executable ‘working’ program or component
Product documentation: artifacts to support product (design)
Process: the way persons work (individually, or as a team)
Guidelines, step-by-step check lists, design methods, . . .
Process documentation: describes/prescribes a process
Focus on: methodical process, producing documented product
Not on product itself; not on creation of process documentation
c© 2014, T. Verhoeff @ TUE.NL 15/28 Software Construction: Lecture 1
Quick Intro to Imperative Core of Java
• Chapters 2, 3, and 7 of book by David Eck
• Comments: /*... */ /** ... */ //...
• Types, Values, Literals: int long boolean String double
• Expressions: (1 + 2) * 3 == 9 "ab"+ "c"
• Variables, Assignment Statements: variable = expression;
• Statement Blocks: { ... }
• Control Statements: if else, while, for
• Console Output: System.out.println(...);
• Arrays: int[] a = { 0, 1, 2 }; ... a[expression] ...
c© 2014, T. Verhoeff @ TUE.NL 16/28 Software Construction: Lecture 1
How to Practice with Java
Install DrJava and do some Interactions
> 2+2
4
> import javax.swing.JFrame;
> JFrame jf = new JFrame(); // no window visible
> jf.show(); // now locate the window on your computer screen
> jf.getWidth()
128
> jf.setSize(640, 480);
> jf.setTitle("Hello, here I am!");
c© 2014, T. Verhoeff @ TUE.NL 17/28 Software Construction: Lecture 1
What Is a Java Program?
• At compile time: Collection of classes ,
where each class has
– typed variables , also known as fields or attributes or
member variables
– methods , optionally with typed return value and parameters
• At run time: also a collection of objects ,
where each object is instantiated from a class,
and each variable in an object has a value from its type
c© 2014, T. Verhoeff @ TUE.NL 18/28 Software Construction: Lecture 1
Java Programs in Week 1
• One class
– without variables, but with constant definitions:
final static Type NAME = expression;
– one (static) method
with parameters (input) and a return value (output)
• No objects
c© 2014, T. Verhoeff @ TUE.NL 19/28 Software Construction: Lecture 1
Simple Java Program
1 public class EchoArgs {
2
3 public static void main(String[] args) {
4 for (String arg : args) {
5 System.out.println(’"’ + arg + ’"’);
6 }
7 }
8 }
In DrJava Interactions: run EchoArgs Hello HSE! produces
"Hello"
"HSE!"
c© 2014, T. Verhoeff @ TUE.NL 20/28 Software Construction: Lecture 1
Important Notes
In Java, a class named Xyz must be stored in a file named Xyz.java
In Java, by default, execution starts at public static void main
which must have one string array parameter String[] args
In DrJava, this main is invoked by run ...
In DrJava, static method m of class C is invokved by C.m(...)
c© 2014, T. Verhoeff @ TUE.NL 21/28 Software Construction: Lecture 1
Program for Dividing Candy
1 /**2 * k kids together receive c candies.
3 * They wonder whether it is possible
4 * to divide all candies fairly.
5 *6 * If this is possible, they also want to know
7 * how many candies each of them gets.
8 * This is a number q such that c == q * k .
9 *10 * k, c, and q are non-negative integers, less than 10ˆ18.
11 *
c© 2014, T. Verhoeff @ TUE.NL 22/28 Software Construction: Lecture 1
Program for Dividing Candy
1 <!--//# BEGIN TODO: Name, group, and date-->
2 <p><font color="red"><b>Replace this line</b></font></p>
3 <!--//# END TODO-->
4 */
5 // -----8<----- cut line -----8<-----
c© 2014, T. Verhoeff @ TUE.NL 22/28 Software Construction: Lecture 1
Program for Dividing Candy
1 public class Candy {2
3 /**4 * Returns a negative value to indicate5 * that the division is impossible, and otherwise,6 * returns the number of candies each kid gets.7 */8 static long divide(long k, long c) {9 long result; // value to be returned
10 // put your solution here11 result = 5; // just some value to make it compile12 return result;13 }14 }
In DrJava Interactions: Candy.divide(3, 15) produces 5
c© 2014, T. Verhoeff @ TUE.NL 22/28 Software Construction: Lecture 1
Important Constraints
• Put in your name, group, and the date, above cut line with --8<--
• This week
– only one static method; no auxiliary methods, no objects
– no package statements
– no import statements
• Write readable code (see Coding Standard)
• Test your program before submitting (Quality Assurance)
• Submit your own work (individual assignments)
c© 2014, T. Verhoeff @ TUE.NL 23/28 Software Construction: Lecture 1
Program for Dice Game
1 /**2 * Consider the following dice game.
3 * Five players each roll once per round.
4 * Player 1 rolls a (fair) dodecahedron,
5 * having 12 faces with the numbers 1 through 12.
6 * The other players (2 through 5) roll two fair dice,
7 * each having 6 faces with the numbers 1 through 6.
8 * The player with the unique highest roll
9 * wins the round. If the highest roll is
10 * not unique, then there is no round winner.
11 *
c© 2014, T. Verhoeff @ TUE.NL 24/28 Software Construction: Lecture 1
Program for Dice Game
1 <!--//# BEGIN TODO: Name, id, and date-->
2 <p><font color="red"><b>Replace this line</b></font></p>
3 <!--//# END TODO-->
4 */
5 // -----8<----- cut line -----8<-----
c© 2014, T. Verhoeff @ TUE.NL 24/28 Software Construction: Lecture 1
Program for Dice Game
1 public class DiceGame {
2
3 /** Number of players, >= 1 */
4 final static int NUM_PLAYERS = 5;
5
6 /** Index for frequency of rounds without winner */
7 final static int NO_WINNER = 0;
c© 2014, T. Verhoeff @ TUE.NL 24/28 Software Construction: Lecture 1
Program for Dice Game
1 /**2 * Simulates r >= 0 rounds of the dice game and
3 * returns how often each player won.
4 * The return value is an array, where
5 * index 0 counts the number rounds without winner, and
6 * index i > 0 counts the number of rounds won by player i.
7 */
8 static public int[] simulate(int r) {
c© 2014, T. Verhoeff @ TUE.NL 24/28 Software Construction: Lecture 1
Program for Dice Game
1 static public int[] simulate(int r) {
2 int[] result; // winning frequencies
3 result = new int[1 + NUM_PLAYERS]; // initialize to 0
4 //# BEGIN TODO: Provide your solution
5 // Replace this line
6 //# END TODO
7 return result;
8 }
9
10 }
c© 2014, T. Verhoeff @ TUE.NL 24/28 Software Construction: Lecture 1
More Practicing
• codingbat.com
c© 2014, T. Verhoeff @ TUE.NL 25/28 Software Construction: Lecture 1
How to Write Readable Java Source Code
• Code Conventions for the Java Programming Language
• Java Coding Standards by ESA BSSC (2005)
• Concerns: layout, naming, commenting, structure
c© 2014, T. Verhoeff @ TUE.NL 26/28 Software Construction: Lecture 1
Coding Standard for 2IPC0
10 simple conventions
Layout:
• NetBeans > Source > Format : default settings do a good job
• Declare no more than one field or local variable per line
Comments:
• Each public class, constructor, method, field has doc comment
• Each non-public class, constructor, method, field, local variable
has normal (non-doc) comment
c© 2014, T. Verhoeff @ TUE.NL 27/28 Software Construction: Lecture 1
Summary
• Consult the course page: www.win.tue.nl/˜wstomv/edu/sc-hse/
• Study Material
• Imperative Core of Java
• DrJava, codingbat.com
• Register in peach3: ext.peach3.nl
• Assignments: Candy, DiceGame
c© 2014, T. Verhoeff @ TUE.NL 28/28 Software Construction: Lecture 1