A guide to programming in java chapter 4 critical thinking
- a) List four legal identifier names
. b) List four illegal identifier names and explain why each is illegal.
- a) In two statements, declare a variable named numBeads and assign it the value 5.
- b) In one statement, declare a variable named numBeads and assign it the value 5.
- a) What is the final value of yourNumber after the last statement executes?
intmyNumber = 5;
intyourNumber = 4;
myNumber = yourNumber * 2;
yourNumber = myNumber + 5;
- b) What is the final value of yourNumber after the last statement executes?
intyourNumber = 4;
myNumber = yourNumber + 7;
yourNumber = myNumber;
- Determine the appropriate data type for each of the following values:
- a) the number of basketballs in a department store.
- b) the price of a basketball.
- c) the number of players on a basketball team.
- d) the average age of the players on a basketball team.
- e) whether a basketball player has received a jersey or not.
- f) the first initial of a basketball player’s first name.
- a) What is the difference between a primitive data type and an abstract data type?
- b) What is the difference between a class and an object?
- Assume a class named Team defines a sports team.
- a) Methods define the actions in a class and typically include action words in their name. For example, getTeamName is a method name that returns the name of the team. List three more possible method names for the Team class
- b) List three possible object names of type Team..
- The java.util package contains a class named R ando m. Write a statement that makes the Random class accessible to an application.
- What is the value of each of the following expressions?
- a) 5 + 7 – 3
- b) 10 * 2 – 3
- c) 10 * (2 – 3)
- d) 8 – 3 * 2
- e) 10 / 5 * 4
- f) 10 / 2 + 3
- g) 6 % 3 + 4
- h) 12 % 5 * 3
- i) 12 % (5 * 3)
- What is the result of the following expression when x is 2005? When x is 1776? When x is 39?
- Write each equation as a valid Java expression: a) (geometry) b) (business) c) (geometry) d) (geometry) e) (algebra) f) (physics) g) (business)
- Using the following declarations, rewrite the statements to include the appropriate type casting, rounding where necessary. If type casting is not necessary, explain why: int j = 5; double k = 1.6; int y; double z; a) y = j * k; b) z = j * k
- a) y = j * k;
- b) z = j * k;
- c) z = k * k;
- d) j = k; e) k = j;
- f) y = j + 3;
- Compare the way the / and % operators perform to the effects of type casting.
- Rewrite the statements below using the appropriate assignment operator: a) total = total + 10; b) numStones = numStones – 1; c) days = days % 24; d) price = price * 1.2;
- Determine if each of the following are better represented by a variable or a constant and then write declarations using appropriate data types and descriptive identifiers: a) the number of votes received by an election candidate b) the percentage of votes won by a candidate c) the first, middle, and last initials of an election candidate d) the year of the election d) Values typed by the user cannot be used in an application. e) The Scanner class is part of a Java package. f) The + operator has higher precedence than the – operator. g) The – operator has lower precedence than the % operator. h) byte is a keyword. i) The identifiers apple and Apple are considered the same in Java. j) Errors that violate the rules of Java are called semantic errors
- Determine if each of the following segments of code contain a syntax error, logic error, or runtime error. Explain. a) duble salary; b) intnumHats c) length == 12; d) int test1 = 90; int test2 = 85; double avg; avg = test1 + test2 / 2; e) double x = 12; double y = 0; double z; z = x / y; f) double payCheck = 120.00; NumberFormat money = NumberFormat.getPercentInstance(); System.out.println(money.format(payCheck);
- Determine if each of the following are true or false. If false, explain why.
- a) An identifier can contain spaces.-
- b) Data of type double is sometimes referred to as floating point-
. c) An abstract data type is also called a primitive data type.-