Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Write a public method called triple, which is passed an int as a parameter an

ID: 3670348 • Letter: 1

Question

1. Write a public method called triple, which is passed an int as a parameter and returns its triple.
Example: 4 --> 12

2. Write a method called averageOfTwo, which is passed two int values and which returns a double value - their average. Thus if averageOfTwo is passed the int values 5 and 6, it should return 5.5.
Hint: divide the sum of the arguments by 2.0.

3. The method evenSquares takes a single int parameter, n, (for example, 10000), and then prints all of the (positive) even perfect squares less than n, each on a separate line. Notice that evenSquares has a void return type, since all it does is print integers to the console. Be sure to use the println method to print each entry of your output.
Example: if n = 40, your code should print
4
16
36
(Hint: your method should be built around a for loop with a test component that asks if the square of the control variable (say, k) is < n. Thus, the loop should terminate as soon as k*k equals or exceeds n.)

public void evenSquares(int n) {

5. Write a public method called average, which is passed three ints, and returns their average as a double.
Example: (5, 9, 6) --> 6.66666666666666
(Hint: be sure to divide the sum of the three input values by 3.0, and NOT the int value 3.)
Note that in this example you must write a complete method, including the method header line.

6. Write a public method called largerStr, which is passed two Strings, s1 and s2, and returns true if s1 is strictly longer than s2, false otherwise.
Example: largerStr("here", "there") --> false
Example: largerStr("there", "here") --> true
Example: largerStr("hear", "here") --> false

public boolean largerStr (String s1, String s2) {

7. Write a public method called signFlip, which is passed a double and returns that value with the opposite sign.
Example1: 4.0 --> -4.0
Example2: -3.14 --> 3.14
Note that in this example you must write a complete method, including the method header line.

8. Enter the code that will print the pattern "01" 100 times. Use a for loop to do this. The code should print the sequence on one line:
0101010101010101010101010101010101010101010101...

9. The Duplex class represents a duplex house. The main method of the DuplexTester class creates a Duplex object and prints out some information about it.


This is one example of how to use the Duplex class. Running the main method as written above gives the following output.


Here is an incomplete version of the Duplex class. Complete the definition so that the DuplexTester driver will work properly with your enhanced definition for Duplex.

public class Duplex {

   private String owner;

   public Duplex (String name) {
      owner = name;
   }

   public String getOwner() {
      return owner;
   }

Explanation / Answer

1.

public int triple( int a)
{
return( a*3 );
}

2.

public double averageOfTwo(int a, int b)
{
return ((double) (a+b))/2;
}

3.

public void evenSquares(int n) {
    int max = Math.floor(Math.sqrt(n));
    for(int k=1; k<=max; k++){
        System.out.println(k*k);
    }
}

5.

public double average (int num1, int num2, int num3)
{
return ((double) (num1+num2+num3)) / 3.0;
}

6.
public boolean largerStr(String a, String b)
{
int stringA = a.length();
int stringB = b.length();
if ( stringA > stringB )
return false;
else
return true;
}

7.
public double signFlip(double x)
{
return (x * -1.0)
}


8.
public class Duplex
{
private String owner;

public Duplex (String name) {
owner = name;
}

public String getOwner() {
return owner;
}

public void setOwner(String newOwner){
owner = newOwner;
}

}