Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Objects and Classes

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Objects and Classes

    I am just trying to figure out basic classes/objects. I am trying to write a program where it will return the area of a triangle. I realize this can be done easier another way but I am just trying to figure out how these things work....


    import java.util.Scanner;
     
    class Triangle {
    	double height;
    	double base;
     
    	double getArea() {
    		return 1/2 * height * base;
    	}
    }
     
    public class study {
    	public static void main(String[]args) {
    		Triangle Triangle = new Triangle();
    		Scanner in = new Scanner(System.in);
    		System.out.println("Height?");
    		Triangle.height = in.nextInt();
    		System.out.println("Base?");
    		Triangle.base = in.nextInt();
    		Triangle.getArea();
    		System.out.println("The area of a triangle with height " + Triangle.height + " and base " + Triangle.base + " is " + Triangle.getArea()); 
    	}	
    }

    It keeps printing the null value 0.0 for area of a triangle.


  2. #2
    Junior Member
    Join Date
    May 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Objects and Classes

    try "return (0.5 * height * base);" instead of "return 1/2 * height * base;" It may be the 1/2 vs. 0.5

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Objects and Classes

    That worked. Thank you. Can't believe it was that simple.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,646
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: Objects and Classes

    1/2 is integer division which returns a value of 0. Use 0.5
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Objects and Classes
    By Kristenw17 in forum Object Oriented Programming
    Replies: 1
    Last Post: April 23rd, 2013, 12:43 AM
  2. Polymorphorism with array objects of different classes?
    By EDale in forum Collections and Generics
    Replies: 7
    Last Post: April 14th, 2013, 03:54 PM
  3. Classes and Array of objects
    By dynasty in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 27th, 2013, 01:53 AM
  4. Two problems (Dealing with Classes and Objects)
    By AustinStanley in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 4th, 2012, 07:17 PM
  5. Understanding Classes and Objects
    By AustinStanley in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 9th, 2012, 10:35 AM