Home > Back-end >  Java round
Java round

Time:10-02

Write a class representative round, including the center of the circle (Point) and the radius of the two properties, write setRadius method to test the input value of r, r is positive, there is a corresponding output, r negative program error reminder: "the circular radius cannot be negative,"

CodePudding user response:

Public class Circle {
Private double [] point;
Private double radius;

Public double [] getPoint () {
Return point.
}
Public void setPoint (double [] point) {
This. Point=point;
}
Public double getRadius () {
Return the radius.
}
Public void setRadius (double radius) {
If (radius> 0 {
System. The out. Println (" ok ");
Enclosing the radius=radius;
} else {
Throw new RuntimeException (" circular radius cannot be negative ");
}
}

Public static void main (String [] args) {
Circle Circle=new Circle ();
//circle. SetRadius (7.7);
Circle. SetRadius (7.7);
}


}

CodePudding user response:

 
Package practice;

Public class Circle {
Private double [] point;
Private double radius;

Public double [] getPoint () {
Return point.
}
Public void setPoint (double [] point) {
This. Point=point;
}
Public double getRadius () {
Return the radius.
}
Public void setRadius (double radius) {
If (radius> 0 {
System. The out. Println (" ok ");
Enclosing the radius=radius;
} else {
Throw new RuntimeException (" circular radius cannot be negative ");
}
}

Public static void main (String [] args) {
Circle Circle=new Circle ();
Circle. SetRadius (7.7);//no problem greater than 0
//circle. SetRadius (7.7);//less than 0 throws an exception information
}

}

CodePudding user response:

Point the most want is defined as a kind of
 class Point {
Private int x=0;
Private int y=0;
Public Point () {}
Public Point (int x, int y) {this.=x x; This. Y=y; }
Public Point (Point p) {this. X=p.x; This. Y=p.y; }
Int getX () {return x; }
Int getY () {return y; }
Void your setX (int x) {this.=x x; }
Void setY (int y) {this. Y=y; }
}

Public class Circle {
Private Point center=new Point ();
Private double radius=0;
Public Circle () {}
Public Circle radius, (double) {setRadius (radius); }
Public Circle (Point center, double radius) {setCenter (center); SetRadius (radius); }
Public void setRadius (double radius) {
If (radius & lt; 0 {
System. The out. Println (" the circular radius cannot be negative ");
return;
}
Enclosing the radius=radius;
}
Public void setCenter (Point center) {
If (center==null) return;
This. Center=center;
}
Public void setCenter (int x, int y) {
SetCenter (new Point (x, y));
}
Public double getRadius () {return radius; }
Public Point getCenter () {return center; }

Public static void main (String [] args) {
Try {
Circle c=new Circle ();
Scanner sc=new Scanner(System.in);
System. The out. Printf (" please enter the radius of the circle: ");
Double r=sc, nextDouble ();
C.s. etRadius (r);
System. The out. Printf (". The radius of the circle is: % 2 f \ n ", c.g etRadius ());
} the catch (Throwable e) {
e.printStackTrace();
}
}
}
  • Related