1 / 14

We input data from whoever is using the program by using the SavitchIn class

We input data from whoever is using the program by using the SavitchIn class A class is another Java program that you can use without having to rewrite it each time To ask the user for an integer, we use: SavitchIn.readLineInt( ) Example: int testScore = 0;

keahi
Download Presentation

We input data from whoever is using the program by using the SavitchIn class

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. We input data from whoever is using the program by using the SavitchIn class • A class is another Java program that you can use without having to rewrite it each time • To ask the user for an integer, we use: SavitchIn.readLineInt( ) • Example: int testScore = 0; System.out.println(“Enter test score: “); testScore = SavitchIn.readLineInt( );

  2. The SavitchIn command changes, depending on what kind of variable you are asking for • Int: SavitchIn.readLineInt( ) • Double: SavitchIn.readLineDouble( ) • Char: SavitchIn.readLineNonwhiteChar( ) (why nonWhite?? This way if the user types spaces -- “white space” -- before whatever they type, Java ignores the spaces) • String: SavitchIn.readLine( ) • Demo

  3. Type Casting • Let’s say you have a double that you need to convert into an integer • This can be accomplished by type casting: int x = 0; double y = 3.7; x = (int)y; // the (int) is where the type casting occurs

  4. Integer vs. Double division • Guess the result of the following: int x = 5; int y = 2; int z = x/y; • Dividing with integers: the decimal part of the answer is truncated (erased). • To be accurate, use at least one double, or one decimal in your division.

  5. int x = 5; int y = 2; double z = x/y; what does z equal now? 2.0

  6. int x = 5; double y = 2; double z = x/y; what does z equal now? 2.5

  7. int x = 5; int y = 2; double z = double(x/y); what does z equal now? 2.0 why? because x/y = 2, then 2 is type-casted (converted) into a double.

  8. int x = 5; int y = 2; double z = double(x)/y; what does z equal now? 2.5 why? Notice the slight change in parentheses from the previous slide. Now x is type-casted from 5 to 5.0. Next, 5.0/2 = 2.5. Remember, as long as one of the two numbers being divided is a double, then the result is a double.

  9. Variables do not have to be used in order to work with doubles and integers. For example: System.out.println(9/4); (this would display 2) System.out.println(9.0/4); (this would display 2.25) System.out.println(9/4.0); (this would display 2.25) System.out.println(9.0/4.0); (this would display 2.25) Notice that as long as one of the two numbers being divided is a double, then the answer will be a double.

  10. Modulus Division • Using the modulus symbol, %, determines the remainder 10 % 6 = 4 13 % 5 = ? = 3 283 % 100 = ? = 83 26 % 2 = ? = 0 27 % 2 = ? = 1 (any even #) % 2 = ? = 0 (any odd #) % 2 = ? = 1

  11. Constants • A constant is a variable whose value is expected to remain the same throughout a program • It is considered “good programming style” to use constants for certain variables – for instance, interest rate on a loan, pi, etc • To declare a constant, add the word final: final double RATE = 4.25; • Constants are always written in uppercase • See TempConverter, p.77

  12. Class Libraries and Packages • A class library is a set of packages, each of which contains classes (files) of code that can be used by programmers • You can use this code simply by importing a package into your program, with a single line of code • Top line of code: import library.package.class • This is why Object-Oriented languages like Java are so popular – you can use code someone else has written, over and over again

  13. Formatting Numbers • Java provides an easy way to format #s into percents, currency, and decimals • For percent and currency, you must import java.text.NumberFormat • For decimals, you must import java.text.DecimalFormat • Example: p97 & 99

  14. Assignments (files should be named with this format: P117num4) • P. 117-118 • #4 (Ignore “Create… application to”; look up formula online) • #5 (remember: “floating point value” is another name for a double) • #7 (hints: use both modulus division and integer division; how many seconds are in an hour?) • #11 (start by asking user for # of gallons used, starting odometer #, and ending odometer #) • #12 (ask the user how many of each coin; also, see how the variable subtotal is formatted on p.97)

More Related