Ask your own question, for FREE!
Computer Science 15 Online
OpenStudy (anonymous):

methods

OpenStudy (woodrow73):

Hey

OpenStudy (anonymous):

hey

OpenStudy (anonymous):

Write a program that will read an arbitrary number of sets of triangle sides using only integer values. The program should: 1. Prompt the user for sets of numbers and process them until the user submits the numbers 0 0 0, which will terminate the program. 2. For each set of three numbers, the program should print the values read. 3. For each set of three numbers, the program should decide if the numbers represent the sides of a valid triangle. 4. If the numbers could not represent a valid triangle, display an appropriate error message. 5. If the numbers are valid, the program should determ

OpenStudy (anonymous):

this is what i am working on

OpenStudy (anonymous):

I want to create my own methods to calculate the Pythagorean Theorem, then do the side type and then angle type then print all the info about the triangle together.

OpenStudy (anonymous):

so, how would I set up the part inside the method to make it so that it identifies it, but then doens't print anything until the end.

OpenStudy (woodrow73):

you can just call they pythagorean method at the end and print it there?

OpenStudy (anonymous):

Right, but then I would be fitting in the angle and side classification to the Pythagorean method? So it would end up being just one method???

OpenStudy (woodrow73):

eh doesn't have to be. If you keep it as it is now, and just have the pythagorean method 'return' the information to the main method-- then you can manipulate that information when/where you want.

OpenStudy (anonymous):

so how would I code that, as in how would I code so that the method returns the info to the main method?

OpenStudy (woodrow73):

``` public class HypotenuseCalc { public static void main(String[]args) { double hypotenuse = 0; int side1 = 4; int side2 = 3; hypotenuse = pythag(side1, side2); System.out.print(hypotenuse); playWithHypotenuse(hypotenuse); } public static double pythag(int s1, int s2) { return Math.sqrt((Math.pow(s1, 2)) + (Math.pow(s2, 2))); } public static void playWithHypotenuse(int hyp) { int bignum = hyp*1000000; System.out.println("\nHypotenuse*1million= " + bignum); } } ``` using methods is an art, and takes on more levels of meaning when you learn the significance of static, inheritance ect... That may be the syntax, though I didn't explain what each part did exactly - let me know what parts you want me to clarify

OpenStudy (woodrow73):

note- change the parameter in method playWithHypotenuse to (double hyp) & change bignum to a double. Mistakes

OpenStudy (e.mccormick):

You could make each triangle an object and have methods on it to say if it is valid or not. Or, yo can make a boolian method that returns true or false based on evaluation the triangle. Also, I don;t know why you said Pythagorean. It does not say these are right traingles.

OpenStudy (anonymous):

true but the Pythagorean theorem also determines whether the angle is obtuse acute or right based on if a^2 + b^2 is equal to greater than or less than c^2

OpenStudy (e.mccormick):

Is that the rest of #5? Cause that got cut off...

OpenStudy (e.mccormick):

You could return a variable code or string. A numberic code would be 0 for invalid, 1 for accute, etc. A string would be just the descriptive word.

OpenStudy (e.mccormick):

And, woodrow73 ``` public static double pythag(int s1, int s2) { return Math.sqrt((Math.pow(s1, 2)) + (Math.pow(s2, 2))); ``` That is not needed for the types of evaluations this is doing. A simple comparison of s1*s1 + s2*s2 to s3*s3.

OpenStudy (e.mccormick):

Well, you need to find the longest (largest) first...

OpenStudy (lyrae):

To solve the intial problem of determing if the three values makes a valid triangle I suggest that you take look at the Triangle Inequality Theorem.

OpenStudy (anonymous):

yes, got that.

OpenStudy (anonymous):

so how would I do a loop so that if an invalid triangle is given, I keep asking the user to input a correct value until valid output is given?

OpenStudy (anonymous):

and how would I do a boolean so that if .... = true else = false sort of thing

OpenStudy (woodrow73):

By valid input for the triangles, would you be testing for whether 1) all 3 inputs are numbers 2) all 3 inputs are positive 3) all 3 inputs are not decimals?

OpenStudy (woodrow73):

& by your boolean statement- could you elaborate more?

OpenStudy (e.mccormick):

You can use an if/else to get the results of the true/false. Might want to sort the input before sending to the method so you can say what the shorter and longer are if needed.

OpenStudy (e.mccormick):

