Test Bank For Absolute Java 5th Edition by Walter Savitch

<< Test Bank Diversity and Society Race Ethnicity and Gender 5th Edition by Joseph F. Healey Test Bank For Absolute C++ 5th Edition by Walter Savitch >>
Product Code: 222
Availability: In Stock
Price: $24.99
Qty:     - OR -   Add to Wish List
Add to Compare

Test Bank For Absolute Java 5th Edition by Walter Savitch

Description

WITH ANSWERS

 

Absolute Java 5th Edition by Walter Savitch  
Test Bank 

Chapter 2
Console Input and Output

  • nMultiple Choice
    • Valid arguments to the System.out objects println method include:

(a)  Anything with double quotes

(b)  String variables

(c)  Variables of type int

(d)  All of the above

  • D
  • Which statement or group of statements produces the output: Java programming is fun!
    • out.print(Java programming);

System.out.print(is fun!);

  • out.println(Java programming is fun!);
  • out.println(Java programming);

System.out.println( is fun!);

  • out.print(Java programming)

System.out.println( is fun!);

  • D
  • If a hyphen is added after the % in a format specifier, the output will be _________.
    • Left justified
    • Right justified
    • Centered
    • None of the above
      • A
    • The statement: System.out.printf(%6.2f, 597.7231); displays:
      • 723
      • 72
      • 72
      • None of the above
        • B
      • The Java method printf is based on the ________ language.
        • Pascal
        • C++
        • C
        • ADA
          • C
        • The class NumberFormat allows you to specify a constant representing which countrys currency format should be used. To use this constant you must import:
          • util.Locale
          • util.Currency
          • util.Properties
          • None of the above.
            • A
          • Standard code libraries in Java are called:
            • Methods
            • Classes
            • Packages
            • Statements
              • C
            • What does the following code output?

DecimalFormat percent = new DecimalFormat(0.00%);

System.out.println(percent.format(0.308));

 

  • 080%
  • 80%
  • .0308%
  • 0%
    • B
  • What does the following code output?

DecimalFormat dfQuestion = new DecimalFormat(#0.##E0);

System.out.println(dfQuestion.format(12.7896987));

 

  • 79E0
  • 8E0
  • 28E1
  • .13E2
    • A
  • What Java package includes the class Scanner?
    • awt
    • swing
    • io
    • util
      • D
    • nTrue/False
      • Efficiency is lost in importing the entire package instead of importing the classes you use.
        • False
      • Every Java program automatically imports the java.util package.
        • False
      • The new line character is represented as .
        • True
      • The method printf is used the same way as the method println but has the added feature that allows you to add formatting instructions.
        • False
      • The printf method can be used to output multiple formatted values.
        • True
      • The Scanner class has a method next that allows an entire line of string text to be read.
        • False
      • Echoing input is good programming practice because it can reveal problems in the input.
        • True
      • nShort Answer/Essay
        • Write a Java statement to display your name in the console window.

System.out.println(Wally Wonders);

  • Write ONE Java statement to display your first and last name on two separate lines.

System.out.print(Wally Wonders);

  • Write Java statements to apply currency formatting to the number 100. Indicate the package you need to import.

import java.text.NumberFormat;

NumberFormat nfMoney = NumberFormat.getCurrencyInstance();

System.out.println(nfMoney.format(100));

  • Write a Java program to create and display 57.32% using the DecimalFormat class. Include the necessary import statement to use the DecimalFormat class.

import java.text.DecimalFormat;

public class decimalClass

{

public static void main(String[] args)

{

DecimalFormat df = new DecimalFormat(00.00%);

System.out.println(df.format(.5732));

}

}

  • Explain the significance of the pattern string used by the DecimalFormat object, and give an example of a valid pattern.
    • The pattern string represents the format in which the number passed to the DecimalFormat object is formatted. The pattern can either specify the exact number of digits before and after the decimal, or it can specify the minimum numbers of digits.  The character 0 is used to represent a required digit and the character # is used to represent optional digits.  Valid patterns include:  0.00, #0.0##.
  • What does it mean to prompt the user?
    • Prompting the user means to display a meaningful message to the user asking for some type of input. An example of prompting the user would be displaying a JOptionPane to ask the user to input their name.
  • Why is echoing user input a good programming practice?
    • Echoing input is a technique that is commonly used to allow the user to check their input for accuracy before it is actually sent to the program for processing. This technique reduces the chances of errors in the program.
  • If there is no loss of efficiency in importing an entire Java package instead of importing only classes you use into your program, why would you not just import the entire package?
    • Importing only the classes you need into your program makes your program easier to read as well as aiding in documenting the program. Program readability is very important since humans read computer programs, too.
  • Write a complete Java console application that prompts the user for two numbers, multiplies the numbers, and then displays the result to the user.

import java.util.Scanner;

 

public class  ConsoleMultiply

{

public static void main(String[] args)

{

//Create the scanner object for console input

Scanner keyboard = new Scanner(System.in);

 

//Prompt the user for the first number

System.out.print(Enter the first integer: );

 

//Read the input

int firstNumber = keyboard.nextInt();

 

//Prompt the user for the second number

System.out.print(Enter the second integer: );

 

//Read the second number

int secondNumber = keyboard.nextInt();

 

System.out.println(firstNumber + * + secondNumber + is

+ firstNumber * secondNumber);

}

}

  • What do the format specifiers d, f, e, g, s and c represent?
    • The format specifiers d, f, e, and g are all used for numeric representation. Specifier d represents a decimal integer, specifier f represents a fixed-point floating-point number, specifier e represents E-notation floating-point, and specifier g represents general floating-point in which Java secedes whether to use E-notation.

The format specifiers s and c are used for string and character representation, respectively.

 

  • Write a Java statement to create and initialize a Scanner object named input.
    • Scanner input = new Scanner(System.in);
  • What is whitespace and why is it import when reading input from the keyboard using the Scanner class?
    • Whitespace is any string of characters, such as blank spaces, tabs, and line breaks, that prints as whitespace when written on (white) paper. Whitespace servers as delimiters for many of the Scanner class methods.Chapter 4
      Defining Classes I

      • nMultiple Choice
        • The new operator:
          • allocates memory
          • is used to create an object of a class
          • associates an object with a variable that names it.
          • All of the above.
            • D
          • A method that performs some action other than returning a value is called a __________ method.
            • null
            • void
            • public
            • private
              • B
            • The body of a method that returns a value must contain at least one _________ statement.
              • void
              • invocation
              • thows
              • return
                • D
              • A variable whose meaning is confined to an object of a class is called:
                • instance variable
                • local variable
                • global variable
                • none of the above
                  • A
                • A variable whose meaning is confined to a method definition is called an/a
                  • instance variable
                  • local variable
                  • global variable
                  • none of the above
                    • B
                  • In Java, a block is delimited by:
                    • ( )
                    • /* */
                    • { }
                      • D
                    • In Java, call-by-value is only used with:
                      • objects
                      • primitive types
                      • this
                      • all of the above
                        • B
                      • The parameter this refers to
                        • instance variables
                        • local variables
                        • global variables
                        • the calling object
                          • D
                        • When you want the parameters in a method to be the same as the instance variables you can use the _____ parameter.
                          • String
                          • hidden
                          • default
                          • this
                            • D
                          • Two methods that are expected to be in all Java classes are:
                            • getName and setName
                            • toString and equals
                            • compareTo and charAt
                            • toLowerCase and toUpperCase
                              • B
                            • A program whose only task is to test a method is called a:
                              • driver program
                              • stub
                              • bottom-up test
                              • recursive method
                                • A
                              • Java has a way of officially hiding details of a class definition. To hide details, you mark them as _________.
                                • public
                                • protected
                                • private
                                • all of the above
                                  • C
                                • A set method is:
                                  • an accessor method
                                  • a mutator method
                                  • a recursive method
                                  • none of the above
                                    • B
                                  • Accessor methods:
                                    • return something equivalent to the value of an instance variable.
                                    • promotes abstraction
                                    • both A and B
                                    • none of the above
                                      • C
                                    • A more eloquent approach in implementing mutator methods is to return a ________ value.
                                      • int
                                      • char
                                      • boolean
                                      • double
                                        • C
                                      • A _________ states what is assumed to be true when the method is called.
                                        • prescript
                                        • postscript
                                        • precondition
                                        • postcondition
                                          • C
                                        • The name of a method and the list of ________ types in the heading of the method definition is called the method signature.
                                          • parameter
                                          • argument
                                          • return
                                          • primitive
                                            • A
                                          • nTrue/False
                                            • An object of class A is an instance of class A.
                                              • True
                                            • An invocation of a method that returns a value can be used as an expression any place that a value of the Type_Returned can be used.
                                              • True
                                            • The Java language supports global variables.
                                              • False
                                            • In a method invocation, there must be exactly the same number of arguments in parentheses as there are formal parameters in the method definition heading.
                                              • True
                                            • When you give a command to run a Java program, the runtime system invokes the class constructor.
                                              • False
                                            • Inside a Java method definition, you can use the keyword this as a name for the calling object.
                                              • True
                                            • Boolean expressions may be used to control if-else or while statements.
                                              • True
                                            • The modifier private means that an instance variable can be accessed by name outside of the class definition.
                                              • False
                                            • It is considered good programming practice to validate a value passed to a mutator method before setting the instance variable.
                                              • True
                                            • Mutator methods can return integer values indicating if the change of the instance variable was a success.
                                              • False
                                            • Method overloading is when two or more methods of the same class have the same name but differ in number or types of parameters.
                                              • True
                                            • Java supports operator overloading.
                                              • False
                                            • Only the default constructor has the this
                                              • False
                                            • nShort Answer/Essay
                                              • Write a Java method that prints the phrase Five purple people eaters were seen munching Martians.

      /** void method that prints a phrase */

      public void printPhrase()

      {

      System.out.println(Five purple people eaters were seen munching Martians!);

      }

      • What is the purpose of the new operator?
        • The new operator is used to create an object of a class and associate the object with the variable that names it. Memory is also allocated for that object.
      • Write a Java method that returns the value of PI, where PI = 3.1415926535.

      /** method that returns the value of PI */

      public double piValue()

      {

      return 3.1415926535;

      }

      • Define the terms arguments and parameters. How are they different?
        • Parameters, also referred to as formal parameters, are used to define the list of variables included in the parenthesis in a method signature. An argument is the value actually sent to the method upon invocation.  The terms parameters and argument are very similar in meaning and are often used interchangeably by programmers.  The terms differ slightly in that an argument is the actual value that replaces the parameter within the method.
      • Write a method called power the computes xn where x and n and positive integers. The method has two integer parameters and returns a value of type long.

      /** x and n are nonnegative integers */

      public long power(int x, int n)

      {

      long result = 1;

      /** check for positive numbers */

      if((x >= 0) && (n >= 0))

      {

      /** raise x to the nth power */

      for(int i = n; i > 0; i)

      result *= x;

      }

      else

      {

      result = 0;

      System.out.println(Fatal error.positive integers required!);

      }

      return result;

      }

      • Write a method called Greeting that displays a personalized greeting given a first name.

      /** method to display a personalized greeting */

      public static void greeting(String name)

      {

      System.out.println(Hello + name);

      }

      • Discuss a situation in which it is appropriate to use the this
        • One common situation that requires the use of the this parameter occurs when you have parameters of the method with the same name as the instance variables of the class. Otherwise, if an instance variable is referenced directly by name within its class, this is understood.
      • Write a method called isEqual that returns a Boolean value. The method compares two integers for equality.

      /** method to compare to integers for equality */

      public static boolean isEqual(int x, int y)

      {

      return x == y;

      }

      • Discuss the public and private modifiers in context of methods and instance variables.
        • The modifiers public and private can both be used with methods and instance variables. Any instance variable can be labeled either public or private. The modifier public means that there are no restrictions on where the instance variable can be used.  The modifier private means that the instance variable cannot be accessed by name outside of the class definition.

      The modifiers public and private before a method definition have a similar meaning.  If the method is labeled public, there are no restrictions on its usage.  If the method is labeled private, the method can only be used in the definition of another method of the same class.

      Normal good programming practices require that all instance variables be private and typically most methods be public.

       

      • Create a class named Appointment that contains instance variables startTime, endTime, dayOfWeek (valid values are Sunday through Saturday), and a date which consists of a month, day and year. All times should be in military time, therefore it is appropriate to use integers to represent the time.  Create the appropriate accessor and mutator methods.

      public class Appointment

      {

      private int startTime;

      private int endTime;

      private String dayOfWeek;

      private String month;

      private int day;

      private int year;

      /** Mutator methods */

      public void setStartTime(int st)

      {

      /** valid range for military time is 0-2400 */

      if((st >= 0) && (st <= 2400))

      startTime = st;

      }

      public void setEndTime(int et)

      {

      /** valid range for military time is 0-2400 */

      if((et >= 0) && (et <= 2400))

      endTime = et;

      }

      public void setDayOfWeek(String dow)

      {

      /** Valid values are the strings Sunday Saturday */

      if(checkDayOfWeek(dow))

      dayOfWeek = dow;

      else

      System.out.println(Fatal error.invalid day of week value!);

      }

      public void setMonth(String m)

      {

      /** Valid values are strings January December */

      if(checkMonth(m))

      month = m;

      else

      System.out.println(Fatal errorinvalid month value!);

      }

      public void setDay(int d)

      {

      /** Valid days in a date are the integers 1 31 */

      if((d >= 1) && (d <= 31))

      day = d;

      }

      public void setYear(int y)

      {

      if(y >= 0)

      year = y;

      }

      /** Accessor methods */

      public int getStartTime()

      {

      return startTime;

      }

      public int getEndTime()

      {

      return endTime;

      }

      public String getDayOfWeek()

      {

      return dayOfWeek;

      }

      public String getMonth()

      {

      return month;

      }

      public int getDay()

      {

      return day;

      }

      public int getYear()

      {

      return year;

      }

      /** Facilitator methods */

      private boolean checkDayOfWeek(String d)

      {

      if(d.equalsIgnoreCase(Sunday) || d.equalsIgnoreCase(Monday) ||

      d.equalsIgnoreCase(Tuesday) || d.equalsIgnoreCase(Wednesday) ||

      d.equalsIgnoreCase(Thursday) || d.equalsIgnoreCase(Friday) ||

      d.equalsIgnoreCase(Saturday) || d.equalsIgnoreCase(Sunday))

      return true;

      else

      return false;

      }

      private boolean checkMonth(String month)

      {

      if(month.equalsIgnoreCase(January) || month.equalsIgnoreCase(February) ||

      month.equalsIgnoreCase(March) || month.equalsIgnoreCase(April) ||

      month.equalsIgnoreCase(May) || month.equalsIgnoreCase(June) ||

      month.equalsIgnoreCase(July) || month.equalsIgnoreCase(August) ||

      month.equalsIgnoreCase(September) ||

      month.equalsIgnoreCase(October) ||

      month.equalsIgnoreCase(November) || month.equalsIgnoreCase(December))

      return true;

      else

      return false;

      }

      }

       

      • Discuss the importance of accessor and mutator methods and how they apply to the abstraction concept.
        • It is considered good programming practice to make all instance variables private, but there are times when the values of the instance variables need to be accessed or updated.  Accessor methods allow you to obtain the data, while mutator methods allow you to change the data in a class object.  A mutator method can verify the validity of a value before updating the instance variable.
      • Write preconditions and postconditions for the power method you wrote in question #4.

      /** Precondition:  All instance variables of the calling object have values.

      Postcondition:  A positive integer value equal to x to the power n is returned.

      */

      • Add two constructors to the Appointment class created in question #9. Include a default constructor and a constructor to initialize an Appointment to suitable arguments.

      /** Class constructors */

      public Appointment()

      {

      startTime = 0;

      endTime = 0;

      dayOfWeek = ;

      month = ;

      day = 0;

      year = 0;

      }

       

      public Appointment(int st, int et, String dow, String m, int d, int y)

      {

      setStartTime(st);

      setEndTime(et);

      setDayOfWeek(dow);

      setMonth(m);

      setDay(d);

      setYear(y);

      }

       

      • Write a complete Java class that uses the console window to prompt the user for a sentence to parse. Use the StringTokenizer class to echo the tokens back to the console.

       

      import java.util.Scanner;

      import java.util.StringTokenizer;

       

      public class Parser

      {

      public static void main(String args[])

      {

      Scanner keyboard = new Scanner(System.in);

      System.out.print(Enter a sentence and Ill display each word you entered: );

      String sentence = keyboard.nextLine();

      //Parse the string into tokens and echo back to the user

      StringTokenizer tk = new StringTokenizer(sentence, );

      System.out.println(Here are the tokens: );

      while(tk.hasMoreTokens())

      {

      System.out.println(tk.nextToken());

      }

      }

      }

      • Write a Java class that represents a Student with instance variables name, id, and gpa. Include constructors, accessor, mutator and any facilitator methods you may need.

      public class Student

      {

      private String name;

      private String id;

      private double gpa;

       

      /** Constructors */

      public Student()

      {

      name = null;

      id = null;

      gpa = 0.0;

      }

      public Student(String n, String i, double g)

      {

      name = n;

      id = i;

      gpa = g;

      }

      /** Accessor methods */

      public String getName()

      {

      return name;

      }

      public String getID()

      {

      return id;

      }

      public double getGPA()

      {

      return gpa;

      }

       

      /** Mutator methods */

      public void setName(String n)

      {

      name = n;

      }

       

      public void setID(String i)

      {

      id = i;

      }

       

      public void setGPA(double g)

      {

      if((g >= 0) && (g <= 4))

      gpa = g;

      }

       

      /** Facilitator methods */

      public String toString()

      {

      return (name + + id + + gpa);

      }

      public boolean equals(Student s)

      {

      return ((name.equalsIgnoreCase(s.name)) && (id.equalsIgnoreCase(s.id)) &&

      (s.gpa == gpa));

      }

      }

       

      • Write a driver program to test your Student class created in question #14.

      public class StudentTest

      {

      public static void main(String args[])

      {

      Student firstStudent = new Student();

      Student secondStudent = new Student(Wally Wonders, xyz557, 2.54);

       

      System.out.println(First Student statistics: );

      System.out.println(firstStudent.getName() +

      + firstStudent.getID() + + firstStudent.getGPA());

      System.out.println(Second Student statistics: );

      System.out.println(secondStudent.getName() +

      + secondStudent.getID() + + secondStudent.getGPA());

       

      firstStudent.setName(Sharon Smith);

      firstStudent.setID(wyh886);

      firstStudent.setGPA(3.99);

       

      System.out.println(firstStudent);

      System.out.println(secondStudent);

       

      System.out.println(Comparing student objects, evalutes to +

      firstStudent.equals(secondStudent));

      }

      }

      • Rewrite the following method using the this

      public void setDay(int d)

      {

      //Valid days in a date are the integers 1 31

      if((d >= 1) && (d <= 31))

      day = d;

      }

      public boolean setDay(int day)

      {

      if((day >=1) && (day <= 31))

      {

      this.day = day;

      return true;

      }

      else

      {

      return false;

      }

      }

      Chapter 12
      UML and Patterns

      • nMultiple Choice
        • UML is a graphical language that is used for designing and documenting software created within the _____________ framework.
          • top-down programming
          • bottom-up programming
          • structured programming
          • object oriented programming
            • D
          • The first section of a UML class diagram specifies the:
            • class members
            • class name
            • class methods
            • class modifiers
              • B
            • The second section of a UML class diagram specifies the:
              • class members
              • class name
              • class methods
              • class modifiers
                • A
              • The third section of a UML class diagram specifies the:
                • class members
                • class name
                • class methods
                • class modifiers
                  • C
                • In a UML class diagram, the tilde (~) indicates:
                  • public access
                  • protected access
                  • private access
                  • package access
                    • D
                  • In a UML class diagram, the minus (-) indicates:
                    • public access
                    • protected access
                    • private access
                    • package access
                      • C
                    • In a UML class diagram, the plus (+) indicates:
                      • public access
                      • protected access
                      • private access
                      • package access
                        • A
                      • In a UML class diagram, the sharp (#) indicates:
                        • public access
                        • protected access
                        • private access
                        • package access
                          • B
                        • The arrowheads in an inheritance diagram point:
                          • North
                          • South
                          • East
                          • West
                            • A
                          • Any construct that allows you to cycle through all the items in a container is called:
                            • Pointer
                            • Instance variable
                            • Iterator
                            • All of the above
                              • C
                            • An example of a container is a/an:
                              • array
                              • vector
                              • linked list
                              • all of the above
                                • D
                              • nTrue/False
                                • Pseudocode is a mixture of programming language and English.
                                  • True
                                • UML was designed to be used with structured programming methodology.
                                  • False
                                • Class diagrams must contain all class members.
                                  • False
                                • A container is a class whose objects hold multiple pieces of data.
                                  • True
                                • Any construct that allows you to cycle through all the items in a container is an iterator.
                                  • True
                                • The Adaptor pattern transforms one class into a different class without changing the underlying class.
                                  • True
                                • In a quick sort algorithm, a value called a splitting value is an arbitrarily selected value from the array to sort.
                                  • True
                                • nShort Answer/Essay
                                  • Define pseudocode.
                                    • Pseudocode is a combination of English like statements and high-level programming statements used to describe a software component.
                                  • Draw a UML class diagram for a class that represents a sphere.
                                  • Draw a UML class diagram for a class that represents a savings account.
                                  • Draw a UML inheritance diagram for possible classes used to represent a bank account.

Write a review

Your Name:


Your Review: Note: HTML is not translated!

Rating: Bad           Good

Enter the code in the box below:



 

Once the order is placed, the order will be delivered to your email less than 24 hours, mostly within 4 hours. 

If you have questions, you can contact us here