CS1101: Programming Methodology

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.
Hand Crafting your own program By Eric Davis for CS103.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java.
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.
Chapter 2 Getting Started with Java Part B. Topics Components of a Java Program –classes –methods –comments –import statements Declaring and creating.
10 ThinkOfANumber program1July ThinkOfANumber program CE : Fundamental Programming Techniques.
©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;
Writing algorithms using the for-statement. Programming example 1: find all divisors of a number We have seen a program using a while-statement to solve.
1 BUILDING JAVA PROGRAMS CHAPTER 3 THE SCANNER CLASS AND USER INPUT.
CS1101: Programming Methodology Aaron Tan.
CS1101: Programming Methodology
CS1101: Programming Methodology Aaron Tan.
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
CS1101: Programming Methodology Aaron Tan.
General Programming Introduction to Computing Science and Programming I.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014
Getting Started with Java Recitation – 1/23/2009 CS 180 Department of Computer Science, Purdue University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 2 Getting Started with Java Animated Version.
Programming Concept Chapter I Introduction to Java Programming.
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 1 Java Basics Numerical Data.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
CS1101: Programming Methodology Aaron Tan.
CS1101: Programming Methodology
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
CSci 111 – computer Science I Fall 2014 Cynthia Zickos WRITING A SIMPLE PROGRAM IN JAVA.
Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009.
CS1101: Programming Methodology
CS1101: Programming Methodology Aaron Tan.
CS1101: Programming Methodology Aaron Tan.
CS1101: Programming Methodology
CSC1401 Strings (text). Learning Goals Working with Strings as a data type (a class) Input and output of Strings String operations.
CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014.
Introduction Chapter 1 8/31 & 9/1 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)
CS1101X: Programming Methodology Recitation 4 Design Issues and Problem Solving.
CS1101: Programming Methodology Preparing for Practical Exam (PE)
Programming Fundamentals I Java Programming Spring 2009 Instructor: Xuan Tung Hoang TA: Tran Minh Trung Lab 03.
©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.
CS1101: Programming Methodology Preparing for Practical Exam (PE)
CS1101: Programming Methodology
CS1101: Programming Methodology
GCSE Computing: Programming GCSE Programming Remembering Python.
بسم الله الرحمن الرحيم CPCS203: Programming II. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr.
Interactive Programs Programs that get input from the user 1 In PowerPoint, click on the speaker icon then click the "Play" button to hear the narration.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Introduction to programming in java
Intro to OOP with Java, C. Thomas Wu Getting Started with Java
Something about Java Introduction to Problem Solving and Programming 1.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Strings CSE 1310 – Introduction to Computers and Programming
Building Java Programs
Getting Started with Java
Computer Science Club 1st November 2019.
Presentation transcript:

CS1101: Programming Methodology

Week 2: Java Basics  Last week:  Introduction  Writing algorithms (pseudo-codes)  This week:  Chapter 1: Intro to OOP and Software Development  Chapter 2: Getting Started with Java  DrJava  CourseMarker  Next week:  Chapter 3: Numerical Data  Chapter 4: Defining Your Own Classes – Part 1 © CS1101 (AY Semester 1)Week2 - 2

Reminder  Have you read the relevant chapters and PowerPoint files BEFORE coming to this lecture? © CS1101 (AY Semester 1)Week2 - 3

Chapter 1 Intro to OOP and S/W Dev Let’s go to Chapter 1. © CS1101 (AY Semester 1)Week2 - 4

Chapter 2 Getting Started with Java Let’s go to Chapter 2. We will skip the Date and SimpleDateFormat classes. © CS1101 (AY Semester 1)Week2 - 5

