Java: Chapter 3.

Declare a variable named myMenu suitable for holding references to Menu objects .

Menu myMenu ;

Declare a reference variable of type File named myFile .

File myFile;

Suppose a reference variable of type Double called myDouble has already been declared . There is also a double variable x that has been declared and initialized . Create an object of type Double with the initial value of x and assign it to the reference v

Double myDouble2 = new Double(x);
myDouble = myDouble2;

Suppose a reference variable of type String called myString has already been declared . Create an object of type String and assign it to the reference variable myString .

String myString2 = new String();
myString = myString2;

Suppose a reference variable of type File called myFile has already been declared . Create an object of type File with the initial file name input.dat and assign it to the reference variable myFile .

myFile = new File("input.dat");

One of the constructors for PrintStream class has a single OutputStream argument . Assume that dos is a variable that references an OutputStream object . Create a PrintStream object using dos and assign the resulting reference to ps, a PrintStream variabl

ps = new PrintStream(dos);

Assume that arithmetic is a reference to an object that has a method named add , that accepts two int arguments and returns their sum .
Two int variables , euroSales and asiaSales , have already been declared and initialized . Another int variable , euras

eurasiaSales = arithmetic.add(euroSales, asiaSales);

Assume that dateManager is a reference to an object that has a method named printTodaysDate , that accepts no arguments and returns no value . Write a statement that calls printTodaysDate .

dateManager.printTodaysDate();

Assume that dataTransmitter is a variable that refers to an object that provides a method , sendSignal that takes no arguments . Write the code for invoking this method .

dataTransmitter.sendSignal();

Assume that dataTransmitter is a variable that refers to an object that provides a method , named sendObject . There is one argument for this method , a reference to a Customer object .
Assume that there is a reference to an object of type Customer , in a

dataTransmitter.sendObject(john_doe);

Assume that dataTransmitter is a variable that refers to an object that provides a method , named sendNumber . There is one int argument for this method . Invoke this method and use the number 5 as an argument .

dataTransmitter.sendNumber(5);

Assume that dataTransmitter is a variable that refers to an object that provides a method , named sendDouble . There is one double argument for this method .
Assume that a double variable called x has already been declared and initialized to some value .

dataTransmitter.sendDouble(x);

Assume that dataTransmitter is a variable that refers to an object that provides a method , named sendTwo . There are two arguments for this method : a double and an int . Invoke the method with the double value of 15.955 and the int valueof 133 .

dataTransmitter.sendTwo(15.955,133);

Assume that logger is a reference to an object that has a method named printErrorDescription , that accepts one int argument and returns no value .
Write a statement that invokes the method printErrorDescription , passing it the value 14

logger.printErrorDescription(14);

Assume that logger is a reference to an object that has a method named printLarger that accepts two int arguments and returns no value .
Two int variables , sales1 and sales2 , have already been declared and initialized .
Write a statement that calls prin

logger.printLarger(sales1, sales2);

Assume there is a class AirConditioner that supports the following behaviors : turning the air conditioner on and off. The following methods are provide this behavior : turnOn and turnOff . Both methods take no arguments and return no value .
Assume there

myAC.turnOn();

Assume there is a class AirConditioner that supports the following behaviors : turning the air conditioner on and off. The following methods are provide this behavior : turnOn and turnOff . Both methods take no arguments and return no value .
Assume there

myAC.turnOff();

Assume there is a class AirConditioner that supports the following behaviors : turning the air conditioner on and off, and setting the desired temperature. The following methods provide this behavior : turnOn and turnOff , and setTemp , which accepts an i

myAC.setTemp(72);

Assume there is a class AirConditioner that supports the following behaviors : turning the air conditioner on and off, setting the desired temperature, and reporting the previously set temperature. The following methods provide this behavior : turnOn and

currentTemp = myAC.getTemp();

Assume there is a class AirConditioner that supports the following behaviors : turning the air conditioner on and off, and checking if the air conditioner is on or off. The following methods provide this behavior : turnOn and turnOff , setTemp , and isOn

status = myAC.isOn();

Assume there is a class AirConditioner that supports the following behaviors : turning the air conditioner on and off. The following methods provide these behaviors : turnOn and turnOff . Both methods accept no arguments and return no value .
Assume there

officeAC = new AirConditioner();
officeAC.turnOn();

Assume there is a class AirConditioner that supports the following behaviors : turning the air conditioner on and off, and setting the desired temperature. The following methods provide these behaviors : turnOn and turnOff , which accept no arguments and

officeAC = new AirConditioner();
officeAC.turnOn();
officeAC.setTemp(69);

rite the declaration of a String variable named title .

String title = new String();

Declare a String variable named mailingAddress .

String mailingAddress = new String();

Write the declaration of three String variables named win , place , and show .

String win = new String();
String place = new String();
String show = new String();

rite the declaration of a String variable named foreground and initialize it to "black" .

String foreground = new String("black");

Write the declaration of two String variable named background and selectionColor and initialize them to "white" and "blue" respectively.

String background = new String ( "white");
String selectionColor = new String ( "blue" );

Declare a String variable named empty , and initialize it to the empty String .

String empty = new String();

Declare a String variable named oneSpace , and initialize it to a String consisting of a single space.

String oneSpace = new String (" ");

Assume that the String variable named foreground has already been declared . Assign it the value "red" .

foreground = "red";

Assume that theString variable named text has already been declared . Assign to it the empty string .

text = "";

There are two String variables , s1 and s2, that have already been declared and initialized . Write some code that exchanges their values .
Declare any other variables as necessary.

String temp = new String();
temp = s1;
s1 = s2;
s2 = temp;

Given a String variable address , write a String expression consisting of the string "http://" concatenated with the variable 's String value . So, if the variable refers to "www.turingscraft.com", the value of the expression would be "http://www.turingsc

http://" + address

Write an expression that concatenates the String variable suffix onto the end of the String variable prefix .

prefix + suffix

Given three String variables that have been declared and given values , gold, silver, and bronze, write an expression whose value is the values of each these variables joined by a newline character . So if gold, silver, and bronze, had the values "Arakawa

gold + "\n" + silver + "\n" + bronze

Given three String variables that have been declared and given values , firstName, middleName, and lastName, write an expression whose value is the values of each these variables joined by a single space. So if firstName, middleName, and lastName, had the

firstName + " " + middleName + " " + lastName

Given a String variable word , write a String expression that parenthesizes the value of word . So, if word contains "sadly", the value of the expression would be the String "(sadly)

(" + word + ")

Given three int variables that have been declared and given values , areaCode, exchange, and lastFour, write a String expression whose value is the String equivalent of each these variables joined by a single hyphen (-) So if areaCode, exchange, and lastF

areaCode + "-" + exchange + "-" + lastFour

Given four int variables that have been declared and given values , octet1, octet2, octet3, and octet4, write a String expression whose value is the String equivalent of each these variables joined by a single period (.) So if octet1, octet2, octet3, and

octet1 + "." + octet2 + "." + octet3 + "." + octet4

Given a String variable named sentence that has been initialized , write an expression whose value is the number of characters in the String referred to by sentence .

sentence.length()

Write an expression whose value is the number of characters in the following String :
"CRAZY!\n\\\t\\\\\\\\\\n. . . .\\ \\\r\007'\\'\"TOOMUCH!"
Suggestion: copy/paste the above String into your code-- it's too crazy to try to copy it by typing.

CRAZY!\n\\\t\\\\\\\\\\n. . . .\\ \\\r\007'\\'\"TOOMUCH!".length()

Given the String variable str, write an expression that evaluates to the character at index 0 of str.

str.charAt(0)

Assume that name is a variable of type String that has been assigned a value . Write an expression whose value is the first character of the value of name . So if the value of name were "Smith" the expression 's value would be 'S'.

name.charAt(0)

Assume that name is a variable of type String that has been assigned a value . Write an expression whose value is the second character of the value of name . So if the value of name were "Smith" the expression 's value would be 'm'.

name.charAt(1)

Given the String variable name , write an expression that evaluates to the third character of name .

name.charAt(2)

Given a String variable named sentence that has been initialized , write an expression whose value is the index of the very last character in the String referred to by sentence .

sentence.length() - 1

Given a String variable named sentence that has been initialized , write an expression whose value is the the very last character in the String referred to by sentence .

sentence.charAt(sentence.length() - 1)

Assume that name is a variable of type String that has been assigned a value . Write an expression whose value is the last character of the value of name . So if the value of name were "Blair" the expression 's value would be 'r'.

name.charAt(name.length() - 1)

Assume that word is a variable of type String that has been assigned a value . Write an expression whose value is a String consisting of the first three characters of the value of word . So if the value of word were "dystopia" the expression 's value woul

word.substring(0,3)

Assume that name is a variable of type String that has been assigned a value . Write an expression whose value is a String containing the first character of the value of name . So if the value of name were "Smith" the expression 's value would be "S".

name.substring(0,1)

Assume that name is a variable of type String that has been assigned a value . Write an expression whose value is a String containing the second character of the value of name . So if the value of name were "Smith" the expression 's value would be "m".

name.substring(1,2)

Write an expression that results in a String consisting of the third through tenth characters of the String s .

s.substring(2,10)

Assume that name is a variable of type String that has been assigned a value . Write an expression whose value is a String containing the last character of the value of name . So if the value of name were "Smith" the expression 's value would be "h".

name.substring(name.length() - 1,name.length())

Assume that word is a variable of type String that has been assigned a value . Write an expression whose value is a String consisting of the last three characters of the value of word . So if the value of word were "biggest" the expression 's value would

word.substring(word.length() - 3,word.length())

Assume that given , middle and family are three variables of type String that have been assigned values . Write an expression whose value is a String consisting of the first character of given followed by a period followed by the first character of middle

given.charAt(0) + "." + middle.charAt(0) + "." + family.charAt(0) + ".

Given the String variable address , write an expression that returns the position of the first occurrence of the String "Avenue" in address .

address.indexOf("Avenue")

Assume that sentence is a variable of type String that has been assigned a value . Assume furthermore that this value is a String consisting of words separated by single space characters with a period at the end. For example: "This is a possible value of

int start = sentence.indexOf(" ") + 1;
int end = sentence.indexOf(" ", start+1);
secondWord = sentence.substring(start,end);

Write a sequence of statements that finds the first comma in the String line , and assigns to the variable clause the portion of line up to, but not including the comma. You may assume that an int variable pos , as well as the variables line and clause ,

pos = line.indexOf(',');
clause = line.substring(0,pos);

Assume that sentence is a variable of type String that has been assigned a value . Assume furthermore that this value is a String consisting of words separated by single space characters with a period at the end. For example: "This is a possible value of

firstWord = sentence.substring(0,sentence.indexOf(' '));

Write a statement that reads a word from standard input into firstWord. Assume that firstWord. has already been declared as a String variable . Assume also that stdin is a variable that references a Scanner object associated with standard input.

firstWord = stdin.next();

Write a statement that reads an integer value from standard input into val. Assume that val has already been declared as an int variable . Assume also that stdin is a variable that references a Scanner object associated with standard input.

val = stdin.nextInt();

Write a statement that reads a floating point value from standard input into temperature. Assume that temperature. has already been declared as an double variable . Assume also that stdin is a variable that references a Scanner object associated with stan

temperature = stdin.nextDouble();

Assume that name has been declared suitably for storing names (like "Misha", "Emily" and "Sofia"). Assume also that stdin is a variable that references a Scanner object associated with standard input Write some code that reads a value into name then print

name = stdin.next();
System.out.println("Greetings, " + name);

Assume that name has been declared suitably for storing names (like "Amy", "Fritz" and "Moustafa"). Assume also that stdin is a variable that references a Scanner object associated with standard input. Write some code that reads a value into name then pri

name = stdin.next();
System.out.println("Greetings, " + name + "!!!");

Assume that name and age have been declared suitably for storing names (like "Abdullah", "Alexandra" and "Zoe") and ages respectively. Assume also that stdin is a variable that references a Scanner object associated with standard input. Write some code th

name = stdin.next();
age = stdin.nextInt();
System.out.println("The age of " + name + " is " + age);

Three business partners are forming a company whose name will be of the form "Name1, Name2 and Name3". However, they can't agree whose name should be first, second or last. Help them out by writing code that reads in their three names and prints each poss

name1 = stdin.next();
name2 = stdin.next();
name3 = stdin.next();
System.out.println(name1 + ", " + name2 + " and " + name3);
System.out.println(name1 + ", " + name3 + " and " + name2);
System.out.println(name2 + ", " + name1 + " and " + name3);
System.ou

Given an int variable datum that has already been declared , write a few statements that read an integer value from standard input into this variable .

Scanner stdin = new Scanner( System.in);
datum = stdin.nextInt();

Assume the availability of class named DateManager that provides a static method , printTodaysDate , that accepts no arguments and returns no value . Write a statement that calls printTodaysDate .

DateManager.printTodaysDate();

Assume the availability of class named DataTransmitter that provides a static method , sendSignal that takes no arguments . Write the code for invoking this method .

DataTransmitter.sendSignal();

Write the code for invoking a static method named sendNumber , provided by the DataTransmitter class . There is one int argument for this method . Invoke this method and use the number 5 as an argument .

DataTransmitter.sendNumber(5);

rite the code for invoking a static method named sendDouble , provided by the DataTransmitter class . There is one double argument for this method .
Assume that a double variable called x has already been declared and initialized to some value . Use this

DataTransmitter.sendDouble(x);

Write the code for invoking a static method named sendTwo , provided by the DataTransmitter class . There are two arguments for this method : a double and an int . Invoke the method with the double value of 15.955 and the int value of 133 .

DataTransmitter.sendTwo(15.955,133);

Assume the availability of class named Logger that provides a static method , printErrorDescription , that accepts one int argument and returns no value .
Write a statement that invokes the method printErrorDescription , passing it the value 14 .

Logger.printErrorDescription(14);

Assume the availability of a class named Logger that provides a static method , printLarger that accepts two int arguments and returns no value .
Two int variables , sales1 and sales2 , have already been declared and initialized .
Write a statement that c

Logger.printLarger(sales1, sales2);

Assume the availability of a class named Arithmetic that provides a static method , add , that accepts two int arguments and returns their sum .
Two int variables , euroSales and asiaSales , have already been declared and initialized . Another int variabl

eurasiaSales = Arithmetic.add(euroSales, asiaSales);

The area of a square is stored in a double variable named area . Write an expression whose value is length of one side of the square.

Math.sqrt(area)

The area of a square is stored in a double variable named area . Write an expression whose value is length of the diagonal of the square.

Math.sqrt(area*2)

The length of a rectangle is stored in a double variable named length , the width in one named width . Write an expression whose value is the length of the diagonal of the rectangle.

Math.sqrt(Math.pow(length,2) + Math.pow(width,2))

If a right triangle has sides of length A, B and C and if C is the largest, then it is called the "hypotenuse" and its length is the square root of the sum of the squares of the lengths of the shorter sides (A and B). Assume that variables a and b have be

Math.sqrt(Math.pow(a,2) + Math.pow(b,2))

The Math class provides a static method , max , that accepts two int arguments and returns the value of the larger one.
Two int variables , population1 and population2 , have already been declared and initialized .
Write an expression (not a statement !)

Math.max(population1, population2)

he Math class provides a static method , max , that accepts two int arguments and returns the value of the larger one.
Four int variables , population1 , population2 , population3 , and population4 have already been declared and initialized .
Write an exp

Math.max(Math.max(population1,population2),Math.max(population3,population4))

`Suppose a reference variable of type Double called myDouble is already declared . Create an object of type Double with the initial value of 1.5 and assign it to the reference variable myDouble .

myDouble = new Double(1.5);

Write a single statement that declares a reference variable of type Integer named myInt , creates an object of type Integer with the initial value of 75 , and assigns it to the reference variable .

Integer myInt = new Integer(75);

Suppose a reference variable of type Integer called myInt is already declared . Create an object of type Integer with the initial value of 1 and assign it to the reference variable myInt .

myInt = new Integer(1);

Suppose a reference variable of type Long called myLong is already declared . Create an object of type Long with the initial value of two billion and assign it to the reference variable myLong .

myLong = new Long(2000000000);

The________ class allows non-GUI applications to interact with users using dialog boxes.

JOptionPane

The JOptionPane method that gets input from the user is

showInputDialog

Data returned by JOptionPane is always of the _________ type (excluding yes-or-no questions).

String

Given three String variables that have been declared and given values , firstName, middleName, and lastName, write an expression whose value is the initials of the three names : the first letter of each, joined together. So if firstName, middleName, and l

+ firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0)

One of the constructors for PrintStream class has a single OutputStream argument . Assume that dos is a variable that references an OutputStream object . Create a PrintStream object using dos and assign the resulting reference to ps, a PrintStream variabl

ps = new PrintStream(dos);

Object reference

The identifier of the object.

Instating an object

Creating an object of a class. (Think of classes as a class of similar types of functions and data

Instance of the class

The actual object in the class.

Instance variables or fields.

Are variables an constants of any primitive data type. (Byte, short, int, float, double, long, char, Boolean.)

Methods

The operations of a class which is a se to data, retrieve the current values of data, and perform other class related functions on the data.

Calling the method

Invoking a method on a object.

API

Application programming interface. ( tells programmer how to initiate objects and how to call the class methods.

Constructor

A special method of the class which has the same name as the class. The drop of the constructor is to assign initial values of the date of the class.

Object data

The data stored in the memory location.

Accessor methods

Enable clients to access the value of the instance variables of an object. (example equals getDay().

Mutator methods

Enable a client to change the value of the instance variables of an object. (example setMonth (int mm) allows user to change to integer value of a. specific month.

Object reference

Memory in a location

Static method

Is a method that is called without initiating an object. Usually used for methods that are called once.

Next method

Allows the client to input data from the java Console. You must use the java.util.scanner;

System.out.println

prints program output to screen.

NumberFormat getCurrencyIstance ()

creates a format for money. Example: NumberFormat priceFormat: will print $xx.xx

NumberFormat getPercentInstance()

static that creates a format object for percentages. ex. will print%xx.xx

JOptionPane class needs what to execute?

javax.swingpackage

showInputDialog

is used to get input from the user