javarect
① java Rect.java輸出問題
使用Java,double 進行運算時,經常出現精度丟失的問題,總是在一個正確的結果左右偏0.0000**1。 特別在實際項目中,通過一個公式校驗該值是否大於0,如果大於0我們會做一件事情,小於0我們又處理其他事情。 這樣的情況通過double計算出來的結果去和0比較大小,尤其是有小數點的時候,經常會因為精度丟失而導致程序處理流程出錯。
這里有篇文章,你可以去看看怎麼處理
http://zhaow-381002134.javaeye.com/blog/420369
② 用JAVA編寫程序,編寫一個Rect類 ,要求,(1)Rect類的私有成員變數有:a:double型,代表長 b: doubl
class Rect{
private double a ;
private double b;
Rect(){
this.a = 0;
this.b = 0;
}
Rect(double len,double width){
this.a = len;
this.b = width;
}
public Double area(){
return this.a* this.b;
}
public void display(){
System.out.println(this.area());
}
}
public class userect {
public static void main(String[] args){
Rect rect1 = new Rect();
rect1.display();
Rect rect2 = new Rect(10,10);
rect2.display();
}
}
③ java 編寫一個矩形類 rect 要求如下:
publicclassRect{
privateintlength;
privateintwidth;
privateintstartX;
privateintstartY
publicRect(){}
publicRect(intlength,intwidth){
this.length=length;
this.width=width;
}
publicRect(intlength,intwidth,intstartX,intstartY){
this.length=length;
this.width=width;
this.startX=startX;
this.startY=startY;
}
//不知道你要什麼成員方法,我隨便點....
publicvoidlouzhuhao(){
System.out.println("樓主好....");
}
publicintgetLength(){
returnlength;
}
publicvoidsetLength(intlength){
this.length=length;
}
publicintgetWidth(){
returnwidth;
}
publicvoidsetWidth(intwidth){
this.width=width;
}
publicintgetStartX(){
returnstartX;
}
publicvoidsetStartX(intstartX){
this.startX=startX;
}
publicintgetStartY(){
returnstartY;
}
publicvoidsetStartY(intstartY){
this.startY=startY;
}
}
④ Java編寫一個矩形類,並計算面積和周長
class Rectangle{
private int width = 2;
private int length = 1;
public int getWidth(){
return this.width;
}
public void setWidth(int w){
this.width = w;
}
public int getLength(){
return this.length;
}
public void setLength(int l){
this.length = l;
}
public int getArea(){
return this.length * this.width;
}
public int getCircumference(){
return (this.length + this.width) * 2;
}
public Rectangle(){}
public Rectangle(int l, int w){
this.length = l;
this.width = w;
}
}
public class demo{
public static void main(String[] args) {
Rectangle rect = new Rectangle(30, 8);
System.out.print("長方形的面積是:");
System.out.println(rect.getArea());
System.out.printf("長方形的周長是:%d\n", rect.getCircumference());
}
}
⑤ Java 編寫一個矩形類Rect
public class Rect {
private double length;//矩形的長
private double width;//矩形的寬
public Rect() {}//默認構造器
public Rect(double length,double width) {
this.length = length;
this.width = width;
}
/**
* 取得矩形的面積
* */
public double getArea(){
return this.getLength() * this.getWidth();
}
/**
* 取得矩形的周長
* */
public double getPerimeter(){
return (this.getLength() + this.getWidth()) * 2;
}
/**
* 取得矩形的面積,需傳入矩形長與寬
* */
public double getArea(double length,double width){
return length * width;
}
/**
* 取得矩形的周長,需傳入矩形長與寬
* */
public double getPerimeter(double length,double width){
return (length + width) * 2;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
}
⑥ 用java定義一個「矩形」(Rect)類。要求如下: 可以生成具有特定長和寬
/**
*Java技術交流:451359628
*Java技術指導:201856584
*@authorGRcoder
*/
publicclassRect{
privatelongrectLong;
privatelongrectWide;
publicRect(){
super();
}
publicRect(longrectLong,longrectWide){
super();
this.rectLong=rectLong;
this.rectWide=rectWide;
}
//面積
publiclonggetArea(){
returnthis.rectLong*this.rectWide;
}
//周長
publiclonggetPerimeter(){
return(this.rectLong+this.rectWide)*2;
}
publiclonggetRectLong(){
returnrectLong;
}
publicvoidsetRectLong(longrectLong){
this.rectLong=rectLong;
}
publiclonggetRectWide(){
returnrectWide;
}
publicvoidsetRectWide(longrectWide){
this.rectWide=rectWide;
}
@Override
publicStringtoString(){
return"Rect[rectLong="+rectLong+",rectWide="+rectWide+"]";
}
}
⑦ 用java創建一個矩形類Rectangle,其屬性為各點的坐標。
class Point{
protected:
double x;
double y;
public:
Point(double a,double b){x=a;y=b;}
Point(){x=0.0;y=0.0;}
double GetX(){return x;}
double GetY(){return y;}
};
class Rect{
protected:
Point p1;
Point p2;//p1是左下角,p2是右上角
public:
Rect(double i,double j,double,k,double l):p1(i,j),p2(k,l){}
double Area()
{return ((p2.GetX()-p1.GetX())*(p1.GetY()-p2.GetY()));}
};
//用於測試的主函數
void main()
{
Rect rect1(2,3,4,5);
cout<<rect1.Area()<<endl;
}
⑧ java調用GetWindowRect怎麼傳參數
GetWindowRect方法的完整描述
booleancom.sun.jna.platform.win32.User32.GetWindowRect(HWNDarg0,RECTarg1)
Note:couldbefound.
傳入的參數是HWND 窗口的句柄 ,根據你自己的需求傳入窗口句柄
User32INSTANCE=(User32)Native.loadLibrary(User32.class,W32APIOptions.UNICODE_OPTIONS);
HWNDhwnd=User32.INSTANCE.FindWindow("yourWindowClassName","yourWindowName");//參數是:窗口的類,和窗口的標題
//這兩個參數,至少一個不為null
// RECT 參數,可以提前構造一個 ,代碼如下
WinDef.RECTrect=newWinDef.RECT();
最後調用GetWindowRect方法
User32.INSTANCE.GetWindowRect(hwnd,rect);//最後獲取到的數據都存在了rect裡面,可以取出來用
intmyWidth=rect.right-rect.left;//右頂點-左頂點=寬
intmyHight=rect.bottom-rect.top;//下頂點-上頂點=高