Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Chapter 2 Exercises 1. What are the three categories of Java identifiers? Give an example of each category. Identifiers fall into one of three categories: identifiers that we invent, identifiers that another programmer has chosen, and identifiers that are part of the Java language. Identifiers we invent: sample, args Identifiers invented by others: System, out, println, main Identifiers included in the Java language: public, static, void, class 2. Is programming style, such as indentation, a requirement of the Java language? Programming style is not a requirement of the Java language. Although it is good to develop a basic style that uses white space, brace alignment and indentation to make the program easy for the human reader. 3. Write statements to display the following phrase exactly as shown: To be or not to be System.out.println("To be or"); System.out.println("not to be"); 4. Write statements to display the following phrase in one line of output. To be or not to be System.out.println("To be or not to be"); 5. What is the difference between the // comment notation and the /* */ comment notation? The // notation represents a single line comment and the /* */ notation represents a comment that spans several lines. 6. What is the purpose of the Javadoc utility? Give an example of a Javadoc utility comment. The Javadoc utility produces an HTML document that describes the classes and methods that use the Javadoc comment. /** This is a javadoc comment. It can span several lines. */ 7. What primitive data type would you use to represent the most precise real number? double 8. Write a statement to declare a variable to represent the sales tax percentage. Write a second statement to assign the variable the value of 9.25%. Then write a statement that computes the sales tax on a purchase of $19.95 and stores the result in a properly declared variable called salesTax. What is the value of salesTax? double salesTaxRate; salesTaxRate = .0925; double salesTax = 19.95 * salesTaxRate; The value of salesTax is 1.845375. 9. Write a statement that displays the label “Sales tax:” and appends the calculated value from the previous exercise to the end of the label. System.out.println("Sales tax:" + salesTax); 10. When the following sequence of statements executes, what is displayed? double length = 5.0; double width = 8.5; System.out.println("Length: " + length); System.out.println("Width: " + width); System.out.print("Area: "); System.out.println(length * width); Length: 5.0 Width: 8.5 Area: 42.5 11. Which of the following are literals? count, ‘a’, 7.5, letter, 10, salesTax ‘a’, 7.5, 12. and 10 Write a statement to declare a named constant for a sales tax rate of 7.5%. final double salesTaxRate = .075; 13. Why is it good practice to use named constants, as opposed to using literals? Named constants not only make a program more readable, but it also drastically reduces the chance of an error in the value of the constant that could be introduced if the literal value is used instead. 14. Consult the documentation of the Java class library for the Scanner class. What method would you use to read a string from a Scanner object? next() 15. Write a statement that asks the user to enter the sales tax rate. Write statements to create a Scanner object, and then use it to read and store the rate as a real number. System.out.print("Enter the sales tax rate: "); Scanner keyboard = new Scanner(System.in); double rate = keyboard.nextDouble(); 16. Write an application that prints the following pattern: * ** *** **** ***** ***** **** *** ** * public class Pattern { public static void main(String[] args) { System.out.println("*"); System.out.println("**"); System.out.println("***"); System.out.println("****"); System.out.println("*****"); System.out.println("*****"); System.out.println("****"); System.out.println("***"); System.out.println("**"); System.out.println("*"); } } 17. Write an application that prompts for your name, address, birthday and hobby. Display each on separate lines, properly labeled. import java.util.*; /* This program prompts for your name, address, birthday and hobby, then displays each on separate lines, properly labeled. */ public class AboutYou { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String name, streetAddress, city, state, zip, birthday, hobby; //Collect all the inputs System.out.print("Enter your name: "); name = keyboard.nextLine(); System.out.print("Enter your street address: "); streetAddress = keyboard.nextLine(); System.out.print("Enter your city: "); city = keyboard.nextLine(); System.out.print("Enter your state: "); state = keyboard.nextLine(); System.out.print("Enter your zip: "); zip = keyboard.nextLine(); System.out.print("Enter your birthday: "); birthday = keyboard.nextLine(); System.out.print("Enter your favorite hobby: "); hobby = keyboard.nextLine(); } } //Display the inputs to the user System.out.println(); System.out.println("Name: " + name); System.out.println("Address: " + streetAddress); System.out.println("City: " + city); System.out.println("State: " + state); System.out.println("Zip: " + zip); System.out.println("Birthday: " + birthday); System.out.println("Hobby: " + hobby);