Download Review Problem Set 2

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
ICS 4U1 Java Review – Part 2
At this point, it would be best for you guys to all be using Eclipse (sorry Samin!). The reason
being is that Ready To Program hasn’t been supported in many years. As such, it only works
with older versions of Java and doesn’t have all of the newer features available (particularly
the Scanner class, which will make user input so much easier).
The Scanner Class
The Scanner class is a class in java.util, which allows the user to read values of various types.
There are far more methods in class Scanner than you will need in this course (feel free to
check it out in the Java documentation online). Not only can the Scanner read from the
user’s keyboard, but you could also use it to read from a file! This is done by treating both
user input and file input as a stream. The next bit of input that is available is referred to as a
token. A token can be any variable type. If you’re reading from a file, tokens are broken up
by whitespace (blanks or line breaks). Here’s a brief summary of what you’ll probably want to
know.
To construct a Scanner:
Scanner in = new Scanner(System.in);
// System.in is an InputStream
To terminate a Scanner:
in.close(); // closes the scanner class
Method
int nextInt()
double nextDouble()
String next()
String nextLine()
boolean hasNextLine()
boolean hasNextInt()
boolean hasNextFloat()
Return Value
Returns the next token as an int. If the next token is not an
integer, InputMismatchException is thrown.
Returns the next token as a double. If the next token is not a
floating point number, InputMismatchException is thrown.
Finds and returns the next complete token from this scanner and
returns it as a string; a token is usually ended by whitespace such
as a blank or line break. If no token exists,
NoSuchElementException is thrown.
Returns the rest of the current line (including all blanks that
normally break up tokens), excluding any line separator at the end
of the line.
Returns true if the scanner has another line as its input; false
otherwise.
Returns true if the next token in the scanner can be interpreted as
an int value.
Returns true if the next toke in the scanner can be interpreted as
a float value.
Example:
import java.util.Scanner; // Access Scanner class
public class NumericInput
{
public static void main(String[] args)
{
int num1, num2, sum;
Scanner scan = new Scanner(System.in);
// Request input.
System.out.println(“Input the first number to add:”);
num1 = scan.nextInt(); // Read an integer as input.
System.out.println(“Input the second number to add:”);
num2 = scan.nextInt();
sum = num1 + num2;
System.out.println(“The sum of your numbers is “ + sum);
}
}
Example: Sometimes you want to verify that the next available token is valid. By using clever
looping, this can be done quite easily.
import java.util.Scanner; // Access Scanner class
public class OnlyIntegers
{
public static void main(String[] args)
{
int num;
Scanner scan = new Scanner(System.in);
System.out.println(“Please enter an integer.”);
// Request input and keep going until we get an integer.
while (!scan.hasNextInt()) // Keep looping while the next token is NOT an int.
{
System.out.println(“That’s not an integer!”);
System.out.println(“Please enter an integer.);
// Skip the next token.
scan.next();
}
// The next token MUST be an int (or else the loop would not have exited).
num = scan.nextInt();
System.out.println(“Your number is “ + num);
}
}
Problem Set 2 – due Monday Feb. 2
1. (Making Change)
Write a program that prompts the user for a positive integer value between 1 and
1000, inclusively. The value entered represents a number of pennies – yes, our
beloved extinct copper friend. After the user has entered the number of cents, the
program should then state how many coins are needed to make up that amount of
cents. The user should not be allowed to enter an invalid entry and the program
should allow him/her to continue until a valid entry is made or the letter ‘X’ is input
to close the program.
Save your program as MakingChange.java.
Sample Program Output (user input shown in bold italics):
Enter a number between 1 and
87
87 cents in coins is:
3 quarters
1 dime
0 nickels and
2 pennies
Enter a number between 1 and
Yousuck!
Sorry, that’s not a valid input.
Enter a number between 1 and
1001
Sorry, that’s not a valid input.
Enter a number between 1 and
118
118 cents in coins is:
4 quarters
1 dime
1 nickel and
3 pennies
Enter a number between 1 and
X
Thanks for coming out!
1000:
1000:
1000:
1000:
1000: