The Triangle Class

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 23032 Views - Last Post: 05 March 2012 - 11:23 AM Rate Topic: -----

#1 Joego  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 28
  • Joined: 01-February 10

The Triangle Class

Posted 19 March 2010 - 05:00 PM

This is what I have to do:

Design a class named "Trianlge" that extends "GeometricObject". The class contain:

Three double data fields named "side1,side2, and side3" with default value 1.0 to denote three sides of the triangle.

A no-arg contructor that creates a default triangle.

A constructor that creates a triangle with the specified side1,side2, and side3.

The accessor methods for all three data fields.

A method named getArea() that returns the area of the triangle.

A method named getPerimeter() that returns the Perimeter of the triangle.

A method named toString() that returns a string description of the triangle.

The toString() method is implemented as follows:
return "Triangle: side1 = " + side1 + "side2 = " + side2 + "side3 " + side3;

Draw the UML diagram that involes the classes Triangle and GeometricObject. Implement the class. Write a test program that creates a Triangle object with sides 1,1.5,1, setting color yellow, and filled true, and displays the area, perimeter, color, and whether filled or not.

This is what I have complete: (Let me know if I have everything!!) Thank You!!!!
public class GeometricObject
{
private String color = "Yellow";
private Boolean filled = true;

public String getColor( )
{   
return color;
}
	
public void setColor(String color)  
{   
this.color = color; 
}

public boolean isFilled( )
{ 
return filled; 
}

public void setFilled(boolean filled)
{   
this.filled = filled; 
}


public String toStrings( ) 
{   
return  " Color: " + color + " and filled: " + filled;
}

}




public class Triangle extends GeometricObject
{
private double side1 = 1.0;
private double side2 = 1.0;
private double side3 = 1.0;

public Triangle( ) {}

public Triangle (double side1, double side2, double side3)
{this.side1=side1; this.side2=side2; this.side3=side3;}

public double getSide1( )  {return side1;}
public double setSide2( )  {return side2;}
public double setSide3( )  {return side3;}
public void setSide1 (double side1)  {this.side1=side1;}
public void setSide2 (double side2)  {this.side2=side2;}
public double getArea( ) { return (side1+side2+side3)/2;}
public double getPerimeter( ) { return side1+side2+side3;}
public String toString( ) { return "Side 1 = " + side1 + " Side 2 = " + side2 + " Side 3 = " + side3;  }

}




public class TestTriangle	
{
public static void main (String [ ] args)	
{
Triangle Triangle = new Triangle(1,1.5,1);
System.out.println("The Triangle Sides are "  + Triangle.toString());	
System.out.println("The Triangle’s Area is "  + Triangle.getArea());
System.out.println("The Triangle’s Perimeter is " +  Triangle.getPerimeter());
System.out.println("The Triangle's Color is " + Triangle.getColor());
System.out.println("The Triangle is " + Triangle.isFilled());
}
}



Is This A Good Question/Topic? 1
  • +

Replies To: The Triangle Class

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: The Triangle Class

Posted 19 March 2010 - 05:04 PM

Seems OK to me
Do you have any specific question ?
Was This Post Helpful? 1
  • +
  • -

#3 Joego  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 28
  • Joined: 01-February 10

Re: The Triangle Class

Posted 19 March 2010 - 05:11 PM

Here is the UML: Let me know if I did it correctly!!! Thank YoU!!

GeometricObject

-color: String
-filled: Boolean

+GeometricObject( )
+getColor ( ): String
+setColor ( color: String): void
+isFilled ( ): boolean
+setFilled (filled: Boolean): void
+toString ( ): String





Triangle

-side1: double
-side2: double
-side3: double

+Triangle( )
+Triangle( side1: double.side2: double,side3: double)
+getSide1( ): double
+getSide2( ): double
+getSide3( ): double
+setSide1( side1: double): void
+setSide2( side2: double): void
+setSide3( side3: double): void
+getArea( ):double
+getPerimeter( ): double
+toString ( ): String

View Postpbl, on 19 March 2010 - 04:04 PM, said:

Seems OK to me
Do you have any specific question ?


I just want ot make sure I have everything that it needs!! It works I just want to make sure has everything!
Thank You!!!
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: The Triangle Class

Posted 19 March 2010 - 05:19 PM

as far as the UML is concerned... know nothing about it sorry :)
Was This Post Helpful? 1
  • +
  • -

#5 Joego  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 28
  • Joined: 01-February 10

Re: The Triangle Class

Posted 19 March 2010 - 05:27 PM

View Postpbl, on 19 March 2010 - 04:19 PM, said:

as far as the UML is concerned... know nothing about it sorry :)


Thank You!!!

So my program is good!!! Thank You again!!!
Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: The Triangle Class

Posted 19 March 2010 - 05:39 PM

Joego, he says he doesn't know UML...
You actually are missing a couple of thing :(

Here's my checklist:
-color: String                    -
-filled: Boolean                  -

+GeometricObject( )               -missing
+getColor ( ): String             -
+setColor ( color: String): void  -
+isFilled ( ): boolean            -
+setFilled (filled: Boolean): void-
+toString ( ): String             - 



Also, you may want to verify to make sure your Booleans aren't supposed to be booleans. Also, you are missing a method in your Triangle class
-side1: double              -
-side2: double              -
-side3: double              -

+Triangle( )                -
+Triangle( side1: double.side2: double,side3: double)
                            -
+getSide1( ): double        - 
+getSide2( ): double        -
+getSide3( ): double        -
+setSide1( side1: double): void -
+setSide2( side2: double): void - 
+setSide3( side3: double): void -missing
+getArea( ):double          -
+getPerimeter( ): double    - 
+toString ( ): String       - 


Was This Post Helpful? 2
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: The Triangle Class

Posted 19 March 2010 - 05:40 PM

View PostJoego, on 19 March 2010 - 06:27 PM, said:

View Postpbl, on 19 March 2010 - 04:19 PM, said:

as far as the UML is concerned... know nothing about it sorry :)


Thank You!!!

So my program is good!!! Thank You again!!!

UML was invented just a few years ago
I programmed 37 years without using it
Seems very hot in the pedagogic area but haven't seem them in the field yet :) but if it is what they teach you in school... we will have nice diagrams and hopefully the code to represent them
Was This Post Helpful? 1
  • +
  • -

#8 Joego  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 28
  • Joined: 01-February 10

Re: The Triangle Class

Posted 19 March 2010 - 06:00 PM

View PostDogstopper, on 19 March 2010 - 04:39 PM, said:

Joego, he says he doesn't know UML...
You actually are missing a couple of thing :(

Here's my checklist:
-color: String                    -
-filled: Boolean                  -

+GeometricObject( )               -missing
+getColor ( ): String             -
+setColor ( color: String): void  -
+isFilled ( ): boolean            -
+setFilled (filled: Boolean): void-
+toString ( ): String             - 



Also, you may want to verify to make sure your Booleans aren't supposed to be booleans. Also, you are missing a method in your Triangle class
-side1: double              -
-side2: double              -
-side3: double              -

+Triangle( )                -
+Triangle( side1: double.side2: double,side3: double)
                            -
+getSide1( ): double        - 
+getSide2( ): double        -
+getSide3( ): double        -
+setSide1( side1: double): void -
+setSide2( side2: double): void - 
+setSide3( side3: double): void -missing
+getArea( ):double          -
+getPerimeter( ): double    - 
+toString ( ): String       - 




I added those parts...Thank You!!
Also, what is a method you are talking about????
Was This Post Helpful? 0
  • +
  • -

#9 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: The Triangle Class

Posted 19 March 2010 - 06:03 PM

// This is a class
class Example {

    // This is a variable (or field)
    private int var = 1;
   
    // This is a Constructor
    public Example() {
        ...
    }
 
    // This is a method
    public int getVar() {
        return var;
    }
}


Was This Post Helpful? 1
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: The Triangle Class

Posted 19 March 2010 - 06:06 PM

View PostDogstopper, on 19 March 2010 - 06:39 PM, said:

Joego, he says he doesn't know UML...
You actually are missing a couple of thing :(

Here's my checklist:
-color: String                    -
-filled: Boolean                  -

+GeometricObject( )               -missing
+getColor ( ): String             -
+setColor ( color: String): void  -
+isFilled ( ): boolean            -
+setFilled (filled: Boolean): void-
+toString ( ): String             - 



Also, you may want to verify to make sure your Booleans aren't supposed to be booleans. Also, you are missing a method in your Triangle class
-side1: double              -
-side2: double              -
-side3: double              -

+Triangle( )                -
+Triangle( side1: double.side2: double,side3: double)
                            -
+getSide1( ): double        - 
+getSide2( ): double        -
+getSide3( ): double        -
+setSide1( side1: double): void -
+setSide2( side2: double): void - 
+setSide3( side3: double): void -missing
+getArea( ):double          -
+getPerimeter( ): double    - 
+toString ( ): String       - 


Thank God we have a UML expert on board :) +1 Doggy for full response
Was This Post Helpful? 1
  • +
  • -

#11 Joego  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 28
  • Joined: 01-February 10

Re: The Triangle Class

Posted 19 March 2010 - 06:08 PM

View PostDogstopper, on 19 March 2010 - 05:03 PM, said:

// This is a class
class Example {

    // This is a variable (or field)
    private int var = 1;
   
    // This is a Constructor
    public Example() {
        ...
    }
 
    // This is a method
    public int getVar() {
        return var;
    }
}



How would I fit that in my program?
Was This Post Helpful? 0
  • +
  • -

#12 Joego  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 28
  • Joined: 01-February 10

Re: The Triangle Class

Posted 19 March 2010 - 06:14 PM

[quote name='Joego' date='19 March 2010 - 05:08 PM' timestamp='1269047321' post='964719']
[quote name='Dogstopper' date='19 March 2010 - 05:03 PM' timestamp='1269047018' post='964714']
// This is a class
class Example {

    // This is a variable (or field)
    private int var = 1;
   
    // This is a Constructor
    public Example() {
        ...
    }
 
    // This is a method
    public int getVar() {
        return var;
    }
}



Would it be like this:

public double getArea( ) 
{ 
double area = (side1+side2+side3)/2;
return area;
}

public double getPerimeter( ) 
{
double perimeter = side1+side2+side3;
return perimeter;
}



Was This Post Helpful? 0
  • +
  • -

#13 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: The Triangle Class

Posted 19 March 2010 - 07:06 PM

yep! :)

Or even make it a one-liner:
public double getArea( ) 
{ 
    return(side1+side2+side3)/2;
}

public double getPerimeter( ) 
{
    return side1+side2+side3;
}



Just stick hose methods in the classes specified in the UML
Was This Post Helpful? 1
  • +
  • -

#14 Joego  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 28
  • Joined: 01-February 10

Re: The Triangle Class

Posted 19 March 2010 - 07:31 PM

View PostDogstopper, on 19 March 2010 - 06:06 PM, said:

yep! :)

Or even make it a one-liner:
public double getArea( ) 
{ 
    return(side1+side2+side3)/2;
}

public double getPerimeter( ) 
{
    return side1+side2+side3;
}



Just stick hose methods in the classes specified in the UML



Thank You!

So If I put that stuff in there!! The rest of my program is good to go!!??
Was This Post Helpful? 0
  • +
  • -

#15 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: The Triangle Class

Posted 19 March 2010 - 07:32 PM

Yep!
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2