CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.

Slides:



Advertisements
Similar presentations
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 14 / 2009 Instructor: Michael Eckmann.
Chapter 2 - Introduction to Java Applications
CS 106 Introduction to Computer Science I 09 / 13 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 04 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 01 / 21 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 09 / 14 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 01 / 29 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 04 / 2008 Instructor: Michael Eckmann.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 25 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 22 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 1 CMT1000: Introduction to Programming Ed Currie Lecture 3: Program structure.
CS 106 Introduction to Computer Science I 10 / 09 / 2006 Instructor: Michael Eckmann.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
CS 106 Introduction to Computer Science I 02 / 24 / 2010 Instructor: Michael Eckmann.
Chapter 2 - Introduction to Java Applications
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Console Input Using the Scanner Class Starting with version 5.0, Java includes a class for doing.
CS 106 Introduction to Computer Science I 09 / 18 / 2006 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 01 / 23 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 23 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 09 / 11 / 2006 Instructor: Michael Eckmann.
Chapter 2 Section 2.2 Console Input Using The Scanner CLASS Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska.
JOptionPane class. Dialog Boxes A dialog box is a small graphical window that displays a message to the user or requests input. A variety of dialog boxes.
Simple Programs from Chapter 2 Putting the Building Blocks All Together (corresponds with Chapter 2)
CS 106 Introduction to Computer Science I 01 / 25 / 2010 Instructor: Michael Eckmann.
Object-Oriented Programming - Classes, Objects Methods, Strings 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
OOP (Java): Simple/ OOP (Java) Objectives – –give some simple examples of Java applications and one applet 2. Simple Java Programs Semester.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
Program Flow Program Flow follows the exact sequence of listed program statements, unless directed otherwise by a Java control structure.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Programming Fundamentals 2: Simple/ F II Objectives – –give some simple examples of Java applications and one applet 2. Simple Java.
1 COMP 241: Object-Oriented Programming with Java Fall 2004 Lecture 1 September 27, 2004 Serdar Taşıran.
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
CS 106 Introduction to Computer Science I 01 / 26 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 11 / 2009 Instructor: Michael Eckmann.
Dialog Boxes.
CS 106 Introduction to Computer Science I 09 / 26 / 2007 Instructor: Michael Eckmann.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
CS 106 Introduction to Computer Science I 02 / 01 / 2008 Instructor: Michael Eckmann.
Strings and I/O. Lotsa String Stuff… There are close to 50 methods defined in the String class. We will introduce several of them here: charAt, substring,
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 6_1 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1.
CS 106 Introduction to Computer Science I 01 / 24 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 10 / 2007 Instructor: Michael Eckmann.
Input/Output.
2.5 Another Java Application: Adding Integers
Chapter 2 - Introduction to Java Applications
Introduction to Java part 2
MSIS 655 Advanced Business Applications Programming
Introduction to Java Brief history of Java Sample Java Program
Object Oriented Programming
F II 2. Simple Java Programs Objectives
Chapter 2: Java Fundamentals cont’d
Presentation transcript:

CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Spring 2007 Today’s Topics questions, comments? some if / else structure examples import statement Input and output using JOptionPane and Scanner. converting Strings to int or double pseudocode A programming exercise

another subtlety of the if / else Java always associates an else with the preceding if unless curly braces are used to force it not to do this. example: if (i == 0) if (j == 0) System.out.println("both i and j are 0"); else System.out.println("i is not 0"); The above code will not execute like the programmer thought, regardless of the indentation. The else will be paired with the if (j==0) NOT the if (i == 0). Michael Eckmann - Skidmore College - CS Spring 2007

another subtlety of the if / else An else can be forced by the programmer to pair up with a different (not the immediately preceding) if by using curly braces. if (i == 0) { if (j == 0) System.out.println("both i and j are 0"); } else System.out.println("i is not 0"); The above code will now execute like the programmer intended. Michael Eckmann - Skidmore College - CS Spring 2007

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; Michael Eckmann - Skidmore College - CS Spring 2007

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. Michael Eckmann - Skidmore College - CS Spring 2007

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. Michael Eckmann - Skidmore College - CS Spring 2007

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. Michael Eckmann - Skidmore College - CS Spring 2007

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. Michael Eckmann - Skidmore College - CS Spring 2007

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.”); Michael Eckmann - Skidmore College - CS Spring 2007

javax.swing.JOptionPane Let’s write Hello World using a message dialog box instead of System.out.println. Michael Eckmann - Skidmore College - CS Spring 2007

javax.swing.JOptionPane the showInputDialog method ONLY returns what the user entered as a String That means we can store it in a String variable (we can't store it in an int or a double or other types.) If we want the data as an int or a double or some other type we have to convert it afterwards. Michael Eckmann - Skidmore College - CS Spring 2007

Integer.parseInt and Double.parseDouble these methods take in a String inside the parentheses and return an int or a double respectively. String s = "42"; int x; x = Integer.parseInt(s); // s is a String containing the characters 42 // x is an int containing the numeric value 42 Michael Eckmann - Skidmore College - CS Spring 2007

Integer.parseInt and Double.parseDouble these methods take in a String inside the parentheses and return an int or a double respectively. String s = "98.6"; double x; x = Double.parseDouble(s); // s is a String containing the characters 98.6 // x is an int containing the numeric value 98.6 Michael Eckmann - Skidmore College - CS Spring 2007

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 Michael Eckmann - Skidmore College - CS Spring 2007

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. Michael Eckmann - Skidmore College - CS Spring 2007

java.util.Scanner Read the "gotcha" on pages 95 and 96 of the text. Michael Eckmann - Skidmore College - CS Spring 2007

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 Michael Eckmann - Skidmore College - CS Spring 2007

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? Michael Eckmann - Skidmore College - CS Fall 2006

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 = games played so far Michael Eckmann - Skidmore College - CS Fall 2006

Pseudocode for our example (continued) from this equation, projected homers homers so far = 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 Michael Eckmann - Skidmore College - CS Fall 2006

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. Michael Eckmann - Skidmore College - CS Fall 2006

Pseudocode for our example (continued) Now we can write the program based on our pseudocode. Michael Eckmann - Skidmore College - CS Fall 2006