CS1101X: Programming Methodology Recitation 1 Java Basics Numerical Data.

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. 4 th Ed Chapter Chapter 3 Numerical Data.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 2 Getting Started with Java Animated Version.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
Reading Input from the Console eval function. variable = input("Enter a value: ") The value entered is a string. You can use the function eval to evaluate.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Computer Programming Lab 8.
Chapter Chapter 8 Exceptions and Assertions.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Ch2: Getting Started with Java - Objectives After.
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.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 3 Sample Development Loan Calculator.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 6 Repetition Statements Animated Version.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exceptions and Assertions Animated Version.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four: Defining Your Own Classes *Instantiable.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3 Numerical Data.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: *Sample Development Loan Calculator.
©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.
Chapter 2 Getting Started with Java Part B. Topics Components of a Java Program –classes –methods –comments –import statements Declaring and creating.
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 Chapter 4 Sample Development Revisiting the Loan.
Chapter Four Defining Your Own Classes continued.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Software Engineering Software Development Process.
CS1101X: Programming Methodology Recitation 2 Classes.
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.
Fundamental concepts in Java. Lesson plan Variable declaration, assign statement & practice Design document & practice.
Chapter 6 – Repetition Statements : Objectives After you have read and studied this chapter, you should be able to Implement repetition control in a program.
CS1101: Programming Methodology
Simple Programs from Chapter 2 Putting the Building Blocks All Together (corresponds with Chapter 2)
CIS 260: App Dev I. 2 Objects and Reference Variables n Predefined Java classes you have used:  String —for defining and manipulating strings  Integer.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 3 Numerical Data.
CS1101X: Programming Methodology Recitation 3 Control Structures.
Discussion 5. Lab 3 Not really using rectangle class Return type of getPoint is String instead of Point You are not able to retrieve the point if you.
1 Intro to Java Week 12 (Slides courtesy of Charatan & Kans, chapter 8)
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
Writing JavaScript Functions. Goals By the end of this unit, you should understand … How to breakdown applications into individual, re-usable modules.
Discussion 4. Labs public MyPoint(double xInit, double yInit ) { MyPoint p = new MyPoint(0, 0); } ClassProblem.java recursive java.lang.StackOverflowError.
Chapter 3 Java Input/Output.
CSC Programming I Lecture 6 September 4, 2002.
CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014.
CS1101X: Programming Methodology Recitation 4 Design Issues and Problem Solving.
CS1101X: Programming Methodology Recitation 5 Exceptions & Characters and Strings.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
COMPSCI 172 – Introduction to Objected- Oriented programming in Java Class hour: 12:05-12:55 pm MWF (section 1). 1:10-2:00 pm MWF (section 2) McGraw Room.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Enhanced Car Payment.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 2 Getting Started with Java Animated Version.
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,
بسم الله الرحمن الرحيم 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 Numerical Data
Intro to OOP with Java, C. Thomas Wu Getting Started with Java
Chapter 3 Java Input/Output.
Data types, Expressions and assignment, Input from User
Defining Your Own Classes
Chapter 3 Numerical Data
Chapter 3 Input/Output.
Getting Started with Java
JOptionPane class.
Chapter 2: Java Fundamentals cont’d
Presentation transcript:

CS1101X: Programming Methodology Recitation 1 Java Basics Numerical Data

CS1101X Recitation #12 Problem Statement 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

CS1101X Recitation #13 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

CS1101X Recitation #14 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 monogram

CS1101X Recitation #15 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 JOptionPane for input 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

CS1101X Recitation #16 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); }

CS1101X Recitation #17 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

CS1101X Recitation #18 Step 2: Design (1/2) 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)

CS1101X Recitation #19 Step 2: Design (2/2) 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 “Aaron Ben Cosner” “Aaron” “Ben Cosner” “Ben” “Cosner” “ABC”

CS1101X Recitation #110 Step 2: Code (1/2) /* Chapter 2 Sample Program: Displays the Monogram File: Step 2/Ch2MonogramStep2.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):" );

CS1101X Recitation #111 Step 2: Code (2/2) //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); }

CS1101X Recitation #112 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.

CS1101X Recitation #113 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

CS1101X Recitation #114 Problem Statement Problem statement: Write a loan calculator program that computes both monthly and total payments for a given loan amount, annual interest rate, and loan period.

CS1101X Recitation #115 Overall Plan Tasks:  Get three input values: loanAmount, interestRate, and loanPeriod.  Compute the monthly and total payments.  Output the results.

CS1101X Recitation #116 Required Classes

CS1101X Recitation #117 Development Steps We will develop this program in four steps: 1.Start with code to accept three input values. 2.Add code to output the results. 3.Add code to compute the monthly and total payments. 4.Update or modify code and tie up any loose ends.

CS1101X Recitation #118 Step 1 Design Call the showInputDialog method to accept three input values:  loan amount,  annual interest rate,  loan period. Data types are InputFormatData Type loan amountdollars and centsdouble annual interest ratein percent (e.g.,12.5) double loan periodin yearsint

CS1101X Recitation #119 Step 1 Code (1/2) Directory: Chapter3/Step1 Source File: Ch3LoanCalculator.java Directory: Chapter3/Step1 Source File: Ch3LoanCalculator.java Program source file is too big to list here. From now on, we ask you to view the source files using your Java IDE.

CS1101X Recitation #120 Step 1 Code (2/2) import javax.swing.*; class Ch3LoanCalculator { public static void main (String[] args) { double loanAmount, annualInterestRate; int loanPeriod; String inputStr; //get input values inputStr = JOptionPane.showInputDialog(null, "Loan Amount (Dollars+Cents):"); loanAmount = Double.parseDouble(inputStr); inputStr = JOptionPane.showInputDialog(null, "Annual Interest Rate (e.g., 9.5):"); annualInterestRate = Double.parseDouble(inputStr); inputStr = JOptionPane.showInputDialog(null, "Loan Period - # of years:"); loanPeriod = Integer.parseInt(inputStr); //echo print the input values System.out.println("Loan Amount: $" + loanAmount); System.out.println("Annual Interest Rate: " + annualInterestRate + "%"); System.out.println("Loan Period (years): " + loanPeriod); }

CS1101X Recitation #121 Step 1 Test In the testing phase, we run the program multiple times and verify that  we can enter three input values  we see the entered values echo-printed correctly on the standard output window

CS1101X Recitation #122 Step 2 Design We will consider the display format for out. Two possibilities are (among many others)

CS1101X Recitation #123 Step 2 Code (1/3) Directory: Chapter3/Step2 Source File: Ch3LoanCalculator.java Directory: Chapter3/Step2 Source File: Ch3LoanCalculator.java

CS1101X Recitation #124 Step 2 Code (2/3) import javax.swing.*; class Ch3LoanCalculator { public static void main (String[] args) { double loanAmount, annualInterestRate; double monthlyPayment, totalPayment; int loanPeriod; String inputStr; //get input values inputStr = JOptionPane.showInputDialog(null, "Loan Amount (Dollars+Cents):"); loanAmount = Double.parseDouble(inputStr); inputStr = JOptionPane.showInputDialog(null, "Annual Interest Rate (e.g., 9.5):"); annualInterestRate = Double.parseDouble(inputStr); inputStr = JOptionPane.showInputDialog(null, "Loan Period - # of years:"); loanPeriod = Integer.parseInt(inputStr);

CS1101X Recitation #125 Step 2 Code (3/3) //compute the monthly and total payments monthlyPayment = ; totalPayment = ; //display the result System.out.println("Loan Amount: $" + loanAmount); System.out.println("Annual Interest Rate: " + annualInterestRate + "%"); System.out.println("Loan Period (years): " + loanPeriod); System.out.println("\n"); //skip two lines System.out.println("Monthly payment is $ " + monthlyPayment); System.out.println(" TOTAL payment is $ " + totalPayment); }

CS1101X Recitation #126 Step 2 Test We run the program numerous times with different types of input values and check the output display format. Adjust the formatting as appropriate

CS1101X Recitation #127 Step 3 Design The formula to compute the geometric progression is the one we can use to compute the monthly payment. The formula requires the loan period in months and interest rate as monthly interest rate. So we must convert the annual interest rate (input value) to a monthly interest rate (per the formula), and the loan period to the number of monthly payments.

CS1101X Recitation #128 Step 3 Code (1/3) Directory: Chapter3/Step3 Source File: Ch3LoanCalculator.java Directory: Chapter3/Step3 Source File: Ch3LoanCalculator.java

