Advertisement
SnuggleWuggling

9 25 17

Sep 26th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.97 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Triangle extends geometricObject
  4. {
  5.  
  6. Scanner input = new Scanner(System.in);
  7.  
  8. public static void main(String[] args){
  9.  
  10. double side1 = 1; //All of these could have gone uninitialized, but the problem specifically stated it gets initialized to one
  11. double side2 = 1;
  12. double side3 = 1;
  13. double area; //Because we're using a method to calculate this, we need to store something in the main method
  14. //Otherwise, we wouldn't be able to store the value returned in the method
  15. double perimeter; //Same as area
  16. String color = ""; //Same as area, but Strings have to be initialized in Java.
  17. String answer = ""; //See above
  18.  
  19. String talk = ""; //See above
  20.  
  21.  
  22. System.out.print("Side 1: ");
  23. side1 = Triangle2(side1); //Now that I know there is a triangle class, I could have called (used) 1 method, but time was of the essence
  24. //Essentially what's happening is that the code is looking at the Triangle2 method, but because the method has
  25. //(double first) instead of a blank paranthesis, it's saying "yo I can take some external input and utilize it
  26. //Cause remember, we can't return a value from the main method. We could have done this without the methods, but
  27. //I'm following the rules of the assignment.
  28.  
  29. System.out.print("Side 2: ");
  30. side2 = Triangle3(side2); //Read side1
  31.  
  32. System.out.print("Side 3: ");
  33. side3 = Triangle4(side3); //Read side1
  34.  
  35. perimeter = getPerimeter(side1,side2,side3); //Similar to finding out the lengths of the sides, but it has three arguments.
  36. //Just like how normal code reads, it takes your value from first to last. So it's going to take
  37. //The first input and use it as the first input for the method and so on
  38.  
  39. talk = toString(side1,side2,side3); //The same as the perimeter, but using a string. We had to create a variable so we could
  40. //use it in the output
  41.  
  42. System.out.print("\nWhat color tho: ");
  43.  
  44. color = geometricObject.getColor(color); //Same as the talk variable, but because we're calling from another file we have to put the
  45. //classname.methodname()
  46.  
  47. System.out.print("\nIs it filled in?");
  48.  
  49. answer = geometricObject.logic(answer); //Same as the talk variable
  50.  
  51.  
  52. System.out.print("\nThe area of this god damn triangle is " + getArea(side1,side2,side3) + "\nThe perimeter is: " + perimeter + talk);
  53. System.out.println("\n" + color);
  54.  
  55. //Remember, getArea(side1,side2,side3) is perfectly fine. Like I said, I wouldn't do area in this, but I was doing a string with this
  56. //System.out.print will also do whatever code you're trying to execute, though thats not its main function. It will function the same
  57. //as if you were like area = getArea(side1, side2, side3) in a seperate line of code. Like I said, I don't like doing this with a numerical
  58. //value, because sometimes you will need to use it after you've calculated the new value. With strings, they usually don't change though
  59.  
  60. if (answer == "Shits filled") //If statement so we know that it is what it needs to be
  61. {
  62. answer = "The triangle is filled in"; //answer was originally 'Shits filled' but because of the if statement, we can now change it
  63. }
  64.  
  65. else
  66. {
  67. answer = "Tne triangle is not filled in"; //Same as before
  68. }
  69.  
  70. System.out.println(answer); //Because we've now changed the string via the if statement, it will print what it needs to
  71.  
  72. } //<--- Ugly ass bracket, fuck you jgrasp format yourself
  73.  
  74. public static double Triangle2(double first) //First method we call. Name is irrelevant, but because we're throwing other variables into it
  75. //in the main method. We need to declare a variable so that we can then bring it into the equation
  76. //Again, now that I know there's the Triangle class itself, or I could have made an array, this could
  77. //be simplified to 1 method
  78. {
  79. double x = 1;
  80. Scanner stdin = new Scanner(System.in); //Simple scanner
  81.  
  82. //side1 = side1 * first;
  83.  
  84. x = stdin.nextInt();
  85.  
  86. return x; //We need to return the variable so that side1 in main will now become the variable we entered
  87. //Without return, it will still do what we asked but it won't tell the main statement what it did
  88.  
  89. }
  90.  
  91. public static double Triangle3(double second) //Read Triangle2
  92. {
  93. double side2 = 1;
  94. Scanner stdin = new Scanner(System.in);
  95.  
  96. side2 = stdin.nextInt();
  97. return side2;
  98.  
  99. }
  100.  
  101. public static double Triangle4(double third) //Read Triangle2
  102. {
  103. double side3 = 1;
  104. Scanner stdin = new Scanner(System.in);
  105.  
  106. side3 = stdin.nextInt();
  107. return side3;
  108. }
  109.  
  110.  
  111.  
  112.  
  113. public static double getArea(double x, double y, double z) //Taking in all three variables. Like I said, when you call the function
  114. //It will read left to right. So side1 is now x, side2 is now y, etc.
  115. //xyz are now replaced with the values stored in side1,2,and 3. So we're
  116. //calculating area with the values stored in main
  117. {
  118. double area;
  119. area = (x + y + z)/2;
  120.  
  121. return area;
  122. }
  123.  
  124. public static double getPerimeter(double x, double y, double z) //See area
  125. {
  126. double perimeter;
  127. perimeter = x + y + z;
  128. return perimeter;
  129. }
  130.  
  131. public static String toString(double x, double y, double z) //No calculations, but its doing the same shit as area and perimeter
  132. //But, we're creating the new string that we need to create with the variables
  133. //Because we're creating the string on the fly, we don't need to initialize
  134. //with nothing. We're initializing it with the full string.
  135. {
  136.  
  137. double side1;
  138. double side2;
  139. double side3;
  140.  
  141. side1 = x;
  142. side2 = y;
  143. side3 = z;
  144. String sides = "\nTriangle: side1 = " + side1 + " side 2 = " + side2 + " side3 = " + side3;
  145. return sides;
  146. }
  147.  
  148. }
  149.  
  150.  
  151.  
  152. import java.util.Scanner; //Needed to import the scanner so that we could type shit in
  153.  
  154. public class geometricObject
  155. {
  156. public String color = "Yellow";
  157. //public boolean filled = true; //I commented this out because I couldn't use it in the other method
  158.  
  159.  
  160. public static String getColor(String color2) //Method to call in the Triangle class
  161. {
  162.  
  163. Scanner stdin = new Scanner(System.in); //Scanner so we can type shit in
  164. //boolean filled = true; //Spaghetti code that I didn't realize I fucked up with
  165.  
  166.  
  167. String userColor = "";
  168. userColor = stdin.nextLine();
  169.  
  170. return userColor;
  171. }
  172.  
  173. public static String logic(String response) //So we can enter an answer and make it work
  174. {
  175. char answer; //This way we can make any answer okay
  176. boolean filled = false; //It's false, because being true made compilation errors
  177.  
  178. Scanner input = new Scanner(System.in);
  179.  
  180.  
  181. String asdf = input.nextLine(); //If we had it read from the char, then any extra input would cause the system to crash on the next line
  182. answer = Character.toUpperCase(asdf.charAt(0)); //This takes the string, finds the first character, and makes it uppercase
  183.  
  184. switch (answer)
  185. {
  186. case 'Y': filled = true; //Because we made the first character uppercase, answers like "Yes, Y, yes, y, No, N, no, n" all work
  187.  
  188.  
  189. break;
  190.  
  191. case 'N': filled = false;
  192.  
  193. break;
  194.  
  195. default: System.out.print("Invalid input detected.\nProgram will end now. "); //End the switch statement if they put in a wrong character
  196. System.exit(0);
  197. break;
  198. }
  199.  
  200. if (filled == true) //The switch statement assigned the boolean value
  201. {
  202. asdf = "Shits filled";
  203.  
  204. }
  205.  
  206. else
  207. {
  208. asdf = "Nah g";
  209. }
  210.  
  211. return asdf; //Returns the String that
  212.  
  213. }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement