Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Niranga Dilan
java
2 Answer(s)      3 years and 11 months ago
Posted in : Java Beginners


design a class named triangle that extends geometricobject.the class comtains: * Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle. * A no-arg constructor that creates a default triangle. * A constructor that creates a triangle with the specicied side1, 2, and 3. * the accessor methods for all three data fields. * A method naned getArea() that returns the area of this triangl


Advertisement
View Answers

February 10, 2011 at 2:53 PM


design a class named triangle that extends geometricobject.the class comtains:

  • Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle.
  • A no-arg constructor that creates a default triangle.
  • A constructor that creates a triangle with the specicied side1, 2, and 3.
  • the accessor methods for all three data fields.
  • A method named getArea() that returns the area of this triangle
  • A method named getParameter() that returns the perimeter of this triangle.
  • A method named toString() that returns description for the triangle.

For the formula to computer the area of a triangle, the toString() method is implemented as follows: return "triangle: side1=" + side1 + "side2=" + side2+"side3" + side3;



February 10, 2011 at 2:54 PM


Java Implement triangle class

import java.text.*;
class triangle {
    double side1=1;
    double side2=1;
    double side3=1;

    triangle(){
    }
    triangle(double s1,double s2,double s3){
        this.side1=s1;
        this.side2=s2;
        this.side3=s3;
    }
    public void setSide1(double side1){
        this.side1=side1;
    }
    public double getSide1(){
        return side1;
    }
    public void setSide2(double side2){
        this.side2=side2;
    }
    public double getSide2(){
        return side2;
    }
    public void setSide3(double side3){
        this.side3=side3;
    }
    public double getSide3(){
        return side3;
    }
    public double getArea(){
        double sum = side1 + side2 + side3;
        double s=sum/2;
        double a=s-side1;
        double b=s-side2;
        double c=s-side3;
        double sol=s*a*b*c;
        double area=Math.sqrt(sol);
        return area;
    }

    public static void main(String[] args){
        DecimalFormat df=new DecimalFormat("##.##");
        triangle t1=new triangle();
        System.out.println("Side1: "+t1.getSide1());
        System.out.println("Side2: "+t1.getSide2());
        System.out.println("Side3: "+t1.getSide3());
        System.out.println("Area: "+df.format(t1.getArea()));

        System.out.println();
        triangle t2=new triangle(8,5,7);
        System.out.println("Side1: "+t2.getSide1());
        System.out.println("Side2: "+t2.getSide2());
        System.out.println("Side3: "+t2.getSide3());
        System.out.println("Area: "+df.format(t2.getArea()));

    }
}


Related Tutorials/Questions & Answers:
java
java  diff bt core java and java
java
java  what is java
Advertisements
Java
Java   Whether Java is pure object oriented Language
java
java  different between java & core java
java
java  explain technologies are used in java now days and structure java
java
java   why iterator in java if we for loop
JAVA
JAVA  how the name came for java language as "JAVA
java
java  what is java reflection
java
java  is java open source
java
what is the size of array in java ?  what is the size of array in java ? what is the mean of finalize in java
java
java   in java does not pointers concept but what is nullpointers in java?   nullpointer is a runtime Exception
java
java  write a java program using filenotfoundexception
java
java   difference between class and interface
java
java  why multiple inheritance is not possible in java
java
java  is java purely object oriented language
java
java  how use java method
java
java  send me java interview questions
java
java  write a program in java to acess the email
java
java  Write a java code to print "ABABBABCABABBA
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  give a simple example for inheritance in java
Java
Java  how to do java in command prompt
java
java  sample code for RARP using java
java
java  RARP implementation using java socket
Java
Java  how to draw class diagrams in java
java
java  explain object oriented concept in java
Java
Java   Whether Java is Programming Language or it is SOftware
java
java  what are JAVA applications development tools
java
java  why to set classpath in java
Advertisements
 

 

 

DMCA.com