Skip to content

Commit

Permalink
completing missing Exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
HarryDulaney committed May 13, 2020
1 parent bc6a3e6 commit 93339b4
Show file tree
Hide file tree
Showing 9 changed files with 262 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.idea/
/out/
/java-prog-dan-lang-10th.iml
40 changes: 40 additions & 0 deletions src/ch_02/Ex0215.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package ch_02;
import java.util.Scanner;
/*
2.15 (Geometry: distance of two points) Write a program that prompts the user to enter
two points (x1, y1) and (x2, y2) and displays their distance between them.
The formula for computing the distance is sqrt[(x2 - x1)^2 + (y2 - y1)^2)]. Note that
you can use Math.pow(a, 0.5) to compute sqrt(a). Sample run:
Enter x1 and y1: 1.5 -3.4
Enter x2 and y2: 4 5
The distance between the two points is 8.764131445842194
*/


public class Ex0215 {

public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter x1 and y1: ");
double x1, x2, y1, y2;

x1 = input.nextDouble();
y1 = input.nextDouble();

System.out.println("Enter x2 and y2: ");

x2 = input.nextDouble();
y2 = input.nextDouble();

double exs = Math.pow((x2 - x1),2);
double whys = Math.pow((y2 - y1),2);

double a = Math.pow((exs + whys),0.5);

System.out.println("The distance between the two points is " + a);


}

}
22 changes: 22 additions & 0 deletions src/ch_02/Ex0216.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ch_02;
/*
2.16 (Geometry: area of a hexagon) Write a program that prompts the user to enter the
side of a hexagon and displays its area.
*/

import java.util.Scanner;


public class Ex0216 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.println("Enter a decimal for the length of the side of a hexagon: ");
double side = input.nextDouble();

double operand = (3 * Math.pow(3, 0.5)) / 2;
double res = operand * Math.pow(side, 2);
System.out.println("The area of the hexagon is " + res);

}
}
38 changes: 38 additions & 0 deletions src/ch_02/Ex0217.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ch_02;

import java.util.Scanner;
/*
*2.17 (Science: wind-chill temperature) How cold is it outside? The temperature alone
is not enough to provide the answer. Other factors including wind speed, relative
humidity, and sunshine play important roles in determining coldness outside.
In 2001, the National Weather Service (NWS) implemented the new wind-chill
temperature to measure the coldness using temperature and wind speed. The
formula is
twc = 35.74 + 0.6215ta - 35.75v0.16 + 0.4275tav0.16
where ta is the outside temperature measured in degrees Fahrenheit and v is the
speed measured in miles per hour. twc is the wind-chill temperature. The formula
cannot be used for wind speeds below 2 mph or temperatures below -58 ºF or
above 41ºF.
Write a program that prompts the user to enter a temperature between -58 ºF and
41ºF and a wind speed greater than or equal to 2 and displays the wind-chill
*/

public class Ex0217 {
public static void main(String[] args){
Scanner input = new Scanner(System.in);

System.out.println("Enter the temperature in Fahrenheit between -58°F and 41°F: ");
double temp = input.nextDouble();

System.out.println("Enter the wind speed (>=2) in miles per hour: ");
int windSpeed = input.nextInt();

double vToPow16 = Math.pow(windSpeed,0.16);

double twc = 35.74 + 0.6215 * temp - 35.75 * vToPow16 + 0.4275 * temp * vToPow16;
System.out.println("The wind chill index is " + twc);

}


}
29 changes: 29 additions & 0 deletions src/ch_02/Ex0218.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ch_02;

/*
2.18 (Print a table) Write a program that displays the following table. Cast floating point
numbers into integers.
a b pow(a, b)
1 2 1
2 3 8
3 4 81
4 5 1024
5 6 15625
*/

