1. (Population projection) The U.S. Census Bureau projects population based on the
following assumptions:
■ One birth every 7 seconds
■ One death every 13 seconds
■ One new immigrant every 45 seconds
Write a program to display the population for each of the next five years. Assume the
current population is 312,032,486 and one year has 365 days.
Hint: In Java, if two integers perform division, the result is an integer. The fractional part is truncated.
For example, 5 / 4 is 1 (not 1.25) and 10 / 4 is 2 (not 2.5). To get an accurate result with the fractional part, one of the values involved in the division must be a number with a decimal point. For example, 5.0 / 4 is 1.25 and 10 / 4.0 is 2.5.
Program Code:
//Population.java
public class Population
{//Main method
following assumptions:
■ One birth every 7 seconds
■ One death every 13 seconds
■ One new immigrant every 45 seconds
Write a program to display the population for each of the next five years. Assume the
current population is 312,032,486 and one year has 365 days.
Hint: In Java, if two integers perform division, the result is an integer. The fractional part is truncated.
For example, 5 / 4 is 1 (not 1.25) and 10 / 4 is 2 (not 2.5). To get an accurate result with the fractional part, one of the values involved in the division must be a number with a decimal point. For example, 5.0 / 4 is 1.25 and 10 / 4.0 is 2.5.
Program Code:
//Population.java
public class Population
{//Main method
public static void main(String[] args)
{
//Declare current population as variable population
double population = 312032486;
{
//Declare current population as variable population
double population = 312032486;
//add one
birth every 7 seconds; subtract one
//death
//add one birth every 7 seconds; subtract
one
//death every 13 seconds; add one
immigrant
//every 45 seconds.
//365 days = 1 year | 1 day = 24 hours
| 1 hour
//=60 minutes | 1 minute = 60 seconds
//7 seconds = 1 birth | 13 seconds = 1
death |
//45 seconds = 1
immigrant
//365days * 24hours * 60minutes *
60seconds =
//31536000 seconds
double
numberOfSecondsInYear = 31536000;
//After 1 year, number of births (7 seconds = 1
//birth | plus)
System.out.println("Current
population is
312032486");
//BirthsPerYear is calculated
double birthsPerYear =
numberOfSecondsInYear/7;
//Display BirthsPerYear
System.out.println("BrithsPerYear:"
+birthsPerYear);
//After 1
year, number of deaths (13 seconds = 1
//death | minus)
double deathsPerYear =
numberOfSecondsInYear / 13;
//Display
DeathsPerYear
System.out.println("DeathsPerYear:"
+deathsPerYear);
//After 1 year, number of immigrants
(45 seconds =
1 immigrant | plus)
double
immigrantsPerYear=numberOfSecondsInYear/45;
//Display
ImmigrantsPerYear
System.out.println("ImmigrantsPerYear:"
+ immigrantsPerYear);
//Rate per year
double ratePerYear =
birthsPerYear - deathsPerYear
+
immigrantsPerYear;
//Display the
Result
System.out.println("RatePerYear:
" + ratePerYear);
System.out.println("End of
year 1 the population
is"+(int)(intPop + (1 *
ratePerYear)));
System.out.println("End of
year 2 the population
is"+(int)(intPop + (2 *
ratePerYear)));
System.out.println("End of
year 3 the population
is"+(int)(intPop + (3 *
ratePerYear)));
System.out.println("End of
year 4 the population
is"+(int)(intPop + (4 *
ratePerYear)));
System.out.println("End of
year 5 the population
is"+(int)(intPop + (5 * ratePerYear)));
}//End of the main
}//End of the class
Output:
Current
population is 312032486
BrithsPerYear:
4505142.857142857
DeathsPerYear:
2425846.153846154
ImmigrantsPerYear:
700800.0
RatePerYear:
2780096.7032967033
End of year 1
the population is 314812582
End of year 2
the population is 317592679
End of year 3
the population is 320372776
End of year 4
the population is 323152872
End of year 5
the population is 325932969
_____________________________________________________________
2.(Algebra solve2*2 linear equation) you can use Cramer's rule to solve the following linear equation:
ax+by=e, cx+dy=f;
x=ed-bf/ad-bc; y=af-ed/ad-bc;
write a program that solves the following equation and display the value for x and y:
3.4x+ 50.2y= 44.5
2.1x+.55y=5.9
Program Code:
//CramersRule.java
import java.util.Scanner;
public class CramersRule
{
//Main method
public static void main(String[] args)
_____________________________________________________________
2.(Algebra solve2*2 linear equation) you can use Cramer's rule to solve the following linear equation:
ax+by=e, cx+dy=f;
x=ed-bf/ad-bc; y=af-ed/ad-bc;
write a program that solves the following equation and display the value for x and y:
3.4x+ 50.2y= 44.5
2.1x+.55y=5.9
Program Code:
//CramersRule.java
import java.util.Scanner;
public class CramersRule
{
//Main method
public static void main(String[] args)
{
// declare variables
double a, b, c, d, e, f, n1,
n2, denominator, x, y;
// create Scanner to read user
input
Scanner sc = new Scanner(System.in);
//Display the
values of x,y
System.out.print("The program solve 2x2
linear
equation."+"\n The
following equation and
display the value for x and y:
3.4x+50.2y
=5.9"+"\n
2.1x+.55y=5.9"+"\n a,b,c,d,e,f =
3.4,50.2,2.1,.55,44.5,5.9");
// prompt user to enter details
System.out.print("\n\nEnter a, b, c, d, e, f:
");
//Read the numbers a,b,c,d,e,f
of double datatype
a = sc.nextDouble();
b = sc.nextDouble();
c = sc.nextDouble();
d = sc.nextDouble();
e = sc.nextDouble();
f = sc.nextDouble();
//Assign the values of n1,n2
and denominator
n1 = ((e * d) - (b * f));
n2 = ((a * f) - (e * c));
denominator = ((a * d) - (b * c));
//calculate x and y
x = n1 / denominator;
y = n2 / denominator;
//Check the statement
if (denominator == 0)
{
//Display the equation has no
sol
System.out.println("\nThe equation
has no sol");
}
else
{
//Display the values of X and y
System.out.println("x is "+ x +"and y is "+y);
}//End of the statement
}//End of the main
}//End of the class
Output:
This program solves 2x2 linear equations.
The following equation and display the value for x and y:
3.4x + 50.2y = 44.5
2.1x + .55y = 5.9
a, b, c, d, e, f = 3.4, 50.2, 2.1, .55, 44.5, 5.9
Enter a, b, c, d, e, f: 3.4 50.2 2.1 .55 44.5 5.9
x is 2.623901496861419 and y is 0.7087397392563978
This program solves 2x2 linear equations.
The following equation and display the value for x and y:
3.4x + 50.2y = 44.5
2.1x + .55y = 5.9
a, b, c, d, e, f = 3.4, 50.2, 2.1, .55, 44.5, 5.9
Enter a, b, c, d, e, f: 3.4 50.2 2.1 .55 44.5 5.9
x is 2.623901496861419 and y is 0.7087397392563978
No comments:
Post a Comment