Solutions to Exercises (Chapters 1 Through 4)

Solutions to Exercises (Chapters 1 Through 4)

Full file at

Solutions to Exercises (Chapters 1 through 4)

Dawn Ellis and Frank M. Carrano


Chapter 1 Exercises

Volatile is a property of primary memory whereas nonvolatile is a property of secondary memory. Primary memory is volatile because when the power to the computer system is interrupted, the contents of primary memory are lost. Secondary memory is nonvolatile because when the power to the computer system is interrupted, the contents in secondary memory remain.

Input devices: mouse, keyboard, scanner

Output devices: printer, monitor, and speakers

A memory address is a numerical reference to a physical byte within a computer systems primary memory. The designers of the computer system fix this address. The address cannot be changed, but the contents in which the address refers can be changed.

A binary numeral is a sequence of bits. A bit can have the value of either a 1 or a 0.

10110 =

11001

10101

Java source code is compiled into a file containing byte code when is interpreted by the Java Virtual Machine. The Java Virtual Machine is designed to run on many different operating systems, making a Java a platform independent language.

Chapter 2 Exercises

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

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.

System.out.println("To be or");

System.out.println("not to be");

System.out.println("To be or not to be");

The // notation represents a single line comment and the /* */ notation represents a comment that spans several lines.

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.

*/

double

double salesTaxRate;

salesTaxRate = .0925;

double salesTax = 19.95 * salesTaxRate;

The value of salesTax is 1.845375.

System.out.println("Sales tax:" + salesTax);

Length: 5.0

Width: 8.5

Area: 42.5

‘a’, 7.5, and 10

final double salesTaxRate = .075;

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.

next()

System.out.print("Enter the sales tax rate: ");

Scanner keyboard = new Scanner(System.in);

double rate = keyboard.nextDouble();

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("*");

}

}

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);

}

}

Chapter 3 Exercises

a.18

b.15

c.10

d.-1

e.0

a.4.0

b.4

c.5.33

d.5

int result = x % 2;

If result is not equal to zero, then x is an odd integer.

result = x * x * x;

result = Math.pow(x, 3);

result = Math.cbrt(x);

a.legal

b.illegal

c.legal

d.legal

e.illegal

f.illegal

a.double

b.double

c.long

d.float

e.double

f.long

m = 3

d = 17

y = 2020

f = d + [(26 * (m + 1)) / 10] + y + [y / 4] + 6[y / 100] + [y /400]

f = 17 + [(26 * (3 + 1))/ 10] + 2020 + [2020/4] + 6[2020/100] + [2020 / 400]

f = 2677

w = f % 7

w = 3

3/17/2020 falls on a Tuesday.

a.Math.sqrt(64)

b.Math.abs(-17)

c.Math.exp(-1)

d.Math.cbrt(2)

e.Math.pow(6, 2)

public class MakeTable

{

public static void main(String[] args)

{

System.out.println("X\tY\tMin(x, y)");

System.out.println("10\t5\t" + Math.min(10, 5));

System.out.println("23\t13\t" + Math.min(23, 13));

System.out.println("100\t100\t" + Math.min(100, 100));

System.out.println("-1\t-5\t" + Math.min(-1, -5));

System.out.println("0\t-8\t" + Math.min(0, -8));

}

}

import java.util.*;

public class TempConverter

{

public static void main(String[] args)

{

Scanner keyboard = new Scanner(System.in);

double celsiusTemp, fahrenheitTemp;

System.out.print("Enter the temperature to convert: ");

fahrenheitTemp = keyboard.nextDouble();

celsiusTemp = (5.0/9.0) * (fahrenheitTemp - 32.0);

System.out.print(fahrenheitTemp + " degrees Fahrenheit equals ");

System.out.println(celsiusTemp + " degrees Celsius");

}

}

import java.util.Scanner;

public class Ticket

{

public static void main(String[] args)

{

// describe problem

System.out.println("Is your ticket a winner " +

"(a multiple of 6)?\n");

// get input

Scanner keyboard = new Scanner(System.in);

System.out.print("Enter your ticket number: ");

int ticket = keyboard.nextInt();

// compute winning ticket numbers

int winner = ticket % 6;

// display results

System.out.println();

System.out.println("The ticket number " + ticket +

" has a remainder of " + winner + ".");

System.out.println();

System.out.println("If the remainder is 0, you are a winner!");

} // end main

} // end Ticket

import java.util.Scanner;

public class Multiple

{

public static void main(String[] args)

{

// describe problem

System.out.println("This program determines whether one " +

"number is a multiple of a second number.\n");

// get input

Scanner keyboard = new Scanner(System.in);

System.out.print("Enter your first integer number: ");

int first = keyboard.nextInt();

System.out.print("Enter your second integer number: ");

int second = keyboard.nextInt();

// compute winning ticket numbers

int multiple = first % second;

// display results

System.out.println();

System.out.println("The first number is " + first + ".");

System.out.println("The first second is " + second + ".");

System.out.println("The remainder is " + multiple + ".");

System.out.println();

System.out.println("If the remainder is 0, the first number " +

"is a multiple of the second number.");

} // end main

} // end Multiple

Chapter 4 Exercises

Instantiating an object is simply the process of creating a new instance of a class type. This is usually accomplished using the new operator.

A reference variable is a variable of a class type. A reference variable contains the memory address of the object it refers to. An alias is simply an object with multiple references.

System.out.println("Name\tAddress\tCity\tState\tZip");

The receiving object is one.

54

The first statement only imports the class Date from the java.util package. The second statement imports the entire java.util package.

a.Date aDateObject = new Date();

b.String date = aDateObject.toString();

String month = date.substring(3, 6);

String day = date.substring(6, 8);

String year = date.substring(19);

String datePortion = month + " " + day + ", " + year;

c.String date = aDateObject.toString();

String time = aDateObject.substring(8, 19);

You write the desired value of the BigDecimal object as a string to preserve its accuracy.

a.BigDecimal(String val, MathContext mc)

b.floatValue()

c.movePointLeft(int n)

The equals method is provided not only for the wrapper classes in Java but for other classes as well. The equals method allows one to compare two objects to determine if their data components are equivalent.

Wrapper Class / MAX_VALUE / MIN_VALUE
Byte / /
Character / /uFFFF / /u0000
Double / /
Float / /
Integer / /
Long / /
Short / /

The showConfirmDialog method presents a dialog box that asks a confirming question such as yes, no, or cancel. The showInputDialog method presents a dialog box that accepts input. The showMessageDialog method presents a dialog box that relays a message.

A pseudorandom, uniformly distributed, double value between 0.0 and 1.0 will be returned.

17

17

23

The input dialog reads all data provided by the user as a string. If we were collecting numeric data for computations, the string must be parsed into the correct format before being used in any computation, otherwise, we would get unexpected results. For example, suppose a dialog box was provided to enter two floating-point values to be summed. When the plus sign appears between two strings, concatenation occurs.

import java.util.Scanner;

public class StringDemoEx16

{

public static void main(String[] args)

{

Scanner keyboard = new Scanner(System.in);

System.out.print("Please type your first name, a space, and " +

"your last name: ");

String firstName = keyboard.next();

String lastName = keyboard.next();

int nameLength = firstName.length() + lastName.length();

char firstInitial = firstName.charAt(0);

char lastInitial = lastName.charAt(0);

String initials = firstInitial + ". " + lastInitial + ".";

System.out.println("Hi, " + firstName + " " + lastName +

".\nYour name contains " +

nameLength + " characters.");

System.out.println("Your first name is " + firstName + ".");

System.out.println("Your last name is " + lastName + ".");

System.out.println("Your initials are " + initials);

} // end main

} // end StringDemoEx16

import java.util.Scanner;

public class UpperCase

{

public static void main(String[] args)

{

Scanner keyboard = new Scanner(System.in);

//Collect the information

System.out.println("Please type your name: ");

String name = keyboard.nextLine();

name = name.trim();

System.out.println("Please type your street address: ");

String streetAddress = keyboard.nextLine();

streetAddress = streetAddress.trim();

System.out.println("Please type your city: ");

String city = keyboard.nextLine();

city = city.trim();

System.out.println("Please type your state: ");

String state = keyboard.nextLine();

state = state.trim();

System.out.println("Please type your zip code: ");

String zipCode = keyboard.nextLine();

zipCode = zipCode.trim();

//Convert the information

name = name.toUpperCase();

streetAddress = streetAddress.toUpperCase();

city = city.toUpperCase();

state = state.toUpperCase();

//Display the information

System.out.println(name);

System.out.println(streetAddress);

System.out.println(city + ", " + state + " " + zipCode);

}

}

public class Tree

{

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(" \" \"");

} // end main

} // end Tree

import javax.swing.JOptionPane;

import java.util.Scanner;

public class Extractor

{

public static void main(String[] args)

{

String extractionString = JOptionPane.showInputDialog(

"Enter a string containing an email address:");

//Scan the string for spaces; the 5th space should be the

//start of the email address

Scanner scan = new Scanner(extractionString);

scan.useDelimiter("\\s");

String email = scan.next();

email = scan.next();

email = scan.next();

email = scan.next();

email = scan.next();

JOptionPane.showMessageDialog(null, "Email address: " + email);

System.exit(0);

}

}

import java.util.Random;

import java.util.Scanner;

import java.text.DecimalFormat;

public class MyRandomGenerator

{

public static void main(String[] args)

{

Scanner keyboard = new Scanner(System.in);

System.out.println("Enter a seed for the random number " +

"generator: ");

long seed = keyboard.nextLong();

//Seed the generator

Random generator = new Random(seed);

//Generate 5 random numbers

double firstNumber = generator.nextDouble();

double secondNumber = generator.nextDouble();

double thirdNumber = generator.nextDouble();

double fourthNumber = generator.nextDouble();

double fifthNumber = generator.nextDouble();

DecimalFormat formatter = new DecimalFormat("0.0000");

System.out.println("Five random numbers in the range of 0.0 " +

"to 1.0:");

System.out.println(formatter.format(firstNumber));

System.out.println(formatter.format(secondNumber));

System.out.println(formatter.format(thirdNumber));

System.out.println(formatter.format(fourthNumber));

System.out.println(formatter.format(fifthNumber));

}

}