Java: Chapter 3.

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

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.turingscraft.com".

"http://" + address

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.

Next method

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

Instating an object

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

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

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.

Accessor methods

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

Declare a reference variable of type File named myFile .

File myFile;

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

Static method

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

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

JOptionPane

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

Object reference

Memory in a location

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

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

String

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

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;

Object data

The data stored in the memory location.

Object reference

The identifier of the object.

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.

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

NumberFormat getCurrencyIstance ()

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

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 . Invoke the method , using this variable 's value as an argument .

dataTransmitter.sendDouble(x);

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

foreground = "red";

showInputDialog

is used to get input from the user

JOptionPane class needs what to execute?

javax.swingpackage

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 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 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 concatenates the String variable suffix onto the end of the String variable prefix .

prefix + suffix

System.out.println

prints program output to screen.

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

s.substring(2,10)

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

NumberFormat getPercentInstance()

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

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 standard input.

temperature = stdin.nextDouble();

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

text = "";

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 lastName, had the values "John", "Fitzgerald", and "Kennedy", the expression 's value would be JFK". Alternatively, if firstName, middleName, and lastName, had the values "Franklin", "Delano", and "Roosevelt", the expression 's value would be "FDR".

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

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 + ")"

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

API

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

Instance variables or fields.

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

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 variable 's value as an argument in your method invocation.

DataTransmitter.sendDouble(x);

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 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 DateManager that provides a static method , printTodaysDate , that accepts no arguments and returns no value . Write a statement that calls printTodaysDate .

DateManager.printTodaysDate();

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 variable myDouble .

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

Calling the method

Invoking a method on a object.

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 calls printLarger , passing it sales1 and sales2 .

Logger.printLarger(sales1, sales2);

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 expression (not a statement !) whose value is the largest of population1 , population2 , population3 , and population4 by calling max .

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

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 !) whose value is the larger of population1 and population2 by calling max .

Math.max(population1, population2)

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 been declared as doubles and that a and b contain the lengths of the shorter sides of a right triangle: write an expression for the length of the hypotenuse.

Math.sqrt(Math.pow(a,2) + Math.pow(b,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))

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)

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

Menu myMenu ;

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

String foreground = new String("black");

Declare a String variable named mailingAddress .

String mailingAddress = new String();

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;

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

String oneSpace = new String (" ");

rite the declaration of a String variable named title .

String title = 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();

Instance of the class

The actual object in the class.

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 lastFour, had the values 800, 555, and 1212, the expression 's value would be "800-555-1212". Alternatively, if areaCode, exchange, and lastFour, had the values 212, 867 and 5309 the expression 's value would be "212-867-5309".

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

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 turnOff , setTemp , which accepts an int argument and returns no value , and getTemp , which accepts no value and returns an int . Assume there is a reference variable myAC to an object of this class , which has already been created. There is also an int variable called currentTemp , which has already been declared . Use the reference variable , to invoke a method to retrieve the previously set temperature and store the returned value in currentTemp .

currentTemp = myAC.getTemp();

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 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 variable called john_doe . Invoke the method , using this reference as your argument .

dataTransmitter.sendObject(john_doe);

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 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 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 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 variable , eurasiaSales , has already been declared . Write a statement that calls add to compute the sum of euroSales and asiaSales and that stores this value in eurasiaSales .

eurasiaSales = Arithmetic.add(euroSales, asiaSales);

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 , eurasiaSales , has already been declared . Write a statement that calls add to compute the sum of euroSales and asiaSales and that stores this value in eurasiaSales

eurasiaSales = arithmetic.add(euroSales, asiaSales);

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 values "Big", "Bill", and "Broonzy", the expression 's value would be "Big Bill Broonzy". Alternatively, if firstName, middleName, and lastName, had the values "Jerry", "Lee", and "Lewis", the expression 's value would be "Jerry Lee Lewis".