CS1101X Recitation #129 Step 3 Code (2/3) import javax.swing.*; class Ch3LoanCalculator { public static void main (String[] args) { final int MONTHS_IN_YEAR = 12; double loanAmount, annualInterestRate; double monthlyPayment, totalPayment, monthlyInterestRate; int loanPeriod, numberOfPayments; String inputStr; //get input values inputStr = JOptionPane.showInputDialog(null, "Loan Amount (Dollars+Cents):"); loanAmount = Double.parseDouble(inputStr); inputStr = JOptionPane.showInputDialog(null, "Annual Interest Rate (e.g., 9.5):"); annualInterestRate = Double.parseDouble(inputStr); inputStr = JOptionPane.showInputDialog(null, "Loan Period - # of years:"); loanPeriod = Integer.parseInt(inputStr);

CS1101X Recitation #130 Step 3 Code (3/3) //compute the monthly and total payments monthlyInterestRate = annualInterestRate / MONTHS_IN_YEAR / 100; numberOfPayments = loanPeriod * MONTHS_IN_YEAR; monthlyPayment = (loanAmount * monthlyInterestRate) / (1 - Math.pow(1/(1 + monthlyInterestRate), numberOfPayments ) ); totalPayment = monthlyPayment * numberOfPayments; //display the result System.out.println("Loan Amount: $" + loanAmount); System.out.println("Annual Interest Rate: " + annualInterestRate + "%"); System.out.println("Loan Period (years): " + loanPeriod); System.out.println("\n"); //skip two lines System.out.println("Monthly payment is $ " + monthlyPayment); System.out.println(" TOTAL payment is $ " + totalPayment); }

CS1101X Recitation #131 Step 3 Test We run the program numerous times with different types of input values and check the results.

CS1101X Recitation #132 Step 4 Finalize We will add a program description We will format the monthly and total payments to two decimal places using DecimalFormat. Directory: Chapter3/Step4 Source File: Ch3LoanCalculator.java

CS1101X Recitation #133 Step 4 Code (1/3) import javax.swing.*; import java.text.*; class Ch3LoanCalculator { public static void main (String[] args) { final int MONTHS_IN_YEAR = 12; double loanAmount, annualInterestRate, monthlyPayment, totalPayment, monthlyInterestRate; int loanPeriod, numberOfPayments; String inputStr; DecimalFormat df = new DecimalFormat("0.00"); //describe the program System.out.println("This program computes the monthly and total"); System.out.println("payments for a given loan amount, annual "); System.out.println("interest rate, and loan period."); System.out.println("Loan amount in dollars and cents, e.g., "); System.out.println("Annual interest rate in percentage, e.g., 12.75"); System.out.println("Loan period in number of years, e.g., 15"); System.out.println("\n"); //skip two lines

CS1101X Recitation #134 Step 4 Code (2/3) //get input values inputStr = JOptionPane.showInputDialog(null, "Loan Amount (Dollars+Cents):"); loanAmount = Double.parseDouble(inputStr); inputStr = JOptionPane.showInputDialog(null, "Annual Interest Rate (e.g., 9.5):"); annualInterestRate = Double.parseDouble(inputStr); inputStr = JOptionPane.showInputDialog(null, "Loan Period - # of years:"); loanPeriod = Integer.parseInt(inputStr); //compute the monthly and total payments monthlyInterestRate = annualInterestRate / MONTHS_IN_YEAR / 100; numberOfPayments = loanPeriod * MONTHS_IN_YEAR; monthlyPayment = (loanAmount * monthlyInterestRate) / (1 - Math.pow(1/(1 + monthlyInterestRate), numberOfPayments ) ); totalPayment = monthlyPayment * numberOfPayments;

CS1101X Recitation #135 Step 4 Code (3/3) //display the result System.out.println("Loan Amount: $" + loanAmount); System.out.println("Annual Interest Rate: " + annualInterestRate + "%"); System.out.println("Loan Period (years): " + loanPeriod); System.out.println("\n"); //skip two lines System.out.println("Monthly payment is $ " + df.format(monthlyPayment)); System.out.println(" TOTAL payment is $ " + df.format(totalPayment)); }

CS1101X Recitation #136 End of Recitation #1