Example on String methods (1/2) © CS1101 (AY Semester 1)Week2 - 6 /* Program to test out some String methods in Chapter 2 File: Ch2TestString.java */ class Ch2TestString { public static void main (String[] args) { String text = "I'm studying CS1101."; //or String text = new String("I'm studying CS1101."); //We’ll explain the difference next time. System.out.print("text: " + text); int length = text.length(); System.out.println("text.length(): " + length);

Example on String methods (2/2) © CS1101 (AY Semester 1)Week2 - 7 System.out.println("text.substring(5,8): " + text.substring(5,8)); System.out.println("text.indexOf(\"in\"): " + text.indexOf("in")); // why are there 2 backslashes (\) above? String newText = text + "How about you?"; System.out.print("newText: " + newText); } Download this program from the CS1101 website (“Resources – Lectures”). What is the output of this program?

CourseMarker (CM) You are going to use CourseMarker (CM) for submitting your lab assignments.  Password already ed to you Refer to the following:  CourseMarker website  “Using CourseMarker” PowerPoint file Available on CS1101 website © CS1101 (AY Semester 1)Week2 - 8

Example: Initials This problem is given in the Chapter 2 PowerPoint file, but the program uses JOptionPane class for input. We prefer to use the standard input (Scanner class) and show it here. Problem statement: Write a program that asks for the user’s first, middle, and last names and replies with their initials. © CS1101 (AY Semester 1)Week2 - 9 Example: input:Andrew Lloyd Weber output: ALW

Initials: 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 first letter from each name and create the initials  Output the initials © CS1101 (AY Semester 1)Week2 - 10

Initials: Development Steps We will develop this program in two steps: 1.Start with the program template and add code to get input 2.Add code to compute and display the initials © CS1101 (AY Semester 1)Week2 - 11

Initials: Step 1 Design The program specification states “get the user’s name” but doesn’t say how. We will consider “how” in the Step 1 design. We will use standard input (Scanner class) for input. We have two choices for input style:  Choice 1: Input first, middle and last names separately  Choice 2: Input the full name at once We choose choice 2 because it is easier and quicker for the user to enter the information. © CS1101 (AY Semester 1)Week2 - 12

Initials: Step 1 Code © CS1101 (AY Semester 1)Week /* Chapter 2 Sample Program: Displays the Initials File: Ch2Initials.java */ import java.util.*; class Ch2Initials { public static void main (String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter your full name " + " (first, middle, last): "); String name = scanner.nextLine(); System.out.println("Full name entered: " + name); } Download this program from the CS1101 website. What is the output of this program?

Initials: 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 Question:  What if we had used scanner.next() instead of scanner.nextLine()? Test it out! © CS1101 (AY Semester 1)Week2 - 14

Initials: 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 Examples: John Quincy Adams(okay) John Kennedy(not okay) Harrison,William Henry(not okay) © CS1101 (AY Semester 1)Week2 - 15

Initials: Step 2 Design (cont’d) Given a valid input, we can generate the initials by  breaking the input name into first, middle and last  extracting the first character from each of them  concatenating the three first characters © CS1101 (AY Semester 1)Week “Aaron Ben Cosner” “Aaron” “Ben Cosner” “Ben” “Cosner” “ABC”

Initials: Step 2 Code © CS1101 (AY Semester 1)Week /* Chapter 2 Sample Program: Displays the Initials File: Ch2Initials.java */ import java.util.*; class Ch2Initials { public static void main (String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter your full name " + " (first, middle, last): "); String name = scanner.nextLine(); String space = " ";

Initials: Step 2 Code (cont’d) © CS1101 (AY Semester 1)Week String first = name.substring(0, name.indexOf(space)); name = name.substring(name.indexOf(space)+1, name.length()); String mid = name.substring(0, name.indexOf(space)); String last = name.substring(name.indexOf(space)+1, name.length()); // Compute the initials String initials = first.substring(0,1) + mid.substring(0,1) + last.substring(0,1); // Output the initials System.out.println("Your initials: " + initials); }

Initials: Step 2 Test In the testing phase, we run the program and verify that, for all valid input values, correct initials 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. © CS1101 (AY Semester 1)Week2 - 19

Initials: 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 possible improvement  Improve the initial prompt so the user knows the valid input format requires single spaces between the first, middle and last names. © CS1101 (AY Semester 1)Week2 - 20

Summary for Today You should have learned how to use the following:  DrJava  CourseMarker Basic Java features Some standard (built-in) classes  String  Scanner © CS1101 (AY Semester 1)Week2 - 21

Announcements/Things-to-do (1/2) CM password  Already ed to nus.edu.sg Trial Lab  Released on 15 August 2009, Saturday.  Read Module website, “CA – Labs”.  No hard deadline, but try to submit by the end of this week to ensure that you know how to use CM. © CS1101 (AY Semester 1)Week2 - 22

Announcements/Things-to-do (2/2) To prepare for next week’s lecture:  Read Chapters 3 and 4 and their PowerPoint files before you come for lecture. To prepare for next week’s discussion session:  Download “week3_discussion_qns.pdf” from module website, “CA – Discussion”.  Do the questions before you attend your discussion session. © CS1101 (AY Semester 1)Week2 - 23

End of File © CS1101 (AY Semester 1)Week2 - 24