My Programming Lab Questions and Answers
Declare a variable logfileName that is suitable for holding the name of a file. 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"); 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 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); 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. 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 (); Given a String variable named line1 and given a Scanner reference variable fileInput that remains uninitialized, write a sequence of statements that 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(); Write the definition of a method twice , which receives an integer parameter and returns an integer that is twice the value of the parameter. Write the definition of a method add , which receives two integer parameters and returns their sum. public int add( int x, int y ) { Write the definition of a method powerTo , which receives two parameters. The first is a double and the second is an int . The method returns a double . If the second parameter is negative, the method returns zero. Otherwise it returns the value of the first parameter raised to the power of the second parameter. public double powerTo (double number,int power) answer = Math.pow(number,(double)power); Given that a method receives three parameters a , b , c , of type double, write some code, to be included as part of the method, that checks to see if the value of a is 0; if it is, the code prints the message "no solution for a=0" and returns from the method. System.out.println("no solution for a=0"); Write the definition of a method min that has two int parameters and returns the smaller. public int min (int x,int y){ Write the code for invoking a method named sendSignal . There are no arguments for this method. Assume that sendSignal is defined in the same class that calls it. Write the code for invoking a method named sendVariable . There is one int argument for this method. Assume that an int variable called x has already been declared and initialized to some value. Use this variable's value as an argument in your method invocation. Assume that sendVariable is defined in the same class that calls it. Write the code for invoking 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 value of 133 . Assume that sendTwo is defined in the same class that calls it. printLarger is a method 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 . Assume that printLarger is defined in the same class that calls it. printLarger ( sales1 , sales2 ) ; toThePowerOf is a method that accepts two int arguments and returns the value of the first parameter raised to the power of the second. An int variable cubeSide has already been declared and initialized. Another int variable, cubeVolume , has already been declared. Write a statement that calls toThePowerOf to compute the value of cubeSide raised to the power of 3 and that stores this value in cubeVolume . Assume that toThePowerOf is defined in the same class that calls it. cubeVolume = toThePowerOf ( cubeSide , 3 ) ; max is a method 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 . Assume that max is defined in the same class that calls it. max ( population1 , population2) Write the definition of a method printDottedLine , which has no parameters and doesn't return anything. The method prints to standard output a single line (terminated by a newline) consisting of five periods. public static void printDottedLine () System.out.println("....."); Write the definition of a method printAttitude , which has an int parameter and returns nothing. The method prints a message to standard output depending on the value of its parameter. If the parameter equals 1 , the method prints disagree If the parameter equals 2 , the method prints no opinion If the parameter equals 3 , the method prints agree In the case of other values, the method does nothing. Each message is printed on a line by itself. public static void printAttitude ( int x ) System.out.println ( "disagree" ) ; System.out.println ( "no opinion" ) ; System.out.println ( "agree" ) ; Write the definition of a method printLarger , which has two int parameters and returns nothing. The method prints the larger value of the two parameters to standard output on a single line by itself. public static void printLarger (int x,int y) Declare and instantiate an array named scores of twenty-five elements of type int . int[] scores = new int[25]; Declare an array named taxRates of five elements of type double and initialize the elements (starting with the first) to the values 0.10 , 0.15 , 0.21 , 0.28 , 0.31 , respectively. double[] taxRates = { 0.10 , 0.15 , 0.21 , 0.28 , 0.31 } ; Declare an array reference variable, week, and initialize it to an array containing the strings "mon", "tue", "wed", "thu", "fri", "sat", "sun" (in that order). String[ ] week = {"mon", "tue", "wed", "thu", "fri", "sat", "sun"}; Given that the array monthSales of integers has already been declared and that its elements contain sales data for the 12 months of the year in order (i.e., January, February, etc.), write a statement that writes to standard output the element corresponding to October. Do not write anything else out to standard output. System.out.println(monthSales[9]) ; Given an array temps of double s, containing temperature data, compute the average temperature. Store the average in a variable called avgTemp . Besides temps and avgTemp , you may use only two other variables -- an int variable k and a double variable named total , which have been declared. for( k = 0 ; k<temps.length ; k++ ) total = total + temps[k] ; avgTemp = total/temps.length ; Reversing the elements of an array involves swapping the corresponding elements of the array: the first with the last, the second with the next to the last, and so on, all the way to the middle of the array. Given an array a and two other int variables, k and temp , write a loop that reverses the elements of the array. for( k=0 ; k<a.length/2 ; k++ ) a[k] = a[a.length - k - 1] ; a[a.length - k - 1] = temp ; printArray is a method that accepts one argument, an array of int s. The method prints the contents of the array; it does not return a value. inventory is an array of int s that has been already declared and filled with values. Write a statement that prints the contents of the array inventory by calling the method printArray . printArray( inventory ) ; Write the definition of a method printArray , which has one parameter, an array of int s. The method does not return a value. The method prints out each element of the array, on a line by itself, in the order the elements appear in the array, and does not print anything else. public void printArray( int[] s ) for ( int i = 0 ; i < s.length ; i++ ) System.out.println(s[i]) ; an int array currentMembers that has been declared and initialized, an int variable memberID that has been initialized, and an boolean variable isAMember , write code that assigns true to isAMember if the value of memberID can be found in currentMembers , and that assigns false to isAMember otherwise. Use only k , currentMembers , memberID , and isAMember . for ( k=0 ; k<currentMembers.length ; k++ ) if (memberID==currentMembers[k])
|
Here is some help for MyProgrammingLab Use a finder (Command-f on Macs) and type in your problem to find your question faster.
Rearrange the following code so that it forms a correct program that prints out the letter Q:
public class Q { public static void main(String[] a) { System.out.println("Q"); } }
Given an integer variable count , write a statement that displays the value of count on the screen.
Do not display anything else on the screen -- just the value of count .
System.out.print (count) ;
Given the variables fullAdmissionPrice and discountAmount (already declared and assigned values), write an expression corresponding to the price of a discount admission.
fullAdmissionPrice - discountAmount
The dimensions (width and length) of room1 have been read into two variables: width1 and length1 . The dimensions of room2 have been read into two other variables: width2 and length2 . Write a single expression whose value is the total area of the two rooms.
(width1*length1)+(width2*length2)
Assume that word is a String variable. Write a statement to display the message "Today's Word-Of-The-Day is: " followed by the value of word on a line by itself on standard output. System.out.println("Today's Word-Of-The-Day is: " + word); Write an expression that concatenates the String variable suffix onto the end of the String variable prefix . Declare a variable hasPassedTest , and initialize it to true. boolean hasPassedTest = true; Given an int variable grossPay , write an expression that evaluates to true if and only if the value of grossPay is less than 10,000 . Write an expression that evaluates to true if the integer variable x contains an even value, and false if it contains an odd value. Assume that the variables gpa , deansList and studentName , have been declared and initialized. Write a statement that adds 1 to deansList and prints studentName to standard out if gpa exceeds 3.5. System.out.print(studentName); Write an if/else statement that compares the variable age with 65 , adds 1 to the variable seniorCitizens if age is greater than or equal to 65 , and adds 1 to the variable nonSeniors otherwise. Write an if/else statement that compares the value of the variables soldYesterday and soldToday , and based upon that comparison assigns salesTrend the value -1 or 1 . -1 represents the case where soldYesterday is greater than soldToday ; 1 represents the case where soldYesterday is not greater than soldToday . if (soldYesterday > soldToday) Write an if/else statement that adds 1 to the variable minors if the variable age is less than 18 , adds 1 to the variable adults if age is 18 through 64 and adds 1 to the variable seniors if age is 65 or older. else if(age >= 18 && age <= 64) Given an int variable k that has already been declared, use a while loop to print a single line consisting of 88 asterisks. Use no variables other than k . Given an int variable n that has already been declared and initialized to a positive value, use a while loop to print a single line consisting of n asterisks. Use no variables other than n . Given int variables k and total that have already been declared, use a while loop to compute the sum of the squares of the first 50 counting numbers, and store this value in total . Thus your code should put 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into total . Use no variables other than k and total . total = total + ( k * k ) ; Given an int variable n that has already been declared, write some code that repeatedly reads a value into n until at last a number between 1 and 10 (inclusive) has been entered. ASSUME the availability of a variable, stdin , that references a Scanner object associated with standard input. Write a loop that reads positive integers from standard input, printing out those values that are even, each followed by a space, and that terminates when it reads an integer that is not positive. Declare any variables that are needed. ASSUME the availability of a variable, stdin , that references a Scanner object associated with standard input. Consider this data sequence: "3 11 5 5 5 2 4 6 6 7 3 -8". Any value that is the same as the immediately preceding value is considered a CONSECUTIVE DUPLICATE. In this example, there are three such consecutive duplicates: the 2nd and 3rd 5s and the second 6. Note that the last 3 is not a consecutive duplicate because it was preceded by a 7. Write some code that uses a loop to read such a sequence of non-negative integers, terminated by a negative number. When the code finishes executing, the number of consecutive duplicates encountered is printed. In this case,3 would be printed. int n, prev, consecdups=0;prev=stdin.nextInt();n=stdin.nextInt();while (n>=0) { if (n==prev) consecdups++; prev = n; n=stdin.nextInt();}System.out.println(consecdups); Given an int variable n that has already been declared and initialized to a positive value, use a do...while loop to print a single line consisting of n asterisks. Use no variables other than n . Given int variables k and total that have already been declared, use a do...while loop to compute the sum of the squares of the first 50 counting numbers, and store this value in total . Thus your code should put 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into total . Use no variables other than k and total . for( k=1, total=0; k<=50; k++ ) total += k*k; Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have already been declared, use a do...while loop to compute the sum of the cubes of the first n whole numbers, and store this value in total . Use no variables other than n , k , and total . Given int variables k and total that have already been declared, use a for loop to compute the sum of the squares of the first 50 counting numbers, and store this value in total . Thus your code should put 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into total . Use no variables other than k and total . Assume the int variables i and result have been declared but not initialized. Write a for loop header, i.e. something of the form for the following loop body: When the loop terminates, result should hold the product of the odd numbers between 10 and 20 . for(i=11,result=1;i<20;i+=2) Assume the int variables i ,lo , hi , and result have been declared and that lo and hi have been initialized. Assume further that result has been initialized to the value 0 . Write a for loop that adds the integers from lo up through hi (inclusive), and stores the result in result . Your code should not change the values of lo and hi . Also, do not declare any additional variables -- use only i ,lo , hi , and result . for(i = lo; i <= hi; i++) Given an int variable n that has already been declared and initialized to a positive value, and another int variable j that has already been declared, use a for loop to print a single line consisting of n asterisks. Thus if n contains 5, five asterisks will be printed. Use no variables other than n and j . Write a for loop that prints the integers 0 through 39, separated by spaces. System.out.print(i + " "); Write a for loop that prints in ascending order all the positive multiples of 5 that are less than 175, separated by spaces. for (int i=5; i<175; i+=5) { Write a for loop that prints the integers 50 through 1, separated by spaces. Use no variables other than count . ( int count=50 ; count>=1; count--) System.out.print( count + " "); Assume that the int variables i and j have been declared, and that n has been declared and initialized. Using for loops (you may need more than one), write code that will cause a triangle of asterisks of size n to be output to the screen.
|