Intro To Programming UNIT 1 Flashcards
What should be the first line in a program without import statements and comments
public class FileName {
How do you format the main method declaration
public static void main(String[]args) {
What statement should you use to print to the console without a new line
System.out.print(“Text to be displayed on the console”);
Take note of the capitalization
How do you output a text as a new line in the console
System.out.println(“Text to be displayed”);
How do you print a new line / How do you print a TAB
/n … /t
When a user types on the keyboard where are those characters stored before being interpreted by the program
In the keyboard buffer
What is a low level function to read from the keyboard buffer? What is it’s disadvantages?
Using the System.in method. It’s not powerful because it can only return one character at a time
What is the Java API?
It’s a collection of thousands of useful classes that are stored in sever hundred “packages” or class libraries
When importing as such:
import java.util.Scanner;
What is the package name of Scanner?
What is the fully qualified name?
The package name of scanner is java.util
The fully qualified name is java.util.Scanner
What is accomplished by using an import statement such as:
import java.util.Scanner;
It tells the compiler where to go in the library to find the code it needs to run the program
How would you create a new Scanner object?
include import statements
include java.util.Scanner;
Scanner obj_name = new Scanner(System.in)
What are methods
It’s a function or a block of code that is run when called upon
What methods does a Scanner object have to read from the Keyboard Buffer
nextInt() -> Reads an int
nextDouble() -> Reads a double
nextLine() -> Reads a line of text till the eol
next() -> Reads from the buffer until it hits a space
What is the difference between Scanner.next() and Scanner.nextLine()
nextLine() will read and RETURN ever character that is typed into the keyboard buffer including spaces and EOL characters therefore it empties the keyboard buffer
next() will read characters from the buffer until it hits a space or a EOL. it does not flush the buffer clean and leaves the EOL character behind this applies to nextInt() and nextDouble()
What is the problem of using nextLine() after a next() and what is the solution to the problem?
When it’s called it will immediately look at the keyboard buffer before looking for user input. If a eol character is in the buffer it will stop reading and return.
The solution if you use a next() call before using a nextLine(). You must complete a buffer flush by calling nextLine() after using a next() call. This will empty all EOL characters