© Ron Dai 2019
R. DaiLearn Java with Mathhttps://doi.org/10.1007/978-1-4842-5209-3_33

33. Java Intermediate Solutions

Ron Dai1 
(1)
Seattle, WA, USA
 

For your reference, in this chapter I’ll provide you with answer hints to some of the problems in the earlier chapters. For example, “For 16.” means “Hints for problems in Chapter 16.”

For 16. Pythagorean Triples

  1. 1.

    Instead of using “c,” we may check whether (a2 + b2) is a perfect square number, which is taking a square root of it and validating if it is an integer value.

     
  2. 2.

    Use the example code and check whether the resulting value of (a2 + b2) matches the form of “4n + 1”.

     

For 17. Strong Typed Programming

       public boolean isCollinear(Point p) {
              if (p.getX() == p1.getX() && p1.getX() == p2.getX()) {
                     return true;
              }
              if (this.getSlope(p) == this.getSlope()) {
                     return true;
              }
              return false;
       }
public double getSlope(Point p) {
       if (this.p1.x == this.p.x) {
              throw new
IllegalStateException("Denominator cannot be 0");
       }
       return (double)(this.p.y - this.p1.y) / (this.p.x - this.p1.x);
}

For 18. Conditional Statements

  1. 1.
    It is rewritten as shown here.
           if (num < 10 && num > 0) {
                  System.out.println("It's a one-digit number");
           }
           else if (num < 100) {
                  System.out.println("It's a two-digit number");
           }
           else if (num < 1000) {
                  System.out.println("It's a three-digit number");
           }
           else if (num < 10000) {
                  System.out.println("It's a four-digit number");
           }
           else {
                  System.out.println("The number is not between 1 & 9999");
           }
     
  2. 2.
    A simplified version is shown here.
    if (a == 0) {
           if (b == 0) {...}
           else {...}
    } else {
           if (b != 0) {...}
    }
     

For 19. Switch Statement

switch(color) {
       case 'R':
              System.out.println("The color is red");
              break;
       case 'G':
              System.out.println("The color is green");
              break;
       case 'B':
              System.out.println("The color is black");
              break;
       case 'C':
       default:
              System.out.println("Some other color");
              break;
}

For 21. Counting

  1. 1.

    Define x as the number of children and (2200 – x) is the number of adults, then 1.5 ∗ x + 4 ∗ (2200 – x) = 5,050. Iterate x = 0 up to 2200 to find a solution for x. And it is obvious that there is no more than one solution.

     
  2. 2.

    Define x as the number of correct answers and (10 – x) as the number of incorrect answers, then 5 ∗ x – 2 (10 – x) = 29. Iterate x from 0 up to 10 to find a possible solution for x.

     
  3. 3.

    Iterate a positive integer from 0 to 2001 and check its divisibility with 3, 4, and 5.

     
  4. 4.

    Iterate every three-digit integer number, from 100 up to 999, and check its digits.

     
  5. 5.

    Use a recursive method (referring to the example) to repeatedly pick a plant five times from the three types of plants (defining three types as A, B, C). And then remove duplicates from the combinations. For example: {A, A, B, B, C} is a duplicate of {A, B, A, B, C}.

     

For 23. Exploratory Experimentation of Pi

Utilize the following formula with integer number “r” and approximate the value of “e.”
$$ e=1+frac{1}{1!}+frac{1}{2!}+frac{1}{3!}+dots frac{1}{r!} $$

For 24. Classes in Object-Oriented Programming

  1. 1.

    a)

     
  2. 2.

    b)

     
  3. 3.
             NumberHolder nh = new NumberHolder();
             Nh.anInt = 5;
             Nh.aFloat = 3.2;
             System.out.printIn("anInt=" + Nh.anInt + "; aFloat=" + Nh.aFloat);
     
  4. 4.

    (A), (D)

     
  5. 5.

    (B)

     

For 26. Inheritance – Code Reuse

  1. 1.

    (c)

     
  2. 2.

    (b), (d), (e), (f)

     

For 27. Encapsulation and Polymorphism

  1. 1.
    public interface GeometricObject {
    public abstract double getPerimeter();
           public abstract double getArea();
    }
     
  2. 2.
           public class Circle implements GeometricObject {
           private final double PI = 3.14159;
           protected double radius;
           public Circle(double radius) {
                  this.radius = radius;
           }
           // Implement methods defined in the interface
           @Override
           public double getPerimeter() {
           return 2 * PI * this.radius;
           }
           @Override
           public double getArea() {
                  return PI * this.radius * this.radius;
           }
    }
     

For 28. Array – a Simple and Efficient Data Structure

  1. 1.

    (d)

     
  2. 2.

    { 0, 4, 0, 0, 11, 0, 0, 2 }

     

For 29. Common Pitfalls

  1. 1.

    If you want to get an integer value, why not take an integer input at the beginning?

    This is a corrected version. It is significantly simplified.
    Scanner user_input = new Scanner(System.in);
    System.out.println("a=");
    int a = user_input.nextInt();
    System.out.println("b=");
    int b = user_input.nextInt();
     
  2. 2.

    Does myArray[3] equal “13”?

    Pay attention to the definition of the index of an array element.

     
  3. 3.

    Is it necessary to check stringsArray.length = 0? And, is it a good approach to do countMe.toLowerCase() inside the for-loop?

    This is a recommended version:
    public static int CountStrings(String[] stringsArray, String countMe) {
           int occurences = 0;
           String keyword = countMe.toLowerCase();
           for (int i = 0; i < stringsArray.length; i ++) {
                  if (stringsArray[i].toLowerCase().contains(keyword)) {
                         occurences ++;
                  }
           }
           return occurences;
    }
     
  4. 4.

    Has the myRect ever been initialized?

    There is an important line to update in the main() method as shown here:
    public class SomethingIsWrong {
           public static void main(String[] args) {
                   Rectangle myRect = new Rectangle();
                   myRect.width = 40;
                   myRect.height = 50;
                   System.out.println("myRect's area is " + myRect.area());
           }
    }
     
  5. 5.

    Since the variable temp has been assigned with the value of the first element in array1, do we need to iterate from i=0 inside the for-loop?

    The simple fix is to change from for (int i = 0; ... to for (int = 1; ... in the original function as shown.
    public static int getMaxLength(ArrayList<String> array1)  {
           if(array1.isEmpty()) {
                  return 0;
           }
           else {
                  String temp= array1.get(0);
                  for (int i = 1; i < array1.size(); i++) {
                         if (array1.get(i).length() > temp.length() ) {
                                temp= array1.get(i);
                         }
                  }
                  return temp.length();
           }
    }
     
  6. 6.

    Check the if/else clause.

    The scope of “y > 31 && z > 12” is already covered by the scope of “z > 12 || y > 31”. Therefore, the “else if (...)” part in the original code is meaningless.

     
  7. 7.

    Review the actual usage of the Scanner.

    Due to the same reason stated in 1, the code can be corrected as shown:
    System.out.println("What month were you born in? (1-12)");
    Scanner sc = new Scanner(System.in);
    int al = sc.nextInt();
     
  8. 8.

    Check the if/else clause

    The scope of numToTake > 2 has included the scope of numToTake >= 2 && numToTake < 3. The if and else if conditional clauses need to be rewritten.

     
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.66.156