CS 1401, Quiz #3, Solutions

Date: Tuesday, February 6, 2007
Name:____________________________________________________________
Class section (9 am or 10:30 am): _______________________________

1. For each of the following topics, write "yes" or "no" depending on whether this topic is covered in Chapter 3, the chapter that you were supposed to read before the class:

if-then statements no

storing numbers to a file yes

while loops no

Graphical User Interfaces yes

2. Write a main method that asks the user to enter an integer, doubles this integer, and prints the result. Reminders: a main methods should start with

public static void main(String [] args){
and to read, we must declare a reader, e.g., as
Scanner console = new Scanner(System.in);

Solution:

public static void main(String [] args){
  Scanner console = new Scanner(System.in);
  System.out.println("Please enter an integer.");
  int number = console.nextInt();
  number *= 2;
  System.out.println("The result is " + number);
}
3. The String variable yesterday contains the text "February 5, 2007". What will be the value of the string longAgo after the following command:
longAgo = "Jan" + yesterday.substring(4,yesterday.length());

Answer: January 5, 2007