Monday, June 17, 2013

MyProgrammingLab Solutions


My Programming Lab Questions and Answers



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

String logfileName;

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.

output.print(message);

 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. 

public int twice(int x){
return  x* 2;
}


Write the definition of a method  add , which receives two integer parameters and returns their sum. 


public int add( int x, int y ) { 
return x + 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)
{
double answer = 0 ;
while (power>=0)
{
answer = Math.pow(number,(double)power);
return answer;
}
return 0;
}



 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.

if( a==0.0) 
System.out.println("no solution for a=0");
return;

Write the definition of a method  min that has two  int parameters and returns the smaller. 


public int min (int x,int y){
return Math.min(x,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.

sendSignal();

 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.


sendVariable(x);

 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.

sendTwo(15.955,133) ;

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    )

{

if          (           x       ==        1          )

{

System.out.println          (           "disagree"           )           ;

}

if            (         x         ==        2             )

{

System.out.println        (       "no opinion"           )         ;

}


if           (           x          ==         3         )

{

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)
{
int z = Math.max(x,y);
System.out.println(z);
}


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. 

total = 0 ;
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++ )
{
temp = a[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]) ;
}
}




Given: 
 an  int variable  k , 
 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])
{
isAMember = true ;
break ;
}
else
{
isAMember = false ;
}
}




4 comments:

  1. Assume that a boolean variable isQuadrilateral has been declared , and that another variable , numberOfSides has been declared and initialized . Write a statement that assigns the value true if numberOfSides is exactly 4 and false otherwise.
    NEED HELP WITH THIS PLS

    ReplyDelete
  2. Write a loop that reads positive integers from standard input and that terminates when it reads an integer that is not positive. After the loop terminates , it prints out, on a line by itself, the sum of all the even integers read. Declare any variables that are needed.

    ASSUME the availability of a variable , stdin, that references a Scanner object associated with standard input
    PLS HELP

    ReplyDelete
  3. Hello I am so delighted I located your blog, I really located you by mistake, while I was watching on google for something else, Anyways I am here now and could just like to say thank for a tremendous post and a all round entertaining website. Please do keep up the great work. my programming lab

    ReplyDelete