Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Triangle extends geometricObject
- {
- Scanner input = new Scanner(System.in);
- public static void main(String[] args){
- double side1 = 1; //All of these could have gone uninitialized, but the problem specifically stated it gets initialized to one
- double side2 = 1;
- double side3 = 1;
- double area; //Because we're using a method to calculate this, we need to store something in the main method
- //Otherwise, we wouldn't be able to store the value returned in the method
- double perimeter; //Same as area
- String color = ""; //Same as area, but Strings have to be initialized in Java.
- String answer = ""; //See above
- String talk = ""; //See above
- System.out.print("Side 1: ");
- 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
- //Essentially what's happening is that the code is looking at the Triangle2 method, but because the method has
- //(double first) instead of a blank paranthesis, it's saying "yo I can take some external input and utilize it
- //Cause remember, we can't return a value from the main method. We could have done this without the methods, but
- //I'm following the rules of the assignment.
- System.out.print("Side 2: ");
- side2 = Triangle3(side2); //Read side1
- System.out.print("Side 3: ");
- side3 = Triangle4(side3); //Read side1
- perimeter = getPerimeter(side1,side2,side3); //Similar to finding out the lengths of the sides, but it has three arguments.
- //Just like how normal code reads, it takes your value from first to last. So it's going to take
- //The first input and use it as the first input for the method and so on
- talk = toString(side1,side2,side3); //The same as the perimeter, but using a string. We had to create a variable so we could
- //use it in the output
- System.out.print("\nWhat color tho: ");
- color = geometricObject.getColor(color); //Same as the talk variable, but because we're calling from another file we have to put the
- //classname.methodname()
- System.out.print("\nIs it filled in?");
- answer = geometricObject.logic(answer); //Same as the talk variable
- System.out.print("\nThe area of this god damn triangle is " + getArea(side1,side2,side3) + "\nThe perimeter is: " + perimeter + talk);
- System.out.println("\n" + color);
- //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
- //System.out.print will also do whatever code you're trying to execute, though thats not its main function. It will function the same
- //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
- //value, because sometimes you will need to use it after you've calculated the new value. With strings, they usually don't change though
- if (answer == "Shits filled") //If statement so we know that it is what it needs to be
- {
- answer = "The triangle is filled in"; //answer was originally 'Shits filled' but because of the if statement, we can now change it
- }
- else
- {
- answer = "Tne triangle is not filled in"; //Same as before
- }
- System.out.println(answer); //Because we've now changed the string via the if statement, it will print what it needs to
- } //<--- Ugly ass bracket, fuck you jgrasp format yourself
- public static double Triangle2(double first) //First method we call. Name is irrelevant, but because we're throwing other variables into it
- //in the main method. We need to declare a variable so that we can then bring it into the equation
- //Again, now that I know there's the Triangle class itself, or I could have made an array, this could
- //be simplified to 1 method
- {
- double x = 1;
- Scanner stdin = new Scanner(System.in); //Simple scanner
- //side1 = side1 * first;
- x = stdin.nextInt();
- return x; //We need to return the variable so that side1 in main will now become the variable we entered
- //Without return, it will still do what we asked but it won't tell the main statement what it did
- }
- public static double Triangle3(double second) //Read Triangle2
- {
- double side2 = 1;
- Scanner stdin = new Scanner(System.in);
- side2 = stdin.nextInt();
- return side2;
- }
- public static double Triangle4(double third) //Read Triangle2
- {
- double side3 = 1;
- Scanner stdin = new Scanner(System.in);
- side3 = stdin.nextInt();
- return side3;
- }
- public static double getArea(double x, double y, double z) //Taking in all three variables. Like I said, when you call the function
- //It will read left to right. So side1 is now x, side2 is now y, etc.
- //xyz are now replaced with the values stored in side1,2,and 3. So we're
- //calculating area with the values stored in main
- {
- double area;
- area = (x + y + z)/2;
- return area;
- }
- public static double getPerimeter(double x, double y, double z) //See area
- {
- double perimeter;
- perimeter = x + y + z;
- return perimeter;
- }
- public static String toString(double x, double y, double z) //No calculations, but its doing the same shit as area and perimeter
- //But, we're creating the new string that we need to create with the variables
- //Because we're creating the string on the fly, we don't need to initialize
- //with nothing. We're initializing it with the full string.
- {
- double side1;
- double side2;
- double side3;
- side1 = x;
- side2 = y;
- side3 = z;
- String sides = "\nTriangle: side1 = " + side1 + " side 2 = " + side2 + " side3 = " + side3;
- return sides;
- }
- }
- import java.util.Scanner; //Needed to import the scanner so that we could type shit in
- public class geometricObject
- {
- public String color = "Yellow";
- //public boolean filled = true; //I commented this out because I couldn't use it in the other method
- public static String getColor(String color2) //Method to call in the Triangle class
- {
- Scanner stdin = new Scanner(System.in); //Scanner so we can type shit in
- //boolean filled = true; //Spaghetti code that I didn't realize I fucked up with
- String userColor = "";
- userColor = stdin.nextLine();
- return userColor;
- }
- public static String logic(String response) //So we can enter an answer and make it work
- {
- char answer; //This way we can make any answer okay
- boolean filled = false; //It's false, because being true made compilation errors
- Scanner input = new Scanner(System.in);
- 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
- answer = Character.toUpperCase(asdf.charAt(0)); //This takes the string, finds the first character, and makes it uppercase
- switch (answer)
- {
- case 'Y': filled = true; //Because we made the first character uppercase, answers like "Yes, Y, yes, y, No, N, no, n" all work
- break;
- case 'N': filled = false;
- break;
- default: System.out.print("Invalid input detected.\nProgram will end now. "); //End the switch statement if they put in a wrong character
- System.exit(0);
- break;
- }
- if (filled == true) //The switch statement assigned the boolean value
- {
- asdf = "Shits filled";
- }
- else
- {
- asdf = "Nah g";
- }
- return asdf; //Returns the String that
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement