Friday, October 28, 2011

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















2 comments: