Text User Interfaces. Keyboard IO plus

Size: px
Start display at page:

Download "Text User Interfaces. Keyboard IO plus"

Transcription

1 Text User Interfaces Keyboard IO plus

2 User Interface and Model Model: objects that solve problem at hand. User interface: interacts with user getting input from user giving output to user reporting on status of model. Flexibility in design: identifying these as separate subsystems.

3 Writing standard output System.out: methods for writing to standard output. public void println (String s) Write specified String to standard output and then terminate the line. public void print (String s) Write the specified String to standard output. public void println () Terminate current line by writing line terminator to standard output. public void flush () Flush stream: write buffered output to standard output and flush that stream.

4 Java Input The Scanner Class A standard Java class We will use it to help with keyboard input Part of Java 5 onwards

5 import java.util.scanner; The import tells Java to look for Scanner methods A simple text scanner which parses primitive types and strings Uses a delimiter pattern of whitespace to identify tokens

6 Example Program 1 import java.util.scanner; public class ScannerEx1 { private Scanner myscanner; public ScannerEx1() { myscanner = new Scanner( System.in ); }

7 Explanation Program 1 // Tells Java to look for the Scanner class import java.util.scanner; // Declares a Scanner object called myscanner Scanner myscanner ; // Create a Scanner object called myscanner // will get input from the keyboard myscanner = new Scanner( System.in );

8 Example Program 1 cont public String getline(string prompt) { System.out.print( prompt); // nextline() is a method of the Scanner class // it reads a line of text from the keyboard. } return myscanner.nextline();

9 Some Scanner Methods To read this A number with no decimal point A number with a decimal point A word ending in a blank space A line ( or what remains of the line) A single character Use this nextint(); nextdouble(); next(); nextline(); findinline(. ).charat(0);

10 public int getint(string prompt) { System.out.print( prompt); return myscanner.nextint(); } public double getdouble(string prompt) { System.out.print( prompt); return myscanner.nextdouble(); }

11 public String getword(string prompt) { System.out.print( prompt); return myscanner.next(); } public char getchar(string prompt) { System.out.print( prompt); } return myscanner.findinline(".").charat(0);

12 More on Scanner If there are no tokens in the input stream, it will wait until user keys in a line with non-blank characters. An input stream can be closed with the command close(). If an input stream is closed or terminated attempting to read it will fail.

13 Yet more After reading some input tokens, you can skip the rest of the input tokens in the current line with the method public String nextline() advances past the current line and returns any input that was skipped excluding the line terminator.

14 More Scanner methods Have preconditions: token of form specified by method used, otherwise fails. To verify these preconditions use: hasnextboolean() hasnextint() hasnextdouble() hasnext()

15 Building a simple Text based UI A Fahrenheit to Centigrade Conversion Application The Specification: Allow user to input temperature in Fahrenheit and be told the Centigrade equivalent Allow user to do opposite, i.e Centigrade to Fahrenheit Program menu driven

16 Design Thoughts Need to identify classes a Fahrenheit class? a Centigrade class? why both? How to deal with I/O? Use separate class

17 FahrenheitTemperature Class What services should this class provide? default constructor : what initial value? get Temperature set Temperature get Centigrade equivalent

18 CentigradeTemperature Class What services should this class provide? default constructor : what initial value? get Temperature set Temperature get Fahrenheit equivalent

19 FahrenheitTemperature Class public class FahrenheitTemperature { private double ftemperature; // The Fahrenheit temperature public FahrenheitTemperature() { ftemperature = 32.0; // freezing point of water }

20 FahrenheitTemperature public double getftemperature() { return ftemperature; } Class public double converttocentigrade() { double centigrade; } centigrade = 5.0 * (ftemperature -32.0) / 9.0; return centigrade; public void setftemperature( double newftemperature) { ftemperature =newftemperature; }

21 CentigradeTemperature class Essentially similar to FahrenheitTemperature class Main difference in conversion formula: f = 9.0 * c / Coding left as an exercise

22 Text UI Class Will provide a menu with commands to: Convert from F to C Convert from C to F Quit the program What does the UI need to know? FahrenheitTemperature object CentigradeTemperature object Any thing else? Lets wait and see

23 Text UI Code public class TemperatureConversionUI { // attributes private CentigradeTemperature c ; private FahrenheitTemperature f ; private Scanner myscanner;

24 constructor public TemperatureConversionUI() { c = new CentigradeTemperature(); f = new FahrenheitTemperature (); myscanner = new Scanner(System.in);. }

25 Some thoughts on the menu Consider what it should look like Options are Convert from Fahrenheit to Centigrade enter 1 Convert from Centigrade to Fahrenheit enter 2 To end program enter 3 Enter command :

26 First attempt at Menu public void menu() { int command; displaymenu(); command = getcommand(); execute( command ); }

27 displaymenu private void displaymenu() { System.out.println( Options are ); System.out.println( Convert from Fahrenheit to Centigrade enter 1 ); System.out.println( Convert from Centigrade to Fahrenheit enter 2 ); System.out.println( To end program enter 3 ); }

28 getcommand() private int getcommand() { System.out.print ( Enter command: ); return myscanner.nextint(); }

29 execute() private void execute( int command) { if ( command == 1) doftoc(); else if ( command == 2 ) doctof(); else if ( command == 3) System.exit(0); else System.out.println( Unkown command ); }

30 doftoc() private void doftoc() { System.out.print( Enter F temperature: ); } double ft = myscanner.nextdouble(); f. setftemperature( ft); System.out.println( ft + F equals + f. converttocentigrade() + C );

31 doctof() private void doctof() { System.out.print( Enter C temperature: ); } double ct = myscanner.nextdouble(); c. setctemperature( ct); System.out.println( ct + C equals + c. convertto Fahrenheit() + F );

32 Review the design public void menu() { int command; displaymenu(); command = getcommand(); execute( command ); } Only allows for one command then program closes

33 Better method public void menu() { int command = 0; while ( command! = 3 ) { displaymenu(); command = getcommand(); execute( command ); } }

34 execute() - revised private void execute( int command) { if ( command == 1) doftoc(); else if ( command == 2 ) doctof(); else if ( command == 3) System.out.println( Program closing down ); else System.out.println( Unknown command ); }

35 Java Applications public static void main (String [ ] args)

36 Java applications To run a Java program the JVM needs to know which class to start with It looks for a class containing a method called main There should only be one such class Syntax public static void main (String [ ] args)

37 Example public class Example { } public static void main (String [ ] args) { // code goes here }

38 The main method main must exist main must be public main must be static (class method) main must have a String array parameter Only main can be invoked

39 Use of main Good OOP style requires that main be kept simple Guidelines: create an object call the first method Should NOT be full of complex logic!

40 Main method - example public static void main(string[] args) { TemperatureConversionUI textui = new TemperatureConversionUI() textui.menu(); }

41 Using main in BlueJ Create a new project called ExampleMain Create a class in the project called ExampleMain Type in the following code: (see next slide) Compile the class

42 public class ExampleMain { public static void main (String [] args) { System.out.println("Hello"); } }

43 Using main in BlueJ continued Select the class you will see the constructor listed (ignore) also main will be listed You can invoke main without instantiating an object

44 Summary A Java application will consist of several classes One and only one class will contain the main method The main method will just start the application going The main method will NOT contain any complex logic

45 Some of the plus For some applications it is necessary to save data to files and to get the data from a file This is called data persistence Can use the Scanner class to obtain data from a file

46 Using the Scanner to read a file import java.util.scanner; import java.io.*;.. public void ScannerFileRead( String filename) throws FileNotFoundException { String aline; Scanner discscanner = new Scanner( new File(fileName )); while ( discscanner.hasnext() ) { aline= discscanner.nextline(); System.out.println(aLine); } discscanner.close(); Can use the Scanner methods with the file, e.g. next(), nextint(), } }

47 Explanation throws FileNotFoundException The program expects that the file already exists If it does not it complains new File( filename) Java has a File class. Here we are telling the Scanner to link to the file discscanner.close(); Tell the system that the file is free

48 Output to a file import java.io.*; { public void PrintTextFile( String filename) throws IOException } PrintStream print = new PrintStream( new File( filename) ); print.println( "The world is so full" ); print.println( "Of a number of things," ); print.println( "I'm sure we should all" ); print.println( "Be as happy as kings." ); print.close();

49 Explanation If the file already exists its contents will be destroyed unless the user does not have permission to alter the file. If this is the case, an IOException will be thrown The program will end.

50 Side Notes: Some String operations Many operations for Strings See API documentation for class String Important one public boolean equals(object anobject) Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object

51 Side note: String equality if(input == "bye") { }... tests identity if(input.equals("bye")) { }... tests equality Strings should always be compared with.equals

52 Identity vs equality 1 Other (non-string) objects: :Person Fred :Person Jill person1 person2 person1 == person2?

53 Identity vs equality 2 Other (non-string) objects: :Person Fred :Person Fred person1 person2 person1 == person2?

54 Identity vs equality 3 Other (non-string) objects: :Person Fred :Person Fred person1 person2 person1 == person2?

55 Identity vs equality (Strings) String input = reader.getinput(); if(input == "bye") {... } == tests identity :String "bye" :String ==? "bye" input (may be) false!

56 Identity vs equality (Strings) String input = reader.getinput(); if(input.equals("bye")) {... } equals tests equality :String "bye" :String equals? "bye" input true!

57 Class Variables Classes can have fields Known as Class variables or static variables Exactly one copy of a class variable exists Shared by all instances Syntax: private static type variablename; private static int count;

58 Constants Can declare constants using key word final e.g. final int GRAVITY = 3; convention: constants are ALL CAPITALS

59 example // effect of gravity private static final int GRAVITY = 3; here we have a class constant no matter how many instances are created only one GRAVITY will exist

60 Class variables

61 The Java class library Thousands of classes Tens of thousands of methods Many useful classes that make life much easier A competent Java programmer must be able to work with the libraries.

62 Working with the library You should: know some important classes by name; know how to find out about other classes. Remember: We only need to know the interface, not the implementation.

63 Reading class documentation Documentation of the Java libraries in HTML format; Readable in a web browser Class API: Application Programmers Interface Interface description for all library classes

64 Interface vs implementation The documentation includes the name of the class; a general description of the class; a list of constructors and methods return values and parameters for constructors and methods a description of the purpose of each constructor and method the interface of the class

65 Interface vs implementation The documentation does not include private fields (most fields are private) private methods the bodies (source code) for each method the implementation of the class

66 Using library classes Classes from the library must be imported using an import statement (except classes from java.lang). They can then be used like classes from the current project.

67 Packages and import Classes are organised in packages. Single classes may be imported: import java.util.arraylist; Whole packages can be imported: import java.util.*;

68 Writing class documentation Your own classes should be documented the same way library classes are. Other people should be able to use your class without reading the implementation. Make your class a 'library class'!

69 Elements of documentation Documentation for a class should include: the class name a comment describing the overall purpose and characteristics of the class a version number the authors names documentation for each constructor and each method

70 Elements of documentation The documentation for each constructor and method should include: the name of the method the return type the parameter names and types a description of the purpose and function of the method a description of each parameter a description of the value returned

71 javadoc Class comment: /** * The Responder class represents a response * generator object. It is used to generate * an automatic response. * Michael Kölling 1.0 (30.Mar.2006) */

72 javadoc Method comment: /** * Read a line of text from standard input (the text * terminal), and return it as a set of words. * prompt A prompt to print to screen. A set of Strings, where each String is * one of the words typed by the user */ public HashSet<String> getinput(string prompt) {... }

73 Public vs private Public attributes (fields, constructors, methods) are accessible to other classes. Fields should not be public. Private attributes are accessible only within the same class. Only methods that are intended for other classes should be public.

74 Information hiding Data belonging to one object is hidden from other objects. Know what an object can do, not how it does it. Information hiding increases the level of independence. Independence of modules is important for large systems and maintenance.

More sophisticated behaviour

More sophisticated behaviour Objects First With Java A Practical Introduction Using BlueJ More sophisticated behaviour Using library classes to implement some more advanced functionality 2.0 Main concepts to be covered Using library

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

More Sophisticated Behaviour

More Sophisticated Behaviour More Sophisticated Behaviour Technical Support System V1.0 Produced by: Dr. Siobhán Drohan Mairead Meagher Based on Ch. 5, Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes,

More information

ing execution. That way, new results can be computed each time the Class The Scanner

ing execution. That way, new results can be computed each time the Class The Scanner ing execution. That way, new results can be computed each time the run, depending on the data that is entered. The Scanner Class The Scanner class, which is part of the standard Java class provides convenient

More information

File I/O Array Basics For-each loop

File I/O Array Basics For-each loop File I/O Array Basics For-each loop 178 Recap Use the Java API to look-up classes/method details: Math, Character, String, StringBuffer, Random, etc. The Random class gives us several ways to generate

More information

A token is a sequence of characters not including any whitespace.

A token is a sequence of characters not including any whitespace. Scanner A Scanner object reads from an input source (keyboard, file, String, etc) next() returns the next token as a String nextint() returns the next token as an int nextdouble() returns the next token

More information

Simple Java Input/Output

Simple Java Input/Output Simple Java Input/Output Prologue They say you can hold seven plus or minus two pieces of information in your mind. I can t remember how to open files in Java. I ve written chapters on it. I ve done it

More information

Main loop structure. A Technical Support System. The exit condition. Main loop body

Main loop structure. A Technical Support System. The exit condition. Main loop body Main concepts to be covered More sophisticated behavior Using library classes Reading documentation Using library classes to implement some more advanced functionality 5.0 2 The Java class library Thousands

More information

CITS1001 week 6 Libraries

CITS1001 week 6 Libraries CITS1001 week 6 Libraries Arran Stewart April 12, 2018 1 / 52 Announcements Project 1 available mid-semester test self-assessment 2 / 52 Outline Using library classes to implement some more advanced functionality

More information

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

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O 1 Sending Output to a (Text) File import java.util.scanner; import java.io.*; public class TextFileOutputDemo1 public static void

More information

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling 2

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling 2 !"# $ Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling 2 % % % & ' &' " Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes,

More information

Fundamentals of Programming Data Types & Methods

Fundamentals of Programming Data Types & Methods Fundamentals of Programming Data Types & Methods By Budditha Hettige Overview Summary (Previous Lesson) Java Data types Default values Variables Input data from keyboard Display results Methods Operators

More information

4. Java Project Design, Input Methods

4. Java Project Design, Input Methods 4-1 4. Java Project Design, Input Methods Review and Preview You should now be fairly comfortable with creating, compiling and running simple Java projects. In this class, we continue learning new Java

More information

Using Java Classes Fall 2018 Margaret Reid-Miller

Using Java Classes Fall 2018 Margaret Reid-Miller Using Java Classes 15-121 Fall 2018 Margaret Reid-Miller Today Strings I/O (using Scanner) Loops, Conditionals, Scope Math Class (random) Fall 2018 15-121 (Reid-Miller) 2 The Math Class The Math class

More information

Java Input/Output. 11 April 2013 OSU CSE 1

Java Input/Output. 11 April 2013 OSU CSE 1 Java Input/Output 11 April 2013 OSU CSE 1 Overview The Java I/O (Input/Output) package java.io contains a group of interfaces and classes similar to the OSU CSE components SimpleReader and SimpleWriter

More information

Input. Scanner keyboard = new Scanner(System.in); String name;

Input. Scanner keyboard = new Scanner(System.in); String name; Reading Resource Input Read chapter 4 (Variables and Constants) in the textbook A Guide to Programming in Java, pages 77 to 82. Key Concepts A stream is a data channel to or from the operating system.

More information

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

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved. Java How to Program, 10/e Education, Inc. All Rights Reserved. Each class you create becomes a new type that can be used to declare variables and create objects. You can declare new classes as needed;

More information

AP Computer Science Unit 1. Writing Programs Using BlueJ

AP Computer Science Unit 1. Writing Programs Using BlueJ AP Computer Science Unit 1. Writing Programs Using BlueJ 1. Open up BlueJ. Click on the Project menu and select New Project. You should see the window on the right. Navigate to wherever you plan to save

More information

Reading Input from Text File

Reading Input from Text File Islamic University of Gaza Faculty of Engineering Computer Engineering Department Computer Programming Lab (ECOM 2114) Lab 5 Reading Input from Text File Eng. Mohammed Alokshiya November 2, 2014 The simplest

More information

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

CS 152: Data Structures with Java Hello World with the IntelliJ IDE CS 152: Data Structures with Java Hello World with the IntelliJ IDE Instructor: Joel Castellanos e-mail: joel.unm.edu Web: http://cs.unm.edu/~joel/ Office: Electrical and Computer Engineering building

More information

AP Computer Science Unit 1. Writing Programs Using BlueJ

AP Computer Science Unit 1. Writing Programs Using BlueJ AP Computer Science Unit 1. Writing Programs Using BlueJ 1. Open up BlueJ. Click on the Project menu and select New Project. You should see the window on the right. Navigate to wherever you plan to save

More information

CS 112 Introduction to Programming

CS 112 Introduction to Programming CS 112 Introduction to Programming Summary of Methods; User Input using Scanner Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu Admin

More information

Object-Oriented Programming in Java

Object-Oriented Programming in Java CSCI/CMPE 3326 Object-Oriented Programming in Java Class, object, member field and method, final constant, format specifier, file I/O Dongchul Kim Department of Computer Science University of Texas Rio

More information

Computational Expression

Computational Expression Computational Expression Variables, Primitive Data Types, Expressions Janyl Jumadinova 28-30 January, 2019 Janyl Jumadinova Computational Expression 28-30 January, 2019 1 / 17 Variables Variable is a name

More information

Reading Text Files. 1 Reading from text files: Scanner

Reading Text Files. 1 Reading from text files: Scanner Reading Text Files 1 Reading from text files: Scanner Text files, whether produced by a program or with a text editor, can be read by a program using class Scanner, part of the java.util package. We open

More information

CSCI 1103: File I/O, Scanner, PrintWriter

CSCI 1103: File I/O, Scanner, PrintWriter CSCI 1103: File I/O, Scanner, PrintWriter Chris Kauffman Last Updated: Wed Nov 29 13:22:24 CST 2017 1 Logistics Reading from Eck Ch 2.1 on Input, File I/O Ch 11.1-2 on File I/O Goals Scanner for input

More information

Section 2.2 Your First Program in Java: Printing a Line of Text

Section 2.2 Your First Program in Java: Printing a Line of Text Chapter 2 Introduction to Java Applications Section 2.2 Your First Program in Java: Printing a Line of Text 2.2 Q1: End-of-line comments that should be ignored by the compiler are denoted using a. Two

More information

Example Program. public class ComputeArea {

Example Program. public class ComputeArea { COMMENTS While most people think of computer programs as a tool for telling computers what to do, programs are actually much more than that. Computer programs are written in human readable language for

More information

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. File Input and Output

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. File Input and Output WIT COMP1000 File Input and Output I/O I/O stands for Input/Output So far, we've used a Scanner object based on System.in for all input (from the user's keyboard) and System.out for all output (to the

More information

Introduction to Java Unit 1. Using BlueJ to Write Programs

Introduction to Java Unit 1. Using BlueJ to Write Programs Introduction to Java Unit 1. Using BlueJ to Write Programs 1. Open up BlueJ. Click on the Project menu and select New Project. You should see the window on the right. Navigate to wherever you plan to save

More information

File I/O Introduction to File I/O Text Files The File Class Binary Files 614

File I/O Introduction to File I/O Text Files The File Class Binary Files 614 10.1 Introduction to File I/O 574 Streams 575 Text Files and Binary Files 575 10.2 Text Files 576 Writing to a Text File 576 Appending to a Text File 583 Reading from a Text File 586 Reading a Text File

More information

CS 302: Introduction to Programming

CS 302: Introduction to Programming CS 302: Introduction to Programming Lectures 2-3 CS302 Summer 2012 1 Review What is a computer? What is a computer program? Why do we have high-level programming languages? How does a high-level program

More information

COMP200 INPUT/OUTPUT. OOP using Java, based on slides by Shayan Javed

COMP200 INPUT/OUTPUT. OOP using Java, based on slides by Shayan Javed 1 1 COMP200 INPUT/OUTPUT OOP using Java, based on slides by Shayan Javed Input/Output (IO) 2 3 I/O So far we have looked at modeling classes 4 I/O So far we have looked at modeling classes Not much in

More information

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

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Final Examination First Name: Last Name: McGill ID: Section: Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Final Examination Thursday, December 11, 2008 Examiners: Mathieu Petitpas [Section 1] 14:00

More information

CMSC131. Introduction to your Introduction to Java. Why Java?

CMSC131. Introduction to your Introduction to Java. Why Java? CMSC131 Introduction to your Introduction to Java Why Java? It s a popular language in both industry and introductory programming courses. It makes use of programming structures and techniques that can

More information

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments. Java How to Program, 9/e Education, Inc. All Rights Reserved. } Java application programming } Use tools from the JDK to compile and run programs. } Videos at www.deitel.com/books/jhtp9/ Help you get started

More information

Chapter 4 Classes in the Java Class Libraries

Chapter 4 Classes in the Java Class Libraries Programming Fundamental I ACS-1903 Chapter 4 Classes in the Java Class Libraries 1 Random Random The Random class provides a capability to generate pseudorandom values pseudorandom because the stream of

More information

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University Lecture 2 COMP1406/1006 (the Java course) Fall 2013 M. Jason Hinek Carleton University today s agenda a quick look back (last Thursday) assignment 0 is posted and is due this Friday at 2pm Java compiling

More information

A Quick and Dirty Overview of Java and. Java Programming

A Quick and Dirty Overview of Java and. Java Programming Department of Computer Science New Mexico State University. CS 272 A Quick and Dirty Overview of Java and.......... Java Programming . Introduction Objectives In this document we will provide a very brief

More information

Project 1. Java Data types and input/output 1/17/2014. Primitive data types (2) Primitive data types in Java

Project 1. Java Data types and input/output 1/17/2014. Primitive data types (2) Primitive data types in Java Java Data types and input/output Sharma Chakravarthy Information Technology Laboratory (IT Lab) Computer Science and Engineering Department The University of Texas at Arlington, Arlington, TX 76019 Email:

More information

Chapter 2: Data and Expressions

Chapter 2: Data and Expressions Chapter 2: Data and Expressions CS 121 Department of Computer Science College of Engineering Boise State University April 21, 2015 Chapter 2: Data and Expressions CS 121 1 / 53 Chapter 2 Part 1: Data Types

More information

Chapter 4 Defining Classes I

Chapter 4 Defining Classes I Chapter 4 Defining Classes I This chapter introduces the idea that students can create their own classes and therefore their own objects. Introduced is the idea of methods and instance variables as the

More information

Using APIs. Chapter 3. Outline Fields Overall Layout. Java By Abstraction Chapter 3. Field Summary static double PI

Using APIs. Chapter 3. Outline Fields Overall Layout. Java By Abstraction Chapter 3. Field Summary static double PI Outline Chapter 3 Using APIs 3.1 Anatomy of an API 3.1.1 Overall Layout 3.1.2 Fields 3.1.3 Methods 3.2 A Development Walkthrough 3.2.1 3.2.2 The Mortgage Application 3.2.3 Output Formatting 3.2.4 Relational

More information

Chapter 2: Basic Elements of Java

Chapter 2: Basic Elements of Java Chapter 2: Basic Elements of Java TRUE/FALSE 1. The pair of characters // is used for single line comments. ANS: T PTS: 1 REF: 29 2. The == characters are a special symbol in Java. ANS: T PTS: 1 REF: 30

More information

Lecture Set 2: Starting Java

Lecture Set 2: Starting Java Lecture Set 2: Starting Java 1. Java Concepts 2. Java Programming Basics 3. User output 4. Variables and types 5. Expressions 6. User input 7. Uninitialized Variables 0 This Course: Intro to Procedural

More information

Lecture Set 2: Starting Java

Lecture Set 2: Starting Java Lecture Set 2: Starting Java 1. Java Concepts 2. Java Programming Basics 3. User output 4. Variables and types 5. Expressions 6. User input 7. Uninitialized Variables 0 This Course: Intro to Procedural

More information

Full file at

Full file at Chapter 1 Primitive Java Weiss 4 th Edition Solutions to Exercises (US Version) 1.1 Key Concepts and How To Teach Them This chapter introduces primitive features of Java found in all languages such as

More information

CSCI 1103: File I/O, Scanner, PrintWriter

CSCI 1103: File I/O, Scanner, PrintWriter CSCI 1103: File I/O, Scanner, PrintWriter Chris Kauffman Last Updated: Mon Dec 4 10:03:11 CST 2017 1 Logistics Reading from Eck Ch 2.1 on Input, File I/O Ch 11.1-2 on File I/O Goals Scanner for input Input

More information

CP122 CS I. Chapter 11: File I/O and Exceptions

CP122 CS I. Chapter 11: File I/O and Exceptions CP122 CS I Chapter 11: File I/O and Exceptions Waymo full autonomy vehicles Tech News! Tech News! Waymo full autonomy vehicles CMU and Pitt researchers use AI with fmri to detect suicidal thoughts with

More information

HST 952. Computing for Biomedical Scientists Lecture 8

HST 952. Computing for Biomedical Scientists Lecture 8 Harvard-MIT Division of Health Sciences and Technology HST.952: Computing for Biomedical Scientists HST 952 Computing for Biomedical Scientists Lecture 8 Outline Vectors Streams, Input, and Output in Java

More information

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Objects, Methods, Parameters, Input Lecture 5, Thu Jan 19 2006 based on slides by Kurt Eiselt http://www.cs.ubc.ca/~tmm/courses/cpsc111-06-spr

More information

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M Università degli Studi di Bologna Facoltà di Ingegneria Principles, Models, and Applications for Distributed Systems M tutor Isam M. Al Jawarneh, PhD student isam.aljawarneh3@unibo.it Mobile Middleware

More information

Introduction to Java Applications; Input/Output and Operators

Introduction to Java Applications; Input/Output and Operators www.thestudycampus.com Introduction to Java Applications; Input/Output and Operators 2.1 Introduction 2.2 Your First Program in Java: Printing a Line of Text 2.3 Modifying Your First Java Program 2.4 Displaying

More information

Introduction to Java (All the Basic Stuff)

Introduction to Java (All the Basic Stuff) Introduction to Java (All the Basic Stuff) Learning Objectives Java's edit-compile-run loop Basics of object-oriented programming Classes objects, instantiation, methods Primitive types Math expressions

More information

I/O in Java I/O streams vs. Reader/Writer. HW#3 due today Reading Assignment: Java tutorial on Basic I/O

I/O in Java I/O streams vs. Reader/Writer. HW#3 due today Reading Assignment: Java tutorial on Basic I/O I/O 10-7-2013 I/O in Java I/O streams vs. Reader/Writer HW#3 due today Reading Assignment: Java tutorial on Basic I/O public class Swimmer implements Cloneable { public Date geteventdate() { return (Date)

More information

COMP 202 Java in one week

COMP 202 Java in one week COMP 202 Java in one week... Continued CONTENTS: Return to material from previous lecture At-home programming exercises Please Do Ask Questions It's perfectly normal not to understand everything Most of

More information

Agenda & Reading. Python Vs Java. COMPSCI 230 S Software Construction

Agenda & Reading. Python Vs Java. COMPSCI 230 S Software Construction COMPSCI 230 S2 2016 Software Construction File Input/Output 2 Agenda & Reading Agenda: Introduction Byte Streams FileInputStream & FileOutputStream BufferedInputStream & BufferedOutputStream Character

More information

Methods CSC 121 Fall 2016 Howard Rosenthal

Methods CSC 121 Fall 2016 Howard Rosenthal Methods CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand what a method is in Java Understand Java s Math Class and how to use it Learn the syntax of method construction Learn both void methods

More information

Robots. Byron Weber Becker. chapter 6

Robots. Byron Weber Becker. chapter 6 Using Variables Robots Learning to Program with Java Byron Weber Becker chapter 6 Announcements (Oct 5) Chapter 6 You don t have to spend much time on graphics in Ch6 Just grasp the concept Reminder: Reading

More information

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan COSC 123 Computer Creativity Introduction to Java Dr. Ramon Lawrence University of British Columbia Okanagan ramon.lawrence@ubc.ca Key Points 1) Introduce Java, a general-purpose programming language,

More information

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while CSCI 150 Fall 2007 Java Syntax The following notes are meant to be a quick cheat sheet for Java. It is not meant to be a means on its own to learn Java or this course. For that you should look at your

More information

Introduction to Java Applications

Introduction to Java Applications 2 Introduction to Java Applications OBJECTIVES In this chapter you will learn: To write simple Java applications. To use input and output statements. Java s primitive types. Basic memory concepts. To use

More information

Object Oriented Programming. Java-Lecture 1

Object Oriented Programming. Java-Lecture 1 Object Oriented Programming Java-Lecture 1 Standard output System.out is known as the standard output object Methods to display text onto the standard output System.out.print prints text onto the screen

More information

Chapter 2: Data and Expressions

Chapter 2: Data and Expressions Chapter 2: Data and Expressions CS 121 Department of Computer Science College of Engineering Boise State University January 15, 2015 Chapter 2: Data and Expressions CS 121 1 / 1 Chapter 2 Part 1: Data

More information

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics WIT COMP1000 Java Basics Java Origins Java was developed by James Gosling at Sun Microsystems in the early 1990s It was derived largely from the C++ programming language with several enhancements Java

More information

Week 12. Streams and File I/O. Overview of Streams and File I/O Text File I/O

Week 12. Streams and File I/O. Overview of Streams and File I/O Text File I/O Week 12 Streams and File I/O Overview of Streams and File I/O Text File I/O 1 I/O Overview I/O = Input/Output In this context it is input to and output from programs Input can be from keyboard or a file

More information

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value Lecture Notes 1. Comments a. /* */ b. // 2. Program Structures a. public class ComputeArea { public static void main(string[ ] args) { // input radius // compute area algorithm // output area Actions to

More information

Dining philosophers (cont)

Dining philosophers (cont) Administrivia Assignment #4 is out Due Thursday April 8, 10:00pm no late assignments will be accepted Sign up in labs this week for a demo time Office hour today will be cut short (11:30) Another faculty

More information

Experiment No: Group B_4

Experiment No: Group B_4 Experiment No: Group B_4 R (2) N (5) Oral (3) Total (10) Dated Sign Problem Definition: Write a web application using Scala/ Python/ Java /HTML5 to check the plagiarism in the given text paragraph written/

More information

Computer Science is...

Computer Science is... Computer Science is... Computational complexity theory Complexity theory is the study of how algorithms perform with an increase in input size. All problems (like is n a prime number? ) fit inside a hierarchy

More information

CS 251 Intermediate Programming Java I/O Streams

CS 251 Intermediate Programming Java I/O Streams CS 251 Intermediate Programming Java I/O Streams Brooke Chenoweth University of New Mexico Spring 2018 Basic Input/Output I/O Streams mostly in java.io package File I/O mostly in java.nio.file package

More information

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Starting Out with Java: From Control Structures Through Objects Sixth Edition Starting Out with Java: From Control Structures Through Objects Sixth Edition Chapter 11 I/O File Input and Output Reentering data all the time could get tedious for the user. The data can be saved to

More information

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension. Java How to Program, 9/e Education, Inc. All Rights Reserved. } Covered in this chapter Classes Objects Methods Parameters double primitive type } Create a new class (GradeBook) } Use it to create an object.

More information

Simple Java Reference

Simple Java Reference Simple Java Reference This document provides a reference to all the Java syntax used in the Computational Methods course. 1 Compiling and running... 2 2 The main() method... 3 3 Primitive variable types...

More information

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

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Review Chapters 1 to 4 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections 2.1 2.5 Instructor:

More information

Fall 2017 CISC124 10/1/2017

Fall 2017 CISC124 10/1/2017 CISC124 Today First onq quiz this week write in lab. More details in last Wednesday s lecture. Repeated: The quiz availability times will change to match each lab as the week progresses. Useful Java classes:

More information

Programming with Java

Programming with Java Programming with Java Data Types & Input Statement Lecture 04 First stage Software Engineering Dep. Saman M. Omer 2017-2018 Objectives q By the end of this lecture you should be able to : ü Know rules

More information

Some miscellaneous concepts

Some miscellaneous concepts Some miscellaneous concepts Static, Javadoc and Calculated Data Produced by: Dr. Siobhán Drohan Mairead Meagher Department of Computing and Mathematics http://www.wit.ie/ Topic List Static Variables Static

More information

BlueJ Demo. Topic 1: Basic Java. 1. Sequencing. Features of Structured Programming Languages

BlueJ Demo. Topic 1: Basic Java. 1. Sequencing. Features of Structured Programming Languages Topic 1: Basic Java Plan: this topic through next Friday (5 lectures) Required reading on web site I will not spell out all the details in lecture! BlueJ Demo BlueJ = Java IDE (Interactive Development

More information

c) And last but not least, there are javadoc comments. See Weiss.

