Java第三週作業(1)

課堂練習1:

請定義一個交通工具(Vehicle)的類,其中有:屬性:速度(speed),體積(size)等
方法:移動(move()),設置速度(setSpeed(int speed)),設置體積(setSize(int size))加速speedUp(),減速speedDown()等
在測試類Vehicle中的main()中實例化一個交通工具對象,通過方法給它初始化speed,size的值,並打印出來。另外,調用加速,減速的方法對速度進行改變。


具體代碼:

public class Vehicle {
        double speed;
        double size;
        public Vehicle(double speed,double size){
            this.speed=speed;
            this.size=size;
        }
        public void setSpeed(double speed) {
            this.speed = speed;
            System.out.println("速度修改成功"+speed);
        }
        public void setSize(double size) {
            this.size = size;
            System.out.println("體積修改成功"+size);
        }
        public void move(){
            if(this.speed>0){
                System.out.println("速度修改成功");
            }
            System.out.println("停車無法移動");
        }
        public void speedUp(){
            if(this.speed<=99){
            this.speed +=1;
            System.out.println("加速成功,現在速度爲:"+speed);
            }
            else{
                System.out.println("速度達到最高,請注意安全");
            }

        }
        public void speedDown(){
            if(this.speed >=1){
            this.speed-=1;
            System.out.println("減速成功,現在的速度:"+speed);
            }
            else{
                System.out.println("現在是停車,不能再進行減速");
            }
        }

}
public class VehicleTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
            Vehicle vehicle=new Vehicle(23, 100);
            vehicle.setSize(120);
            vehicle.setSpeed(0);
            vehicle.speedUp();
            vehicle.speedDown();
            vehicle.move();
    }

}

運行結果

這裏寫圖片描述


課堂練習2:

打印當前時間。學習使用Date類和Calendar類。

import java.util.Calendar;
import java.util.Date;


public class NowTime {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
            Calendar calendar=Calendar.getInstance();
            calendar.setTime(new Date());
            int year=calendar.get(Calendar.YEAR);
            int  month=calendar.get(Calendar.MONTH)+1;
            int day =calendar.get(Calendar.DAY_OF_MONTH);
            int hour=calendar.get(Calendar.HOUR_OF_DAY);
            int minute=calendar.get(Calendar.MINUTE);
            int second=calendar.get(Calendar.SECOND);
            System.out.println("使用Calender類打印現在時間:");
            System.out.println(""+year+"年"+month+"月"+day+"日"
                            +hour+"時"+minute+"分"+second+"秒");
            Date date=new Date();  
            System.out.println("使用Date打印日期:");  
            System.out.println("直接打印:當前時間爲:"+date);  
    }

}

運行結果:

這裏寫圖片描述


課堂練習3:

以Point類爲基礎,定義一個平面中的Circle類:
1、編寫一個無參的構造函數;
2、編寫一個有參的構造函數;
3、在主函數中調用無參的構造函數生成圓的實例c1,調用有參的構造函數生成圓的實例c2,調用實例方法判斷c1和c2是否相重疊。

public class Point {
        double x;
        double y;
        Point(){
            super();
        }
        public double getX() {
            return x;
        }
        public void setX(double d) {
            this.x = d;
        }
        public double getY() {
            return y;
        }
        public void setY(double y) {
            this.y = y;
        }


}
public class Circle {
        double r;
        Point point;
        Circle(){
            //無參數的構造方法
            super();
        }
        public Circle(int r, Point point) {
            //有參數的構造方法
            super();
            this.r = r;
            this.point = point;
        }
        public double getR() {
            return r;
        }
        public void setR(double r) {
            this.r = r;
        }
        public Point getPoint() {
            return point;
        }
        public void setPoint(Point point) {
            this.point = point;
        }


}
import java.util.Scanner;


public class hanshuTest {

    /**
     * @param args
     */
    static Circle circle1,circle2;
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //手動輸入圓1
        circle1=new Circle();
        InputData();
        //構造方法產生圓2
        circle2 = new Circle(3,new Point()); 
        circle2.point.x=2;
        circle2.point.y=3;
        System.out.println("構建完成圓二。圓的具體位置:" +
                "橫座標"+circle2.getPoint().x+"縱座標"+circle2.getPoint().getY()+"半徑"+circle2.r);

        judgeTwoCircle();

    }

    private static void judgeTwoCircle() {
        // 判斷兩個圓的位置
        double i=Math.sqrt((circle1.getPoint().getX()-circle2.getPoint().getX())*(circle1.getPoint().getX()-circle2.getPoint().getX())+(circle1.getPoint().getY()-circle2.getPoint().getY())*(circle1.getPoint().getY()-circle2.getPoint().getY()));
        if(i>circle1.getR()+circle2.getR()){
            System.out.println("結果:兩個圓相離");
        }
        else if(i==Math.abs(circle1.getR()+circle2.getR())){
            System.out.println("結果:兩個圓相切");
        }
        else if(i>Math.abs(circle1.getR()-circle2.getR())&&i<circle1.getR()+circle2.getR()){
            System.out.println("結果:兩個圓相交");
        }
        else if(i==Math.abs(circle1.getR()+circle2.getR())){
            System.out.println("結果:兩個圓內切");
        }
        else if(circle1.getPoint().getX()==circle2.getPoint().getX()&&
                circle1.getPoint().getY()==circle2.getPoint().getY()&&
                circle1.getR()==circle2.getR()){
            System.out.println("結果:兩圓重逢");
        }
        else{
            System.out.println("錯誤");
        }


    }

    private static void InputData() {
        // TODO Auto-generated method stub
        System.out.println("請輸入座標點以及半徑的值");
        Scanner scanner=new Scanner(System.in);
        circle1.setPoint(new Point());
        circle1.point.setX(scanner.nextDouble());
        circle1.point.setY(scanner.nextDouble());
        circle1.setR(scanner.nextDouble());
        System.out.println("輸入完成圓一。圓的具體位置:" +
                "橫座標"+circle1.getPoint().x+"縱座標"+circle1.getPoint().getY()+"半徑"+circle1.r);
    }

}

運行結果:

這裏寫圖片描述

發佈了37 篇原創文章 · 獲贊 7 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章