1 / 18

Program 6

Program 6. Any questions?. System.in. Does the opposite of System.out. Reading. Lines of text Single words Integers Doubles Etc. Practice Problem.

cgaley
Download Presentation

Program 6

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. Program 6 • Any questions?

  2. System.in • Does the opposite of System.out

  3. Reading • Lines of text • Single words • Integers • Doubles • Etc.

  4. Practice Problem • Write Java code to ask for a student’s exam score out of 100. Your program is then to match the exam score to a letter grade and print the grade to the screen. The letter grade is to be calculated as follows: 90 and above A 80-89 B 70-79 C 60-69 D below 60 F

  5. Switch Practice • A program is required to read a customer’s name, a purchase amount and a tax code. The tax has been validated and will be one of the following: 0 tax exempt (0%) 1 state sales tax only (3%) 2 federal and state sales tax (5%) 3 special sales tax (7%)The program must then compute the sales tax and the total amount due and print the customer’s name, purchase amount, sales tax and total amount due.

  6. Sentinel-Controlled loops

  7. Practice • Design and write a program that will prompt for, receive, and total a collection of payroll amounts entered by the user until a sentinel amount of -99 is entered. After the sentinel has been entered, display the total payroll amount on the screen.

  8. Problem 3 • Write a program that sums a sequence of integers. Assume that the first integer read specifies the number of values remaining to be entered. Your program should read only one value at a time. A typical input sequence might be 5 100 200 300 400 500

  9. Problem 4 • Write a program that finds the smallest of several integers. Assume that input will end when a sentinel value of –999 is read. Do not count –999 as one of the integers to consider.

  10. Working with Files • Input is very straightforward. • We just connect our Scanner to a File object instead of System.in or a String • Do have to deal with exceptions • Often just declare that we throw the exceptions

  11. throws Clause • Just an announcement that a particular method may throw a particular exception • Used to help the programmers that call the method • (more on exceptions in chapter 11)

  12. Writing Files • Writer classes • Each classes has a purpose • FileWriter – connects to a file • BufferedWriter – makes output more efficient • PrintWriter – lets you use print and println

  13. Using the FileWriters FileWriter fw = new FileWriter(fileName); BufferedWriter bw = new BufferedWriter(fw); PrintWriter out = new PrintWriter(bw); out.println(“the first line”);

  14. Alternative PrintWriter out = new PrintWriter (new BufferedWriter (new FileWriter(fileName))); out.println(“the first line”);

  15. Notes • Using files always potentially throws IOExceptions. • Sometimes we can handle these gracefully. • Sometimes we need to allow the program to report an error and end.

  16. File Practice • Write code to read a file of integers and write a new file with one integer per line of the new file.

  17. File Practice • The files input1.txt and input2.txt each contain a list of numbers of type int in ascending order. Write a program to create a new file, output.txt, that contains all of the numbers from both input files in sorted order. Use a function that will accept as parameters any two open input streams and an open output stream.

  18. More File Practice • Write a program to read a file of data about employee raises and create a file of notes to the employees about their raises. The data is stored in the file “a:\\newSal.txt” in the form:LastName, FirstName OldSal NewSalFor each line, write to “a:\\letters.txt” a note in the form:Dear FirstName LastName,You have received a raise of raisePercentage%. Your new salary is NewSal.Write 3 blank lines before each note and 3 blank lines followed by a line of dashes after each note.

More Related