A boolean method, in general: ``` public static boolean someMethod(int a){ if (a > 5) return true; return false; } ``` This could be used in main code in the form of: ``` if someMethod(userInput) { System.out.println("You input a number greater than 5."); } else { System.out.println("You input 5 or less."); } ``` While this is a trivial example, I hope it shows how a boolean methiod can be used to trigger an if/else inside a different block of code. A more common use of this would be using if a file handle opens, do the work on the flie but else send a message to the user that it was not valid.

OpenStudy (e.mccormick):

@woodrow73 For any triangle, the sum of two sides cn not be shorter than the third. |dw:1416274544856:dw|

OpenStudy (woodrow73):

a ha.. good point.

OpenStudy (anonymous):

public class Triangle { public static boolean someMethod(int a, int b, int c){ if (a + b > c && b + c > a && a + c > b) return true; return false; } public Triangle sideLengths (double side1, double side2, double side3) { System.out.println("Please enter the value for side 1."); side1 = In.getDouble(); side2 = In.getDouble(); side3 = In.getDouble(); if someMethod System.out.println("Please enter a valid number."); else System.out.print(side1 + side2 + side3); }

OpenStudy (anonymous):

public class TriangleClassification { public static void main [String [] args) { System.out.println("Please provide three side lengths of a triangle. Enter 0 0 0 to terminate the program."); double side1 = In.getDouble(); double side2 = In.getDouble(); double side3 = In.getDouble(); } do { if (side1 == side2 == side3) { System.out.println("This triangle is equilateral."); } else if (side1 == side2 != side3 || side1 != side2 == side3 || side2 != side1 == side3) { System.out.println("This triangle is isoceles."); } else if (side1 != side2 != side3) { System.out.println("This triangle is scalene."); } else if (side1 <= 0 || side2 <= 0 || side3 <= 0) { System.out.println("Error! Invalid entry!"); { while (side1 != 0 && side2 !=0 && side3 != 0); { System.out.println("The program was terminated by the user."); } } //I have created a method to calculate the Pythagorean Theorem in order to determine whether the traingle is acute, obtuse or right. public static double Pythagorean Theorem() { if (side1 > side2 && side1 > side3) double c = side1; double a = side2; double b = side3; else if (side2 > side1 && side2 > side3) double c = side2; else if (side3 > side1 && side3 > side2) double c = side3; if ((c*c) = Math.sqrt((a*a) + (b * b))); { System.out.println("This triangle is a right triangle."); } if ((c*c) < Math.sqrt((a*a) + (b*b))); { System.out.println("This triangle is an acute triangle."); } else { System.out.println("This triangle is obtuse."); } }

OpenStudy (anonymous):

I realize that I need a loop for prompting the user and another loop around the whole program until program is terminated by user, and I have to put them together as methods, but I'm having some issues working that into what I have so far.

OpenStudy (woodrow73):

for starters - the closing bracket in your do - while loop is facing the wrong way -- should be: ``` } while(boo); ``` instead of: ``` { while(boo); ```

OpenStudy (woodrow73):

and your do-while loop will continue looping as long as none of the 3 sides equal to 0.

OpenStudy (e.mccormick):

In addition to what woodrow73 pointed out, you did your code as raw text. Sadly, raw text does not copy very well due to some issues with how information is displayed on OpenStudy. If you put a ``` (the one with the ~ on the key) above and below the code, it fixes this. It also does code highlighting. Other ways of fixing this are to use http://dpaste.com/ or http://pastebin.com/ Without being able to copy the code, it can be hard to test it.

OpenStudy (anonymous):

public boolean isIsosceles() if (side1 == side2 != side3 || side2 == side3 != side1|| side1 == side3 != side2) { return true; } else { return false; }

OpenStudy (anonymous):

@e.mccormick would this be acceptable? To do a boolean method for each classification (isoceles, equilateral and scalene, obtuse, acute and right angle) and then at the end have it print Ex: You triangle is obtuse and isoceles. If so, how would I go about doing that at the end?

OpenStudy (e.mccormick):

You could just return the type as a string and use one function to type them. Inside it, if it needs helpers, that is fine too.

OpenStudy (anonymous):

Right, it would be returned at the end as a string though but I couldn't just do this kind of setup because then it would print the classifications at different times, right? if (side1 == side2 == side3) { System.out.println("This triangle is equilateral."); } else if (side1 == side2 != side3 || side1 != side2 == side3 || side2 != side1 == side3) { System.out.println("This triangle is isoceles."); } else if (side1 != side2 != side3) { System.out.println("This triangle is scalene."); } else if (side1 <= 0 || side2 <= 0 || side3 <= 0) { System.out.println("Error! Invalid entry!");

OpenStudy (anonymous):

So how would you recommend approaching this problem using methods?

OpenStudy (e.mccormick):

You can return a string. Don't print it. Return it.

OpenStudy (e.mccormick):

As an incomplete example: ``` public String triangleType(int a, int b, int c) // or whatever your inputs are { \\ if some evaluation return new String("equilateral") \\ else if some evaluation return new String("scalene") \\ else if some evaluation return new String("invalid") } ``` That will just pass back a word. So you make a string in the main method or whatever that is the typeOfTriangle. typeOfTriangle = triangleType(5, 10, 7) Perhaps the input will need to be angles or the order things are put in may need to be highest to lowest.

OpenStudy (anonymous):

Ok, so after I have created all of these methods for classification, I can just return them in the main method like you said, and enclose the entire program with a do while loop if I want the program to keep repeating until the user enters 0 0 0 in which case it would be termianted?

OpenStudy (e.mccormick):

Yah. So all the main has is the while loop and calls to the other methods.

OpenStudy (e.mccormick):

To be object based, each triangle under evaluation would be an object with methods to talk about it... but for what you are doing, static methinds should work very well and are probably less coding!

OpenStudy (anonymous):

Yes! Thank you!

OpenStudy (anonymous):

So here is what I have up to this point public class Triangle { public static void main (String [] args) { public static void setSides() { System.out.println("Please enter three side lengths for a triangle. Enter 0 0 0 to terminate the program."); int side1 = In.getInt(); int side2 = In.getInt(); int side3 = In.getInt(); } public static void isPossible() if (side1 + side2 > side3 && side2 + side3 > side1 && side1 + side3 > side2) { return true; } else } return false; } public String sideType(int side1, int side2, int side3) { \\ if (side1 == side2 == side3) return new String("equilateral") \\ else if (side1 != side2 != side3) return new String("scalene") \\ else if (side1 == side2 != side3 || side2 == side3 != side1 || side1 == side3 != side2) return new String("isoceles") } public String angleType (int side1, int side2, int side3) { if (side1*side1 + side2*side2 == side3*side3 || side1*side1 + side3*side3 == side2*side2 || side2*side2 + side3*side3 == side1*side1) return new String("right angle") else if (side1*side1 + side2*side2 > side3*side3 || side1*side1 + side3*side3 > side2*side2 || side2*side2 + side3*side3 > side1*side1) return new String("acute") else if (side1*side1 + side2*side2 < side3*side3 || side1*side1 + side3*side3 < side2*side2 || side2*side2 + side3*side3 < side1*side1) return new String("obtuse") } So, in order for the program to check, after receiving input is the triangle is possible before moving on and printing an error message accordingly, should this be part of the the setSide value, or should I have isPossible as part of my if statements in the other methods, or how would that work?

OpenStudy (e.mccormick):

Well, in mine, I did that as comments, so the cod won't work. It was just a template for you to see how you could go about it. You need real code rather than comments. Hehe. Also, it is basically impossible to read the code in a post. And if I try to copy it out, it becomes one line because you don't use the code highlighting system...

OpenStudy (e.mccormick):

See, to me, your code becomes: public class Triangle { public static void main (String [] args) { public static void setSides() { System.out.println("Please enter three side lengths for a triangle. Enter 0 0 0 to terminate the program."); int side1 = In.getInt(); int side2 = In.getInt(); int side3 = In.getInt(); } public static void isPossible() if (side1 + side2 > side3 && side2 + side3 > side1 && side1 + side3 > side2) { return true; } else } return false; } public String sideType(int side1, int side2, int side3) { \\ if (side1 == side2 == side3) return new String("equilateral") \\ else if (side1 != side2 != side3) return new String("scalene") \\ else if (side1 == side2 != side3 || side2 == side3 != side1 || side1 == side3 != side2) return new String("isoceles") } public String angleType (int side1, int side2, int side3) { if (side1*side1 + side2*side2 == side3*side3 || side1*side1 + side3*side3 == side2*side2 || side2*side2 + side3*side3 == side1*side1) return new String("right angle") else if (side1*side1 + side2*side2 > side3*side3 || side1*side1 + side3*side3 > side2*side2 || side2*side2 + side3*side3 > side1*side1) return new String("acute") else if (side1*side1 + side2*side2 < side3*side3 || side1*side1 + side3*side3 < side2*side2 || side2*side2 + side3*side3 < side1*side1) return new String("obtuse") }

OpenStudy (anonymous):

oh ok let me fix that. Also, wouldn't your template work though the same way except without comment slashes?

OpenStudy (anonymous):

OpenStudy (anonymous):

I am using a third party, dr. java so I just attached it since it doesn't really work using copy paste

OpenStudy (e.mccormick):

If you put ``` above and below, the copy paste works fine...

OpenStudy (e.mccormick):

``` public class Triangle { public static void main (String [] args) { public static void setSides() { System.out.println("Please enter three side lengths for a triangle. Enter 0 0 0 to terminate the program."); int side1 = In.getInt(); int side2 = In.getInt(); int side3 = In.getInt(); } public static void isPossible() if (side1 + side2 > side3 && side2 + side3 > side1 && side1 + side3 > side2) { return true; } else } return false; } public String sideType(int side1, int side2, int side3) { \\ if (side1 == side2 == side3) return new String("equilateral") \\ else if (side1 != side2 != side3) return new String("scalene") \\ else if (side1 == side2 != side3 || side2 == side3 != side1 || side1 == side3 != side2) return new String("isoceles") } public String angleType (int side1, int side2, int side3) { if (side1*side1 + side2*side2 == side3*side3 || side1*side1 + side3*side3 == side2*side2 || side2*side2 + side3*side3 == side1*side1) return new String("right angle") else if (side1*side1 + side2*side2 > side3*side3 || side1*side1 + side3*side3 > side2*side2 || side2*side2 + side3*side3 > side1*side1) return new String("acute") else if (side1*side1 + side2*side2 < side3*side3 || side1*side1 + side3*side3 < side2*side2 || side2*side2 + side3*side3 < side1*side1) return new String("obtuse") } ``` Like that.

OpenStudy (e.mccormick):

Your scaline test is flawed.

OpenStudy (e.mccormick):

With that order, something can be isoceles but reported as scalene.

OpenStudy (anonymous):

oops yeah, I'll fix that. Thanks for catching that. So, to follow up with what you said before, would the sideType and angleType work ok as they are?

OpenStudy (e.mccormick):

Is this an integer based program? No float/double?

OpenStudy (e.mccormick):

I ask because == and floating point do not play nice.

OpenStudy (anonymous):

yes, this is integer based thankfully

OpenStudy (e.mccormick):

Well, do some test cases and see what you get. Rather than use input, you can do a test harness... as in call the functions with predefined valyes and just print the reuslts. If one breaks, you know what broke it.

OpenStudy (anonymous):

I keep getting an error message when trying to compile before each new method saying illegal start of expression.

OpenStudy (anonymous):

I'm also getting class interface and enum expected before the methods

OpenStudy (e.mccormick):

Does dr. java gibe any pre-cmpile error help?

OpenStudy (e.mccormick):

enum expected generally means it thinks you need something to be declared. It can also mean something is mispelled.

OpenStudy (anonymous):

no, unfortunately it provides no error help. Let me check for any undeclared variables.

OpenStudy (e.mccormick):

If that is the entire code, I can toss it into Eclipse.

OpenStudy (anonymous):

That would be great if you could, also do you think this would be caused by having multiple methods with int side1, int side2 and int side3??

OpenStudy (e.mccormick):

No, they are local to each.

OpenStudy (anonymous):

right, ok good that's what I thought.

OpenStudy (e.mccormick):

There is a lot missing to this code.... hmmm.... int side1 = In.getInt(); But I do not see In.

OpenStudy (anonymous):

oh i think that is in the java utilities package so I should probably put that at the start of the program.

OpenStudy (e.mccormick):

Yep, and that would be enumerating it...

OpenStudy (e.mccormick):

public static void isPossible() You have a return in that. Voids can't return anything. If you need to to return true, it must be a bool, and if it needs to return an int it must be an int, etc. public static int isPossible(){ bla return some_int } public static double isPossible(){ bla return some_double } those are "valid" as opposed to the void with a return.

OpenStudy (anonymous):

ok so if I were to have the method return true as a boolean, how would I make it so that an error message pritns right after input is given saying that the triangle is not possible instead of continuing on with the rest of the program?

OpenStudy (e.mccormick):

You need some sort of end condition that is triggered by the return of the bool. Like an if statement that breaks the while loop if it returns false.

OpenStudy (e.mccormick):

Or if it returns true... It depends on the method. Break on a true result: if (is_bad()) break; Break on a false result if (is_good() == false) break;

OpenStudy (anonymous):

ok let me try that

OpenStudy (e.mccormick):

Are they having you import some class for all the projects you make?

OpenStudy (e.mccormick):

I ask, cause usually inpit from the user needs something like: Scanner sc=new Scanner(System.in); int side1 = sc.nextInt(); int side2 = sc.nextInt(); int side3 = sc.nextInt();

OpenStudy (anonymous):

yes they are having us import it for all projects so far but like you said the scanner is what is usually used.

OpenStudy (e.mccormick):

OK, then that other package must have things like In, which is why Eclipse is yelling at me. LOL.

OpenStudy (anonymous):

Yes! it does contain In, but it still beats at least pasting the entire In file in before every program lol

OpenStudy (e.mccormick):

Yes, code reuse is important. but custom libraries mean my IDE does not know everything it needs to debug this.

OpenStudy (anonymous):

hmm.... it's not liking my code so far. 25 errors lol

OpenStudy (e.mccormick):

Well, rememebr that the class holds everything and that your main only holds itself. The other methofds are inside the class but outside the main.

OpenStudy (anonymous):

``` import java.util.Scanner; public class trianglef { public static void main(String[] args) { double sidea; double sideb; double sidec; Scanner in = new Scanner(System.in); System.out.println("Enter the first side of a triangle: "); sidea = in.nextDouble(); System.out.println("Enter the second side of a triangle: "); sideb = in.nextDouble(); System.out.println("Enter the third side of a triangle: "); sidec = in.nextDouble(); if (((sidea + sideb) <= (sidec)) ||((sidea + sideb) <= (sideb)) ||((sideb +sidec) <= (sidea))) do { System.out.println("Not a traingle. Please try again"); sidea = in.nextDouble(); sideb = in.nextDouble(); sidec = in.nextDouble(); } while (sidea + sideb <= sidec || sidea + sidec <= sideb || sideb + sidec <= sidea); System.out.println("Sides" + " " + sidea + " " + sideb + " " + sidec); if (((sidea * sidea) + (sideb * sideb)) == (sidec * sidec)) System.out.println("right triangle"); if (((sidea * sidea) + (sidec * sidec)) == (sideb * sideb)) System.out.println("right triangle"); if (((sidec * sidec) + (sideb * sideb)) == (sidea * sidea)) System.out.println("right triangle"); //right triangle if ((sidea == sideb) && (sidea == sidec) && (sideb == sidec)) System.out.println("equilateral triangle"); //equilateral triangle if (((sidea == sideb) || (sideb == sidec) || (sidea == sidec)) && !((sidea == sideb) && (sidea == sidec) && (sideb == sidec))) System.out.println("isosceles triangle"); else if (!((sidea == sideb) && (sidea == sidec) && (sideb == sidec))) System.out.println("scalene triangle"); //scalene if (sidea*sidea + sideb*sideb == sidec*sidec || sidea*sidea + sidec*sidec == sideb*sideb || sideb*sideb + sidec*sidec == sidea*sidea) System.out.println("right angle"); else if (sidea*sidea + sideb*sideb > sidec*sidec || sidea*sidea + sidec*sidec > sideb*sideb || sideb*sideb + sidec*sidec > sidea*sidea) System.out.println("acute"); else if (sidea*sidea + sideb*sideb < sidec*sidec || sidea*sidea + sidec*sidec < sideb*sideb || sideb*sideb + sidec*sidec < sidea*sidea) System.out.println("obtuse"); } } ```

OpenStudy (anonymous):

This is the code I have so far because I couln't get it to work when it was individual methods so how would I change the code above so that it uses method overloading?

OpenStudy (woodrow73):

It would be hard to have multiple methods 'overloaded' because you would always have the same parameters. I'd either make multiple methods.. one called testEquilateral() one called testIsosceles() or make a big method that you call once & have it return an int getTriangleInfo() method - could return 1 for equilateral -- 2 for Isosceles -- 3 for scalene.

OpenStudy (anonymous):

Ok I'll try that. I had that before and when I try to compile I get a lot of "illegal start of the expression" at the start of each method. So each method would be set up like public static void isEquilateral() for instance and then in the main method I would just be doing public static void main (String [] args) { isequilateral() isIsoceles() isScalene() } ? And then how would I set up a final print statement to print the side type, angle type and side lengths entered together?

OpenStudy (woodrow73):

well, for those methods to actually test the triangle, you have to put the 3 int values in the method arguments; such as: ``` isequilateral(sidea, sideb, sidec); isIsoceles(sidea, sideb, sidec); isScalene(sidea, sideb, sidec); ``` don't forget the semi-colons at the end & in order to get the information, the methods should return a boolean variable, as to whether it is equilateral/isosceles/scalene

OpenStudy (woodrow73):

This requires special 'syntax' when you actually make the method too - since you will need to make sure it a) has a return value && b) takes 3 ints into it's parameters

OpenStudy (anonymous):

so the return value would be a string in these cases because I want it to return a print statement at the end? So public static String isEquilateral(); for instance?

OpenStudy (woodrow73):

Ya, that works.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!