firstName + " " + middleName + " " + lastName

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 sentence." Assume that there is another variable declared , firstWord , also of type String . Write the statements needed so that the first word of the value of sentence is assigned to firstWord . So, if the value of sentence were "Broccoli is delicious." your code would assign the value "Broccoli" to firstWord .

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

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 followed by a period followed by the first character of family followed by a period: in other words, the initials of the name . So if the values of these three variables were "John" "Fitzgerald" "Kennedy", then the expression 's value would be "J.F.K.".

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

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", "Cohen", and "Slutskaya", the expression , if it were printed would have the names "Arakawa", "Cohen", and "Slutskaya" each appearing on a separate line. (Do NOT print anything in this exercise: just write the expression .)

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

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 sentence." Assume that there is another variable declared , secondWord , also of type String . Write the statements needed so that the second word of the value of sentence is assigned to secondWord . So, if the value of sentence were "Broccoli is delicious." your code would assign the value "is" to secondWord .

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

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 printLarger , passing it sales1 and sales2 .

logger.printLarger(sales1, sales2);

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 int argument and returns no value . Assume there is a reference variable myAC to an object of this class , which has already been created. Use the reference variable , to invoke a method that tells the object to set the air conditioner to 72 degrees.

myAC.setTemp(72);

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 is a reference variable myAC to an object of this class , which has already been created. Using the reference variable , invoke a method to tell the air conditioner objection to turn off.

myAC.turnOff();

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 is a reference variable myAC to an object of this class , which has already been created. Using the reference variable , invoke a method to tell the air conditioner object to turn on.

myAC.turnOn();

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

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

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

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 prints the message "Greetings, NAME !!!" on a line by itself where NAME is replaced the value that was read into name . For example, if your code read in "Hassan" it would print out "Greetings, Hassan!!!" on a line by itself.

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

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 prints the message "Greetings, NAME " on a line by itself, where NAME is replaced the value that was read into name . For example, if your code read in "Rachel" it would print out "Greetings, Rachel" on a line by itself.

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 that reads in a name and an age and then prints the message "The age of NAME is AGE" on a line by itself, where NAME and AGE are replaced by the values read in for the variables name and age. For example, if your code read in "Rohit" and 70 then it would print out "The age of Rohit is 70" on a line by itself. There should NOT be a period in the output .

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

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)

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

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 possible combination exactly once, on a line by itself (that is, each possible combination is terminated with a newline character ). Assume that name1, name2 and name3 have already been declared and use them in your code. Assume also that stdin is a variable that references a Scanner object associated with standard input. For example, if your code read in "Larry", "Curly" and "Moe" it would print out "Larry, Curly and Moe", "Curly, Larry and Moe", etc., each on a separate line.

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.out.println(name2 + ", " + name3 + " and " + name1); System.out.println(name3 + ", " + name2 + " and " + name1); System.out.println(name3 + ", " + name1 + " and " + name2);

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 octet4, had the values 129, 42, 26, and 212 the expression 's value would be "129.42.26.212". Alternatively, if octet1, octet2, octet3, and octet4, had the values 192, 168, 1and 44 the expression 's value would be "192.168.1.44".

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

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 is a reference variable officeAC of type AirConditioner . Create a new object of type AirConditioner and save the reference in officeAC . After that, turn the air conditioner on using the reference to the new object .

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 return no value , and setTemp , which accepts an int argument and returns no value . Assume there is a reference variable officeAC of type AirConditioner . Create a new object of type AirConditioner and save the reference in officeAC . After that, use the reference to turn on the new air conditioner object and set the desired temperature to 69 degrees.

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

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 , have already been declared .

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

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 variable that has already been declared .

ps = new PrintStream(dos);

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)

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

The JOptionPane method that gets input from the user is

showInputDialog

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 , which accepts no argument and returns a boolean indicating whether the air conditioner is on or off. Assume there is a reference variable myAC to an object of this class , which has already been created. There is also a boolean variable status , which has already been declared . Use the reference variable , to invoke a method to find out whether the air conditioner is on and store the result in status .

status = myAC.isOn();

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

str.charAt(0)

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

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 would be "dys".

word.substring(0,3)

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 be "est".

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


संबंधित स्टडी सेट्स

Chapter 1 Graded Quiz Psychology

View Set

Microbiology Exam 3 Homework Questions

View Set

Consequences of the American Civil War

View Set

Unit 1 Test: Basic Economic Concepts (Summative Retest)

View Set

Chapter 25: Growth and Development of the Newborn and Infant

View Set

CCNA1 Intro to Networks: Chapter 5 (Section 5.1.1)

View Set

Pharmacology A- test 2 with rationales

View Set