c) And last but not least, there are javadoc comments. See Weiss. CSCI 151 Spring 2010 Java Bootcamp The following notes are meant to be a quick refresher on Java. It is not meant to be a means on its own to learn Java. For that you would need a lot more detail (for

More information

CS Programming I: File Input / Output

CS Programming I: File Input / Output CS 200 - Programming I: File Input / Output Marc Renault Department of Computer Sciences University of Wisconsin Madison Fall 2017 TopHat Sec 3 (PM) Join Code: 719946 TopHat Sec 4 (AM) Join Code: 891624

More information

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 19: NOV. 15TH INSTRUCTOR: JIAYIN WANG

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 19: NOV. 15TH INSTRUCTOR: JIAYIN WANG CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 19: NOV. 15TH INSTRUCTOR: JIAYIN WANG 1 Notice Assignment Class Exercise 19 is assigned Homework 8 is assigned Both Homework 8 and Exercise 19 are

More information

File Processing. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. A Class for Representing a File

File Processing. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. A Class for Representing a File Unit 4, Part 2 File Processing Computer Science S-111 Harvard University David G. Sullivan, Ph.D. A Class for Representing a File The File class in Java is used to represent a file on disk. To use it,

More information

File Input/Output. Introduction to Computer Science I. Overview (1): Overview (2): CSE 1020 Summer Bill Kapralos. Bill Kapralos.

File Input/Output. Introduction to Computer Science I. Overview (1): Overview (2): CSE 1020 Summer Bill Kapralos. Bill Kapralos. File Input/Output Tuesday, July 25 2006 CSE 1020, Summer 2006, Overview (1): Before We Begin Some administrative details Some questions to consider The Class Object What is the Object class? File Input

More information

Methods CSC 121 Spring 2017 Howard Rosenthal

Methods CSC 121 Spring 2017 Howard Rosenthal Methods CSC 121 Spring 2017 Howard Rosenthal Lesson Goals Understand what a method is in Java Understand Java s Math Class and how to use it Learn the syntax of method construction Learn both void methods

More information

AP Computer Science Unit 1. Programs

AP Computer Science Unit 1. Programs AP Computer Science Unit 1. Programs Open DrJava. Under the File menu click on New Java Class and the window to the right should appear. Fill in the information as shown and click OK. This code is generated

More information

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string COMP 202 Built in Libraries and objects CONTENTS: Introduction to objects Introduction to some basic Java libraries string COMP 202 Objects and Built in Libraries 1 Classes and Objects An object is an

More information

H212 Introduction to Software Systems Honors

H212 Introduction to Software Systems Honors Introduction to Software Systems Honors Lecture #04: Fall 2015 1/20 Office hours Monday, Wednesday: 10:15 am to 12:00 noon Tuesday, Thursday: 2:00 to 3:45 pm Office: Lindley Hall, Room 401C 2/20 Printing

More information

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

Outline. Why Java? (1/2) Why Java? (2/2) Java Compiler and Virtual Machine. Classes. COMP9024: Data Structures and Algorithms COMP9024: Data Structures and Algorithms Week One: Java Programming Language (I) Hui Wu Session 2, 2016 http://www.cse.unsw.edu.au/~cs9024 Outline Classes and objects Methods Primitive data types and operators

More information

Lecture 8 " INPUT " Instructor: Craig Duckett

Lecture 8  INPUT  Instructor: Craig Duckett Lecture 8 " INPUT " Instructor: Craig Duckett Assignments Assignment 2 Due TONIGHT Lecture 8 Assignment 1 Revision due Lecture 10 Assignment 2 Revision Due Lecture 12 We'll Have a closer look at Assignment

More information

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8 Today... Java basics S. Bowers 1 of 8 Java main method (cont.) In Java, main looks like this: public class HelloWorld { public static void main(string[] args) { System.out.println("Hello World!"); Q: How

More information

Programming Assignment Comma Separated Values Reader Page 1

Programming Assignment Comma Separated Values Reader Page 1 Programming Assignment Comma Separated Values Reader Page 1 Assignment What to Submit 1. Write a CSVReader that can read a file or URL that contains data in CSV format. CSVReader provides an Iterator for

More information

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved. 1 3 Introduction to Classes and Objects 2 You will see something new. Two things. And I call them Thing One and Thing Two. Dr. Theodor Seuss Geisel Nothing can have value without being an object of utility.

More information

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2010) - All Sections Midterm Examination

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2010) - All Sections Midterm Examination First Name: Last Name: McGill ID: Section: Faculty of Science COMP-202B - Introduction to Computing I (Winter 2010) - All Sections Midterm Examination Thursday, March 11, 2010 Examiners: Milena Scaccia

More information

CMSC 150 INTRODUCTION TO COMPUTING LAB WEEK 3 STANDARD IO FORMATTING OUTPUT SCANNER REDIRECTING

CMSC 150 INTRODUCTION TO COMPUTING LAB WEEK 3 STANDARD IO FORMATTING OUTPUT SCANNER REDIRECTING CMSC 150 INTRODUCTION TO COMPUTING LAB WEEK 3 STANDARD IO FORMATTING OUTPUT SCANNER REDIRECTING INPUT AND OUTPUT Input devices Keyboard Mouse Hard drive Network Digital camera Microphone Output devices.

More information

Topic 11 Scanner object, conditional execution

Topic 11 Scanner object, conditional execution Topic 11 Scanner object, conditional execution "There are only two kinds of programming languages: those people always [complain] about and those nobody uses." Bjarne Stroustroup, creator of C++ Copyright

More information

DPCompSci.Java.1718.notebook April 29, 2018

DPCompSci.Java.1718.notebook April 29, 2018 Option D OOP 1 if Test a condition to decide whether to execute a block of code. Java Tutorials int x = 10; if (x < 20 { System.out.println(x + " < 20"); Prints 10 < 20 if else Test a condition to decide

More information

Chapter 10. File I/O. Copyright 2016 Pearson Inc. All rights reserved.

Chapter 10. File I/O. Copyright 2016 Pearson Inc. All rights reserved. Chapter 10 File I/O Copyright 2016 Pearson Inc. All rights reserved. Streams A stream is an object that enables the flow of data between a program and some I/O device or file If the data flows into a program,

More information

First Java Program - Output to the Screen

First Java Program - Output to the Screen First Java Program - Output to the Screen These notes are written assuming that the reader has never programmed in Java, but has programmed in another language in the past. In any language, one of the

More information

CS 231 Data Structures and Algorithms, Fall 2016

CS 231 Data Structures and Algorithms, Fall 2016 CS 231 Data Structures and Algorithms, Fall 2016 Dr. Bruce A. Maxwell Department of Computer Science Colby College Course Description Focuses on the common structures used to store data and the standard

More information