daniel_lawson9999

Daniel Lawson Java Chapter 3 Exercises 6-22 even

Apr 3rd, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. /**
  2. * Created by Daniel on 4/3/2016.
  3. */
  4. //-------------------------------------------------------------------------------------------------------------------------------------
  5. //ex6
  6. public class largerAbsValue6 {
  7. public static void main(String[] args){
  8. System.out.println(largerAbsVal(-4,3)); // println statement to test method
  9.  
  10.  
  11. }
  12. public static int largerAbsVal(int value1, int value2){
  13. return Math.max(Math.abs(value1),Math.abs(value2));
  14.  
  15. }
  16. }
  17. //-------------------------------------------------------------------------------------------------------------------------------------
  18. //ex8
  19. /**
  20. * Created by Daniel on 4/3/2016.
  21. */
  22. public class quadratic8 {
  23. public static void main (String[] args){
  24. System.out.println(quadratic(1,-7,12)); // println statement to test method
  25. }
  26. public static String quadratic(int a, int b, int c){
  27. double xplus = (b*-1 + Math.sqrt((b*b)-(4*a*c)*1.0)) / 2*a ;
  28. double xminus = (b*-1 - (Math.sqrt((b*b)-(4*a*c)*1.0)) ) / 2*a ;
  29. String answer = "x = " + (int)xplus + "," + "x = " + (int)xminus;
  30. return answer;
  31. }
  32. }
  33.  
  34. //-------------------------------------------------------------------------------------------------------------------------------------
  35. //ex10
  36. /**
  37. * Created by Daniel on 4/3/2016.
  38. */
  39. public class area10 {
  40. public static void main (String[] args){
  41. System.out.println(area(2.0));
  42. }
  43. public static double area(double r){
  44. double area = Math.PI*(r*r);
  45. return area;
  46.  
  47. }
  48. }
  49.  
  50.  
  51.  
  52.  
  53. //-------------------------------------------------------------------------------------------------------------------------------------
  54. //ex12
  55.  
  56. /**
  57. * Created by Daniel on 4/3/2016.
  58. */
  59. public class scientific12 {
  60. public static void main(String[] args){
  61. System.out.println(scientific(6.23,5)); // println statement to test method
  62. }
  63. public static double scientific (double n, int e){
  64. double answer = n * Math.pow(10.0,e);
  65. return answer;
  66. }
  67. }
  68.  
  69.  
  70. //-------------------------------------------------------------------------------------------------------------------------------------
  71. //ex14
  72. /**
  73. * Created by Daniel on 4/3/2016.
  74. */
  75. public class cylinderSurfaceArea14 {
  76. public static void main(String[] args){
  77. System.out.println(cylinderSurfaceArea(3.0,4.5)); //println statement to test method
  78. }
  79. public static double cylinderSurfaceArea(double r,double h){
  80. double SA = (2*Math.PI*(r*r)) + (2*Math.PI*r*h);
  81. return SA;
  82. }
  83. }
  84.  
  85.  
  86.  
  87. //-------------------------------------------------------------------------------------------------------------------------------------
  88. //ex16
  89. /**
  90. * Created by Daniel on 4/3/2016.
  91. */
  92. public class triangleArea16 {
  93. public static void main(String[] args){
  94. System.out.println(triangleArea(8,5.2,7.1)); //println statement to test method
  95. }
  96. public static double triangleArea(double a, double b, double c){
  97. double s = (a + b + c)/2;
  98. double area = Math.sqrt( s*(s-a)*(s-b)*(s-c));
  99. return area;
  100. }
  101. }
  102.  
  103.  
  104. //-------------------------------------------------------------------------------------------------------------------------------------
  105. //ex18
  106. /**
  107. * Created by Daniel on 4/3/2016.
  108. */
  109. public class vertical18 {
  110. public static void main(String[] args){
  111. vertical("hey now"); // calling the method to test it
  112. }
  113. public static void vertical(String letters){
  114. for(int i =1; i<= letters.length(); i++){
  115. System.out.println(letters.charAt(i-1));
  116. }
  117. }
  118. }
  119.  
  120.  
  121. //-------------------------------------------------------------------------------------------------------------------------------------
  122. //ex20
  123. /**
  124. * Created by Daniel on 4/3/2016.
  125. */
  126.  
  127. import java.util.*;
  128.  
  129. public class inputBirthday20 {
  130. public static void main(String[] args) {
  131. Scanner console = new Scanner(System.in);
  132. inputBirthday(console);
  133.  
  134. }
  135.  
  136. public static void inputBirthday(Scanner con) {
  137. System.out.print("On what day of the month were you born?");
  138. String day = con.next();
  139. System.out.print("What is the name of the month in which you were born?");
  140. String month = con.next();
  141. System.out.print("During what year were you born?");
  142. String year = con.next();
  143. System.out.println("You were born on " + month + " " + day + ", " + year + ".");
  144.  
  145. }
  146. }
  147.  
  148.  
  149.  
  150.  
  151. //-------------------------------------------------------------------------------------------------------------------------------------
  152. //ex22
  153. /**
  154. * Created by Daniel on 4/3/2016.
  155. */
  156. //*note this program would have been MUCH easier with if statements instead of making for loops behave like them
  157. import java.util.*;
  158. public class CollegeAdmit22 {
  159. public static void main(String[] args){
  160. Scanner console = new Scanner(System.in);
  161. calcGPA(console);
  162. }
  163. public static void calcGPA(Scanner con){
  164. int gpaTruth = 0;
  165. int satTruth = 0;
  166. System.out.println("University admission program!");
  167. System.out.print("What is your GPA?");
  168. double gpa = con.nextDouble();
  169. System.out.print("What is your SAT score");
  170. double sat = con.nextDouble();
  171. //*note this program would have been MUCH easier with if statements instead of making for loops behave like them
  172. for(double i= gpa; i >= 1.8 && i>0; i -= i){ // test to see if gpa is passing
  173. gpaTruth = 1;
  174. }
  175. for(double j = sat;j >=900 && j >0;j-=j){ // test ot see if sat is passing
  176. satTruth = 1;
  177. }
  178. for(int i = 1; i == satTruth && i == gpaTruth;i = 0){ // if both are passing
  179. System.out.println("You were accepted");
  180. }
  181. for(int i = 0; i == satTruth || i == gpaTruth;i = 3){ // if one of them is not passing.
  182. System.out.println("You were not accepted"); // I originally used booleans instead of i=3, but then it would loop for ever
  183. }
  184.  
  185. }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment