MyProgrammingLab 4 & 12

Ace your homework & exams now with Quizwiz!

Write an expression that evaluates to true if and only if the string variable s does not equal the String literal end.

!s.equals("end")

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

Write a sequence of statements that create a file named "greeting" and write a single line consisting of "Hello, World!" to that file. Make sure that the line has been flushed to the file and that any system resources used during the course of running these statements have been released. (Do not concern yourself with any possible exceptions here-- assume they are handled elsewhere.)

PrintWriter output = new PrintWriter("greeting"); output.println("Hello, World!"); output.close();

Write a statement that declares a PrintWriter reference variable named output and initializes it to a reference to a newly created PrintWriter object associated with a file named "output.txt". (Do not concern yourself with any possible exceptions here-- assume they are handled elsewhere.)

PrintWriter output = new PrintWriter("output.txt");

Given an initialized String variable fileName, write a sequence of statements that create a file whose name is given by the variable and whose content is a single line consisting of "This Is File: " followed by the name of the file. Make sure that the data written to the file has been flushed from its buffer and that any system resources used during the course of running these statements have been released. (Do not concern yourself with any possible exceptions here-- assume they are handled elsewhere.)

PrintWriter output = new PrintWriter(fileName); output.println("This Is File: "+fileName); output.close();

Given an initialized String variable outfile, write a statement that declares a PrintWriter reference variable named output and initializes it to a reference to a newly created PrintWriter object associated with a file whose name is given by outfile. (Do not concern yourself with any possible exceptions here-- assume they are handled elsewhere.)

PrintWriter output = new PrintWriter(outfile);

Declare a local variable output that is suitable for referring to an object that provides methods for writing to a text file.

PrintWriter output;

Declare a Scanner reference variable fileInput, and assign it a newly created Scanner object that is associated with a file named "data1". (Do not concern yourself with any possible exceptions here-- assume they are handled elsewhere.)

Scanner fileInput = new Scanner(new File("data1"));

Given a String variable named line1, write a sequence of statements that use a Scanner to read the first line of a file named "poem" and stores i in line1. (Do not concern yourself with any possible exceptions here-- assume they are handled elsewhere.)

Scanner fileInput = new Scanner(new File("poem")); line1 = fileInput.nextLine();

Given a Scanner reference variable, stdin, that is associated with standard input, read in two words. Use the first word as the name of a file to create and store the second word in. Make sure that the data written to the file has been flushed from its buffer and that any system resources used during the course of running these statements have been released. (Do not concern yourself with any possible exceptions here-- assume they are handled elsewhere.)

String fileName= stdin.next(); String word = stdin.next(); PrintWriter output = new PrintWriter(fileName); output.println(word); output.close();

Declare a variable logfileName that is suitable for holding the name of a file.

String logfileName;

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

Given a Scanner reference variable named input that has been associated with an input source consisting of a sequence of lines of text and an int variable named count, write the code necessary to count the lines of text in the input source and place that value into count.

count = 0; while (input.hasNext()) { String s = input.nextLine(); count++; }

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

Clunker Motors Inc. is recalling all vehicles in its Extravagant line from model years 1999-2002 as well all vehicles in its Guzzler line from model years 2004-2007. Given a variable modelYear and a String modelName write a statement that prints the message "RECALL" to standard output if the values of modelYear and modelName match the recall details.

if (modelName.equals("Extravagant") && 1999<=modelYear && modelYear<=2002 || modelName.equals("Guzzler") && 2004<=modelYear && modelYear<=2007) System.out.println("RECALL");

Clunker Motors Inc. is recalling all vehicles in its Extravagant line from model years 1999-2002. Given a variable modelYear and a String modelName write a statement that prints the message "RECALL" to standard output if the values of modelYear and modelName match the recall details.

if (modelName.equals("Extravagant") && 1999<=modelYear && modelYear<=2002) System.out.println("RECALL");

Given a String variable named line1 and given a Scanner reference variable stdin that has been assigned a reference to a Scanner object, read the next line from stdin and save it in line1. (Do not concern yourself with any possible exceptions here-- assume they are handled elsewhere.)

line1 = stdin.nextLine();

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

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)

Write an expression that whose value is the fifth character of the String name.

name.charAt(4)

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

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)

Write an expression whose value is a reference to a newly created PrintWriter object associated with a file named "output.txt". (Do not concern yourself with any possible exceptions here-- assume they are handled elsewhere.)

new PrintWriter("output.txt")

Given an initialized String variable output, write an expression whose value is a reference to a newly created PrintWriter object associated with a file whose name is contained in output. (Do not concern yourself with any possible exceptions here-- assume they are handled elsewhere.)

new PrintWriter(output)

Given a PrintWriter reference variable named output that references a PrintWriter object, write a statement that flushes any buffered output on the stream associated with the object, releases any system resources associated with the object and ceases output operations on the associated stream.

output.close();

Given a PrintWriter reference variable named output that references a PrintWriter object, write a statement that writes the string "Hello, World" to the file output streams to.

output.print("Hello, World");

Given an initialized String variable message, and given a PrintWriter reference variable named output that references a PrintWriter object, write a statement that writes the string referenced by message to the file output streams to.

output.print(message);

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

Clunker Motors Inc. is recalling all vehicles in its Extravagant line from model years 1999-2002. A boolean variable named recalled has been declared. Given a variable modelYear and a String modelName write a statement that assigns true to recalled if the values of modelYear and modelName match the recall details and assigns false otherwise.

recalled = (modelName.equals("Extravagant") && 1999<=modelYear && modelYear<=2002);

Clunker Motors Inc. is recalling all vehicles in its Extravagant line from model years 1999-2002 as well all vehicles in its Guzzler line from model years 2004-2007. A boolean variable named recalled has been declared. Given a variable modelYear and a String modelName write a statement that assigns true to recalled if the values of modelYear and modelName match the recall details and assigns false otherwise.

recalled = modelName.equals("Extravagant")&& 1999<=modelYear && modelYear<=2002 || modelName.equals("Guzzler") && 2004<=modelYear && modelYear<=2007;

Write an expression that evaluates to true if and only if the string variable s equals the string "end".

s.equals("end")

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

s.substring(2,10)

Write an expression that evaluates to true if the value of the string variable s1 is greater than the value of string variable s2.

s1.compareTo(s2)>0

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 number of characters in the String referred to by sentence.

sentence.length()

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 the String variable str, write an expression that evaluates to the character at index 0 of str.

str.charAt(0)

Given a Scanner reference variable named input that has been associated with an input source consisting of a sequence of integers and an int variable named total, write the code necessary to add all the integers in the input source and place their sum into total.

total = 0; while (input.hasNext()) total += input.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)


Related study sets

Python Certified Entry-Level Programmer (PCEP): Module 1

View Set

Psych 245 Drugs and Behavior Exam 1

View Set

U-Prep Chapter 21: Teacher and Counselor

View Set

Unit 2 Unity and Diversity AP Bio FINAL STUDY

View Set

Chapter 6: Business Networks and telecommunications

View Set

EMT Chapter 32 Orthopaedic Injuries

View Set

Chapter 40: Fluid, Electrolyte, and Acid-Base Balance

View Set