COMP 110 More Loops and Strings - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

COMP 110 More Loops and Strings

Description:

Convert floating point numbers to integers for modular arithmetic ... get help if you are stumped (before you get frustrated) Read the assignment carefully ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 20
Provided by: michele9
Category:

less

Transcript and Presenter's Notes

Title: COMP 110 More Loops and Strings


1
COMP 110More Loops and Strings
  • Tabitha Peck M.S.
  • February 18, 2008
  • MWF 3-350 pm
  • Philips 367

1
2
Announcements
  • Programs
  • START EARLY!!!!
  • Should take 10-15 hours
  • Should NOT take 20 hours
  • Get help if you are stumped
  • Office hours are helpful
  • Read Instructions

2
3
Announcements
  • Program 2
  • Format to 2 decimal places
  • Convert floating point numbers to integers for
    modular arithmetic
  • String parsing
  • Comments and pseudocode

3
4
Program 2 Grades
5
Formatting Decimals (Jan 30)
  • import java.text.
  • DecimalFormat df new DecimalFormat("0.00")
  • df.format(myVariable)
  • Example code on class website

5
6
  • import java.text.DecimalFormat
  • public class DecFormat
  • public static void main(String args)
  • DecimalFormat df new
    DecimalFormat("0.00")
  • DecimalFormat df2 new
    DecimalFormat("0.0000")
  • double d 12.345678
  • System.out.println("my double with two
    decimal places " df.format(d))
  • System.out.println("my double with four
    decimal places " df2.format(d))

6
7
Converting to Integers
  • You must convert floating-point numbers to
    integers to perform modular arithmetic.
  • Modular arithmetic is defined for integers!!!
  • Type Casting (Jan 23)
  • myInt (int)myFoat
  • answer (int)operand1 (int) operand2

7
8
String parsing
  • Lab 2
  • Requests for more practice
  • Today More Practice

9
Comments
  • double answer op1 op2
  • answer 100answer
  • int ans (int)answer
  • answer (double)ans
  • answer answer/100
    JOptionPane.showMessageDialog(null, inputString
    " " answer)

10
Comments
  • /For every calculation here on out, once the
    original operation is done, the answer is
    multiplied by 100, changed to an integer, then
    divided by 100 in order to give it the two
    decimal places required by the assignment. This
    comment will not be repeated for these operations
    again./

11
Pseudocode
  • Figure out how to solve the problem BEFORE you
    start coding
  • Who had a hard time parsing the input string?
  • What if you spent an hour before coding thinking
    about how to solve it?

12
  • I originally had a little trouble trying to
    figure out how to start (program2) as well. But
    on the way home Friday, it hit me that the user
    was inputting a string and that string had to be
    converted to the parts needed. Since the user
    was required to input a string with spaces, I
    figured using the spaces to convert the needed
    information would be the easiest way.

13
Pseudocode
  • Input a string (num operator num)
  • Hmm? There is a space between everything.
  • Find index of first space
  • Get substring from beginning to first space
  • Convert to double

14
Pseudocode
  • Once you think about how to solve the problem
  • Then you start coding

15
Announcements (continued)
  • Project 3
  • Assigned today
  • Due March 4
  • If you have problems come see me (soon)
  • Coming to class late

16
You should
  • NOT spend gt 10 hours per week on this class (on
    average)
  • start assignments early
  • get help if you are stumped (before you get
    frustrated)
  • Read the assignment carefully

17
Questions?
  • Suggestions / concerns / comments
  • Please send them to me

17
18
Today in COMP 110
  • Practice String Parsing
  • Practice loops - code on web
  • Worksheet
  • hand in for class participation
  • Answers posted on website tonight

18
19
Wednesday
  • Classes
  • you will need this for program 3

19
Write a Comment
User Comments (0)
About PowerShow.com