Results 1 to 16 of 16
  1. #1
    Join Date
    Feb 2012
    Posts
    8
    Rep Power
    0

    Default Help with Methods and Logic

    Thank you for viewing this thread. I'm working on a program that call methods for a Triangle Program. This program returns the type of triangle after user input (sidea,sideb,sidec), then returns the perimeter and area.
    I'm have a issue getting the user input to override the constructor method that has to be set as 1.

    So instead of the output calculating user input appropriately. My output is this

    What type of triangle: equilateral triangle <-- being returned
    The area of the triangle is: 1.0 <-- being returned
    The perimeter of the triangle is: 3.0 <-- being returned

    Any help is greatly appreciated!


    Java Code:
     	public class TriangleTester
     	{
     	     
    	    public static void main(String args[])
     	    {
     	        Triangle test = new Triangle(1,1,1);
     			  Triangle test2 = new Triangle();
    			  double sidea;
    			  double sideb;
    			  double sidec;
    
    			  
     	         
     	        System.out.println("What type of triangle: " + test.test2());
     	        System.out.println("The area of the triangle is: " + test.getArea());
    	        System.out.println("The perimeter of the triangle is: " + test.getPerimeter());
    
    			   	     
     	    }
     	}


    ----------------------------------------------------------------------
    Java Code:
    public class Triangle 
    
    {
    
        
    
        private double sidea = 1.0;
        private double sideb = 1.0;
        private double sidec = 1.0;
          
    
        public Triangle()
    
        {
    
    
        }
    
        
    
        public Triangle(double sidea, double sideb, double sidec)
    
        {
    	 
        this.sidea = sidea;
        this.sideb = sideb;
        this.sidec = 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();	    
    
        }
    
    
    
        public double getArea()
    
        {
    
            return sidea * sideb * sidec;
    
        }
    
    
      
    
        public double getPerimeter()
    
        {
    
            return sidea + sideb + sidec;
    
        }
    
    	 public String test2()
    	 
    	 {
    	 
     	 if (((sidea * sidea) + (sideb * sideb)) == (sidec * sidec))
    	 {
    	 return "right triangle";
    	 }
    	 if (((sidea * sidea) + (sidec * sidec)) == (sideb * sideb))
    	 {
    	 return "right triangle";
    	 }
    	 if (((sidec * sidec) + (sideb * sideb)) == (sidea * sidea))
    	 {
    	 return "right triangle"; 
    
    	 }
    	 if ((sidea == sideb) && (sidea == sidec) && (sideb == sidec))
    	 {
    	 return "equilateral triangle";		
    		
    	 }
    	 if (((sidea == sideb) || (sideb == sidec) || (sidea == sidec)) && !((sidea == sideb) && (sidea == sidec) && (sideb == sidec)))
    	 {
    	 return "isosceles triangle";
    	 }
    
    	 if (!((sidea == sideb) && (sidea == sidec) && (sideb == sidec)))
    	 {
    	 return "scalene triangle";
    	 }	
    
    	 else
    	 return "not a triangle"; 
    	 
    	 
    	 }
    	 	 
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Help with Methods and Logic

    You'll want to get the Scanner code out of the Triangle constructor as it's throwing out the data entered in via the constructor's parameters. In fact the Triangle class shouldn't have any Scanner code or be concerned with direct user interaction as that type of code should be in other classes (likely your main method).

  3. #3
    Join Date
    Feb 2012
    Posts
    8
    Rep Power
    0

    Default Re: Help with Methods and Logic

    public class TriangleTester
    {

    public static void main(String args[])
    {
    Triangle test = new Triangle(1,1,1);
    Triangle test2 = new Triangle();
    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();

    System.out.println("What type of triangle: " + test.test2());
    System.out.println("The area of the triangle is: " + test.getArea());
    System.out.println("The perimeter of the triangle is: " + test.getPerimeter());


    }
    }
    Moved to main and same output as before

    What type of triangle: equilateral triangle <-- returned
    The area of the triangle is: 1.0 <-- returned
    The perimeter of the triangle is: 3.0 <-- returned

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Help with Methods and Logic

    That's the output that I would expect given your current code. What do you expect the output to be?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Help with Methods and Logic

    But note that your formula for area of a triangle is not the correct one given in most geometry textbooks.
    Also note that you're accepting input from the user and then discarding the input unused. I'm not sure what you want to do with it.

  6. #6
    Join Date
    Feb 2012
    Posts
    8
    Rep Power
    0

    Default Re: Help with Methods and Logic

    I was hoping for the user input to be run with test2,getArea and getPerimeter how could I accomplish this with my current code?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Help with Methods and Logic

    Suggestions:
    • Get the user input before creating your Triangle object.
    • Use the numbers that the user gives you when calling the Triangle's constructor. Currently you're calling one constructor with hard-code values of 1.
    • You probably only need to create one Triangle object, not two like you're currently doing.

  8. #8
    Join Date
    Feb 2012
    Posts
    8
    Rep Power
    0

    Default Re: Help with Methods and Logic

    Could you give me an example, I'm moving things around and its not working for me.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Help with Methods and Logic

    Quote Originally Posted by worldsbestbatman View Post
    Could you give me an example, I'm moving things around and its not working for me.
    No, better to work with your code. That's how you learn and that's how it works here.
    Show your latest best attempt and tell us what's wrong.

  10. #10
    Join Date
    Feb 2012
    Posts
    8
    Rep Power
    0

    Default Re: Help with Methods and Logic

    No problem here is my latest attempt, I appreciate the help!

    Java Code:
    import java.util.Scanner;
    public class Triangle 
    
    {
    
        
    
        private double sidea = 1.0;
        private double sideb = 1.0;
        private double sidec = 1.0;
          
    
        public Triangle()
    
        {
    
    
        }
    
        
    
        public Triangle(double sidea, double sideb, double sidec)
    
        {
    	 
        this.sidea = sidea;
        this.sideb = sideb;
        this.sidec = sidec;
        }
    
    
    
        public double getArea()
    
        {
    
            return sidea * sideb * sidec;
    
        }
    
    
      
    
        public double getPerimeter()
    
        {
    
            return sidea + sideb + sidec;
    
        }
    
    
    	public String Triangle()
    	 
    	 {
    	 
     	 if (((sidea * sidea) + (sideb * sideb)) == (sidec * sidec))
    	 {
    	 return "right triangle";
    	 }
    	 if (((sidea * sidea) + (sidec * sidec)) == (sideb * sideb))
    	 {
    	 return "right triangle";
    	 }
    	 if (((sidec * sidec) + (sideb * sideb)) == (sidea * sidea))
    	 {
    	 return "right triangle"; 
    
    	 }
    	 if ((sidea == sideb) && (sidea == sidec) && (sideb == sidec))
    	 {
    	 return "equilateral triangle";		
    		
    	 }
    	 if (((sidea == sideb) || (sideb == sidec) || (sidea == sidec)) && !((sidea == sideb) && (sidea == sidec) && (sideb == sidec)))
    	 {
    	 return "isosceles triangle";
    	 }
    
    	 if (!((sidea == sideb) && (sidea == sidec) && (sideb == sidec)))
    	 {
    	 return "scalene triangle";
    	 }	
    
    	 else
    	 return "not a triangle"; 
    	 
    	 
    	 }
    	 	 
    
    
    
    }

    Java Code:
    import java.io.*;
     	import java.util.*;
    	 
     	public class TriangleTester
     	{
     	     
    	    public static void main(String args[])
     	    {
     	        Triangle test = new triangle();
     			  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();	   
     	         
     	        System.out.println("What type of triangle: " + test.triangle());
     	        System.out.println("The area of the triangle is: " + test.getArea());
    	        System.out.println("The perimeter of the triangle is: " + test.getPerimeter());
    
    			   	     
     	    }
     	}

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Help with Methods and Logic

    First of all your code won't compile as written, will it? If you get compilation errors you must tell us.

    And you're still trying to create your Triangle before you get your input. You should get sidea, sideb, and sidec from the user first, then you create your Triangle object, but don't use the default constructor that takes no parameters, use the constructor that takes 3 numbers. What numbers should you be passing into your constructor?

    Also and again you will want to change your Triangle's getArea() method as your method is way too simplistic, and worse, it's wrong: that's not how you calculate the area of a triangle. Google this and you'll see the correct formula.

  12. #12
    Join Date
    Feb 2012
    Posts
    8
    Rep Power
    0

    Default Re: Help with Methods and Logic

    Sorry I meant to add this


    Java Code:
    TriangleTester.java:9: error: cannot find symbol
     	        Triangle test = new triangle();
     	                            ^
      symbol:   class triangle
      location: class TriangleTester
    TriangleTester.java:23: error: cannot find symbol
     	        System.out.println("What type of triangle: " + test.triangle());
     	                                                           ^
      symbol:   method triangle()

  13. #13
    Join Date
    Feb 2012
    Posts
    8
    Rep Power
    0

    Default Re: Help with Methods and Logic

    I want to pass the user input of sidea,sideb,sidea into my constructor

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Help with Methods and Logic

    Quote Originally Posted by worldsbestbatman View Post
    Java Code:
    TriangleTester.java:9: error: cannot find symbol
     	        Triangle test = new triangle();
     	                            ^
      symbol:   class triangle
      location: class TriangleTester
    TriangleTester.java:23: error: cannot find symbol
     	        System.out.println("What type of triangle: " + test.triangle());
     	                                                           ^
      symbol:   method triangle()
    So you're trying to create a Triangle object by calling a constructor named triangle. Capitalization matters and the constructor name must match the class name exactly, so your compiler is correct to complain.


    Quote Originally Posted by worldsbestbatman View Post
    I want to pass the user input of sidea,sideb,sidea into my constructor
    Exactly. So that's what you should do.

  15. #15
    Join Date
    Feb 2012
    Posts
    8
    Rep Power
    0

    Default Re: Help with Methods and Logic

    I figured it out!! thank you

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Help with Methods and Logic

    You're welcome and congratulations!

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Java Noob, trying to call methods from another methods
    By gabrielpr12 in forum New To Java
    Replies: 8
    Last Post: 11-17-2011, 09:07 PM
  3. Replies: 1
    Last Post: 10-23-2011, 12:37 PM
  4. Replies: 1
    Last Post: 10-05-2011, 12:15 AM
  5. Need help in logic
    By nn12 in forum New To Java
    Replies: 3
    Last Post: 03-23-2011, 06:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •