Chapter 1 Writing a Program Fall 2011. Class Overview Course Information –On the web page and Blackboard –www.uncp.edu/home/lilliec/www.uncp.edu/home/lilliec/

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 7 IO, Files, and URLs.
Advertisements

Lecture 15: I/O and Parsing
Simple Programs Simple Strings Writing some programs Only in Parameters? I/O Gadget.
Java File I/O. File I/O is important! Being able to write and read from files is necessary and is also one common practice of a programmer. Examples include.
MOD III. Input / Output Streams Byte streams Programs use byte streams to perform input and output of 8-bit bytes. This Stream handles the 8-bit.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 3: Program Statements 3.6 – Iterators – p
Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
CS 206 Introduction to Computer Science II 01 / 20 / 2009 Instructor: Michael Eckmann.
Chapter 1: Computer Systems
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()
Feb. 23, 2004CS WPI1 CS 509 Design of Software Systems Lecture #5 Monday, Feb. 23, 2004.
1 Fall 2008ACS-1903 for Loop Reading files String conversions Random class.
Guide To UNIX Using Linux Third Edition
Chapter 3b Standard Input and Output Sample Development.
CS 206 Introduction to Computer Science II 01 / 23 / 2009 Instructor: Michael Eckmann.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Two Ways to Store Data in a File Text format Binary format.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented.
Class Library, Formatting, Wrapper Classes, and JUnit Testing
Java: Chapter 1 Computer Systems Computer Programming II.
Chapter 1 Introduction. Goal to learn about computers and programming to compile and run your first Java program to recognize compile-time and run-time.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Ex3 Preview, Swing tutorial Ex1 review Amit Shabtay.
Chapter 11 Arrays Continued
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
1 Software Construction and Evolution - CSSE 375 Exception Handling - Principles Steve Chenoweth, RHIT Above – Exception handling on the ENIAC. From
CS61B L02 Using Objects (1)Garcia / Yelick Fall 2003 © UCB Kathy Yelick Handout for today: These lecture notes Computer Science 61B Lecture 2 – Using Objects.
Overview of Programming and Problem Solving Textbook Chapter 1 1.
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
Chapter 15 Text Processing and File Input/Output Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin,
Introduction IS Outline  Goals of the course  Course organization  Java command line  Object-oriented programming  File I/O.
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Chapter 9 1 Chapter 9 – Part 2 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
©SoftMoore ConsultingSlide 1 Lexical Analysis (a.k.a. Scanning)
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
Simple Java I/O Part I General Principles. Streams All modern I/O is stream-based A stream is a connection to a source of data or to a destination for.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
Data-Driven Programs Eric Roberts CS 106A February 26, 2016.
XuanTung Hoang 1 Something to discuss Feedbacks on Midterm Exam Final exam and term project  Final exam requires solid knowledge/skills in Java  Be more.
1 Week 1 Introduction, Writing a Program, Building a System Software Engineering Spring Term 2016 Marymount University School of Business Administration.
1 Week 1 Introduction, Writing a Program, Building a System Software Engineering Fall Term 2015 Marymount University School of Business Administration.
1 Problem Solving  The purpose of writing a program is to solve a problem  The general steps in problem solving are: Understand the problem Dissect the.
CSC 222: Object-Oriented Programming
Chapter 5: Enhancing Classes
Lexical Analysis (a.k.a. Scanning)
Working with Java.
Reading from a file A file is typically stored on your computers hard drive. In the simplest case, lets just assume it is text. For a program to use.
Chapter 11 Object-Oriented Design
JAVA COLLECTIONS LIBRARY
Chapter 3: Using Methods, Classes, and Objects
Data-Driven Programs Eric Roberts CS 106A February 26, 2010.
CHAPTER 5 JAVA FILE INPUT/OUTPUT
I/O Basics.
Part I General Principles
Lecture 3: Working with Data Structures
week 1 - Introduction Goals
Topics Introduction to File Input and Output
Chapter 1: Computer Systems
Test Driven Lasse Koskela Chapter 2: Beginning TDD
Instructor: Alexander Stoytchev
Topics Introduction to File Input and Output
Chapter 1: Creating a Program.
Presentation transcript:

Chapter 1 Writing a Program Fall 2011

Class Overview Course Information –On the web page and Blackboard – –Syllabus –Assignments –Homework –Exams –Attendance Policy Textbook –Tsui & Karam, Essentials of Software Engineering

Objectives Analyze issues for simple programs –Requirements –Design Constraints –Testing –Error Estimation –Implementation details Understand sequence of activities Preview of future topics

Requirements Requirements – define and qualify system –Defined by client, with help from engineer –Functional – define what must be done –Non-Functional – qualify the functional ones Design constraints –On design or implementation –Programming language, platforms etc

A Simple Problem Given a collection of lines of text (strings) stored in a file, sort them in alphabetical order and write them to another file This is the requirement

Functional requirements Input format –Character size –Line separator Specify Sorting –Numbers –Upper/lowercase Special cases Boundaries Error Conditions

Nonfunctional requirements Performance Real-time ? Modifiability

Design Constraints User Interface –GUI, CLI, Web … Typical input and size Platforms Schedule

