1.

R1. Number Types

Numbers are essential to computing. In Java there are two fundamental numeric types: integers and floating-point numbers. Integers have no fractional part, whereas floating-point numbers do. Variables of either type are represented by user-specified names. Space in the computer's memory is associated with the name so that the variable acts like a container for the numeric data.

Declaring a variable reserves space in memory and associates a specified name with that space. When a variable is declared, it contains whatever value is left over from the previous use of the memory space. Initializing a variable sets it to a specific value. If no specific value is known, it's a good idea to initialize a new variable to zero in order to avoid having it contain a random value.

type

name

= value;


int

identifier;


/* Variable declared but not initialized */

int

identifier

= 0;

/* Variable declared and initialized to zero */

double

identifier

= value;

/* Variable declared and initialized to another value*/


Correct the errors in the following variable declarations:

1) double 3.14159;
2) int;
3) double = 314.00
4) int double_value = 314.00;
5) int_value 314;
6) double working_value 314;


Answer:


2.

When declaring a variable, you must also give it a name. A valid name is made up of characters from the set:

ABCDEFGHIJKLMNOPQRSTUVWXYZ

upper case letters

abcdefghijklmnopqrstuvwxyz

lower case letters

0123456789

digits

_

the underscore character


Actually, Java permits you to use other alphabetical characters, such as Ä and ç in variable names as well, but these characters may cause difficulty if you want to move your source code to other systems. It is best to stick to Roman letters without accents.

A variable name cannot begin with a digit, and the name must be unique. Neither you nor the compiler would be able to distinguish between variables with the same name. For some simple cases, using single letters such as i, j, k is enough, but as programs grow larger this becomes less informative. Like the name for a product or service, a variable name should do two things:

   - be unique
   - express the purpose
   
A good variable name is descriptive enough to indicate to a human reader how a variable is being used, for example countMessages,
userPreference, or customerName.
   
Complete the following table by renaming the bad variable names and providing descriptions.


Bad Variable Name

Variable Renamed

Description

double current profit;

1)______________

2)______________

3)______________

4)______________

counts products sold this month

double %increase;

5)______________

6)______________

7)______________

8)______________

dollars earned this month



Answer:


3.

R2. Constants
   
Like variables, any numeric constants used by your program should also have informative names, for example: final int FLASH_POINT_PAPER = 451 or final int BOILING_POINT_WATER = 212. Using named constants transforms statements such as int y = 451 - x + 212 into the more intelligible int y = FLASH_POINT_PAPER - x +
     BOILING_POINT_WATER.
   
Give definitions for each of the constant descriptions listed below.

Constant Definition

Description

1)______________

Number of days in a week

2)______________

Number of weeks in a year

3)______________

Minimum wage per hour



Answer:


4.

R3. Assignment

Values on both sides of an assignment (=) operator should be checked to verify that the data received matches the data type of the variable to which it will be assigned. Fix the right-hand sides of the following assignments so that they are the correct type for the variable on the left side.

int x = Math.sqrt(4);

1)_____________

double y = "3";

2)_____________

String z = 3.14;

3)_____________



Answer:


5.

What is wrong with each of these assignments?

Statement

Error

a + 2 = 3;

1)__________

Math.PI = 3;

2)__________

x == x + 1;

3)__________



Answer:


6.

Computations with floating-point numbers have finite precision. What values are assigned in the following assignments? in:

Statement

Value

double x = Math.pow(10, 20) + 1;

1)_________

double y = x - 1;

2)_________

double z = x - y;;

3)_________



Answer:


7.

P1. Programming with Assignments

Enhance the Purse class from the book so that pennies can be added to the purse. Assume an instance field

private int pennies;

has been added to the Purse class. Supply an appropriate method, and be sure to comment it.


Answer:


8.

To compute the new total, you will need to multiply the number of pennies by 0.01. Supply an appropriate constant definition. The constant should be accessible only to methods in the Purse class.


Answer:


9.

Change the getTotal method to add the pennies to the total.


Answer:


10.

Supply a class whose main method tests the enhancement you just implemented.


Answer:


11.

R4. Arithmetic

Recalling what you've learned about integers and floating-point values, what value is assigned to x by each of the following?

int x = 6 / 3;

1)_____________

int x = 7 / 3;

2)_____________

double x = 7 / 3;

3)_____________

int x = 7 % 3;

4)_____________

int x = 6 % 3;

5)_____________

int x = 999 / 1000;

6)_____________

double x = 999.0 / 1000.0;

7)_____________

int x = (int)(999 / 1000.00);

8)_____________



Answer:


12.

Translate the following algebraic expressions into Java:

y = x + 1/2

1)____________

y = x2 + 2x + 1

2)____________

    X

y = ---

   1-x


3)____________



Answer:


13.

R5. Strings
Many programs manipulate not just numbers but also text. Java stores text in string variables. They are declared and assigned in a manner similar to a numeric variable. The sequence may have any length. A string may even have a length of zero. Such a string is called an empty string, and it is denoted as "". For example,

String name = "Joan Wilson";

assigns the 11 characters enclosed in quotes to the variable name.

Given:

   String firstName = "Joan";
   String lastName = "Wilson";
   String name;

1) What value will name contain after

    name = firstName + lastName;

2) How could the preceding be revised to give a more readable result?


Answer:


14.

What will be the resulting substring in the following examples?

string name = "John Smith"

string firstName = name.substring(0, 4);

1)____________

string name = "John Smith"

string lastName = name.substring(5, 6);

2)____________

string name = "Jane Smith"

string name2 = name.substring(1, 11);

3)____________



Answer:


15.

P2. String Programming

Using substring and concatenation, give a program containing a sequence of commands that will extract characters from inputString = "The quick brown fox jumps over the lazy dog" to make outputString = "Tempus fugit". Then print outputString.


Answer:


16.

The number 3 and the string "3" are completely different values. But for various purposes, numeric types sometimes need to be used as strings, and for calculation purposes, strings that contain numbers need to be used as numbers.

In Java, it is very easy to convert a number to a string. Simply concatenate with the empty string "". For example, if x has the value 3, then "" + x is the string "3".

Consider the class

class SalesActivity
{
   . . .
   private int year;
   private double percentIncrease;
   private String article;
}

Write a method String getDescription to return a sentence of the form "In ..., there was a net increase of ...% in sales of ...".

For example, if the settings of the fields are:

year: 1996
increase: 14.7
what: widgets

then return the string

"In 1996, there was a 14.7% increase in sales of widgets."


Answer:


17.

Using JOptionPane.showInputDialog, ask the user to supply two integers a and b. Then print out the values a / b and a % b.

Note that you need to use the Integer.parseInt method to convert the input strings to integer values.


Answer: