# Assignment #1—Golden Ratio Solved

Learning Outcomes

When you have completed this assignment, you should understand:

How to design, compile, run and check a simple and complete Java program on your own.

The effect of escape sequences on printed strings.

How to write basic static methods.

The flow of control (i.e. the effects of method calls and expression evaluation).

How to format and document a Java program.

The Golden Ratio is used to help design aesthetically pleasing shapes by artists and architects, and also shows up in a surprising number of ways in nature. The Golden Ratio is also called the “Divine Proportion.” It is an irrational number (it cannot be written as the ratio of two integers), and is typically symbolized with the Greek letter φ (phi). As a continued fraction pattern, it is the simplest continued fraction, as it consists of all 1’s:

The pattern in the denominator repeats infinitely. The Golden Ratio is not typically included as a constant in part of a programming language’s package library, such as π = Math.PI in Java. One possible application of φ is as some part of a procedural graphics program (artwork generated by math formulae).

Write a Java program called GoldenRatio.java that does the following.

Create a variable of type double called phi. Set phi = (1 + Math.sqrt(5)) / 2.

Estimate φ:

create a variable of type double called approxPhi,

set approxPhi to 1.0,

set approxPhi to its reciprocal plus 1.0, (d) repeat Step (c) four more times.

Then print the following statements:

The Golden Ratio is approximately <value of approxPhi here.

The Golden Ratio is <value of phi here.

Compile your program and make sure there are no errors, and you have the above output. Your approximation should agree with the actual value of φ in the first two digits.

Next, approximate φ again in your program:

set approxPhi to 1.0,

set approxPhi to its reciprocal plus 1.0, (c) repeat Step (b) ninety-nine more times.

Then print the following statements:

Now for a more accurate approximation.

The Golden Ratio is approximately <value of approxPhi here.

The Golden Ratio is <value of phi here.

The Golden Ratio can be used to generate a famous sequence of numbers called the Fibonacci numbers. The first Fibonacci number is f1 = 1, and the second is f2 = 1. Any Fibonacci number is the sum of the previous two: fn+2 = fn+1 + fn, for n ≥ 3. Observe the first nine Fibonacci numbers:

1,1,2,3,5,8,13,21,34,...

After your previous code, calculate these Fibonacci numbers as follows. Declare and assign the following variables:

int f1 = 1;

int f2 = 1;

int fn = 0;

Write a for-loop with a loop-control variable i that iterates from 3 to 9, inclusive, with each iteration performing the following steps:

set fn equal to the sum of f1 and f2

set f1 equal to f2

set f2 equal to fn

out.println("f " + i + " = " + fn); (e) call checkFibonacci( approxPhi, i ) as described below, (f) call starArt( f1, f2 ) as described below. Now add the method checkFibonacci to your program:

public static void checkFibonacci( double approxPhi, int n ) { double A = Math.pow(approxPhi, n); double B = Math.pow(1 - approxPhi, n); double Fibonacci = (A - B) / Math.sqrt(5); long check fn = Math.round( Fibonacci );

System.out.println("check f " + n + " = " + check fn);

}

Add another method starArt to your program that takes two integer parameters, a height and a width. Inside starArt, use two nested loops to print as output a rectangle of star (*) characters, with just less than 2.5*width characters per line of height number of lines. (The terminal/console window typically has non-square character sizes, and so we need this adjustment in width to get proper visual comparison of width against height here.)

Compile your program and make sure there are no errors. The output should give the 3rd to the 9th Fibonacci number, with each number followed by a rectangle of stars in an aspect ratio that is said to be more pleasing to the eye than other aspect ratios.

The ratio of Fibonacci numbers fn+1/fn also approximates φ as n increases. The Golden Ratio and the Fibonacci numbers can be used in many ways to hold the human eye’s attention. The arrangement of branches of many trees loosely follow the sequence of Fibonacci numbers in efforts to maximize the amount of light their leaves can photosynthesize. The Mona Lisa—arguably the world’s most famous work of art—is among the notable applications of the Golden Ratio.

The Fibonacci numbers are named after an Italian mathematician born in the year 1170, but appeared in history much earlier in the work of Indian mathematics. Fibonacci used this sequence of numbers to describe the growth of a hypothetical rabbit population. As a tribute to this idea, add one more method to your program called printRabbit that outputs the following ascii art rabbit:

(\(\

( -.-) o (")(")

This rabbit art uses the backslash, dash, period, double quote, underscore, and lowercase o characters, as well as left and right parenthesis. Make sure to call printRabbit at the end of your main method.

When you have completed this assignment, you should understand:

How to design, compile, run and check a simple and complete Java program on your own.

The effect of escape sequences on printed strings.

How to write basic static methods.

The flow of control (i.e. the effects of method calls and expression evaluation).

How to format and document a Java program.

The Golden Ratio is used to help design aesthetically pleasing shapes by artists and architects, and also shows up in a surprising number of ways in nature. The Golden Ratio is also called the “Divine Proportion.” It is an irrational number (it cannot be written as the ratio of two integers), and is typically symbolized with the Greek letter φ (phi). As a continued fraction pattern, it is the simplest continued fraction, as it consists of all 1’s:

The pattern in the denominator repeats infinitely. The Golden Ratio is not typically included as a constant in part of a programming language’s package library, such as π = Math.PI in Java. One possible application of φ is as some part of a procedural graphics program (artwork generated by math formulae).

Write a Java program called GoldenRatio.java that does the following.

Create a variable of type double called phi. Set phi = (1 + Math.sqrt(5)) / 2.

Estimate φ:

create a variable of type double called approxPhi,

set approxPhi to 1.0,

set approxPhi to its reciprocal plus 1.0, (d) repeat Step (c) four more times.

Then print the following statements:

The Golden Ratio is approximately <value of approxPhi here.

The Golden Ratio is <value of phi here.

Compile your program and make sure there are no errors, and you have the above output. Your approximation should agree with the actual value of φ in the first two digits.

Next, approximate φ again in your program:

set approxPhi to 1.0,

set approxPhi to its reciprocal plus 1.0, (c) repeat Step (b) ninety-nine more times.

Then print the following statements:

Now for a more accurate approximation.

The Golden Ratio is approximately <value of approxPhi here.

The Golden Ratio is <value of phi here.

The Golden Ratio can be used to generate a famous sequence of numbers called the Fibonacci numbers. The first Fibonacci number is f1 = 1, and the second is f2 = 1. Any Fibonacci number is the sum of the previous two: fn+2 = fn+1 + fn, for n ≥ 3. Observe the first nine Fibonacci numbers:

1,1,2,3,5,8,13,21,34,...

After your previous code, calculate these Fibonacci numbers as follows. Declare and assign the following variables:

int f1 = 1;

int f2 = 1;

int fn = 0;

Write a for-loop with a loop-control variable i that iterates from 3 to 9, inclusive, with each iteration performing the following steps:

set fn equal to the sum of f1 and f2

set f1 equal to f2

set f2 equal to fn

out.println("f " + i + " = " + fn); (e) call checkFibonacci( approxPhi, i ) as described below, (f) call starArt( f1, f2 ) as described below. Now add the method checkFibonacci to your program:

public static void checkFibonacci( double approxPhi, int n ) { double A = Math.pow(approxPhi, n); double B = Math.pow(1 - approxPhi, n); double Fibonacci = (A - B) / Math.sqrt(5); long check fn = Math.round( Fibonacci );

System.out.println("check f " + n + " = " + check fn);

}

Add another method starArt to your program that takes two integer parameters, a height and a width. Inside starArt, use two nested loops to print as output a rectangle of star (*) characters, with just less than 2.5*width characters per line of height number of lines. (The terminal/console window typically has non-square character sizes, and so we need this adjustment in width to get proper visual comparison of width against height here.)

Compile your program and make sure there are no errors. The output should give the 3rd to the 9th Fibonacci number, with each number followed by a rectangle of stars in an aspect ratio that is said to be more pleasing to the eye than other aspect ratios.

The ratio of Fibonacci numbers fn+1/fn also approximates φ as n increases. The Golden Ratio and the Fibonacci numbers can be used in many ways to hold the human eye’s attention. The arrangement of branches of many trees loosely follow the sequence of Fibonacci numbers in efforts to maximize the amount of light their leaves can photosynthesize. The Mona Lisa—arguably the world’s most famous work of art—is among the notable applications of the Golden Ratio.

The Fibonacci numbers are named after an Italian mathematician born in the year 1170, but appeared in history much earlier in the work of Indian mathematics. Fibonacci used this sequence of numbers to describe the growth of a hypothetical rabbit population. As a tribute to this idea, add one more method to your program called printRabbit that outputs the following ascii art rabbit:

(\(\

( -.-) o (")(")

This rabbit art uses the backslash, dash, period, double quote, underscore, and lowercase o characters, as well as left and right parenthesis. Make sure to call printRabbit at the end of your main method.

You'll get 1 file (5.5KB)