1. Let us assume that we have already defined an input object in as
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));Write a main method that asks a user for name of his or her favorite pet, and then prints a message
Your favorite pet was (this name).For example, if your favorite pet was a pony called Little Seabiscuit, then the computer should print the following message:
Your favorite pet was Little Seabiscuit.Do not forget that the header of the main method is:
public static void main(String[] args) throws IOException
Answer:
public static void main(String[] args) throws IOException{
String petName;
System.out.println("What was your favorite pet's name?");
petName = in.readLine();
System.out.println("Your favorite pet was" + petName + ".");
}
2. Suppose that you already have variables start and end that describe the times when the class starts and when the class ends. Write a statement (piece of Java code) that would define a new variable duration and assigns to it the value end - start.
Answer: int duration = end - start;