public class Ex0218 {
public static void main(String[] args){
System.out.println("a b pow(a,b)");
int a,b;
a = 1; b =2;
System.out.println(a + " " + b + " " + (int)Math.pow(a,b));
a++; b++;
System.out.println(a + " " + b + " " + (int)Math.pow(a,b));
a++; b++;
System.out.println(a + " " + b + " " + (int)Math.pow(a,b));
a++; b++;
System.out.println(a + " " + b + " " + (int)Math.pow(a,b));
a++; b++;
System.out.println(a + " " + b + " " + (int)Math.pow(a,b));
}
}
31 changes: 31 additions & 0 deletions src/ch_02/Ex0220.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ch_02;

import java.util.Scanner;

/*
*2.20 (Financial application: calculate interest) If you know the balance and the annual
percentage interest rate, you can compute the interest on the next monthly payment
using the following formula:
interest = balance * (annualInterestRate/1200)
Write a program that reads the balance and the annual percentage interest rate and
displays the interest for the next month.
* Example:
* Enter balance and interest rate (e.g., 3 for 3%): 1000 3.5
The interest is 2.91667
*/
public class Ex0220 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter balance and interest rate: (e.g., 3 for 3%): ");

double balance = input.nextDouble();
double rate = input.nextDouble();
double interest = balance * (rate/1200);
double roundedInterest = Math.round(interest * 100000)/100000.0;
System.out.println("The interest rate is: " + roundedInterest);




}
}
35 changes: 35 additions & 0 deletions src/ch_02/Exercise0209.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ch_02;

import java.util.Scanner;

/*2.9 (Physics: acceleration) Average acceleration is defined as the change of velocity
divided by the time taken to make the change, as shown in the following formula:
a =
v1 - v0
t
Write a program that prompts the user to enter the starting velocity v0 in meters/
second, the ending velocity v1 in meters/second, and the time span t in seconds,
and displays the average acceleration. Here is a sample run:
*/

public class Exercise0209 {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.println("Enter v0, v1, and t: ");
double v0,v1,t;

v0 = input.nextDouble();
v1 = input.nextDouble();
t = input.nextDouble();

double a = (v1 - v0) / t;

System.out.println("Acceleration is " + a);


}


}
34 changes: 34 additions & 0 deletions src/ch_02/Exercise0211.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package ch_02;

/*
2.11 (Population projection) Rewrite Programming Exercise 1.11 to prompt the user
to enter the number of years and displays the population after the number of years.
Use the hint in Programming Exercise 1.11 for this program. The population
should be cast into an integer.
*/

import java.util.Scanner;

public class Exercise0211 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of years: ");
int numYears = input.nextInt();

int currentPopulation = 312_032_486;
double daysInYear = 365.00;
double secInYear = daysInYear * 24 * 60 * 60;
double birthsPerYear = 7 / 60.0 * secInYear;
System.out.println(birthsPerYear);
double deathsPerYear = 13 / 60.0 * secInYear;
double imigrationPerYear = 45 / 60.0 * secInYear;
System.out.println(deathsPerYear);
System.out.println(imigrationPerYear);

double changePerYear = imigrationPerYear + birthsPerYear - deathsPerYear;
double totalPopAfterYears = changePerYear * numYears;
int result = (int) (totalPopAfterYears + currentPopulation);
System.out.println("The population in " + numYears + " years will be " + result);
System.out.println("Since the change per year is " + (int)(changePerYear));
}
}
30 changes: 30 additions & 0 deletions src/ch_02/Exercise0212.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ch_02;

import java.util.Scanner;

/*
(Physics: finding runway length) Given an airplane’s acceleration a and take-off
speed v, you can compute the minimum runway length needed for an airplane to
take off using the following formula:
length =v^2 / 2*a
Write a program that prompts the user to enter v in meters/second (m/s) and the
acceleration a in meters/second squared (m/s2), and displays the minimum runway
length.
*/
public class Exercise0212 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.println("Enter speed and acceleration: ");
int v = input.nextInt();
double acc = input.nextDouble();

double numerator = Math.pow(v, 2);
double denominator = 2 * acc;
double minRunwayLength = numerator / denominator;
System.out.println("The minimum runway length for this airplane is "
+ minRunwayLength);


}
}

0 comments on commit 93339b4

Please sign in to comment.