Getting Started with Java Recitation – 1/23/2009 CS 180 Department of Computer Science, Purdue University.

Slides:



Advertisements
Similar presentations
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 2 Getting Started with Java.
Advertisements

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 2 Getting Started with Java Animated Version.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Ch2: Getting Started with Java - Objectives After.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Program development.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Discussion 3. Questions? Term test? Lectures? Labs?
Hand Crafting your own program By Eric Davis for CS103.
Selection Statements & Exam 1 Review Recitation – 2/13/2009 CS 180 Department of Computer Science, Purdue University.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Arrays Recitation – 10/(9,10)/2008 CS 180 Department of Computer Science, Purdue University.
03 Data types1June Data types CE : Fundamental Programming Techniques.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Structure of.
Lecture 5 Strings and more I/O COMP1681 / SE15 Introduction to Programming.
● Basic Computation Recitation – 09/(04,05)/2008 CS 180 Department of Computer Science, Purdue University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Structure of.
Numerical Data Recitation – 01/30/2009
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 1 Introduction to Computers and Programming in JAVA.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 1 CMT1000: Introduction to Programming Ed Currie Lecture 3: Program structure.
Chapter Chapter 2 Getting Started with Java.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
Programming with Java standard classes. Java API Application Programming Interface Provides hundreds of standard classes that can be incorporated into.
Chapter 2 Getting Started with Java Part B. Topics Components of a Java Program –classes –methods –comments –import statements Declaring and creating.
CS 180 Recitation 8 / {30, 31} / Announcements Project 1 is out –Due 10:00pm –Start early! –Use the newsgroup for your questions –LWSN B146.
Chapter 3b Standard Input and Output Sample Development.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Software Engineering Software Development Process.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer.
Hello AP Computer Science!. What are some of the things that you have used computers for?
CS1101: Programming Methodology
Simple Programs from Chapter 2 Putting the Building Blocks All Together (corresponds with Chapter 2)
General Programming Introduction to Computing Science and Programming I.
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 2 Getting Started with Java Animated Version.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Computer Programming 2 Lab(1) I.Fatimah Alzahrani.
CS1101X: Programming Methodology Recitation 3 Control Structures.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
CS1101X: Programming Methodology Recitation 1 Java Basics Numerical Data.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
Static Class Methods CSIS 3701: Advanced Object Oriented Programming.
CSci 111 – computer Science I Fall 2014 Cynthia Zickos WRITING A SIMPLE PROGRAM IN JAVA.
CSC Programming I Lecture 6 September 4, 2002.
Chapter 2 Getting Started with Java. Objectives After you have read and studied this chapter, you should be able to Identify the basic components of Java.
1 Program Input Software Design Chapter 4. 2 You Will Want to Know... Prompting for and reading values into a program. Accessing data from a file. What.
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Java’s String Class.
Introduction Chapter 1 8/31 & 9/1 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Hello Computer Science!. Below is an example of a Hello World program in JAVA. While it is only three lines of code, there are many things that are happening.
Style guide in JAVA From a lecture by Dr. Rahman.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 1 Introduction to Computers, Programs,
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 2 Getting Started with Java Animated Version.
1 Project 3 String Methods. Project 3: String Methods Write a program to do the following string manipulations: Prompt the user to enter a phrase and.
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,
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
بسم الله الرحمن الرحيم CPCS203: Programming II. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr.
Intro to OOP with Java, C. Thomas Wu
OBJECT ORIENTED PROGRAMMING I LECTURE 7 GEORGE KOUTSOGIANNAKIS
Chapter 3 GC 101 Java Fundamentals.
Intro to OOP with Java, C. Thomas Wu Getting Started with Java
OBJECT ORIENTED PROGRAMMING I LECTURE 7 GEORGE KOUTSOGIANNAKIS
String Input ICS 111: Introduction to Computer Science I
Strings CSE 1310 – Introduction to Computers and Programming
Getting Started with Java
Presentation transcript:

Getting Started with Java Recitation – 1/23/2009 CS 180 Department of Computer Science, Purdue University

Project 1 Now posted on the class webpage. Due Wed, Jan. 28 at 10 pm. Start early! All questions on the class newsgroup. Evening consulting hours from Monday to Wednesday during 7-10 p.m. in LWSN B146.

How to Solve This? Problem statement:  Write a program that asks for the user’s first, middle, and last names and replies with their initials.  Example: input: Andrew Lloyd Weber output: ALW How do you understand this problem?  Input restraints or error tolerance?  Ask once or multiple times? ……

Overall Plan Identify the major tasks the program has to perform.  We need to know what to develop before we develop! Tasks:  Get the user’s first, middle, and last names  Extract the initials and create the monogram  Output the monogram

Development Steps We will develop this program in two steps:  Start with the program template and add code to get input  Add code to compute and display the monogram Any more step in real life?  Do not forget to test every part of your program  Debug and improve your program

Step 1 Design The program specification states “get the user’s name” but doesn’t say how. How to get input?  Use JOptionPane (standard class)  Input Style Choice #1 Input first, middle, and last names separately  Input Style Choice #2 Input the full name at once  We choose Style #2 because it is easier and quicker for the user to enter the information

Why Use Standard Classes Don’t reinvent the wheel. When there are existing classes that satisfy our needs, use them. Learning how to use standard Java classes is the first step toward mastering OOP. Before we can learn how to define our own classes, we need to learn how to use existing classes.

JOptionPane for Output Using showMessageDialog of the JOptionPane class is a simple way to bring up a window with a message. JOptionPane.showMessageDialog(null, “How are you?”); How to show multiple lines of text?  Another line: “\n”

JOptionPane for Input Using showInputDialog of the JOptionPane class is another way to input a string. JOptionPane.showInputDialog(null, “Your full name:”);

String The textual values passed to the showMessageDialog method are instances of the String class. A sequence of characters separated by double quotes is a String constant. There are close to 50 methods defined in the String class. We will introduce three of them here: substring, length, and indexOf. We will also introduce a string operation called concatenation.

Usage of String Object Declaration String name; Creation name = new String(“Jane Java”); We can combine them together String name = new String(“Jane Java”); Indexing from 0 to length-1  Referring to the string name, which character’s index is 3?

String Methods Assume str is a String object and properly initialized to “Purdue!”. Substring: str.substring(i, j)  What is str.substring(1, 3) ? Length: str.length()  What is str.length() ? Substring: str.indexOf(substr)  What is str.indexOf(“ue”) ? Concatenation: str1 + str2  What is “Hi! ” + str ? Refer to Java API or lecture slides for more information

Step 1 Code /* Chapter 2 Sample Program: Displays the Monogram File: Step1/Ch2Monogram.java */ import javax.swing.*; class Ch2Monogram { public static void main (String[ ] args) { String name; name = JOptionPane.showInputDialog(null, “Enter your full name (first, middle, last):”); JOptionPane.showMessageDialog(null, name); }

Step 1 Test In the testing phase, we run the program and verify that  we can enter the name  the name we enter is displayed correctly Why do we test before finishing the whole problem?  How to find a small bug in a large room?  What about finding a small bug on a small piece of paper?

Step 2 Design Our programming skills are limited, so we will make the following assumptions:  input string contains first, middle, and last names  first, middle, and last names are separated by single blank spaces Example  John Quincy Adams (okay)  John Kennedy (not okay)  Harrison, William Henry (not okay)

Step 2 Design Given the valid input, we can compute the monogram by  breaking the input name into first, middle, and last  extracting the first character from them  concatenating three first characters

Step 2 Code /* Chapter 2 Sample Program: Displays the Monogram File: Step1/Ch2Monogram.java */ import javax.swing.*; class Ch2Monogram { public static void main (String[ ] args) { String name, first, middle, last, space, monogram; space = “ ” ; //Input the full name name = JOptionPane.showInputDialog(null, “Enter your full name (first, middle, last):”);

Step 2 Code //Extract first, middle, and last names first = name.substring(0, name.indexOf(space)); name = name.substring(name.indexOf(space)+1, name.length()); middle = name.substring(0, name.indexOf(space)); last = name.substring(name.indexOf(space)+1, name.length()); //Compute the monogram monogram = first.substring(0, 1) + middle.substring(0, 1) + last.substring(0,1); //Output the result JOptionPane.showMessageDialog(null, "Your monogram is " + monogram); }

Step 2 Test In the testing phase, we run the program and verify that, for all valid input values, correct monograms are displayed. We run the program numerous times. Seeing one correct answer is not enough. We have to try out many different types of (valid) input values.

Program Review The work of a programmer is not done yet. Once the working program is developed, we perform a critical review and see if there are any missing features or possible improvements One suggestion  Improve the initial prompt so the user knows the valid input format requires single spaces between the first, middle, and last names Any other suggestion?

More Standard Classes Standard output: System.out.print(…) System.out.print(“Welcome to\nPurdue”); Standard input: System.in Scanner Scanner scanner = new Scanner(system.in); Date Date today = new Date(); System.out.print(today.toString() + “\n”);  SimpleDateFormat Refer to Java API or lecture slides for more information

Coding Style Take a careful look at the coding standards on the class website Develop or keep your own good coding style Good for readers, good for yourself

Quiz Write some code to print the following stuff: Hey! Well done!  Hint: System.out.print(…) Declare a String object school and let its value be “Purdue University”.