该类包含:
>
一个无参数构造函数,用于创建具有指定边1、边2和边3的三角形。
所有三个数据字段的访问器方法。
名为getArea()的方法,该方法返回此三角形的区域。
一个名为GetPermission()的方法,返回此三角形的周长。
*一个名为toString()的方法,返回三角形的字符串描述。
有关计算三角形面积的公式,请参见编程练习2.15(java编程入门第9版)。toString()方法的实现如下:
返回"三角形:边1="边1"边2="边2"边3"边3;
为类三角形和几何对象绘制UML图,并实现这些类。编写一个测试程序,提示用户输入三角形的三条边、颜色和布尔值,以指示三角形是否已填充。程序应该用这些边创建一个三角形对象,并使用输入设置颜色和填充属性。该程序应显示面积、周长、颜色和真或假,以指示填充与否。
我在练习中遇到麻烦的部分是测试程序。
如果你能帮助我,告诉我如何从输入中生成三角形,我将非常感激。
我目前的代码是:
几何对象
public class GeometricObject {
private String color = " white ";
private boolean filled;
private java.util.Date dateCreated;
public GeometricObject() {
dateCreated = new java.util.Date();
}
public GeometricObject(String color, boolean filled) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
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 java.util.Date getDateCreated() {
return dateCreated;
}
public String toString() {
return "Created on " + dateCreated + "\n color: " + color + " and 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 setSide1() {
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 void setSide3(double side3) {
this.side3 = side2;
}
public double getArea() {
return (side1 + side2 + side3) / 2;
}
public double getPerimeter() {
return side1 + side2 + side3;
}
public String toString() {
return " Triangle: Side 1 = " + side1 + " Side 2 = " + side2
+ " Side 3 = " + side3;
}
}
测试程序。
import java.util.Scanner;
public class TestTriangle {
private double side1 = 1.0;
private double side2 = 1.0;
private double side3 = 1.0;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter three sides of the Triangle");
double side1 = input.nextDouble();
double side2 = input.nextDouble();
double side3 = input.nextDouble();
System.out.println("Enter the color of the Triangle");
String color = input.next();
System.out.println(" Is the Triangle filled? Reply with 'True' or 'False' ");
String filled = input.next();
}
{
new Triangle(side1, side2, side3);
//How do i get the information into theTriangle?
System.out.println("The Triangle Sides are \n side 1: " + side1 + "\n Side 2: " + side2 + "\n Side 3: " + side3);
System.out.println("The Triangle's Area is " + (side1 + side2 + side3) / 2);
System.out.println("The Triangle's Perimeter is "
+ (side1 + side2 + side3));
System.out.println("The Triangle's Color is " + //what goes here?);
System.out.println("Is the Triangle filled? " + //what goes here?);
}
}
几何对象
package geometricobject;
import java.util.Scanner;
/**
*
* @author Kirubel
*/
abstract class GeometricObject
{
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
/** Construct a default geometric object */
protected GeometricObject()
{
}
/** Construct a geometric object with color and filled value */
protected GeometricObject(String color, boolean filled)
{
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
/** Return color */
public String getColor()
{
return color;
}
/** Set a new color */
public void setColor(String color)
{
this.color = color;
}
/** Return filled. Since filled is boolean ,
* the get method is named isFilled */
public boolean isFilled()
{
return filled;
}
/** Set a new filled */
public void setFilled(boolean filled)
{
this.filled = filled;
}
/** Get dateCreated */
public java.util.Date getDateCreated()
{
return dateCreated;
}
@Override
public String toString() {
return "created on " + dateCreated + "\ncolor: " + color +
" and filled: " + filled;
}
/** Abstract method getArea */
public abstract double getArea();
/** Abstract method getPerimeter */
public abstract double getPerimeter();
/**
* @param args the command line arguments
*/
}
三角形
class Triangle extends GeometricObject
{
public double side1;
public double side2;
public double side3;
public Triangle() //default constructor
{
side1=1.0;
side2=1.0;
side3=1.0;
}
public Triangle(double side1,double side2,double side3) //peremetrised constructor
{
this.side1=side1;
this.side2=side2;
this.side3=side3;
}
@Override
public double getPerimeter() //function for getting peremeter for triangle
{
return(side1+side2+side3) ;
}
@Override
// function for getting area of triangle
public double getArea()
{
double area,s;
s=side1+side2+side3;
area = Math.sqrt(s * (s- side1) * (s - side2) * (s - side3));
return(area);
}
@Override
public String toString()//to print the data
{
String s;
s="Triangle:side1 "+ side1 +" side2 " + side2 + " side3 "+side3;
return(s);
}
private String getSide1() {
// TODO Auto-generated method stub
return null;
}
private String getPerimeter(double d) {
// TODO Auto-generated method stub
return null;
}
private String getPerimeter(String string) {
// TODO Auto-generated method stub
return null;
}
private String getSide3() {
// TODO Auto-generated method stub
return null;
}
private String getSide2() {
// TODO Auto-generated method stub
return null;
}
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.println("Enter three sides of the Triangle");
double side1 = input.nextDouble();
double side2 = input.nextDouble();
double side3 = input.nextDouble();
System.out.println("Enter the color of the Triangle");
String color = input.next();
System.out.println(" Is the Triangle filled? Reply with 'True' or 'False' ");
String filled = input.next();
}
{
Triangle triangle = new Triangle(side1, side2, side3);
triangle.setFilled(isFilled());
triangle.setColor(getColor());
// Display
System.out.println("The Triangle Sides are \n side 1: "
+ triangle.getSide1() + "\n Side 2: " + triangle.getSide2()
+ "\n Side 3: " + triangle.getSide3());
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("Is the Triangle filled? " + triangle.isFilled());
}
}
您需要像这样创建一个新的三角形
对象,这样您就有了一个引用
Trangle triangle = new Triangle(side1, side2, side3);
// ^^^^^^ This is the most important thing you're missing. You need a reference
// point for your object. That's the only way you can access it's
// properties.
您还需要设置它的填充
和颜色
属性
triangle.setFilled(filled);
triangle.setColor(color);
然后,您可以像这样调用它的方法:
System.out.println("The Triangle Sides are \n side 1: "
+ triangle.getSide1() + "\n Side 2: " + triangle.getSide2()
+ "\n Side 3: " + triangle.getSide3());
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("Is the Triangle filled? " + triangle.isFilled());
您可以访问GeometricObject
的isFiller()
,setFiller()
,getColor()
和setColor()
,因为Trigle
是GeometricObject
(扩展
),因此它继承了所有方法。
顺便说一下,这不是计算三角形面积的方法:
public double getArea() {
return (side1 + side2 + side3) / 2; // This is so wrong
}
查看此链接了解正确的配方
编辑:代码的另一个问题
public double setSide1() {
return side1;
}
public double setSide2() {
return side2;
}
public double setSide3() {
return side3;
}
/**** Should Be ******/
public double getSide1() {
return side1;
}
public double getSide2() {
return side2;
}
public double getSide3() {
return side3;
}
编辑:三角形公式
public double getArea() {
int p = getPerimeter() / 2
return Math.sqrt(p * ((p - side1) * (p - side2) * (p - side3));
}
我在Android应用程序中有一个配置类,存储在Realm数据库中。我只是按照单例设计模式对其进行了编辑,所以配置只有一个实例。 问题是,该类现在有一个私有构造函数,可以在不调用我的“getInstance”方法的情况下防止实例化。然而,RealmObject似乎需要一个公共构造函数。 难道不可能有一个类使用单例设计模式扩展RealmObject吗? 这仅仅是我必须解释的领域的限制吗? 下面是上下
Triangle 是使用点集三角剖分技术(delaunay triangulation technique)构建艺术图像的工具。它输入图像,然后将其转换为由三角形块组成的抽象图像。 安装和使用 $ go get github.com/esimov/triangle/cmd/triangle$ go install 支持命令 $ triangle --help 支持标签
问题内容: 我正在学习Java,并且希望将自己的课堂变成可观察的课堂。 但是我已经有了它扩展了另一个类。 我该怎么办? 问题答案: 我建议避免完全使用该类,而应 定义事件特定的侦听器和相应的事件定义 。然后在您的类中定义一个侦听器列表,以及添加和删除侦听器以及向其传播事件的方法(请参见下文)。 强制您使用它来表示事件,然后使用来检查事件类型,这是一种丑陋的非OO方法,并使代码更难以理解。如果查看j
你好,有人知道如何发现扩展< code>ArrayBlockingQueue的类吗?例如,我想监视下面的< code>MyBufferQueue类 在属于java库的< code>ArrayBlockingQueue类中,有这样一个方法: 我遇到的问题是,当我监视类< code>MyBufferQueue时,以及在测试期间访问方法< code > ArrayBlockingQueue . put(
Question leetcode: Triangle | LeetCode OJ lintcode: (109) Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. Note Bo
Triangle 描述 Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4]