A) new
B) new
C) new
D) new
Scanner(inputFileName)
Scanner(outputFileName)
Scanner(inputFile)
Scanner(System.in)
INFO1103IntroductiontoProgramming,Semester2,2016
C) If a file named "dataOut.txt" already exists, new data will be added to the end of the file.
D) If a file named "dataOut.txt" already exists, a new file named "dataOut_1.txt" will be created and
used.
3. Your program must read an existing text file. You want the program to terminate if the file does not exist.
Which of the following indicates the correct code for the main method header?
A) public
B) public
C) public
D) public
static
static
static
static
void
void
void
void
main(String[]
main(String[]
main(String[]
main(String[]
4. You have opened a command prompt window and you have entered the following:
java myProg Bob Smith
static
static
static
static
void
void
void
void
main(String[]
main(String[]
main(String[]
main(String[]
A) This code constructs an object of type IllegalArgumentException and throws the object.
B) This code throws an existing IllegalArgumentException object.
C) This code constructs an object of type IllegalArgumentException and reserves it for future use.
D) This code will not compile.
INFO1103IntroductiontoProgramming,Semester2,2016
A) This method must handle the exception in the body of the method.
B) This method will be terminated if the file cannot be located.
C) This method must use a throw statement to pass the error back to its caller.
D) It cannot be determined how the method must handle the exception if the file cannot be located.
9. When a program throws an exception within a method that has no try-catch block, which of the following
statements about exception handling is true?
A) Execution will continue with the next statement in the method.
B) The current method terminates immediately.
C) The current method must decide whether to continue or terminate.
D) The user must decide whether to continue or terminate the program.
10. Which is the correct statement?
try
{
File inputFile = new File(filename);
Scanner in = new Scanner(inputFile);
. . .
}
catch (Exception e)
{
}
A) This code will not catch a FileNotFoundException that occurs in the try block.
B) This code will pass any exceptions back to its caller.
C) This code will catch exceptions that occur in the try block but will do nothing about the exceptions.
D) This code will not catch any exceptions that occur in the try block.
INFO1103IntroductiontoProgramming,Semester2,2016
1. Hello
Write a program WriteHello.java that does the following:
1)
2)
3)
4)
5)
6)
3. Reading numbers separated with delimiters that are not white space
The same task as in the previous exercise but now the numbers are separated with commas, as in the file
numbers_with_commas.txt, available from eLearning\Labs. Write a program named
NumbersWithDilimiter.javathat reads integer numbers separated with commas from a file and prints
their sum. Hints:
1) Use the method useDelimiter from the class Scanner to specify that the delimiter is ,
2) Read a whole line with Scanner and then use another Scanner object to separate it into parts, e.g.:
Scannerin=newScanner(newFile(filename));//scannerforreadingfromthefile
while(in.hasNextLine())
{
Stringline=in.nextLine();
ScannerlineBreaker=newScanner(line);//scannerforbreakingthelineintoparts
lineBreaker.useDelimiter(",");//specifyingthedelimiter
while(lineBreaker.hasNext())
{
currentNumber=lineBreaker.nextInt();//getthenextnumber
}
Modify your code so that it can read numbers separated with any delimiter that is not a sequence of digits.
4
INFO1103IntroductiontoProgramming,Semester2,2016
4. Expenses
Download the file travel.txtfromeLearning\Labs. Here is an extract from it:
Barcelona;food;20.00
Barcelona;hotel;200
Madrid;travel;85.50
Madrid;hotel;150.00
...
It contains data about the expenses during a trip. The first entry is the city, the second is the category of the
expenses (travel, hotel or food) and the last one is the amount spent in dollars. The data is separated with
semicolon and space (; ).
Write a program Expenses.java that:
1) Asks the user to enter the name of the input file (you can use travel.txt for testing)
2) Reads the data from the specified file and prints on the screen the total amount for each of the three
categories.
3) Handle 2 types of exceptions using a try-catch block FileNotFoundExceptionwhen the input file is
not found and NoSuchElementExceptionwhen the file format is incorrect. Display an error message
in each case.
User dialog:
Enterthefilenamewiththeexpenses:travel.txt
20.0
65.8
80.8
Travelexpenses:140.50
Hotelexpenses:455.00
Foodexpenses:80.80
Enterthefilenamewiththeexpenses:trabel.txt
Filenotfound!
5. Real numbers
Write a program RealNumbers.javathat:
1) Asks the user to enter a sequence of real numbers, i.e. floating-point numbers. (Note that integer
numbers are also a valid entry). You can assume that they are separated with white space.
2) When the user enters a value that is not a number, ask him to try again. If the second entry is correct,
continue. If there are 2 incorrect entries, print the sum and quit reading the user input (this is also the
condition to stop the program).
3) Use a trycatch block to handle exceptions, e.g. InputMismatchException, and print
appropriate message, e.g. That was not a floating-point value. Try again!.
User dialog:
Enteralistoffloatingpointvalues:
23.5678.106587.10g
Thatwasnotafloatingpointvalue.Tryagain!
67.6
INFO1103IntroductiontoProgramming,Semester2,2016
10
hk
Thatwasnotafloatingpointvalue.Tryagain!
&gh
Againanonfloatingpointvalue.
Stoptwoconsecutivenonfloatingpointnumbers.
Thesumis:331.36
This means that the most popular baby name (i.e. the name ranked 1) was Michael for boys and
Jessica for girls. Michael was given to 462085 babies in the current year, which represented 2.2506
% of all births. Similarly, Jessica was given to 302962 babies which was 1.5436% of all births. The
second most popular name for boys was Christopher, etc. This is a real data collected from
http://www.ssa.gov/OACT/babynames.
2) Write 2 other files: boynames.txt and girlname.txt. Each of them contains the boys and girls
names separately (one name in a row), and at the end the total number of births (i.e. the sum of
column 2 for boys) and the total percentage of births (i.e. the sum of column 3 for boys). E.g. here is
an extract from boynames.txt:
Michael
Christopher
Matthew
Joshua
6
INFO1103IntroductiontoProgramming,Semester2,2016
Jacob
Nicholas
Brennon
Derik
Total births: 17611776, total percent: 85.78040000000018