CS 106 Introduction to Computer Science I



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

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

Comp 248 Introduction to Programming

Chapter 2: Elements of Java

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

Introduction to Java

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

Visit us at

Install Java Development Kit (JDK) 1.8

Introduction to Java. CS 3: Computer 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.

Building Java Programs

Basics of Java Programming Input and the Scanner class

Lecture 5: Java Fundamentals III

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

Using Files as Input/Output in Java 5.0 Applications

Building Java Programs

Object-Oriented Programming in Java

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

6.1. Example: A Tip Calculator 6-1

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

AP Computer Science Static Methods, Strings, User Input

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

Some Scanner Class Methods

Week 1: Review of Java Programming Basics

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

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

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

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

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

Topic 11 Scanner object, conditional execution

Introduction to Programming

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

Chapter 3. Input and output. 3.1 The System class

Chapter 2 Elementary Programming

Building Java Programs

CS 106 Introduction to Computer Science I

java Features Version April 19, 2013 by Thorsten Kracht

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

JAVA.UTIL.SCANNER CLASS

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

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

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

Sample CSE8A midterm Multiple Choice (circle one)

Reading Input From A File

Programming and Data Structures with Java and JUnit. Rick Mercer

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

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

The following program is aiming to extract from a simple text file an analysis of the content such as:

Computer Programming I

Chapter 2 Introduction to Java programming

Principles of Software Construction: Objects, Design, and Concurrency. Design Case Study: Stream I/O Some answers. Charlie Garrod Jonathan Aldrich

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

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

13 File Output and Input

Chapter 1. Introduction to Computers, Programs, and Java

Interactive Programs and Graphics in Java

Introduction to Java Applets (Deitel chapter 3)

Text file I/O and simple GUI dialogues

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

Programming in Java Course Technology, a part of Cengage Learning.

Java Crash Course Part I

- User input includes typing on the keyboard, clicking of a mouse, tapping or swiping a touch screen device, etc.

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

Programming Languages CIS 443

Lecture 1 Introduction to Java

Event-Driven Programming

Lab 1A. Create a simple Java application using JBuilder. Part 1: make the simplest Java application Hello World 1. Start Jbuilder. 2.

Course Intro Instructor Intro Java Intro, Continued

D06 PROGRAMMING with JAVA

Programming Fundamentals I CS 110, Central Washington University. November 2015

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Java Interview Questions and Answers

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

Decision Logic: if, if else, switch, Boolean conditions and variables

Assignment # 2: Design Patterns and GUIs

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.

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

CS 121 Intro to Programming:Java - Lecture 11 Announcements

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

CSE 8B Midterm Fall 2015

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

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

Quick Introduction to Java

Understanding class definitions

Introduction to Object-Oriented Programming

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

Programmierpraktikum

Line-based file processing

READING DATA FROM KEYBOARD USING DATAINPUTSTREAM, BUFFEREDREADER AND SCANNER

Building Java Programs

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

Chapter 8. Living with Java. 8.1 Standard Output

Interactive Applications (CLI) and Math

Decision-making Computer Science Lesson to Prepare for UIL Computer Science Contest

AP Computer Science Java Subset

LOOPS CHAPTER CHAPTER GOALS

Introduction to Java Lecture Notes. Ryan Dougherty

Transcription:

CS 106 Introduction to Computer Science I 01 / 29 / 2014 Instructor: Michael Eckmann

Today s Topics Comments and/or Questions? import user input using JOptionPane user input using Scanner psuedocode

import statement Normally we need to tell the compiler where to look for the class or method we are using in the Java API. This is done by the use of the import statement at the beginning of your source code file. To use a class called JOptionPane to create dialog boxes, we need to add the following line to the beginning of our code: import javax.swing.joptionpane;

import statement The import statement is of the form import package.class; javax.swing is a package and JOptionPane is the class we want to use in it. import javax.swing.* would allow us to use any class in the javax.swing package.

import statement We didn t need to import anything to use System.out in the HelloWorld program because System is a class that is part of the java.lang package and java.lang gets implicitly imported in all Java programs. Java API Specification is on the web and is where you ll find all the classes available to you in the Java API. There is a link to it on our course web page.

javax.swing.joptionpane Contains methods to show dialog boxes. JOptionPane.showMessageDialog displays messages in a dialog box. JOptionPane.showInputDialog allows for user input in a dialog box. Nicer to use than reading from / printing to the console screen.

JoptionPane class for I/O showmessagedialog takes two parameters, the first of which should always be null for now. The second is a String that should be outputted. showinputdialog takes one parameter which is a String that displays --- it should tell the user what to enter. The user's input is returned as a String.

JoptionPane class for I/O Example usage: import javax.swing.joptionpane; // need this line above your class String input_string; // variable to store the user's input JOptionPane.showMessageDialog(null, Hey ); input_string = JOptionPane.showInputDialog( Enter something. );

javax.swing.joptionpane Let s write Hello World using a message dialog box instead of System.out.println.

java.util.scanner Scanner input_scan = new Scanner(System.in); methods that you can call on your scanner object include: nextint() --- reads an int from keyboard nextdouble() --- reads a double from keyboard nextline() --- reads a line (as a String) from keyboard next() --- reads a word from the keyboard --- which is a string of nonwhitespace chars delimited by whitespace. whitespace is \n, blank space character, \t, \r

java.util.scanner nextline() --- reads a line (as a String) from keyboard this method consumes the \n but does not make it part of the String that is returned. String s1, s2; Scanner my_scan = new Scanner(System.in); s1 = my_scan.nextline(); s2 = my_scan.nextline(); if input is: CS106 Introduction to Computer Science I there is a \n after CS106 in the input (e.g. user hit enter key), but s1 will be CS106 and s2 will be Introduction to Computer Science I neither will have \n as a character in its String.

java.util.scanner There's an unfortunate problem with Scanner when using one to get both numeric and String input. If you need to get both numeric and String input from user input from the keyboard I recommend creating two Scanners, one that only gets numeric input (nextint, nextdouble) and one that only gets the String input (nextline, next).

Pseudocode pseudocode is an informal use of English to describe what a program is to do and in what order pseudocode is not an actual computer programming language it is used prior to writing actual code to help the programmer in the planning stages of a program

Example Application Exercise write a program to compute the number of projected home runs a baseball player will hit for the season based on how many homers he s hit so far. Output should look like: player s name is projected to hit number home runs in 162 games. Any ideas?

Pseudocode for our example get player s name from the user get the number of homeruns so far get the number of games played so far compute the number of projected homeruns for this player based on a season of 162 games by using the following calculation projected homers homers so far --------------------- = ------------------------- 162 games played so far

Pseudocode for our example (continued) from this equation, projected homers homers so far --------------------- = ------------------------- 162 games played so far we can multiply both sides of the equation by 162 and get projected homers = homers so far * 162 / games played so far

Pseudocode for our example (continued) Print out the following with actual values for player s name and number player s name is projected to hit number home runs in 162 games. Pseudocode could be more fleshed out than what we have done here --- use as much or as little detail in pseudocode as you prefer.

Pseudocode for our example (continued) Now we can write the program based on our pseudocode.