Friday, October 28, 2011

Techniques: Divisibility, Odd, Even, Exchanges, On, Off, Toggle.


Techniques1

Divisibility

1.  Write an expression that evaluates to true if the value of the integer variable numberOfPrizes is divisible (with no remainder) by the integer variable numberOfParticipants.  Assume that numberOfParticipants is not zero.
Example:  So if numberOfPrizes had the value 42 and numberOfParticipants had the value 7 then your expression would be true because 42 is divisible by 7 with no remainder.

On the other hand, if numberOfPrizes had the value 34 and numberOfParticipants had the value 8 then your expression would be false because when 34 is divided by 8, the remainder is 2.


(numberOfPrizes%numberOfParticipants==0)


2.  Write an expression that evaluates to true if the value of the integer variable widthOfBox is not divisibleby the value of the integer variable widthOfBook.  Assume that widthOfBook is not zero.  ("Not divisible" means has a remainder.)

(widthOfBox%widthOfBook!=0)


OddEven

1.  Write an expression that evaluates to true if the integer variable x contains an even value, and false if it contains an odd value.

(x%2==0)


Exchanges

1.  Given two int variables, i and j, which have been declared and initialized, and two other int variables, itemp and jtemp, which have been declared, write some code that swaps the values in i and j by copying their values to itemp and jtemp respectively, and then copying itemp and jtemp to j and i respectively.

jtemp=i;
itemp=j;
j=jtemp;
i=itemp;


2.  Given three already declared int variables, i, j, and temp, write some code that swaps the values in i and j.  Use temp to hold the value of i and then assign j's value to i.  The original value of i, which was saved in temp, can now be assigned to j.

temp=i;
i=j;
j=temp;


3.  Given two int variables, firstPlaceWinner and secondPlaceWinner, write some code that swaps their values.  Declare any additional variables as necessary.

int temp=firstPlaceWinner;
firstPlaceWinner=secondPlaceWinner;
secondPlaceWinner=temp;


4.  Given two double variables, bestValue and secondBestValue, write some code that swaps their values.  Declare any additional variables as necessary.

double temp=bestValue;
bestValue=secondBestValue;
secondBestValue=temp;


5.  Given an array arr, of type int, along with two int variables i and j, write some code that swaps the values of arr[i] and arr[j].  Declare any additional variables as necessary.

int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;


OnOffToggle

1.  Write a statement that toggles the value of onOffSwitch.  That is, if onOffSwitch is false, its value is changed to true; if onOffSwitch is true, its value is changed to false.

if(onOffSwitch==false)
{
onOffSwitch=true;
}
else
{
onOffSwitch=false;
}




Messages & Sending Messages


Messages

Messages

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

2.  Write the code for invoking a method named sendNumber.  There is one int argument for this method.  Send the number 5 as an argument to this method.
Assume that sendNumber is defined in the same class that calls it.

sendNumber(5) ;


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



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


5.  Write the code for invoking a method named sendObject.  There is one argument for this method which is of type Customer.
Assume that there is a reference to an object of type Customer, in a variable called John_Doe.  Use this reference as your argument.
Assume that sendObject is defined in the same class that calls it.

sendObject(John_Doe) ;


Sending_Messages

1.  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.
Example:  So, as a result of your code executing, the turnOn() method of the AirCondition object that myAC refers to will be invoked.


myAC.turnOn() ;


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


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


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


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


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


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






Reference Types & Methods


Reference_Types


1.  Declare a reference variable of type File named myFile.

File myFile ;


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

Menu myMenu ;


3.  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.
Example:  So, after your code executed, there would be a variable, myInt, referencing an Integerobject, whose value is 75.

Integer myInt = new Integer(75) ;


Methods
Invocation-1
ParameterLess-1

1.  printTodaysDate is a method that accepts no arguments and returns no value.  Write a statement that calls printTodaysDate .

Assume that printTodaysDate is defined in the same class that calls it.

printTodaysDate() ;


Scalar_variables-1

1.  printErrorDescription is a method that accepts one int argument and returns no value.

Write a statement that invokes the method printErrorDescription, passing it the value 14.

Assume that printErrorDescription is defined in the same class that calls it.

printErrorDescription ( 14 ) ;


2.  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.

printLarger (   sales1   ,   sales2   )                ;


3.  add is a method 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.

Assume that add is defined in the same class that calls it.

eurasiaSales =       add              (                   euroSales       ,      asiaSales          )     ;


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


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


6.  max is a method 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.

Assume that max is defined in the same class that calls it.

max ( max ( population1 , population2 ) ,




max ( population3 , population4 ) )


Definitions-1

ParameterLess-3

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

Scalar_variables-3

1.  Write the definition of a method printGrade, which has a char parameter and returns nothing.  The method prints on a line by itself the message string Grade:  followed by the char parameter (printed as a character) to standard output.  Don't forget to put a new line character at the end of your line.
Example:  Thus if the value of the parameter is 'A', the method prints out

Grade: A

public          static            void        printGrade     (   char    x    )

{

System.out.println            ("Grade: "                +             x)        ;

}


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

}


}


3.  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);
}


4.  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 2*x;
}


5.  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;
}


6.  Write the definition of a method dashedLine, with one parameter, an int.

If the parameter is negative or zero, the method does nothing.  Otherwise it prints a complete line terminated by a newline to standard output consisting of dashes (hyphens) with the parameter's value determining the number of dashes.  The method returns nothing.

public void dashedLine (int a)
{
if (a>0)
{
int i;
for (i=1;i<=a;i=i+1)
{
System.out.print("-");
}
System.out.println("");
}
}


7.  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;
}


Examples

1.  Given the integer variables x, y, and z, write a fragment of code that assigns the smallest of x, y, and z to another integer variable min.

Assume that all the variables have already been declared and that x, y, and z have been assigned values.
Example:  So if the value of x is 19 the value of y is 13, and the value of z is 23, then after your code executes, the value of min would be 13.

min = Math.min(Math.min(x,y),z);


2.  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);
}


3.  Write a method min that has three string parameters and returns the smallest.

public String min (String x, String y, String z)
{
String min = " ";
if ((x.compareTo(y)<0) && (x.compareTo(z)<0))
{
min=x;
}
else if ((y.compareTo(z)<0) && (y.compareTo(x)<0))
{
min=y;
}
else if ((z.compareTo(x)<0) && (z.compareTo(y)<0))
{
min=z;
}
return min;
}


4.  Write a method max that has two string parameters and returns the larger of the two.


public String max (String x, String y)
{
String max;
if (x.compareTo(y)>0)
{
max=x;
}
else
{
max=y;
}
return max;
}