Presentation is loading. Please wait.

Presentation is loading. Please wait.

March 2005 1/18R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class Three exam questionsThree exam questions Lab with filesLab with.

Similar presentations


Presentation on theme: "March 2005 1/18R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class Three exam questionsThree exam questions Lab with filesLab with."— Presentation transcript:

1 March 2005 1/18R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class Three exam questionsThree exam questions Lab with filesLab with files

2 March 2005 2/18R. Smith - University of St Thomas - Minnesota Problem 12 Write a series of statements that collects an integer from the user via JOptionPane and keeps trying until the user types an integer between 20 and 40. Use a “trailing decision” loop to do this. Provide the necessary variable declarations. You do not need to provide statements that might appear outside the main declaration.

3 March 2005 3/18R. Smith - University of St Thomas - Minnesota Problem 13 Write a series of statements that collects an integer from the user via the Scanner class and keeps trying until the user types an integer between 20 and 40. Use a “leading decision” loop to do this. Provide the necessary variable declarations. You do not need to provide statements that might appear outside the main declaration.

4 March 2005 4/18R. Smith - University of St Thomas - Minnesota Problem 14 Write a series of statements that collects 12 “doubles” from System.in using the Scanner class, calculates the average, and prints it out to System.out. Use a for loop. Include any variable declarations that may be required. You do not need to provide statements that might appear outside the main declaration.

5 March 2005 5/18R. Smith - University of St Thomas - Minnesota Working with Files Finding the file name and checking itFinding the file name and checking it Reading from a text fileReading from a text file –Making Scanner work in your favor Start with numbersStart with numbers Read text at the endRead text at the end –Checking for end of file

6 March 2005 6/18R. Smith - University of St Thomas - Minnesota Input Files Use Scanner class and File classUse Scanner class and File class import java.util.Scanner;import java.util.Scanner; import java.io.*;import java.io.*; public static void main(String[] args) throws IOExceptionpublic static void main(String[] args) throws IOException File fn; // identify the file to readFile fn; // identify the file to read Scanner sc; // for reading the dataScanner sc; // for reading the data fn = new File(“filename.txt”);fn = new File(“filename.txt”); sc = new Scanner(fn);sc = new Scanner(fn);

7 March 2005 7/18R. Smith - University of St Thomas - Minnesota Checking special conditions On File objectsOn File objects –File fn –if (fn.exists()) – checks for existence Create the File object with the desired nameCreate the File object with the desired name Do the exists() method to check if it existsDo the exists() method to check if it exists On Scanner objectsOn Scanner objects –Scanner sc –if (sc.hasNext()) – true if there’s more data

8 March 2005 8/18R. Smith - University of St Thomas - Minnesota Lab 10 - Carpeting Open a file with a predefined nameOpen a file with a predefined name –Like lab10data.txt –Contains several lines of this form: Double for widthDouble for width Double for lengthDouble for length Double for cost per square footDouble for cost per square foot Text name of a roomText name of a room Read each lineRead each line –Multiply width by length by cost –Print a line giving room name, width, length, and cost –Run a total of the cost Once the file is done, print out the total costOnce the file is done, print out the total cost All output to System.outAll output to System.out

9 March 2005 9/18R. Smith - University of St Thomas - Minnesota Examples Example InputExample Input 8 10 5.00 Bedroom 10 12 5.00 Living Rom 15 11 10.00 Dining Room Example OutputExample Output Bedroom 8 x 10 for $400.00 Living Room 10 x 12 for $900.00 Dining Room 15 x 11 for $1650.00

10 March 2005 10/18R. Smith - University of St Thomas - Minnesota That’s it. Questions?Questions? Creative Commons License This work is licensed under the Creative Commons Attribution-Share Alike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.


Download ppt "March 2005 1/18R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class Three exam questionsThree exam questions Lab with filesLab with."

Similar presentations


Ads by Google