1:設計一個長方形Rectangle類,包含左上角坐標(x1,y1)、長(w)和高(h)四個資料成員。撰寫長方形類的以下方法:
1、撰寫合適的建構式初始化一個矩形;
2、撰寫計算矩形面積的方法getArea();
3、撰寫計算判斷矩形是否包含點的方法Contains(int x,int y).
輸入
輸入一個矩形的左上角坐標、長和高;
輸入一個坐標x,y;
輸出
長方形的面積
判定點(x,y)是否在矩形內。如果在矩形內,請如樣例格式輸出在長方形內,如不在,請如樣例格式輸出在長方形外。
System.out.println("("+i+","+j+")"+"在長方形("+a+","+b+"),"+"w="+x+","+"h="+(int)y+"內");
或
System.out.println("("+i+","+j+")"+"在長方形("+a+","+b+"),"+"w="+x+","+"h="+(int)y+"外");
}
難度
入門
輸入示例
1 4
5 3
2 2
輸出示例
area=15.0
(2,2)在長方形(1,4),w=5,h=3內
2:設計一個圓類,包含圓心和半徑兩個資料成員。1、撰寫合適的建構式初始化一個圓;2、撰寫計算圓面積的方法getArea()。3、撰寫計算是否包含點的contains(Point),Contains(int x,int y).
輸入
輸入一個圓的圓心和半徑;
輸入一個點P;
輸入一個坐標x,y;
輸出
判定點P和坐標是否在圓內。
輸出格式如下:
System.out.println("("+p2.getX()+","+p2.getY()+")在圓("+p1.getX()+","+p1.getY()+"),r="+p1.getR()+"內");
System.out.println("("+p2.getX()+","+p2.getY()+")不在圓("+p1.getX()+","+p1.getY()+"),r="+p1.getR()+"內");
System.out.println("("+x3+","+y3+")在圓("+p1.getX()+","+p1.getY()+"),r="+p1.getR()+"內");
System.out.println("("+x3+","+y3+")不在圓("+p1.getX()+","+p1.getY()+"),r="+p1.getR()+"內");
難度
一般
輸入示例
3 5 2
5 5
3 4
輸出示例
(5,5)不在圓(3,5),r=2.0內
(3,4)在圓(3,5),r=2.0內
uj5u.com熱心網友回復:
2.import java.util.Scanner;public class lop {
public static void main(String[] args) {
// TODO Auto-generated method stub
int x1,x2,x4,x5,x6,x7;
double x3;
Scanner sc=new Scanner(System.in);
x1=sc.nextInt();
x2=sc.nextInt();
x3=sc.nextDouble();
x4=sc.nextInt();
x5=sc.nextInt();
x6=sc.nextInt();
x7=sc.nextInt();
circle p1=new circle(x1,x2,x3);
point k=new point(x4,x5);
p1.Containspoint(x4, x5);
p1.Containspoint(x6, x7);
}
}
class circle
{
int x,y;
double r;
public circle(int x,int y,double r)
{ this.x=x;this.y=y;this.r=r;}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
public double getarea() {
double p=3.14*this.r*this.r;
return p;
}
public boolean Containspoint(int a,int b){
boolean i=false;
double d = Math.abs(a - x);
double e= Math.abs(b - y);
if(d * d + e * e < r * r){
System.out.println("("+a+","+b+")在圓("+x+","+y+"),r="+r+"內");
return true;
}else{
System.out.println("("+a+","+b+")不在圓("+x+","+y+"),r="+r+"內");
return false;
}
}
}
class point{
int x,y;
public point(int a,int b) {
x=a;y=b;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/110247.html
標籤:Eclipse
上一篇:PID微分器與濾波器的愛恨情仇
