Code in Java Exercise 1 of 3: Assume you want to build a farm house. You are get
ID: 3588206 • Letter: C
Question
Code in Java
Exercise 1 of 3:
Assume you want to build a farm house. You are getting three offers to buy land. The three pieces of land look as follows (measurements in yards):
Land 1 has a cost of $100 per square foot.
Land 2 has a cost of $125 per square foot.
Land 3 has a cost of $142 per square foot.
Calculate the costs to buy each land and print them in the console.
(watch out, the measurements on the plan are in yards, while the given prices are per square foot!)
Exercise 2 of 3:
In mathematics, the Fibonacci numbers are the numbers in an integer sequence, called the Fibonacci sequence, and are characterized by the fact that every number after the first two is the sum of the two preceding ones:
The value of each number can also be expressed as:
Write a program that calculates the first 20 Fibonacci numbers and prints them to console. Then, of those twenty numbers, print only those numbers to console again, which are also prime numbers.
Exercise 3 of 3:
Using nested loops, write a program the prints the following patterns to console:
1z0 70 Land 1 30 50 100 120 40 50 50 70 50 Land 3 50 10 Land 2 20Explanation / Answer
CostOfEachLand.java
public class CostOfEachLand {
public static void main(String[] args) {
//Declaring variables
int costOfSqftLand1 = 100, costOfSqftLand2 = 125, costOfSqftLand3 = 142;
//Calculating and displaying the cost of each land
System.out.println("Cost of land#1 is :$" + costOfSqftLand1 * ((100 * 70) + (120 * 20)));
System.out.println("Cost of land#2 is :$" + costOfSqftLand2 * ((50 * 50) + (10 * 55) + (120 * 30)));
System.out.println("Cost of land#3 is :$" + costOfSqftLand3 * ((70 * 50) + (0.5 * 50 * 40) + (0.5 * 50 * 50)));
}
}
_________________
Output:
Cost of land#1 is :$940000
Cost of land#2 is :$831250
Cost of land#3 is :$816500.0
____________________
2)
FibonacciNumbers.java
public class FibonacciNumbers {
public static void main(String[] args) {
System.out.println("Displaying the first 20 fibonacci Numbers :");
// Displaying the first 30 fibonacci numbers using for loop
for (int i = 0; i < 20; i++) {
System.out.print(fibonacci(i));
if (i < 19) {
System.out.print(", ");
}
}
}
// This method will generate the fibonacci numbers
public static int fibonacci(int n) {
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
}
_________________
Output:
Displaying the first 20 fibonacci Numbers :
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181
__________________
3)
a)
A.java
public class A {
public static void main(String[] args) {
for (int i = 0; i <= 8; i++)
{
for (int j = 0; j < i; j++)
{
System.out.print(" # ");
}
System.out.println(" ");
}
}
}
__________________________________________________
Output:
#
# #
# # #
# # # #
# # # # #
# # # # # #
# # # # # # #
# # # # # # # #
____________________________________________________
b)
B.java
public class B {
public static void main(String[] args) {
for (int i = 8; i >= 1; i--)
{
for (int j = 0; j < i; j++)
{
System.out.print(" # ");
}
System.out.println(" ");
}
}
}
______________________________________________
Output:
# # # # # # # #
# # # # # # #
# # # # # #
# # # # #
# # # #
# # #
# #
#
____________________________________________________
C)
C.java
public class C {
public static void main(String[] args) {
for (int i = 0; i < 8; i++)
{
for (int j = 1; j <= 8; j++)
{
if (i < j)
System.out.print("# ");
else
System.out.print(" ");
}
System.out.println();
}
}
}
________________________________________________
Output:
# # # # # # # #
# # # # # # #
# # # # # #
# # # # #
# # # #
# # #
# #
#
___________________________________________________
d)
D.java
public class D {
public static void main(String[] args) {
for (int i = 8; i >= 0; i--)
{
for (int j = 0; j < 8; j++)
{
if (i > j)
System.out.print(" ");
else
System.out.print(" #");
}
System.out.println(" ");
}
}
}
___________________________________
Output:
#
# #
# # #
# # # #
# # # # #
# # # # # #
# # # # # # #
# # # # # # # #
_____________________________________________
e)
E.java
public class E {
public static void main(String[] args) {
int n = 7;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if ( (i==0) || (j==0) || (i == n - 1) || (j == n - 1) )
{
System.out.print(" #");
}
else
{
System.out.print(" ");
}
} System.out.println();
}
}
}
_______________________________________
Output:
# # # # # # #
# #
# #
# #
# #
# #
# # # # # # #
_____________________________________
f)
package org.students;
public class F {
public static void main(String[] args) {
int n = 7;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if ( (i==j) || (i==0) || (i == n - 1))
{
System.out.print(" # ");
}
else
{
System.out.print(" ");
}
} System.out.println();
}
}
}
_______________________________________________
Output:
# # # # # # #
#
#
#
#
#
# # # # # # #
_____________________________________________
g)
G.java
public class G {
public static void main(String[] args) {
int n = 7;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if ( (i==0) || (i == n - 1) || (i + j == n - 1) )
{
System.out.print(" # ");
}
else
{
System.out.print(" ");
}
} System.out.println();
}
}
}
_______________________________________
Output:
# # # # # # #
#
#
#
#
#
# # # # # # #
_____________________________________________
h)
I.java
public class I {
public static void main(String[] args) {
int n = 7;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if((i == 0) || (i == j) || (i + j == n - 1) || (i == n - 1))
{
System.out.print(" # ");
}
else
{
System.out.print(" ");
}
} System.out.println();
}
}
}
_____________________________________________
Output:
# # # # # # #
# #
# #
#
# #
# #
# # # # # # #
___________________________________________
J)
J.java
public class J {
public static void main(String[] args) {
int n = 7;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if((i == 0) || (j == 0) || (i == n - 1) || (j == n - 1)|| (i == j) || (i + j == n - 1))
{
System.out.print(" # ");
}
else
{
System.out.print(" ");
}
} System.out.println();
}
}
}
____________________________________________
Output:
# # # # # # #
# # # #
# # # #
# # #
# # # #
# # # #
# # # # # # #
_____________Could you rate me well.Plz .Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.