Design Decisions Programming Languages Algorithms

Testing White-Box – test the code as written Black-Box – assume no knowledge of code Unit testing – by programmer, on each piece Integration Testing – Put the units together into bigger system Acceptance testing – if it fails, client rejects program

Estimating How much effort is required ? –Usually done in person-months Cost –Once know the effort can estimate cost Time / Scheduling –Once know the effort can estimate schedule

Implementation Rules Be consistent Choose names carefully Test before using –Test, test, test Know thy libraries Do code reviews

Basic Design Class StringSorter –Read –Sort –Write –Wrapper to do Read then Sort then Write Will unit-test each method Will use ArrayList to hold the lines

import java.io.*; // for Reader(s), Writer(s), IOException import java.util.*; // for List, ArrayList, Iterator public class StringSorter { ArrayList lines; public void readFromStream(Reader r) throws IOException { BufferedReader br=new BufferedReader(r); lines=new ArrayList(); while(true) { String input=br.readLine(); if(input==null) break; lines.add(input); } Implement

public class TestStringSorter extends TestCase { private ArrayList make123() { ArrayList l = new ArrayList(); l.add("one"); l.add("two"); l.add("three"); return l; } public void testReadFromStream() throws IOException{ Reader in=new FileReader("in.txt"); StringSorter ss=new StringSorter(); ArrayList l= make123(); ss.readFromStream(in); assertEquals(l,ss.lines); } Test

Figure 1.5: Junit GUI

static void swap(List l, int i1, int i2) { Object tmp=l.get(i1); l.set(i1, l.get(i2)); l.set(i2, tmp); } Implement

public void testSwap() { ArrayList l1= make123(); ArrayList l2=new ArrayList(); l2.add("one"); l2.add("three"); l2.add("two"); StringSorter.swap(l1,1,2); assertEquals(l1,l2); } Test

static int findIdxBiggest(List l, int from, int to) { String biggest=(String) l.get(0); int idxBiggest=from; for(int i=from+1; i<=to; ++i) { if(biggest.compareTo(l.get(i))<0) {// it is bigger biggest=(String)l.get(i); idxBiggest=i; } return idxBiggest; } Figure 1.8: findIdxBiggest method Implement

public void testFindIdxBiggest() { StringSorter ss=new StringSorter(); ArrayList l = make123(); int i=StringSorter.findIdxBiggest(l,0,l.size()- 1); assertEquals(i,1); } Figure 1.9: testFindIdxBiggest method Test

public void sort() { for(int i=lines.size()-1; i>0; --i) { int big=findIdxBiggest(lines,0,i); swap(lines,i,big); } Figure 1.10: sort method Implement

public void testSort1() { StringSorter ss= new StringSorter(); ss.lines=make123(); ArrayList l2=new ArrayList(); l2.add("one"); l2.add("three"); l2.add("two"); ss.sort(); assertEquals(l2,ss.lines); } Figure 1.11 testSort1 method Test

Know thy library void sort() { java.util.Collections.sort(lines); } A sort routine already exists in java (and most other languages)

public void writeToStream(Writer w) throws IOException { PrintWriter pw=new PrintWriter(w); Iterator i=lines.iterator(); while(i.hasNext()) { pw.println((String)(i.next())); } Figure 1.13: writeToStream method Implement

public void testWriteToStream() throws IOException{ // write out a known value StringSorter ss1=new StringSorter(); ss1.lines=make123(); Writer out=new FileWriter("test.out"); ss1.writeToStream(out); out.close();// then read it and compare Reader in=new FileReader("in.txt"); StringSorter ss2=new StringSorter(); ss2.readFromStream(in); assertEquals(ss1.lines,ss2.lines); } Test

public void sort(String inputFileName, String outputFileName) throws IOException { Reader in=new FileReader(inputFileName); Writer out=new FileWriter(outputFileName); StringSorter ss=new StringSorter(); ss.readFromStream(in); ss.sort(); ss.writeToStream(out); in.close(); out.close(); } Implement

public void testSort2() throws IOException { // write out a known value StringSorter ss1=new StringSorter(); ss1.sort("in.txt","test2.out"); ArrayList l=new ArrayList(); l.add("one"); l.add("three"); l.add("two"); // then read it and compare Reader in=new FileReader("test2.out"); StringSorter ss2=new StringSorter(); ss2.readFromStream(in); assertEquals(l,ss2.lines); } Figure 1.16: testSort2 method Test

Command-Line interface import java.io.IOException; public class StringSorterCommandLine { public static void main(String args[]) throws IOException { if(args.length!=2) { System.out.println("Use: cmd inputfile outputfile"); } else { StringSorter ss=new StringSorter(); ss.sort(args[0],args[1]); }

A Bad GUI public class StringSorterBadGUI { public static void main(String args[]) throws IOException { try { StringSorter ss=new StringSorter(); String inFileName=JOptionPane.showInputDialog ("Please enter input file name"); String outFileName=JOptionPane.showInputDialog ("Please enter output file name"); ss.sort(inFileName, outFileName); } finally { System.exit(1); }

A Better Interface

A Better GUI Click any button, to get the open dialog